-
Notifications
You must be signed in to change notification settings - Fork 66
Fix the ORDER BY does not resolve alias from SELECT statement. #1831
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
Conversation
4b75ac5
to
7ffa5e3
Compare
CROSS-COMMIT-REPORT-PARTIQLCORE ✅
Testing DetailsResult Details
Now Passing TestsThe following 9 test(s) were previously FAILING in BASE but are now PASSING in TARGET. Before merging, confirm they are intended to pass: Click here to see
Now Passing TestsThe following 4 test(s) were previously IGNORED in BASE but are now PASSING in TARGET. Before merging, confirm they are intended to pass: Click here to see
CROSS-COMMIT-REPORT-PARTIQLEXTENDED ✅
Testing DetailsResult Details
|
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
ea6cdd3
to
b7c0f57
Compare
partiql-planner/src/main/kotlin/org/partiql/planner/internal/SqlPlanner.kt
Outdated
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
...l-planner/src/test/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupportTest.kt
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
d491400
to
5662a2f
Compare
44f859d
to
42db1de
Compare
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
val parentStack: ArrayDeque<ExprQuerySet> = ArrayDeque(), | ||
val aliasMap: MutableMap<ExprQuerySet, MutableMap<String, Expr>> = mutableMapOf() |
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.
In the prior review, you were using an only ArrayDeque()
as the context. Why are we switching to use a parentStack
and an aliasMap
. From my understanding, it seems like after we exit from some nested context and perform the rewrite over the ORDER BY
sets we don't need to keep that alias mapping anymore.
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 old approach works fine for nested queries but works 'blindly' for set operators as orderby belongs to a querysetExpr without SFW. The new approach provide a better structure awareness context for orderby and allow it to explicitly skip for set op.
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.
I'm still a bit confused on the changes to the rewriter's context (specifically in this commit 6412350). Previously was an ArrayDeque<MutableMap<String, Expr>>
but now you've changed it to a stack and an alias map/list. It seems redundant to store the alias map after visiting an inner SFW where it might not be used anymore in the outer SFW.
The old approach works fine for nested queries but works 'blindly' for set operators as orderby belongs to a querysetExpr without SFW.
What do you mean by "blindly"? There are ways in the visitor pattern to skip over set ops.
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 me explain a bit in detail.
In ArrayDeque<MutableMap<String, Expr>>
old approach,
Nested query
stack(empty)
ExprQuerySet:(stack(map1))
body:SFW
select:
project 1(add to map1))
from:
ExprQuerySet: stack(map1, map2)
body:SFW
select:
project 2 (add to map2))
from: T
orderby: Expr 2 (replace from map2)
stack.pop() // stack(map1)
orderby:Expr 1 (replace from map1)
stack.pop() // stack(empty)
Setop Query
stack(empty)
ExprQuerySet:(stack(map1))
body:op
lhs:ExprQuerySet // stack(map1,map2)
select:
project 1(add to map2))
from: T
stack.pop()
rhs:ExprQuerySet stack(map1,map3)
select:
project 1(add to map3))
from: T
stack.pop() // (stack(map1))
orderby:Expr 1 (replace from map1) // map1 is empty as there is always no `SELECT`.
stack.pop() // stack(empty)
The reason I swithed to have a parent stack is
- The old approach skipped orderby "blindly" as map is empty and without knowing if it belongs to ExprQuerySet which has setOp body.
There are ways in the visitor pattern to skip over set ops.
- We should not skip setop (inner query main contains order by) but skip order by when its parent has a setop body. The new
parentStack
approach give some additional information. - It is true that for now outer SFW does not need alias context of inner SFW. It looks different sql system has different behavior of the final table when set is present. Some uses the result of first SFW as final table schema which means outer SFW can have reference from first SFW. It is quite ambiguous since we do not have partiql specification for set op. I can potentially can use a stack with parent and alias map instead.
...l-planner/src/test/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupportTest.kt
Outdated
Show resolved
Hide resolved
test/partiql-tests-runner/src/test/resources/config/eval/skip-eval-core.txt
Outdated
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/SqlPlanner.kt
Outdated
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
partiql-planner/src/main/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupport.kt
Outdated
Show resolved
Hide resolved
...l-planner/src/test/kotlin/org/partiql/planner/internal/transforms/OrderByAliasSupportTest.kt
Outdated
Show resolved
Hide resolved
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.
Think we're almost there. Some minor comments
Relevant Issues
Fix the ORDER BY does not resolve alias from SELECT statement.
Description
Before
Now
How it works now is reference in order by will be replaced if there is projection in select.
will be rewritten to
SELECT a + b AS c FROM T ORDER BY c
SELECT a + b AS c FROM T ORDER BY a + b
It also fixed nested query
New issue Order BY does not work with aggregation function as alias.
Other Information
License Information
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.