Repository navigation
Conversation
…art with LIMIT and ORDER BY positions (#2712)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2712. Fixes #2711, #3134, #3135 and #3136.
The problem
A part of a set operation written in parentheses did not keep its own ORDER BY, LIMIT, OFFSET, TOP or set operation. The grammar copied the clauses of the statement onto the first SELECT, and
queryfn3guessed where to apply them.With
A= 3, 1, 2,B= 9, 8, 2 andC= 5, 4. SQLite 3.43 needsSELECT * FROM (...)around a part in parentheses.(SELECT a FROM A ORDER BY a LIMIT 1) UNION ALL SELECT a FROM BSELECT a FROM A UNION ALL (SELECT a FROM C ORDER BY a LIMIT 1) ORDER BY a DESC(SELECT 1 AS a UNION ALL SELECT 2) UNION ALL SELECT 3(SELECT 1 AS a UNION ALL SELECT 2) EXCEPT SELECT 1SELECT a FROM A UNION (SELECT a FROM B ORDER BY a LIMIT 1) UNION SELECT a FROM A ORDER BY aWhat changed
src/alasqlparser.jisonhasSelect,SelectBodyandSelectCorein place of the threeSelectalternatives,SelectWithoutOrderOrLimit,UnionClause,UnionableSelect, bothParenthesizedSelectrules andStatement: ParenthesizedSelect.SelectBodyis right recursive, so a chain keeps its right-to-left order. The chain is built as before (head.unionall = rest).yy.headOfPart(src/engine/yy.js) gives the head ofpart UNION rest. When a part in parentheses has its own LIMIT, TOP or set operation, the head is a new SELECT that reads the part as its only source, likeSELECT * FROM (part). It takes over the modifier and INTO of the part, and itstoStringprints the part as written.queryfn3losesunionBranchHasOrder,orderApplied,limitAppliedand the first copy of the sort-key block. ORDER BY and LIMIT of a query now always run after its set operation.compileInScopeloses the fourif (!query.orderfn && this.union.order)blocks. No test and no result of the parity check changed.subqueryDatafnpassesquery.oldscopefor that head only), andisCorrelatedlooks into that part. So the part can read the outer row in EXISTS, IN, a scalar subquery and APPLY, and IN does not keep the answer of the first row. Other subqueries in FROM and JOIN are as on main. A correlated one still throws a TypeError, which is left to Compile every subquery to one form with a kind and correlation from the binding, and read FROM subqueries, views and CTEs through one table source #3164, and an IN over an uncorrelated one still runs it once.compileOrdercounts a position in the columns of the first SELECT, also behind a new head. So(SELECT a FROM A LIMIT 3) UNION ALL SELECT a FROM B ORDER BY 1sorts.Where this differs from the issue text
SelectPrimary: SelectClause ... | LPAR Select RPAR. With that rule,SELECT ((SELECT 1) UNION ALL SELECT 2)anda IN ((SELECT ...) UNION ALL SELECT ...)became parse errors. After( ( Select )the parser must choose between a subquery expression and a part of a set operation, and jison chose the expression. SoLPAR Select RPARandLPAR Select RPAR UnionOp SelectBodyare alternatives ofSelectBody. The set operator is then a shift, as it was before. This also tells the action that the part is in parentheses without a marker.headOfPart, with the comment "removed in a later breaking step".SELECT TOP 1 a FROM A UNION ALL SELECT TOP 1 a FROM Bgives 3, 9 on main, asqueryfn3kept the TOP to the first SELECT when the next part had an ORDER BY, LIMIT or TOP. Without such a part the TOP limits the whole result (SELECT TOP 2 a FROM A UNION ALL SELECT a FROM Cgives 3, 1). Both stay. A first SELECT with TOP and no parentheses gets the new head when a later part has an ORDER BY, LIMIT or TOP, and is under the same 4.x condition otherwise. On main only the next part counted, and here every later part counts, as for the ORDER BY. TOP of a SELECT in a set operation also limits the SELECTs after it #3196 has the rest of this.Grammar conflicts
yarn jisonreports 243 conflicts on main (e82aab0) and 106 with this change.Tests
test/test2712.jsis new. It covers (SELECT ... LIMIT 1) UNION ALL SELECT ... applies the LIMIT to the whole result #2711, ORDER BY after a parenthesised part with its own LIMIT sorts only the rows of the first part #3134, A parenthesised UNION ALL followed by another UNION ALL loses rows of the parenthesised part #3135, EXCEPT after a parenthesised UNION ALL is ignored #3136 and a part in the middle of a chain for UNION, UNION ALL, EXCEPT and INTERSECT, with and without CORRESPONDING, with the rows of SQLite 3.43. Each statement runs twice throughalasql.compile. 12 of its 15 cases fail on main. The other three hold what stays as it was. Case O counts the calls of a user function in an IN over a subquery in FROM with a JOIN, so the subquery runs once. Cases K to N are a correlated part in parentheses, TOP in each SELECT without parentheses, a LIMIT after a set operation in parentheses as a later part, and a position in the ORDER BY of the statement.test/test2414.jsD, E and N expect SQLite's rows. P does too, as it is A parenthesised UNION ALL followed by another UNION ALL loses rows of the parenthesised part #3135.test/test1965.jsH does too, as it is ORDER BY after a parenthesised part with its own LIMIT sorts only the rows of the first part #3134.test/dialect/expected-differencesloses 15 entries that now give SQLite's answer. 30 entries have a new alasql answer. Each of these 30 gives the same rows as its spelling withSELECT * FROM (part), so what is left is the right-to-left order (Evaluate set operations left to right with INTERSECT binding tighter, with a dialect keeping the old order #202) and the duplicate rows of UNION and EXCEPT (UNION removes duplicates in N × M time and keeps duplicates of the second SELECT #2744, Compare the rows of set operations and DISTINCT with one key function #3167).yarn test-parser, the dialect test, the run-state check and the binding check pass.Old against new
A matrix of 3,004 statements ran on the build of main and on this build, each twice through
alasql.compile, and in SQLite 3.43. It has every pair of parts (plain, in parentheses, with ORDER BY, with LIMIT, TOP or OFFSET) for the four operators with and without CORRESPONDING, chains of three, and set operations in parentheses on either side, each with and without ORDER BY and LIMIT on the statement.No statement outside these bugs changed, also not in row order.
A second matrix of 2,880 statements has TOP with and without parentheses in the first, middle and last part, against parts with ORDER BY and LIMIT, with a name or a position in the ORDER BY of the statement. SQLite ran it with each TOP as a LIMIT in
SELECT * FROM (part).90 correlated statements (EXISTS, NOT EXISTS, IN, NOT IN, = ANY, a scalar subquery, CROSS APPLY and OUTER APPLY over a part in parentheses with LIMIT, TOP or a set operation) give the rows of main in 74 cases. 9 throw a TypeError as on main (a correlated subquery in FROM that is not a part, #3164). 7 lost rows on main through #2711.
Parity differences
Reference is the merge base e82aab0. Outside
test/test2712.jsthe differences are results 19, errors 32, generated code 83 and only in the new recording 1, diverged 0. The reference also runstest/test2712.js, where 12 cases fail on it, which adds results 28, errors 1, generated code 21 and only in the new recording 86.test815.js(an xlsx date).r['$$$0']=r['a']), as it is no longer the head of the chain. In 13 the comparator thatcompileInScopecompiled from the ORDER BY of the next part is gone ((no code)). In 11 the statement has a new head, so it has more functions and they line up differently (3 against 5).DROP TABLE test8intest1965.js. Case H failed on main before it reached that line, as its expectation is updated.test/parity/api-allowed.txt. They arealasql.yy.setOperation,alasql.yy.headOfPartandalasql.yy.Select.fields(parenthesised).alasql.min.jsgoes from 548,109 to 546,737 bytes.Results (19)
Parse errors that are gone (32)
Generated code by file (83)
Two more result changes
SELECT TOP 1 * REMOVE COLUMN b FROM A UNION ALL SELECT TOP 1 a FROM Bnow hasb: undefinedin the first row, as the statement gets a new head and a subquery in FROM keeps a removed column as undefined (REMOVE COLUMN and PIVOT in a FROM subquery are lost: SELECT * FROM (SELECT * REMOVE COLUMN b FROM t) #3194).SELECT ((SELECT MAX(B.a) FROM B LIMIT 1) UNION ALL SELECT 0)as a scalar gives undefined, where main gives 9. Main already gives undefined without the LIMIT.Not done
Select.toStringstill prints the ORDER BY of a statement before its set operation, as on main.*does not sort, with and without a new head (ORDER BY of a set operation is ignored for some positions and for expressions over output columns #3147).