-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Print arithmetic and comparison expressions in human-readable format. #12783
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
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 |
|---|---|---|
|
|
@@ -13,8 +13,11 @@ | |
| */ | ||
| package com.facebook.presto.sql.planner.planPrinter; | ||
|
|
||
| import com.facebook.presto.metadata.FunctionManager; | ||
| import com.facebook.presto.spi.ConnectorSession; | ||
| import com.facebook.presto.spi.block.Block; | ||
| import com.facebook.presto.spi.function.FunctionMetadataManager; | ||
| import com.facebook.presto.spi.function.StandardFunctionResolution; | ||
| import com.facebook.presto.spi.relation.CallExpression; | ||
| import com.facebook.presto.spi.relation.ConstantExpression; | ||
| import com.facebook.presto.spi.relation.InputReferenceExpression; | ||
|
|
@@ -25,20 +28,26 @@ | |
| import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
| import com.facebook.presto.spi.type.Type; | ||
| import com.facebook.presto.sql.planner.LiteralInterpreter; | ||
| import com.facebook.presto.sql.relational.FunctionResolution; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static java.lang.String.format; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| public final class RowExpressionFormatter | ||
| { | ||
| private final ConnectorSession session; | ||
| private final FunctionMetadataManager functionMetadataManager; | ||
| private final StandardFunctionResolution standardFunctionResolution; | ||
|
|
||
| public RowExpressionFormatter(ConnectorSession session) | ||
| public RowExpressionFormatter(ConnectorSession session, FunctionManager functionManager) | ||
| { | ||
| this.session = requireNonNull(session, "session is null"); | ||
| this.functionMetadataManager = requireNonNull(functionManager, "function manager is null"); | ||
| this.standardFunctionResolution = new FunctionResolution(functionManager); | ||
| } | ||
|
|
||
| public String formatRowExpression(RowExpression expression) | ||
|
|
@@ -57,12 +66,32 @@ public class Formatter | |
| @Override | ||
| public String visitCall(CallExpression node, Void context) | ||
| { | ||
| if (standardFunctionResolution.isArithmeticFunction(node.getFunctionHandle()) || standardFunctionResolution.isComparisonFunction(node.getFunctionHandle())) { | ||
|
||
| String operation = functionMetadataManager.getFunctionMetadata(node.getFunctionHandle()).getOperatorType().get().getOperator(); | ||
| return String.join(" " + operation + " ", formatRowExpressions(node.getArguments()).stream().map(e -> "(" + e + ")").collect(toImmutableList())); | ||
| } | ||
| else if (standardFunctionResolution.isCastFunction(node.getFunctionHandle())) { | ||
| return String.format("CAST(%s AS %s)", formatRowExpression(node.getArguments().get(0)), node.getType().getDisplayName()); | ||
| } | ||
| else if (standardFunctionResolution.isNegateFunction(node.getFunctionHandle())) { | ||
| return "-(" + formatRowExpression(node.getArguments().get(0)) + ")"; | ||
| } | ||
| else if (standardFunctionResolution.isSubscriptFunction(node.getFunctionHandle())) { | ||
| return formatRowExpression(node.getArguments().get(0)) + "[" + formatRowExpression(node.getArguments().get(1)) + "]"; | ||
| } | ||
| else if (standardFunctionResolution.isBetweenFunction(node.getFunctionHandle())) { | ||
| List<String> formattedExpresions = formatRowExpressions(node.getArguments()); | ||
| return String.format("%s BETWEEN (%s) AND (%s)", formattedExpresions.get(0), formattedExpresions.get(1), formattedExpresions.get(2)); | ||
| } | ||
| return node.getDisplayName() + "(" + String.join(", ", formatRowExpressions(node.getArguments())) + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String visitSpecialForm(SpecialFormExpression node, Void context) | ||
| { | ||
| if (node.getForm().equals(SpecialFormExpression.Form.AND) || node.getForm().equals(SpecialFormExpression.Form.OR)) { | ||
| return String.join(" " + node.getForm() + " ", formatRowExpressions(node.getArguments()).stream().map(e -> "(" + e + ")").collect(toImmutableList())); | ||
| } | ||
| return node.getForm().name() + "(" + String.join(", ", formatRowExpressions(node.getArguments())) + ")"; | ||
| } | ||
|
|
||
|
|
||
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.
Let's use
StandardFunctionResolutionfor the constructor; so it's more general