From 34d8485bc61420172e5af34baaceccb0b6339a15 Mon Sep 17 00:00:00 2001 From: kasiafi <30203062+kasiafi@users.noreply.github.com> Date: Thu, 10 Jun 2021 13:31:31 +0200 Subject: [PATCH] Fix planning of pattern navigation offset The offset value is a LongLiteral. Before this change, it was rewritten by the TranslationMap, which could lead to replacing the literal with a symbol reference, if the map already contained a mapping for that literal. It resulted in a failure in LogicalIndexExtractor, where a literal was expected. After this change, the offset argument is not rewritten. --- .../java/io/trino/sql/planner/TranslationMap.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/TranslationMap.java b/core/trino-main/src/main/java/io/trino/sql/planner/TranslationMap.java index 797a302acb6e6..96994d9f48871 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/TranslationMap.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/TranslationMap.java @@ -50,7 +50,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Verify.verify; -import static com.google.common.collect.ImmutableList.toImmutableList; import static io.trino.sql.planner.ScopeAware.scopeAwareKey; import static java.lang.String.format; import static java.util.Objects.requireNonNull; @@ -214,9 +213,14 @@ public Expression rewriteIdentifier(Identifier node, Void context, ExpressionTre public Expression rewriteFunctionCall(FunctionCall node, Void context, ExpressionTreeRewriter treeRewriter) { if (analysis.isPatternRecognitionFunction(node)) { - List rewrittenArguments = node.getArguments().stream() - .map(argument -> treeRewriter.rewrite(argument, null)) - .collect(toImmutableList()); + ImmutableList.Builder rewrittenArguments = ImmutableList.builder(); + if (!node.getArguments().isEmpty()) { + rewrittenArguments.add(treeRewriter.rewrite(node.getArguments().get(0), null)); + if (node.getArguments().size() > 1) { + // do not rewrite the offset literal + rewrittenArguments.add(node.getArguments().get(1)); + } + } // Pattern recognition functions are special constructs, passed using the form of FunctionCall. // They are not resolved like regular function calls. They are processed in LogicalIndexExtractor. return coerceIfNecessary(node, new FunctionCall( @@ -228,7 +232,7 @@ public Expression rewriteFunctionCall(FunctionCall node, Void context, Expressio false, Optional.empty(), node.getProcessingMode(), - rewrittenArguments)); + rewrittenArguments.build())); } // TODO handle aggregation in pattern recognition context (and handle its processingMode)