-
Notifications
You must be signed in to change notification settings - Fork 3.6k
ConnectorExpression pushdown #7994
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,86 +13,163 @@ | |
| */ | ||
| package io.trino.sql.planner; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import io.airlift.slice.Slice; | ||
| import io.airlift.slice.Slices; | ||
| import io.trino.Session; | ||
| import io.trino.metadata.LiteralFunction; | ||
| import io.trino.metadata.ResolvedFunction; | ||
| import io.trino.security.AllowAllAccessControl; | ||
| import io.trino.spi.expression.Call; | ||
| import io.trino.spi.expression.ConnectorExpression; | ||
| import io.trino.spi.expression.Constant; | ||
| import io.trino.spi.expression.FieldDereference; | ||
| import io.trino.spi.expression.FunctionName; | ||
| import io.trino.spi.expression.Variable; | ||
| import io.trino.spi.type.Decimals; | ||
| import io.trino.spi.type.RowType; | ||
| import io.trino.spi.type.Type; | ||
| import io.trino.spi.type.TypeSignature; | ||
| import io.trino.spi.type.VarcharType; | ||
| import io.trino.sql.PlannerContext; | ||
| import io.trino.sql.analyzer.TypeSignatureProvider; | ||
| import io.trino.sql.tree.AstVisitor; | ||
| import io.trino.sql.tree.BinaryLiteral; | ||
| import io.trino.sql.tree.BooleanLiteral; | ||
| import io.trino.sql.tree.CharLiteral; | ||
| import io.trino.sql.tree.DecimalLiteral; | ||
| import io.trino.sql.tree.DoubleLiteral; | ||
| import io.trino.sql.tree.Expression; | ||
| import io.trino.sql.tree.FunctionCall; | ||
| import io.trino.sql.tree.LikePredicate; | ||
| import io.trino.sql.tree.LongLiteral; | ||
| import io.trino.sql.tree.NodeRef; | ||
| import io.trino.sql.tree.NullLiteral; | ||
| import io.trino.sql.tree.QualifiedName; | ||
| import io.trino.sql.tree.StringLiteral; | ||
| import io.trino.sql.tree.SubscriptExpression; | ||
| import io.trino.sql.tree.SymbolReference; | ||
| import io.trino.type.JoniRegexp; | ||
| import io.trino.type.Re2JRegexp; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.google.common.base.Verify.verify; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static io.airlift.slice.SliceUtf8.countCodePoints; | ||
| import static io.trino.SystemSessionProperties.isComplexExpressionPushdown; | ||
| import static io.trino.sql.planner.ExpressionInterpreter.evaluateConstantExpression; | ||
| import static io.trino.type.LikeFunctions.LIKE_PATTERN_FUNCTION_NAME; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public final class ConnectorExpressionTranslator | ||
| { | ||
| private ConnectorExpressionTranslator() {} | ||
|
|
||
| public static Expression translate(Session session, ConnectorExpression expression, Map<String, Symbol> variableMappings, LiteralEncoder literalEncoder) | ||
| public static Expression translate(Session session, ConnectorExpression expression, PlannerContext plannerContext, Map<String, Symbol> variableMappings, LiteralEncoder literalEncoder) | ||
| { | ||
| return new ConnectorToSqlExpressionTranslator(variableMappings, literalEncoder).translate(session, expression); | ||
| return new ConnectorToSqlExpressionTranslator(session, plannerContext, literalEncoder, variableMappings) | ||
| .translate(session, expression) | ||
| .orElseThrow(() -> new UnsupportedOperationException("Expression is not supported: " + expression.toString())); | ||
| } | ||
|
|
||
| public static Optional<ConnectorExpression> translate(Session session, Expression expression, TypeAnalyzer types, TypeProvider inputTypes) | ||
| public static Optional<ConnectorExpression> translate(Session session, Expression expression, TypeAnalyzer types, TypeProvider inputTypes, PlannerContext plannerContext) | ||
| { | ||
| return new SqlToConnectorExpressionTranslator(types.getTypes(session, inputTypes, expression)) | ||
| return new SqlToConnectorExpressionTranslator(session, types.getTypes(session, inputTypes, expression), plannerContext) | ||
| .process(expression); | ||
| } | ||
|
|
||
| private static class ConnectorToSqlExpressionTranslator | ||
| { | ||
| private final Map<String, Symbol> variableMappings; | ||
| private final Session session; | ||
| private final PlannerContext plannerContext; | ||
| private final LiteralEncoder literalEncoder; | ||
| private final Map<String, Symbol> variableMappings; | ||
|
|
||
| public ConnectorToSqlExpressionTranslator(Map<String, Symbol> variableMappings, LiteralEncoder literalEncoder) | ||
| public ConnectorToSqlExpressionTranslator(Session session, PlannerContext plannerContext, LiteralEncoder literalEncoder, Map<String, Symbol> variableMappings) | ||
| { | ||
| this.variableMappings = requireNonNull(variableMappings, "variableMappings is null"); | ||
| this.session = requireNonNull(session, "session is null"); | ||
| this.plannerContext = requireNonNull(plannerContext, "plannerContext is null"); | ||
| this.literalEncoder = requireNonNull(literalEncoder, "literalEncoder is null"); | ||
| this.variableMappings = requireNonNull(variableMappings, "variableMappings is null"); | ||
| } | ||
|
|
||
| public Expression translate(Session session, ConnectorExpression expression) | ||
| public Optional<Expression> translate(Session session, ConnectorExpression expression) | ||
| { | ||
| if (expression instanceof Variable) { | ||
| return variableMappings.get(((Variable) expression).getName()).toSymbolReference(); | ||
| String name = ((Variable) expression).getName(); | ||
| return Optional.of(variableMappings.get(name).toSymbolReference()); | ||
| } | ||
|
|
||
| if (expression instanceof Constant) { | ||
| return literalEncoder.toExpression(session, ((Constant) expression).getValue(), expression.getType()); | ||
| return Optional.of(literalEncoder.toExpression(session, ((Constant) expression).getValue(), expression.getType())); | ||
| } | ||
|
|
||
| if (expression instanceof FieldDereference) { | ||
| FieldDereference dereference = (FieldDereference) expression; | ||
| return new SubscriptExpression(translate(session, dereference.getTarget()), new LongLiteral(Long.toString(dereference.getField() + 1))); | ||
| return translate(session, dereference.getTarget()) | ||
| .map(base -> new SubscriptExpression(base, new LongLiteral(Long.toString(dereference.getField() + 1)))); | ||
| } | ||
|
|
||
| if (expression instanceof Call) { | ||
| return translateCall((Call) expression); | ||
| } | ||
|
|
||
| throw new UnsupportedOperationException("Expression type not supported: " + expression.getClass().getName()); | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| protected Optional<Expression> translateCall(Call call) | ||
| { | ||
| if (call.getFunctionName().getCatalogSchema().isPresent()) { | ||
| return Optional.empty(); | ||
| } | ||
| QualifiedName name = QualifiedName.of(call.getFunctionName().getName()); | ||
| List<TypeSignature> argumentTypes = call.getArguments().stream() | ||
| .map(argument -> argument.getType().getTypeSignature()) | ||
| .collect(toImmutableList()); | ||
| ResolvedFunction resolved = plannerContext.getMetadata().resolveFunction(session, name, TypeSignatureProvider.fromTypeSignatures(argumentTypes)); | ||
|
|
||
| // TODO Support ESCAPE character | ||
| if (LIKE_PATTERN_FUNCTION_NAME.equals(resolved.getSignature().getName()) && call.getArguments().size() == 2) { | ||
| return translateLike(call.getArguments().get(0), call.getArguments().get(1)); | ||
| } | ||
|
|
||
| FunctionCallBuilder builder = FunctionCallBuilder.resolve(session, plannerContext.getMetadata()) | ||
| .setName(name); | ||
| for (int i = 0; i < call.getArguments().size(); i++) { | ||
| Type type = resolved.getSignature().getArgumentTypes().get(i); | ||
| Expression expression = ConnectorExpressionTranslator.translate(session, call.getArguments().get(i), plannerContext, variableMappings, literalEncoder); | ||
| builder.addArgument(type, expression); | ||
| } | ||
| return Optional.of(builder.build()); | ||
| } | ||
|
|
||
| protected Optional<Expression> translateLike(ConnectorExpression value, ConnectorExpression pattern) | ||
| { | ||
| Optional<Expression> translatedValue = translate(session, value); | ||
| Optional<Expression> translatedPattern = translate(session, pattern); | ||
| if (translatedValue.isPresent() && translatedPattern.isPresent()) { | ||
| return Optional.of(new LikePredicate(translatedValue.get(), translatedPattern.get(), Optional.empty())); | ||
|
Member
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. |
||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| static class SqlToConnectorExpressionTranslator | ||
| public static class SqlToConnectorExpressionTranslator | ||
| extends AstVisitor<Optional<ConnectorExpression>, Void> | ||
| { | ||
| private final Session session; | ||
| private final Map<NodeRef<Expression>, Type> types; | ||
| private final PlannerContext plannerContext; | ||
|
|
||
| public SqlToConnectorExpressionTranslator(Map<NodeRef<Expression>, Type> types) | ||
| public SqlToConnectorExpressionTranslator(Session session, Map<NodeRef<Expression>, Type> types, PlannerContext plannerContext) | ||
| { | ||
| this.session = requireNonNull(session, "session is null"); | ||
| this.types = requireNonNull(types, "types is null"); | ||
| this.plannerContext = requireNonNull(plannerContext, "plannerContext is null"); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -149,6 +226,61 @@ protected Optional<ConnectorExpression> visitNullLiteral(NullLiteral node, Void | |
| return Optional.of(new Constant(null, typeOf(node))); | ||
| } | ||
|
|
||
| @Override | ||
| protected Optional<ConnectorExpression> visitFunctionCall(FunctionCall node, Void context) | ||
| { | ||
| if (!isComplexExpressionPushdown(session)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| if (node.getFilter().isPresent() || node.getOrderBy().isPresent() || node.getWindow().isPresent() || node.getNullTreatment().isPresent() || node.isDistinct()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| String functionName = ResolvedFunction.extractFunctionName(node.getName()); | ||
|
|
||
| if (LiteralFunction.LITERAL_FUNCTION_NAME.equalsIgnoreCase(functionName)) { | ||
| Object value = evaluateConstant(node); | ||
| if (value instanceof JoniRegexp) { | ||
|
martint marked this conversation as resolved.
Outdated
|
||
| Slice pattern = ((JoniRegexp) value).pattern(); | ||
| return Optional.of(new Constant(pattern, VarcharType.createVarcharType(countCodePoints(pattern)))); | ||
| } | ||
| if (value instanceof Re2JRegexp) { | ||
| Slice pattern = Slices.utf8Slice(((Re2JRegexp) value).pattern()); | ||
| return Optional.of(new Constant(pattern, VarcharType.createVarcharType(countCodePoints(pattern)))); | ||
| } | ||
| return Optional.of(new Constant(value, types.get(NodeRef.of(node)))); | ||
| } | ||
|
|
||
| ImmutableList.Builder<ConnectorExpression> arguments = ImmutableList.builder(); | ||
| for (Expression argumentExpression : node.getArguments()) { | ||
| Optional<ConnectorExpression> argument = process(argumentExpression); | ||
| if (argument.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| arguments.add(argument.get()); | ||
| } | ||
|
|
||
| // Currently, plugin-provided and runtime-added functions doesn't have a catalog/schema qualifier. | ||
| // TODO Translate catalog/schema qualifier when available. | ||
| FunctionName name = new FunctionName(functionName); | ||
| return Optional.of(new Call(typeOf(node), name, arguments.build())); | ||
| } | ||
|
|
||
| @Override | ||
| protected Optional<ConnectorExpression> visitLikePredicate(LikePredicate node, Void context) | ||
|
martint marked this conversation as resolved.
|
||
| { | ||
| // TODO Support ESCAPE character | ||
| if (node.getEscape().isEmpty()) { | ||
| Optional<ConnectorExpression> value = process(node.getValue()); | ||
| Optional<ConnectorExpression> pattern = process(node.getPattern()); | ||
| if (value.isPresent() && pattern.isPresent()) { | ||
| return Optional.of(new Call(typeOf(node), new FunctionName(LIKE_PATTERN_FUNCTION_NAME), List.of(value.get(), pattern.get()))); | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| protected Optional<ConnectorExpression> visitSubscriptExpression(SubscriptExpression node, Void context) | ||
| { | ||
|
|
@@ -174,5 +306,19 @@ private Type typeOf(Expression node) | |
| { | ||
| return types.get(NodeRef.of(node)); | ||
| } | ||
|
|
||
| private Object evaluateConstant(Expression node) | ||
| { | ||
| Type type = typeOf(node); | ||
| Object value = evaluateConstantExpression( | ||
| node, | ||
| type, | ||
| plannerContext, | ||
| session, | ||
| new AllowAllAccessControl(), | ||
| ImmutableMap.of()); | ||
| verify(!(value instanceof Expression), "Expression %s did not evaluate to constant: %s", node, value); | ||
| return value; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.