Repository navigation
fix(collection): LIST distinct keeps first-occurrence order; -0.0 equals 0.0 in float vector cells - #1372
Conversation
singaraiona
left a comment
There was a problem hiding this comment.
Review of 4087250. No correctness bug in the change.
- Order: first-occurrence order holds on every LIST route at
-c 1and-c 8, against a 200k-row oracle: distinct, group keys,by:keys, union/except/sect both ways, per-group distinct and count distinct. - -0.0: a constant-hash oracle build over 470 seeds (mixed F64/F32 cells with ±0.0, NaN and null, long cells at the 4096-byte edges, nested, dict and table cells, TIMESPAN mixes, 70k-row parallel paths) is byte-identical to this build, so no equal pair hashes differently.
- Tests: reverting each commit fails its tests.
Two items:
- The docs overstate the order. Typed numeric and temporal
distinctreturns ascending order, on dev and here:(distinct [3 1 3 2])is[1 2 3],(distinct [2024.01.02 2024.01.01])is ascending, and only SYM, STR and LIST keep first occurrence. The new math.md line and the functions.md row say vectors keep first occurrence; say it for lists, and leave typed order as it is documented today. I've raised the typed-order question separately, since all-functions.md already says "preserving order". - Two more answer changes to list (both right under the new rule):
(diverse (list [0.0] (enlist (neg 0.0))))goes true → false;- table
distinctwith -0.0 and 0.0 cells,(count (distinct (table [k] (list (list [7 7] [0.0] (enlist (neg 0.0))))))), goes 3 → 2.
Not blocking: F64x200 cells are about 15% slower (the disclosed long-cell scan), and group over F32x16 cells at -c 8 is about 9% slower (64-byte cells, short path).
Found on the way, already on dev, filed separately: joins on LIST key columns compare cell pointers (wrong matches); find with a LIST needle of vector cells; pivot over a LIST column.
4087250 to
1b42dd0
Compare
|
Thanks. Both items are done in 1b42dd0 and in the body. The branch is rebased onto dev da47b28.
The rebase crosses #1413 and #1376, which also change LIST key hashing. On the new base, |
1b42dd0 to
d20fee9
Compare
|
Rebased onto dev 8c18b24.
|
…d vector (#1478) ## Bug `distinct` over a numeric or temporal vector returned its values sorted, not in first-occurrence order (#1477): - `(distinct [3 1 3 2])` gave `[1 2 3]`; - `(distinct [true false true])` gave `[false true]`; - `select … by:` distinct came back sorted per group. SYM, STR, GUID and LIST keep first occurrence, and the docs say "Remove duplicates, preserving order". anton decided A on the board (T-707): every typed vector keeps first-occurrence order. ## Cause `distinct_vec_eager` (`src/ops/collection.c`) collected one first-occurrence row id per value. For non-SYM, non-STR, non-GUID types it then sorted those ids by value (`distinct_sort_indices`, a qsort with a per-type comparator) before gathering them. The sort-index fast path (`ray_index_distinct_ids`) returns one first-occurrence row per value, in value order. ## Fix - The value sort and its comparator are gone. The hash passes (hashset, GUID set, SYM and dict-STR presence arrays) and the parallel radix kernel (`distinct_radix_first_ids`, which sweeps its first-row flags by row) already give the ids in row order. - The sort-index fast path puts its rows back in row order: `distinct_ids_in_row_order`, a bitmap over the rows, or a qsort for at most 64 ids. - `select`, `by:`, lazy chains (`OP_DISTINCT`) and parted columns all go through the same function. - A string atom's `distinct` (unique characters, sorted) is not a typed vector and is unchanged. LIST order is #1361 (#1372). ## Performance 10M rows, release, best of 3, two runs (ms). The machine was loaded by other test runs (load 23–44). | | 1 core dev | 1 core PR | multi-core dev | multi-core PR | |---|---|---|---|---| | I64, 1k distinct values | 22.9 | 15.9 | 10.2 | 9.9 | | I64, 1M distinct values | 91.5 | 34.9 | 69.6 | 19.1 | | I64, all distinct | 399.4 | 68.1 | 353.3 | 46.7 | | F64, 100k distinct values | 97.1 | 102.5 | 97.5 | 94.4 | | I64, 1M distinct, sort index | 85.8 | 83.9 | 81.6 | 92.9 | | `select {d: (distinct v) by: k}` | 56.0 | 55.8 | 30.4 | 30.4 | Dropping the value sort removes an O(u log u) qsort. ## Tests - `test/rfl/collection/distinct_first_order.rfl` (new, `@also-unfused`) covers: - every typed vector, with nulls; - 200k–300k rows through the radix kernel, against an oracle: `group`'s first row of each value, in row order; - a null between duplicates; - a sort index, against the same oracle (it fails with the reordering removed); - `select`, `by:`, a lazy chain, `count` and a parted table. - Tests that pinned the sorted order now expect first-occurrence order: - `test/rfl/collection/distinct.rfl`, `collection_branch_cov.rfl`, `null/distinct.rfl`, `lazy/chains.rfl`, `ops/idiom.rfl`, `ops/idiom_branch_cov.rfl`, `ops/idx_route.rfl`, `agg/count_distinct.rfl`, `group/strlen_grow.rfl` and `agg/first_last_positional.rfl` (the last one keeps its null-first input); - `test/test_idx_route.c` `distinct_order_contract` is now `{5,1,9,3,7}`; - `test/test_group_extra.c` `count_distinct_runs_partitions` now checks that 1M all-distinct values come back equal to the input. - Full suite: 5588 of 5592 passed (4 skipped, 0 failed). ## Docs - `docs/docs/reference/all-functions.md` and `docs/docs/language/functions.md`: the `distinct` row states the rule, with `(distinct [3 1 3 2])` → `[3 1 2]`. `mkdocs build --strict` passes. Fixes #1477
distinct over a LIST deduplicated in first-occurrence order and then moved the atoms ahead of the vectors, which also scrambled the vectors: (distinct (list [1] [2] 3 [4] 5)) gave [3 5 [1] [4] [2]]. Drop the reorder so a list keeps the order the docs describe and typed vectors already give. Refs #1361.
atom_eq compared two F64 or F32 vector cells by memcmp and ray_key_hash hashed their bytes, so [0.0] and (enlist (neg 0.0)) were two cells in distinct, except, sect, union, group, select by:, count distinct, xkey and keyed-table ==, while the atoms and identical call them equal. Compare float cells by memcmp and, only when the bytes differ, on their bits with -0.0 cleared to +0.0: an integer mask the release build's -fno-signed-zeros cannot fold. Hash a cell of up to 64 bytes by mixing those bits directly, 16 bytes a step; a longer one is hashed in place a 4096-byte chunk at a time, through a normalising copy only for a chunk that holds a -0.0. NaN bits are kept, so the F64 null still equals only itself. Other vector cells keep memcmp and byte hashing. Refs #1361.
d20fee9 to
9015fea
Compare
|
Rebased onto dev 676ff3c, after #1478. Only the three docs files conflicted, and I resolved them by keeping #1478's text and its
Suites: |
singaraiona
left a comment
There was a problem hiding this comment.
Verified at 9015fea, rebased onto dev after #1478; the src changes are identical to the reviewed d20fee9.
- LIST order: LIST
distinctkeeps first-occurrence order (1242 configs against the first-occurrence oracle). - Signed zero: -0.0 equals 0.0 in F64/F32 vector cells and nothing else merges (600-seed model oracle).
- Speed and tests: no slowdown, and the test fails on dev.
Approved.
Fixes #1361
Two fixes, one commit each.
1. distinct over a LIST keeps first-occurrence order
Cause.
ray_distinct_fn(src/ops/collection.c:2497-2508 on dev e3dbd9c) dedups LIST cells in first-occurrence order and then runs a swap loop that moves atoms ahead of vectors. That breaks the documented "Remove duplicates, preserving order" and also scrambles the vectors:(distinct (list [1] [2] 3 [4] 5))gave[3 5 [1] [4] [2]].Fix. The swap loop is gone. The scan path (under 64 cell pairs) and the hashed path (
list_set_t) both already keep cells in order.Answers that change. Any LIST distinct holding both atoms and vectors now comes back in input order:
(distinct (list [1] [2] 3 [4] 5))is[[1] [2] 3 [4] 5], and(distinct (list [3i 3i] 2i [3i 3i] 2i))is(list [3i 3i] 2i). Four existing pins expected the old order and are updated with a comment pointing here: distinct.rfl:32, collection_branch_cov.rfl:699, list_cell_sets.rfl:356-358 (the 3000-cell MK set), and distinct_exact_int.rfl:10 (added by #1453, which pinned atoms ahead of vector cells).2. -0.0 equals 0.0 in F64 and F32 vector cells
Cause.
atom_eqcompared two vector cells of one type withmemcmp(src/ops/collection.c:1866), andkey_hashhashed their bytes (collection.c:2061). Between atoms, and underidentical, -0.0 equals 0.0. Inside vector cells it did not, so[0.0]and(enlist (neg 0.0))stayed two cells in distinct, except, sect, union, group,select ... by:, count distinct, xkey and keyed-table==. Both of those functions are shared by every one of these consumers: the list sets, group's LIST path, the grouped and parallel LIST key paths in agg_engine.c, xkey and keyed==in tblop.c and cmp.c, and the table and dict column compares throughatom_eq_cell_col. Nested lists recurse through the same two functions, so fixing them fixes every consumer at any depth. The other hashes a vector cell can reach agree already:ray_atom_hash(dict cells) andhs_hash_row's LIST arm hash a vector by type and length only, andspan_vec_hash(a TIMESPAN in the set) rounds each element throughray_hash_f64, which already clears -0.0.Fix.
memcmp. Equal bytes are equal cells, which is the common case on a hash hit. Only when the bytes differ is each element compared on its bits, with -0.0 cleared byb & ((b == SIGN) - 1). That mask is integer arithmetic, so the release build's-fno-signed-zeroscannot fold it away the way it can foldv + 0.0. NaN bits are left alone, so the F64 null still equals only the same null bits, as before.ray_hash_bytes. A chunk that holds one goes through a cold normalising copy and is hashed as the +0.0 chunk would be.memcmpand the byte hash.Answers that change.
group,
select ... by:and count distinct over such cells merge them, at any core count. The parallel directory path was checked at -c 8 with 200k rows. Keyed==with a -0.0 key now finds the 0.0 key. Existing pins updated, each with a comment pointing here: xkey_vector_keys.rfl:15, container_equality_vector_keys.rfl:20, list_cell_sets.rfl:197-211 (rule 8) and span_vec_cell_hash.rfl:341, 342, 368.Not changed:
in. The issue's(in (list (enlist (neg 0.0))) (list [0.0]))is still[[false]].inwith a list on the left tests each leaf atom, and an atom never equals a vector cell, so(in (list [0.0]) (list [0.0]))is[[false]]on dev too. The leaf -0.0 already matches a 0.0 atom ((in (list N) (list 0.0))is[[true]]). Getting[[true]]for the issue's example would mean changingin's leaf rule, which is outside this fix.Docs
distinct: built on fix(collection): distinct keeps first-occurrence order for every typed vector #1478's text (every type keeps first-occurrence order,(distinct [3 1 3 2 1])is[3 1 2]). It adds that a list keeps each cell where it first occurs, whether its cells are atoms, vectors or nested lists, and the cell equality rule shared by distinct, except, sect, union, group, xkey and keyed==: in F64 and F32 vector cells -0.0 equals 0.0, and NaN equals only NaN.Tests
New test/rfl/collection/list_cell_rules.rfl, with
@also-unfused. It fails on dev at its first line in both parts: the order cases, and with the order section removed, at(count (distinct (list P N))). It covers:select ... by:, count distinct (grouped and whole), xkey and keyed==. Cells are nested in lists, dicts and tables.[1.0]against[-1.0](only -0.0 is cleared, not every sign bit).identicalto the hashed distinct over 51 copies. group, except, sect and union over the same copies must agree with it.The file also runs in the release-flavour binary (
make test-release). The issue's examples give the new answers from both a clang and a gcc release binary, and group,select ... by:, count distinct, except, sect and union over 200k cells agree at-c 1and-c 8in the clang release binary. Together these confirm the normalisation survives-fno-signed-zeros.Mutations
Each mutant was built into the test binary and run against list_cell_rules.rfl. These fail:
The hash-only mutant is also caught by the differential alone.
Three mutants survive, and none is a wrong answer through equality:
Both still give equal cells equal hashes, so they only weaken the hash.
That one writes past the 8-word buffer, which the non-sanitised test binary does not detect.
Timings
Release build (clang), min of 7 to 15 runs,
taskset -c 4 ./rayforce -c 1, in ms. The baseline is dev's collection.c built in the same tree. A separately built dev tree measured up to 12% apart from it on atom-only paths with identical source, so cross-tree numbers are not comparable at that level. The cells are 1M (F,I: two elements, 97k distinct) unless noted.All cores (28): distinct, in and except are serial and match the table. group over F64 and I64 cells is about 5.0 ms on both, and select by: is 4.3-4.5 ms on both.
Cells of up to 64 bytes are as fast as before or faster. Longer float cells pay the one compare per element that finds a -0.0, about 2.7 ns per 16-element cell. I64 cells are within run-to-run noise.
Independent review
make release, because it is integer masking that-fno-signed-zeroscan't remove.The cost of the rule, measured. Hashing a float cell longer than 64 bytes now includes one compare per element to find a -0.0, which is the per-element cost the rule was accepted with. That makes such cells hash about 1.4-1.6x slower than on dev. For example, an F64 cell of 200 values goes from 40 to 58 ns, and
distinctover 2000 cells of 2000 F64 values from about 2.2 to 3.4 ms. Unrelated to that scan, an unequal float-cell compare costs about 0.7 ns more. Cells of 64 bytes or less are as fast or faster than on dev, and so are non-float cells.Suites
On dev 676ff3c (rebased over #1478; only its three docs files conflicted):
make test(clang): 5590 of 5594 passed, 4 skipped, 0 failed.make clean && make CC=gcc test: 5590 of 5594 passed, 4 skipped, 0 failed.make CC=gcc release: clean.make release && make test-release(ASan+UBSan): 7062 of 7066 passed, 4 skipped, 0 failed.