-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix regexp filter query #2797
base: main
Are you sure you want to change the base?
Fix regexp filter query #2797
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,7 +14,9 @@ | |||||||||
import org.opensearch.sql.data.model.ExprValue; | ||||||||||
import org.opensearch.sql.data.type.ExprCoreType; | ||||||||||
import org.opensearch.sql.expression.Expression; | ||||||||||
import org.opensearch.sql.expression.FunctionExpression; | ||||||||||
import org.opensearch.sql.expression.env.Environment; | ||||||||||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||||||||||
import org.opensearch.sql.opensearch.storage.script.core.ExpressionScript; | ||||||||||
|
||||||||||
/** | ||||||||||
|
@@ -48,6 +50,16 @@ private ExprValue evaluateExpression( | |||||||||
return ExprBooleanValue.of(false); | ||||||||||
} | ||||||||||
|
||||||||||
// refer to https://github.com/opensearch-project/sql/issues/2796 | ||||||||||
if (isRegexpExpression(expression)) { | ||||||||||
assert result.type() == ExprCoreType.INTEGER; | ||||||||||
if (result.integerValue() == 0) { | ||||||||||
result = ExprBooleanValue.of(false); | ||||||||||
} else if (result.integerValue() == 1) { | ||||||||||
result = ExprBooleanValue.of(true); | ||||||||||
Comment on lines
+56
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check will cause additional issue. sql/core/src/main/java/org/opensearch/sql/utils/OperatorUtils.java Lines 37 to 40 in 4326396
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. For instance, the query There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @LantaoJin only when the expression is a REGEXP expression will the conversion be executed. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. The conversion scope should be limited in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have fixed it. Please take another look at your convenience. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain why
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@dai-chen I guess the reason is the behaviour of MySQL (https://dev.mysql.com/doc/refman/8.0/en/regexp.html#operator_regexp). The original request was opened in opendistro-for-elasticsearch/sql#710 and its implementation introduced by opendistro-for-elasticsearch/sql#750. It would be a breaking change if we change its return value type. Besides updating the user doc (https://github.com/opensearch-project/sql/blob/main/docs/user/dql/expressions.rst#regexp-value-test), we need a changelog file to record those breaking changes. Any thoughts cc @penghuo, @chloe-zh There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My proposal are two options:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, option 2 lacks user-friendliness. |
||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
if (result.type() != ExprCoreType.BOOLEAN) { | ||||||||||
throw new IllegalStateException( | ||||||||||
String.format( | ||||||||||
|
@@ -57,4 +69,11 @@ private ExprValue evaluateExpression( | |||||||||
} | ||||||||||
return result; | ||||||||||
} | ||||||||||
|
||||||||||
private boolean isRegexpExpression(Expression expression) { | ||||||||||
return expression instanceof FunctionExpression | ||||||||||
&& ((FunctionExpression) expression) | ||||||||||
.getFunctionName() | ||||||||||
.equals(BuiltinFunctionName.REGEXP.getName()); | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some unit tests in
ExpressionFilterScriptTest
?