Skip to content

Compare the rows of set operations and DISTINCT with one key function #3167

Backlog

Description

@mathiasrw

What happens today

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

  1. Add compare.rowKey(tuple) to src/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 and undefined stay apart. Dates compare by their time value, NaN equals NaN, other objects use their JSON.stringify text.
  2. 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.
  3. 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.
  4. UNION, EXCEPT and INTERSECT build a Set of 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

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-ops is faster and no other group slows down beyond the noise described in test/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.

Depends on

#2712, #3162 and #3163

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions