sql icon indicating copy to clipboard operation
sql copied to clipboard

[BUG] REGEXP filter failed due to IllegalStateException

Open bugmakerrrrrr opened this issue 1 year ago • 0 comments

What is the bug? When executing a SQL query with REGEXP filter, get the following exception:

{
  "error": {
    "reason": "Error occurred in OpenSearch engine: all shards failed",
    "details": "Shard[0]: java.lang.IllegalStateException: Expression has wrong result type instead of boolean: expression [regexp(field1, \"test.*\")], result [1]\n\nFor more details, please send request for Json format to see the raw response from OpenSearch engine.",
    "type": "SearchPhaseExecutionException"
  },
  "status": 500
}

How can one reproduce the bug? Steps to reproduce the behavior:

  1. create an index with the following mappings:
{
  "mappings": {
    "properties": {
      "field1": {
        "type": "keyword"
      }
    }
  }
}
  1. write one doc using bulk API:
{"index": {"_id": 1}}
{"field1": "test search something"}
  1. run the following SQL query:
{
  "query": "SELECT field1 FROM test WHERE field1 REGEXP 'test.*'"
}

Do you have any additional context? The root cause of this bug may be because what ExpressionFilterScript#evaluateExpression expects is a boolean value:

https://github.com/opensearch-project/sql/blob/43263963aca2ada949d55ae01cd38788d5d96fbb/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/ExpressionFilterScript.java#L51-L57

but what OperatorUtils#matchesRegexp returns is an integer value:

https://github.com/opensearch-project/sql/blob/43263963aca2ada949d55ae01cd38788d5d96fbb/core/src/main/java/org/opensearch/sql/utils/OperatorUtils.java#L37-L40

If we modify the return type of OperatorUtils#matchesRegexp, it will impact the return type of the REGEXP function, which could be a breaking change. To avoid this, we could consider converting the integer value to a boolean value in ExpressionFilterScript#evaluateExpression.

bugmakerrrrrr avatar Jul 01 '24 13:07 bugmakerrrrrr