-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7474] LAST in MATCH_RECOGNIZE might return wrong result
#4889
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -22,7 +22,6 @@ | |
| import org.apache.calcite.avatica.util.DateTimeUtils; | ||
| import org.apache.calcite.avatica.util.TimeUnit; | ||
| import org.apache.calcite.avatica.util.TimeUnitRange; | ||
| import org.apache.calcite.linq4j.tree.BinaryExpression; | ||
| import org.apache.calcite.linq4j.tree.BlockBuilder; | ||
| import org.apache.calcite.linq4j.tree.BlockStatement; | ||
| import org.apache.calcite.linq4j.tree.ConstantExpression; | ||
|
|
@@ -4173,43 +4172,27 @@ private static class LastImplementor implements MatchImplementor { | |
|
|
||
| final String alpha = ((RexPatternFieldRef) call.getOperands().get(0)).getAlpha(); | ||
|
|
||
| // TODO: verify if the variable is needed | ||
| @SuppressWarnings("unused") | ||
| final BinaryExpression lastIndex = | ||
| Expressions.subtract( | ||
| Expressions.call(rows, BuiltInMethod.COLLECTION_SIZE.method), | ||
| Expressions.constant(1)); | ||
|
|
||
| // Just take the last one, if exists | ||
| if ("*".equals(alpha)) { | ||
| setInputGetterIndex(translator, i); | ||
| // Important, unbox the node / expression to avoid NullAs.NOT_POSSIBLE | ||
| final RexPatternFieldRef ref = (RexPatternFieldRef) node; | ||
| final RexPatternFieldRef newRef = | ||
| new RexPatternFieldRef(ref.getAlpha(), | ||
| ref.getIndex(), | ||
| translator.typeFactory.createTypeWithNullability(ref.getType(), | ||
| true)); | ||
| final Expression expression = translator.translate(newRef, NullAs.NULL); | ||
| setInputGetterIndex(translator, null); | ||
| return expression; | ||
| } else { | ||
| // Alpha != "*" so we have to search for a specific one to find and use that, if found | ||
| // otherwise pick the last one | ||
| setInputGetterIndex(translator, | ||
| Expressions.call(BuiltInMethod.MATCH_UTILS_LAST_WITH_SYMBOL.method, | ||
| Expressions.constant(alpha), rows, symbols, i)); | ||
|
|
||
| // Important, unbox the node / expression to avoid NullAs.NOT_POSSIBLE | ||
| final RexPatternFieldRef ref = (RexPatternFieldRef) node; | ||
| final RexPatternFieldRef newRef = | ||
| new RexPatternFieldRef(ref.getAlpha(), | ||
| ref.getIndex(), | ||
| translator.typeFactory.createTypeWithNullability(ref.getType(), | ||
| true)); | ||
| final Expression expression = translator.translate(newRef, NullAs.NULL); | ||
| setInputGetterIndex(translator, null); | ||
|
Comment on lines
-4187
to
-4210
Contributor
Author
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. since in both branches the code is almost same, extracted it |
||
| return expression; | ||
| Expressions.call(BuiltInMethod.MATCH_UTILS_LAST_WITH_SYMBOL_OR_LAST.method, | ||
| Expressions.constant(alpha), symbols, i)); | ||
| } | ||
|
|
||
| // Important, unbox the node / expression to avoid NullAs.NOT_POSSIBLE | ||
| final RexPatternFieldRef ref = (RexPatternFieldRef) node; | ||
| final RexPatternFieldRef newRef = | ||
| new RexPatternFieldRef(ref.getAlpha(), | ||
| ref.getIndex(), | ||
| translator.typeFactory.createTypeWithNullability(ref.getType(), | ||
| true)); | ||
| final Expression expression = translator.translate(newRef, NullAs.NULL); | ||
| setInputGetterIndex(translator, null); | ||
| return expression; | ||
| } | ||
|
|
||
| private static void setInputGetterIndex(RexToLixTranslator translator, @Nullable Expression o) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,22 @@ C EMPID | |
|
|
||
| !ok | ||
|
|
||
| # Test Simple LAST with expanded column name | ||
|
Contributor
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. Can you please say in a comment whether this is validated on another engine?
Contributor
Author
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 tested for Oracle, Snowflake, BigQuery
Contributor
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. Yes, but if this is in a comment people will trust this test. We have had incorrect test results previously.
Contributor
Author
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'm open to do same as Oracle, Snowflake as well |
||
| # Test case for CALCITE-7474 | ||
| select * | ||
| from "hr"."emps" match_recognize ( | ||
| order by "empid" desc | ||
| measures "commission" as c, | ||
| LAST("hr"."emps"."empid") as empid | ||
| pattern (s up) | ||
| define up as up."commission" < prev(up."commission")); | ||
| C EMPID | ||
| ---- ----- | ||
| 1000 100 | ||
| 500 200 | ||
|
|
||
| !ok | ||
|
|
||
| # Test LAST with Classifier | ||
| select * | ||
| from "hr"."emps" match_recognize ( | ||
|
|
||
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.
not used