-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Ensure Parameters are in proper order for queries having WITH clause #1529
Conversation
0af4123
to
b18148a
Compare
@Override | ||
public Map<NodeRef<Expression>, Integer> visitParameter(Parameter parameter, Void context) | ||
{ | ||
return ImmutableMap.of(NodeRef.of(parameter), position++); |
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.
This will produce an ordering based on traversal order. We need to:
- Collect all the Parameter nodes
- Sort them based on their lexical order (
Parameter.getLocation()
)
b18148a
to
6fc3b0a
Compare
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.
Instead of passing parameters
and parameterPositionMap
everywhere, why not compose them into a single mapping of parameter -> expression (i.e., Map<NodeRef<Parameter>, Expression>
) in Analysis and pass that around?
checkArgument(left.getLocation().isPresent(), "NodeLocation must be present"); | ||
checkArgument(right.getLocation().isPresent(), "NodeLocation must be present"); | ||
|
||
return Long.compare( |
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.
Instead of assembling these into a long, compare based on line first and then based on column number.
{ | ||
List<Expression> parameters = ImmutableList.sortedCopyOf(new ExpressionComparator(), new AstVisitor<List<Expression>, Void>() | ||
{ | ||
@Override |
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.
There's already a class that does this: ParameterExtractor
.
This could all be replaced with:
List<Parameter> parameters = getParameters(...).stream()
.sorted(Comparator.comparing(
parameter -> parameter.getLocation().get(),
Comparator.comparing(NodeLocation::getLineNumber)
.thenComparing(NodeLocation::getColumnNumber)))
.collect(Collectors.toList());
{ | ||
private ParameterUtils() {} | ||
|
||
public static Map<NodeRef<Expression>, Integer> parameterExtractor(Node node) |
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.
The type of this should be Map<NodeRef<Parameter>, Integer>
@@ -81,6 +82,7 @@ | |||
@Nullable | |||
private final Statement root; | |||
private final List<Expression> parameters; | |||
private final Map<NodeRef<Expression>, Integer> parameterPositionMap; |
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.
Make this a Map<NodeRef<Parameter>, Integer>
and rename the variable to parameterOrdinals
Can you add an end-to-end test that involves WITH to make sure parameters are being interpreted correctly? |
6fc3b0a
to
d2b5a01
Compare
92f1af5
to
79f9e7d
Compare
79f9e7d
to
8efe78c
Compare
99325d3
to
8efe78c
Compare
Cherry-pick of trinodb/trino#1529 Fixes prestodb#17012 Co-authored-by: praveenkrishna <praveenkrishna@tutanota.com>
Cherry-pick of trinodb/trino#1529 Fixes #17012 Co-authored-by: praveenkrishna <praveenkrishna@tutanota.com>
Fixes #1191