-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Inline SQL functions at plan time #14931
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
3 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
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
136 changes: 136 additions & 0 deletions
136
...main/src/main/java/com/facebook/presto/sql/planner/iterative/rule/InlineSqlFunctions.java
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 |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.facebook.presto.sql.planner.iterative.rule; | ||
|
|
||
| import com.facebook.presto.Session; | ||
| import com.facebook.presto.common.type.Type; | ||
| import com.facebook.presto.metadata.Metadata; | ||
| import com.facebook.presto.spi.function.FunctionHandle; | ||
| import com.facebook.presto.spi.function.FunctionImplementationType; | ||
| import com.facebook.presto.spi.function.FunctionMetadata; | ||
| import com.facebook.presto.spi.function.SqlInvokedScalarFunctionImplementation; | ||
| import com.facebook.presto.sql.parser.SqlParser; | ||
| import com.facebook.presto.sql.planner.iterative.Rule; | ||
| import com.facebook.presto.sql.tree.Expression; | ||
| import com.facebook.presto.sql.tree.ExpressionTreeRewriter; | ||
| import com.facebook.presto.sql.tree.FunctionCall; | ||
| import com.facebook.presto.sql.tree.NodeRef; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.isInlineSqlFunctions; | ||
| import static com.facebook.presto.metadata.FunctionManager.qualifyFunctionName; | ||
| import static com.facebook.presto.sql.analyzer.ExpressionAnalyzer.getExpressionTypes; | ||
| import static com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes; | ||
| import static com.facebook.presto.sql.relational.SqlFunctionUtils.getSqlFunctionExpression; | ||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class InlineSqlFunctions | ||
| extends ExpressionRewriteRuleSet | ||
| { | ||
| public InlineSqlFunctions(Metadata metadata, SqlParser sqlParser) | ||
| { | ||
| super(createRewrite(metadata, sqlParser)); | ||
| } | ||
|
|
||
| private static ExpressionRewriter createRewrite(Metadata metadata, SqlParser sqlParser) | ||
| { | ||
| requireNonNull(metadata, "metadata is null"); | ||
| requireNonNull(sqlParser, "sqlParser is null"); | ||
|
|
||
| return (expression, context) -> InlineSqlFunctionsRewriter.rewrite( | ||
| expression, | ||
| context.getSession(), | ||
| metadata, | ||
| getExpressionTypes( | ||
| context.getSession(), | ||
| metadata, | ||
| sqlParser, | ||
| context.getVariableAllocator().getTypes(), | ||
| expression, | ||
| emptyList(), | ||
| context.getWarningCollector())); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Rule<?>> rules() | ||
| { | ||
| // Aggregations are not rewritten because they cannot have SQL functions | ||
| return ImmutableSet.of( | ||
| projectExpressionRewrite(), | ||
| filterExpressionRewrite(), | ||
| joinExpressionRewrite(), | ||
| valuesExpressionRewrite()); | ||
| } | ||
|
|
||
| public static class InlineSqlFunctionsRewriter | ||
| { | ||
| private InlineSqlFunctionsRewriter() {} | ||
|
|
||
| public static Expression rewrite(Expression expression, Session session, Metadata metadata, Map<NodeRef<Expression>, Type> expressionTypes) | ||
| { | ||
| if (isInlineSqlFunctions(session)) { | ||
| return ExpressionTreeRewriter.rewriteWith(new Visitor(session, metadata, expressionTypes), expression); | ||
| } | ||
| return expression; | ||
| } | ||
|
|
||
| private static class Visitor | ||
| extends com.facebook.presto.sql.tree.ExpressionRewriter<Void> | ||
| { | ||
| private final Session session; | ||
| private final Metadata metadata; | ||
| private final Map<NodeRef<Expression>, Type> expressionTypes; | ||
|
|
||
| public Visitor(Session session, Metadata metadata, Map<NodeRef<Expression>, Type> expressionTypes) | ||
| { | ||
| this.session = requireNonNull(session, "session is null"); | ||
| this.metadata = requireNonNull(metadata, "metadata is null"); | ||
| this.expressionTypes = expressionTypes; | ||
| } | ||
|
|
||
| @Override | ||
| public Expression rewriteFunctionCall(FunctionCall node, Void context, ExpressionTreeRewriter<Void> treeRewriter) | ||
| { | ||
| List<Type> argumentTypes = new ArrayList<>(); | ||
| List<Expression> rewrittenArguments = new ArrayList<>(); | ||
| for (Expression argument : node.getArguments()) { | ||
| argumentTypes.add(expressionTypes.get(NodeRef.of(argument))); | ||
| rewrittenArguments.add(treeRewriter.rewrite(argument, context)); | ||
| } | ||
|
|
||
| FunctionHandle functionHandle = metadata.getFunctionManager().resolveFunction( | ||
| session.getTransactionId(), | ||
| qualifyFunctionName(node.getName()), | ||
| fromTypes(argumentTypes)); | ||
| FunctionMetadata functionMetadata = metadata.getFunctionManager().getFunctionMetadata(functionHandle); | ||
|
|
||
| if (functionMetadata.getImplementationType() != FunctionImplementationType.SQL) { | ||
| return new FunctionCall(node.getName(), rewrittenArguments); | ||
| } | ||
| return getSqlFunctionExpression( | ||
| functionMetadata, | ||
| (SqlInvokedScalarFunctionImplementation) metadata.getFunctionManager().getScalarFunctionImplementation(functionHandle), | ||
| metadata, | ||
| session.getSqlFunctionProperties(), | ||
| rewrittenArguments); | ||
| } | ||
| } | ||
| } | ||
| } |
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.