-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Change RowExpressionFormatter output to be closer to valid SQL #16335
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
|
|
||
| import com.facebook.presto.common.block.Block; | ||
| import com.facebook.presto.common.type.Type; | ||
| import com.facebook.presto.common.type.VarbinaryType; | ||
| import com.facebook.presto.metadata.FunctionAndTypeManager; | ||
| import com.facebook.presto.spi.ConnectorSession; | ||
| import com.facebook.presto.spi.function.FunctionMetadata; | ||
|
|
@@ -79,8 +80,25 @@ else if (standardFunctionResolution.isSubscriptFunction(node.getFunctionHandle() | |
| return formatRowExpression(session, node.getArguments().get(0)) + "[" + formatRowExpression(session, node.getArguments().get(1)) + "]"; | ||
| } | ||
| else if (standardFunctionResolution.isBetweenFunction(node.getFunctionHandle())) { | ||
| List<String> formattedExpresions = formatRowExpressions(session, node.getArguments()); | ||
| return String.format("%s BETWEEN (%s) AND (%s)", formattedExpresions.get(0), formattedExpresions.get(1), formattedExpresions.get(2)); | ||
| List<String> formattedExpressions = formatRowExpressions(session, node.getArguments()); | ||
| return String.format("%s BETWEEN (%s) AND (%s)", formattedExpressions.get(0), formattedExpressions.get(1), formattedExpressions.get(2)); | ||
| } | ||
| else if (standardFunctionResolution.isLikeFunction(node.getFunctionHandle())) { | ||
| RowExpression value = node.getArguments().get(0); | ||
| CallExpression patternCallExpression = (CallExpression) node.getArguments().get(1); | ||
|
|
||
| // second LIKE argument is: | ||
| // CAST(pattern as LikePattern), if escape is not present | ||
| // LIKE_PATTERN(pattern, escape), if escape is present | ||
| if (standardFunctionResolution.isCastFunction(patternCallExpression.getFunctionHandle())) { | ||
| RowExpression pattern = patternCallExpression.getArguments().get(0); | ||
| return String.format("%s LIKE %s", formatRowExpression(session, value), formatRowExpression(session, pattern)); | ||
| } | ||
| else { | ||
| RowExpression pattern = patternCallExpression.getArguments().get(0); | ||
| RowExpression escape = patternCallExpression.getArguments().get(1); | ||
| return String.format("%s LIKE %s ESCAPE %s", formatRowExpression(session, value), formatRowExpression(session, pattern), formatRowExpression(session, escape)); | ||
| } | ||
| } | ||
| FunctionMetadata metadata = functionMetadataManager.getFunctionMetadata(node.getFunctionHandle()); | ||
| return node.getDisplayName() + (metadata.getVersion().hasVersion() ? ":" + metadata.getVersion() : "") + "(" + String.join(", ", formatRowExpressions(session, node.getArguments())) + ")"; | ||
|
|
@@ -123,12 +141,19 @@ public String visitConstant(ConstantExpression node, ConnectorSession session) | |
| } | ||
|
|
||
| Type type = node.getType(); | ||
| if (node.getType().getJavaType() == Block.class) { | ||
| if (type.getJavaType() == Block.class) { | ||
| Block block = (Block) value; | ||
| // TODO: format block | ||
| return format("[Block: position count: %s; size: %s bytes]", block.getPositionCount(), block.getRetainedSizeInBytes()); | ||
| } | ||
| return type.getDisplayName().toUpperCase() + " " + value.toString(); | ||
|
|
||
| String valueString = "'" + value.toString().replace("'", "''") + "'"; | ||
|
|
||
| if (VarbinaryType.isVarbinaryType(type)) { | ||
| return "X" + valueString; | ||
|
||
| } | ||
|
|
||
| return type.getTypeSignature().getBase().toUpperCase() + valueString; | ||
| } | ||
| } | ||
| } | ||
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.
You can get the type name generically with
type.getTypeSignature().getBase()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.
done