Skip to content

fix(union): the result is a set, the duplicates of the left operand go too - #1399

Open
vbmithr wants to merge 5 commits into
RayforceDB:devfrom
vbmithr:fix/union-dedup
Open

vbmithr wants to merge 5 commits into
RayforceDB:devfrom
vbmithr:fix/union-dedup

Conversation

@vbmithr

@vbmithr vbmithr commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

The owner decided (#1385, option A) that union is a set: (union [1 1 2] [2 3]) is [1 2 3]. The result is the distinct elements of the left operand, then those of the right it lacks, each once, in order of first appearance; it equals distinct of the concatenation. except and sect keep the duplicates of the left operand, as documented; the docs now say so side by side.

Both paths change: typed vectors (a hash set over the left operand, rows gathered only when it holds a duplicate) and boxed lists. On a span/point pair (TIMESTAMP or TIME against TIMESPAN) the left operand is deduplicated by its own cells, because the keys it is compared with can make two timestamps a day apart collide; the second hash set that needs is built only when two keys collide (equal cells have equal keys, so all-distinct keys mean no duplicate).

Answers that change: any union whose left operand had a duplicate. Three older tests pinned that (union_rhs_distinct, list_cell_sets, span_vec_cell_hash); for list operands they now assert identity with distinct of the concatenation, next to a few literal expectations so a change to distinct does not carry union along.

Performance, release build, best of 5, dev vs this branch (ms, laptop, -c 4): i64 5M distinct 585 vs 575, heavy duplicates 72 vs 70, moderate duplicates 467 vs 441, sym 38 vs 38, str 42 vs 38, f64 75 vs 75, empty left 315 vs 294, 30k-cell lists 0.62 vs 0.62; span/point (10M TIMESTAMP against a TIMESPAN) distinct left 596 vs 569, duplicate left 261 vs 259, 1000-row left 225 vs 226. The reviewer's example of an all-distinct left, which was 65% slower with an always-built second set, is now at parity. One run of the duplicate-left span/point case showed a 3.7 s outlier that did not repeat in four more runs (about 255-330 ms against dev's 255-262); I did not find a cause and put it down to system load, which I cannot prove.

Land order: after #1478 and #1372 (the distinct order) merge, this is rebased on dev, list_cell_rules.rfl is repinned for the set rule, and the docs conflicts are resolved by keeping their first-occurrence wording.

Closes #1385

@singaraiona singaraiona left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review of b0adcb2: clean.

  • Cause: ray_union_fn is the only implementation, and both its typed and list paths now keep first appearance. The span/point split (left deduplicated by its own cells, right probed by span keys) is right.
  • Oracle: 20 cases across every type, null/NaN/-0.0, span pairs both ways, mixed lists and empty operands. Each gives the same elements as (distinct (concat a b)) in order of first appearance.
  • Sanitizers and memory: ASan clean; memory flat over repeated calls.
  • Performance: within noise, except F64 at about +5%.
  • Tests: all four changed tests fail on dev.

Two small notes:

  • list_cell_sets and span_vec_cell_hash now compare union with distinct (concat …), so a change to distinct would silently carry union along. Keep a few literal expectations per rule.
  • The bench comment and the body name a private hostname; please drop it from bench/union_set.rfl.

This changes union's results for any left operand with duplicates, so it needs a maintainer decision; I've asked for one and will report back here. Please hold the merge until then.

@vbmithr

vbmithr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Both notes done: list_cell_sets and span_vec_cell_hash keep their distinct (concat ...) comparisons and now also carry a few literal expectations (counts and values per rule), so a change to distinct no longer carries union along; and the hostname is gone from bench/union_set.rfl and from the body. Holding the merge for the maintainers' decision on the changed results, as you said.

@singaraiona

Copy link
Copy Markdown
Collaborator

The owner has decided this one: union is a set, so its result drops duplicates of the left operand too, and equals distinct of the concatenation (option A). This PR's direction is accepted. Please make sure the docs and the except/sect notes state the difference clearly, and we'll finish the code review on that basis.

@vbmithr

vbmithr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Docs and notes done at 7fce041: the reference and the function table now say that union returns a set and that except and sect keep the duplicates of the left operand (with examples, and a pointer to distinct for a set from the other two), and union.rfl pins the three behaviours side by side with literal expectations.

@vbmithr

vbmithr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

CI note: build-and-test (macos-latest, debug, clang) failed on 7fce041 with one test, rfl/io/read_until_eof, hitting the 600 s worker timeout (5518 of 5567 passed, 48 skipped, 1 failed, 709 s). That test reads FIFOs, and its own header says a missing writer makes read block in fopen and hang rather than fail; nothing in this PR touches IO or that path, and the same job passed on the earlier head (b0adcb2), as did the Linux jobs here. It looks like a runner flake. I cannot re-run it (no admin rights); could a maintainer re-run the failed job? I did not push an empty commit to retrigger it.

@singaraiona

Copy link
Copy Markdown
Collaborator

Reviewed at 7fce041 against the owner's decision (union is a set). The semantics are right:

Before merge:

  1. The span/point route is 65% slower on an all-distinct left operand, because the left now always goes through a second hashset (hs_own). For example, (union (as 'TIMESTAMP (til 10000000)) (as 'TIMESPAN (% (til 10000000) 50000))) takes 801–826 ms on dev against 1355–1385 ms here. The body says the extra work runs only when the left has a duplicate, which isn't true on this route. Please skip the second pass when the left is duplicate-free (the hash insert can report it), or state the cost.
  2. Land order. fix(collection): distinct keeps first-occurrence order for every typed vector #1478 and fix(collection): LIST distinct keeps first-occurrence order; -0.0 equals 0.0 in float vector cells #1372 (both distinct order) merge first. Then rebase onto dev and repin list_cell_rules.rfl:84 ((count (union Z …)) -- 61 becomes 4 under the set rule). Resolve the docs conflicts in functions.md, all-functions.md and math.md by keeping their first-occurrence wording.
  3. Body and commit text. Cite the owner's decision rather than "decided by the principle", and drop the "a mixed list keeps its own order (distinct puts atoms first)" sentence, including from 06da44f's message, since fix(collection): LIST distinct keeps first-occurrence order; -0.0 equals 0.0 in float vector cells #1372 removes that. A squash keeps the message.
  4. Nits. Add one typed case above 65,536 rows pinned against a literal count, and refresh bench/union_set.rfl's numbers.

@vbmithr

vbmithr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Done at 6c267cf (rebased on current dev, three commits):

  1. Span/point slowdown: fixed. The second hash set is now built only when two keys collide, since equal cells have equal keys and all-distinct keys mean no duplicate. Your example (10M TIMESTAMP against a TIMESPAN, all-distinct left): dev 566-588 ms, this branch 569-583 ms (laptop, release, -c 4, best of 5), so the 65% is gone. Duplicate-left on that route: 261 vs 259 ms in the bench run, 272-327 vs 255-262 in four repeats; one first run showed a 3.7 s outlier that did not repeat and that I could not explain (load average was about 4 at the time), so treat the duplicate-left cost as "a few percent to ~20%".
  2. Land order: understood. fix(collection): distinct keeps first-occurrence order for every typed vector #1478 and fix(collection): LIST distinct keeps first-occurrence order; -0.0 equals 0.0 in float vector cells #1372 are not merged yet, and list_cell_rules.rfl does not exist on dev yet, so I will rebase, repin line 84 and resolve the docs conflicts (keeping their first-occurrence wording) once they land.
  3. Body and commit text: the body cites the owner's decision, the mixed-list sentence is gone from the body and from the commit messages (I rebuilt the three commits), and nothing in the tree mentions it.
  4. Nits: union.rfl has typed cases above 65,536 rows against literal counts (also lists, and a TIMESTAMP against a TIMESPAN with and without duplicates on the left), and bench/union_set.rfl has fresh numbers plus the span/point cases.

CI: the macOS debug job still shows the read_until_eof timeout on the earlier head and needs a re-run by a maintainer; this push starts new runs.

@vbmithr

vbmithr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

#1478 is merged, so I rebased on dev (clean, no docs conflicts yet): head is now ce6f1e9. The union, list_cell, span_vec_cell, span_point, distinct and collection tests pass on top of it. #1372 is still open and list_cell_rules.rfl is not on dev, so the repin of line 84 and any docs conflicts wait for it; I will rebase again when it lands. The push also restarts CI, which should replace the earlier macOS timeout.

…o too

The owner decided (RayforceDB#1385, option A) that union is a set: its result drops the duplicates
of the left operand as well, and is the distinct elements of the left operand followed by
those of the right it lacks, each once, in order of first appearance. (union [1 1 2] [2 3])
was [1 1 2 3]: only the right operand was checked, and the reference page says "Set union
(deduplicated)".

Both paths change: the typed vectors (a hash set over the left operand; its rows are
gathered only when it holds a duplicate) and the boxed lists.

On a span/point pair (a TIMESTAMP or TIME against a TIMESPAN) the left operand is told
apart by its own cells: the keys it is compared with at the other side can make two points
collide (two timestamps a day apart have one time of day), which the list path never did.
The second hash set is built only when two keys collide: equal cells have equal keys, so
a left whose keys are all distinct holds no duplicate.

Tests: union.rfl states the set invariants, with the per-type, boxed, hashed-size and null
cases, counts above a morsel (65,536 rows) against literals, and a TIMESTAMP against a
TIMESPAN with and without duplicates on the left. The assertions of union_rhs_distinct,
list_cell_sets and span_vec_cell_hash that pinned the duplicates say that, for list
operands, union is identical to distinct of the concatenation, next to a few literal
expectations so that a change to distinct does not carry union along.

Closes RayforceDB#1385
The reference and the function table state it, with examples, and a pointer to distinct
for a set from the other two. union.rfl pins the three behaviours side by side.
…s and span/point

Release build, best of 5, dev and the change side by side in the header: all within noise,
the span/point route included (10M TIMESTAMP against a TIMESPAN: distinct left 596 -> 569
ms, duplicate left 261 -> 259 ms).
(count (union Z (list [0.0 0.0] [1.0 0.0] P))) was 61: Z's 60 elements, duplicates
included, plus one new cell. A union is a set (RayforceDB#1385), so it is 4, as (union (list P) Z)
on the next line already was.
@vbmithr

vbmithr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

#1478 and #1372 are merged, so as planned I rebased on dev (head 06a48f2, four commits):

  • One conflict, in list_cell_sets.rfl, resolved by taking dev's distinct expectations for rule 8 (-0.0 and 0.0 now meet as vector cells, fix(collection): LIST distinct reorders cells, and vector cells treat -0.0 and 0.0 as different #1361) and keeping the union lines as the set-rule comparison with distinct of the concatenation. The docs applied without conflicts.
  • Repinned list_cell_rules.rfl:84: (count (union Z (list [0.0 0.0] [1.0 0.0] P))) is 4 under the set rule (it was 61: Z's 60 elements plus one), as (union (list P) Z) on the next line already was. It is a separate small commit.
  • Tests: union, list_cell (all five, including list_cell_rules), span_vec_cell, span_point, distinct, collection/, and the null, query, group, type, sort and lang directories all pass on the fast flavour. Not the full suite, and not ASan/UBSan: fafnir is unreachable from here.

@singaraiona singaraiona left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at 06a48f2, against the owner's decision that union is a set.

  • Correctness: (union a b) equals (distinct (concat a b)) across every type and type pair, both orders, nulls, -0.0/NaN, and 12 to 100k rows, at -c 1/28 and under ASan, with 0 mismatches.
  • Speed: the span/point all-distinct case is back at dev speed. A duplicate-heavy left operand pays about 10% for the dedup the decision requires.
  • Tests, docs and naming: the tests fail on dev, the docs are right, and naming is clean.

Approved; thanks for the follow-up.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

union keeps the duplicates of its left operand, the reference page says it is deduplicated

2 participants