Repository navigation
Remove order keys before comparing rows in a set operation to close #2610 - #2611
Merged
Merged
Conversation
…laSQL#2610 When both SELECTs of a set operation have their own ORDER BY, queryfn3 sorts the first SELECT before the set operation and writes the order keys $$$0, $$$1, ... into its rows for that sort. The keys were removed only at the end, after UNION / EXCEPT / INTERSECT had compared the rows with deepEqual, which fails when the number of keys differs. INTERSECT found no common rows, EXCEPT removed nothing and UNION kept duplicates. Delete the order keys right after that sort. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
# Conflicts: # src/38query.js
Add the cases from AlaSQL#2612 (EXCEPT with LIMIT 2, UNION with ORDER BY and no LIMIT, INTERSECT with no ORDER BY on the first SELECT) and run every query twice, so the cached statement is checked too. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Contributor
Author
|
@mathiasrw thank you for merging #2608 and #2609! About #2612: this PR is the fix for it. I filed the same bug as #2610 last night and opened this PR for it before #2612 came in, so the two issues are duplicates. Close whichever you prefer. I have updated this branch:
I understand you want #2582 merged first, since it rewrites the same block. There is no rush. Once #2582 is in, I will update this branch again and re-run everything. |
Member
|
Thank you @Anikesh0001 - great work, and thanks for being so quick. I closed #2612 as the duplicate, since you filed #2610 first. I also added an improvements to test D to have the rows sorted before comparing, so the test no longer depends on the order UNION returns them in. |
This was referenced Oct 20, 2026
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 #2610
Closes #2612
Problem
When both SELECTs of a set operation have their own
ORDER BY,INTERSECTfinds no common rows,EXCEPTremoves nothing, andUNIONkeeps duplicates. No error is raised. Reproduced ondevelop(4d9a956) with the statement cache off, so this is separate from #2605. The queries and results are in #2610.Cause
In
queryfn3(src/38query.js), when the right operand has its ownORDER BY/LIMIT, the first SELECT is sorted before the set operation. For that sort, the code writes the order keys$$$0,$$$1, ... into each of its rows. They were removed only at the end ofqueryfn3, after the set operation.arrayUnionDeep,arrayExceptDeepandarrayIntersectDeepcompare rows withdeepEqual, which returnsfalsewhen the number of keys differs. So{a: 1, $$$0: 1}never matched{a: 1}from the other SELECT.Change
Delete those order keys from the first SELECT's rows right after that sort. In this pattern the final
ORDER BYis not applied, so nothing reads the keys afterwards.query.removeKeysstill removes them at the end, as before. That is now a no-op for these rows.UNION ALLdoes not compare rows, so it was not affected. A test checks that it still keeps duplicates.Tests
test/test2610.jscovers the queries from #2610 and #2612. Each is run twice, so the cached statement is checked too (#2605):INTERSECTwithORDER BY+LIMIT, and withORDER BYonlyEXCEPTwithLIMIT 2andLIMIT 3on the first SELECTUNIONremoves the duplicate, with and withoutLIMITUNION ALLkeeps the duplicate (guard test)INTERSECTwith noORDER BYon the first SELECT (guard test)Results, after merging
develop(with #2608 and #2609) into this branch:bun test ./test/test2610.jswithdevelop'ssrc/38query.js: 2 pass (F, G), 5 fail (A–E).bun test ./test/test2610.js ./test/test2605.js ./test/test2603.jswith this change: 19 pass, 0 fail.yarn test-only(full suite, aftersh build.sh): 2286 pass, 213 skip, 0 fail.prettier --list-differenton both changed files: clean.I did not run
yarn test-browser.Overlap with #2582
#2582 rewrites the same block of
queryfn3. Once it is merged, I will update this branch again and re-run the tests.Note: I used Claude Code to help investigate this issue and draft the change and tests. I reviewed the result, checked which tests fail without the fix, and ran the checks above locally.
🤖 Generated with Claude Code