Skip to content

Remove order keys before comparing rows in a set operation to close #2610 - #2611

Merged
mathiasrw merged 5 commits into
AlaSQL:developfrom
Anikesh0001:fix/setop-order-keys
Oct 20, 2026
Merged

mathiasrw merged 5 commits into
AlaSQL:developfrom
Anikesh0001:fix/setop-order-keys

Conversation

@Anikesh0001

@Anikesh0001 Anikesh0001 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2610
Closes #2612

Problem

When both SELECTs of a set operation have their own ORDER BY, INTERSECT finds no common rows, EXCEPT removes nothing, and UNION keeps duplicates. No error is raised. Reproduced on develop (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 own ORDER 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 of queryfn3, after the set operation. arrayUnionDeep, arrayExceptDeep and arrayIntersectDeep compare rows with deepEqual, which returns false when 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 BY is not applied, so nothing reads the keys afterwards. query.removeKeys still removes them at the end, as before. That is now a no-op for these rows.

UNION ALL does not compare rows, so it was not affected. A test checks that it still keeps duplicates.

Tests

test/test2610.js covers the queries from #2610 and #2612. Each is run twice, so the cached statement is checked too (#2605):

  • A, B: INTERSECT with ORDER BY + LIMIT, and with ORDER BY only
  • C: EXCEPT with LIMIT 2 and LIMIT 3 on the first SELECT
  • D, E: UNION removes the duplicate, with and without LIMIT
  • F: UNION ALL keeps the duplicate (guard test)
  • G: INTERSECT with no ORDER BY on the first SELECT (guard test)

Results, after merging develop (with #2608 and #2609) into this branch:

  • bun test ./test/test2610.js with develop's src/38query.js: 2 pass (F, G), 5 fail (A–E).
  • bun test ./test/test2610.js ./test/test2605.js ./test/test2603.js with this change: 19 pass, 0 fail.
  • yarn test-only (full suite, after sh build.sh): 2286 pass, 213 skip, 0 fail.
  • prettier --list-different on 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

…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>
Anikesh0001 and others added 2 commits October 20, 2026 11:37
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>
@Anikesh0001

Copy link
Copy Markdown
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.

@mathiasrw

mathiasrw commented Oct 20, 2026 •

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants