Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private MatchUtils() {
}

/**
* Returns the row with the highest index whose corresponding symbol matches, null otherwise.
* Returns the highest index whose corresponding symbol matches, -1 otherwise.
*
* @param symbol Target Symbol
* @param rows List of passed rows
Expand All @@ -44,6 +44,23 @@ public static <E> int lastWithSymbol(String symbol, List<E> rows, List<String> s
return -1;
}

/**
* Returns the highest index whose corresponding symbol matches, startIndex otherwise.
*
* @param symbol Target Symbol
* @param symbols Corresponding symbols to rows
* @return index or startIndex
*/
public static int lastWithSymbolOrLast(String symbol, List<String> symbols,
int startIndex) {
for (int i = startIndex; i >= 0; i--) {
if (symbol.equals(symbols.get(i))) {
return i;
}
}
return startIndex;
}

public static void print(int s) {
System.out.println(s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used


// 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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ public enum BuiltInMethod {
MATCHER_BUILDER_BUILD(Matcher.Builder.class, "build"),
MATCH_UTILS_LAST_WITH_SYMBOL(MatchUtils.class, "lastWithSymbol", String.class,
List.class, List.class, int.class),
MATCH_UTILS_LAST_WITH_SYMBOL_OR_LAST(MatchUtils.class, "lastWithSymbolOrLast", String.class,
List.class, int.class),
EMITTER_EMIT(Enumerables.Emitter.class, "emit", List.class, List.class,
List.class, int.class, Consumer.class),
MERGE_JOIN(EnumerableDefaults.class, "mergeJoin", Enumerable.class,
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/resources/sql/match.iq
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ C EMPID

!ok

# Test Simple LAST with expanded column name
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested for Oracle, Snowflake, BigQuery
BigQuery is able to process it and return the result
Oracle and Snowflake fail on validation that only vars from PATTERN are allowed, however they still allow usage of non expanded names
also put this in jira

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to do same as Oracle, Snowflake as well
however then not clear why it should work for non expanded columns

# 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 (
Expand Down
Loading