Repository navigation
Backlog
Labels
Milestone
Description
What happens today
doDistinct(src/select/queryfn.js, lines 564-584) builds a text key from the row values joined with a backtick, objects throughJSON.stringify, and keeps the last row of each key.- UNION, EXCEPT and INTERSECT (
queryfn3, lines 308-405) callarrayUnionDeep,arrayExceptDeepandarrayIntersectDeep(src/utils/helpers.js, lines 16-71). They compare every row with every row throughdeepEqual, so the time is N x M, and any extra property on a row takes part (INTERSECT, EXCEPT and UNION compare rows wrongly when both SELECTs have their own ORDER BY #2610, Remove order keys before comparing rows in a set operation to close #2610 #2611).
The two rules disagree.
| Values | DISTINCT today | Set operations today |
|---|---|---|
1 and '1' |
same | different |
NULL and 'null' |
same | different |
undefined and '' |
same | different |
NaN and NaN |
same | different |
| two dates with different times | different | same |
'x`y', 'z' and 'x', 'y`z' (two columns) |
same | different |
What to change
- Add
compare.rowKey(tuple)tosrc/engine/compare.js. Each value becomes a type tag plus its text, with a length for text values, so types stay apart and no separator can collide. NULL andundefinedstay apart. Dates compare by their time value,NaNequalsNaN, other objects use theirJSON.stringifytext. - Build the key from the values in output-column order. Where rows are still objects, read them in that order. No sort key or helper value is part of the tuple.
- DISTINCT keeps the last row of each key. Rows whose key is integer-like today (one column, a non-negative integer or such a string) come first in ascending order, then the others in first-seen order.
- UNION, EXCEPT and INTERSECT build a
Setof the keys of one side and look up the other. UNION returns all rows of the right side, then the left rows whose key is new. EXCEPT and INTERSECT return the left rows, duplicates included, whose key is not or is in the right side. The chain stays evaluated right to left.
distinctArray for SELECT COLUMN DISTINCT, the DISTINCT ON key and the recursive CTE key stay as they are. DISTINCT and set operations on tuples are done here, not in #3162.
What stays the same
- Every result except the rows of the table above, which follow the rule "types apart, dates by value".
- Row order and the right-to-left order of a chain.
- Duplicates inside the right side of UNION (UNION removes duplicates in N × M time and keeps duplicates of the second SELECT #2744) and duplicates of the left side of EXCEPT and INTERSECT.
- CORRESPONDING.
Done when
- A new test file named after this issue covers each row of the table for DISTINCT, UNION, EXCEPT and INTERSECT, the DISTINCT order of integer values, a chain of three set operations, and both SELECTs with their own ORDER BY.
bun test/performance/bench.js --group=set-opsis faster and no other group slows down beyond the noise described intest/performance/README.md.- The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Activity