-
Notifications
You must be signed in to change notification settings - Fork 36
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
[TECH DEBT]: Fix ORDER BY
precedence in TSQL grammar
#1256
Comments
Some playing around to explore some of the (SELECT 10 ORDER BY 1) UNION SELECT 20
Let's try a subquery: SELECT * FROM (SELECT 10 ORDER BY 1) UNION SELECT 20
Drop in a SELECT * FROM (SELECT TOP(1) 10 ORDER BY 1) UNION SELECT 20
Extract to a CTE though and it's okay: WITH x AS (SELECT TOP(1) 10 AS n ORDER BY 1)
SELECT * FROM x UNION SELECT 20
|
Cool. Good research, which will help me rejig the grammar to support teh correct precedence (there basically isn't any right now!). Just one point to make in case it isn't me that changes the grammar: We decided very early on to assume that the SQL given to us is syntactically and semantically correct, as we are a transpiler - garbage in, garbage out. So, we don't need to report contextual/semantic errors like the TSQL engine will. Hence we don't try to write grammar rules that tortuously enforces syntax. One should not write grammars like this anyway, we write a grammar that accepts anything that looks like it could be valid, then apply a semantic pass to throw out errors like 'not valid in views' - which we won't do in the transpiler of course as the input code should already be verified accurate. |
This PR updates where we handle `ORDER BY` clauses in TSql: these should be applied after set operations to the complete sequence, not to each individual query within a sequence of set operations. References: - https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16#syntax - https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql?view=sql-server-ver16#c-using-union-of-two-select-statements-with-order-by Tested: - [x] Updated unit tests - [x] Transpiler tests - [X] Functional tests Blocked by #1350. Resolves #1256.
This PR updates where we handle `ORDER BY` clauses in TSql: these should be applied after set operations to the complete sequence, not to each individual query within a sequence of set operations. References: - https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16#syntax - https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql?view=sql-server-ver16#c-using-union-of-two-select-statements-with-order-by Tested: - [x] Updated unit tests - [x] Transpiler tests - [X] Functional tests Blocked by #1350. Resolves #1256.
This PR updates where we handle `ORDER BY` clauses in TSql: these should be applied after set operations to the complete sequence, not to each individual query within a sequence of set operations. References: - https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16#syntax - https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql?view=sql-server-ver16#c-using-union-of-two-select-statements-with-order-by Tested: - [x] Updated unit tests - [x] Transpiler tests - [X] Functional tests Blocked by databrickslabs#1350. Resolves databrickslabs#1256.
This PR updates where we handle `ORDER BY` clauses in TSql: these should be applied after set operations to the complete sequence, not to each individual query within a sequence of set operations. References: - https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16#syntax - https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql?view=sql-server-ver16#c-using-union-of-two-select-statements-with-order-by Tested: - [x] Updated unit tests - [x] Transpiler tests - [X] Functional tests Blocked by databrickslabs#1350. Resolves databrickslabs#1256.
Current Behavior
The TSQL grammar currently allows for
ORDER BY
expressions between set operations (INTERSECT
,EXCEPT
,UNION
) and associates them with the intermediate expression.Expected Behavior
For TSQL the
ORDER BY
clause is applied after all the set operations: it should not be attached to the immediately preceding expression. That is:is equivalent to:
and not this as currently is the case:
Reference: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql?view=sql-server-ver16#c-using-union-of-two-select-statements-with-order-by
Further to this, the following is erroneous:
The documentation refers to this as incorrect which is a little ambiguous: it's open to read that rather than being an error it could be accepted but pointless. Testing shows that it is treated as an error:
yields
Functional Test
This functional test should pass if
ORDER BY
has been implemented correctly.The text was updated successfully, but these errors were encountered: