Skip to content

fix(group): a SYM or STR group key is flagged has-nulls only when a key is null - #1821

Merged
singaraiona merged 1 commit into
devfrom
fix/1819-group-fallback-order
Nov 27, 2026
Merged

singaraiona merged 1 commit into
devfrom
fix/1819-group-fallback-order

Conversation

@singaraiona

Copy link
Copy Markdown
Collaborator

The defect

#1819 raised three ways an allocation failure changes a group's answer in its bytes. The values stay the same. Two of them are the strategy's order and stay as they are; one was a wrong attribute.

  • The has-nulls flag on SYM and STR keys (fixed). ray_group_gather builds the key columns of the v2 routes.
    • It set RAY_ATTR_HAS_NULLS whenever its source may have nulls. For a text column that is always, since its attrs do not tell. The settle pass that clears a spurious flag skips text columns.
    • So every SYM or STR key from those routes came back flagged (attrs 0x43 against 0x03), with or without a failure. The legacy routes flag a key only when one is null.
    • A failed slice-group route groups the selection on the v2 serial-dense route, so it answered the unfailed values with a key column flagged differently.
  • The order of the groups (documented, not changed). A failed allocation on a direct (dense or sparse) route falls back to the hash route, which emits groups in first-seen order instead of key order.
  • The last bits of a float (documented, not changed). A failed per-partition group falls back to the flattened table, which sums in another order.
  • Why those two stay. The order of a result without asc: or desc: follows the strategy (A sort after a group on the shared or partitioned dense kernel breaks the documented tie order (4096+ slots) #1678), and a group that can still finish on the other route should finish.

The fix

  • ray_group_gather scans a text key column, one cell per group, and flags it only when a key is null. Numeric keys are as before.
  • docs/architecture/pipeline.md now says that a strategy that cannot allocate its tables falls back to another one, so a failed allocation can change the order of the groups and the last bits of a float aggregate.

Tests

  • aware/group_sym_key_nulls_flag runs four SYM-keyed groups with v2 on and off: by: s, by: s under where: (in s …) and under where: (> v 5), and a key holding the null symbol.
    • The v2 key column must be flagged exactly when a key is null.
    • The two engines must answer the same bytes once sorted by the key, since each emits its strategy's order.
    • Without the fix: (select {c: (count v) from: TP by: s}): key attrs 43.
  • aware/group_fallback_order sweeps the group seam over every position of (select {s: (sum v) c: (count v) from: F where: (> v 5) by: j}) and (select {s: (sum v) from: F by: w}) (50,000 values), with v2 on and off.
    • Every answer must be oom or, sorted by the key, the unfailed answer byte for byte.
    • At least one position must take the hash fallback, so the sweep reaches it.

Cost

Callgrind, release (gcc), -c 1, 1,048,576 rows, with table setup subtracted:

Shape dev this branch
(select {c: (count v) from: T by: s}) (200,000 SYM groups) and (select {c: (sum v) from: T by: x}) (10,000 STR groups), one run 407,373,957 407,671,665

That is +297,708 instructions (+0.07%), about 1.4 per group, for the scan of the key column. Nothing is added per row.

Checks

  • gcc release and debug builds are clean.
  • make -j4 test TEST_ARGS="-j 4": 5,897 of 5,901 passed, 4 skipped.
  • ASan + UBSan: both tests pass, no sanitizer report.
  • clang-23 -fsyntax-only on agg_engine.c (with and without -DDEBUG) and on test_aware.c (-DDEBUG): clean.
  • mkdocs build --strict: clean.

Fixes #1819

…ey is null

ray_group_gather, which builds the key columns of the v2 routes, set
RAY_ATTR_HAS_NULLS whenever its source may have nulls.  For a text
column that is always, since its attrs do not tell, and the settle
pass that clears a spurious flag skips text columns.  Every SYM or STR
key of those routes came back flagged.  A failed slice-group route,
which groups the selection on the v2 serial-dense route, therefore
answered the unfailed values with a key column flagged otherwise.
The legacy routes flag a key only when one is null.

A text key column is now scanned, one cell per group, and flagged only
when a key is null; numeric keys are as before.

A failed allocation on a direct group route falls back to the hash
route, which emits its groups in first-seen order, and a failed
per-partition group falls back to the flattened table, which sums in
another order.  The order of a result without asc: or desc: follows
the strategy, so this stays; pipeline.md now says so.

aware/group_sym_key_nulls_flag compares the v2 and legacy answers of
four SYM-keyed groups, a null key included, and checks the flag.
aware/group_fallback_order sweeps the group seam over two dense-route
groups, v2 on and off: every answer is the unfailed one or oom, and
sorted by the key the groups and values are the same.

Fixes #1819

@singaraiona singaraiona left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review of 46230c3: READY

The flag fix

I probed the key column's attrs with v2 on and off, against dev 3c30dbd. The cases are SYM and STR keys, with and without null keys, in memory and on a splayed table (narrow-width mapped SYM), bare and under where:.

Key dev, v2 this head, v2
SYM, no null (in memory, by: s and with where:) 43: flagged, no null 03: not flagged
SYM, no null (splayed) 40: flagged 00
SYM with a null key (in memory, splayed) 43 / 40 43 / 40 (flagged, correct)
STR, no null 00 00
STR with a null key 40 40

So the v2 key column is now flagged exactly when a key is null, for both types and both storages. Numeric keys keep the old branch: flag, then ray_group_key_settle_nulls. GUID, whose nullness rests on the flag, stays on that branch.

aw/group_sym_key_nulls_flag fails on dev's agg_engine.c (key attrs 43) and passes here.

Readers of the flag answer the same. Over SYM keys with and without nulls (2,000 rows, 50 groups) I compared release outputs of dev and this head for:

  • (sum (nil? k)), distinct, asc: on the result, regrouping the result, in, asc/desc of the key;
  • left-join of the result with a table holding a null key;
  • nil? over STR keys.

All are identical. The only difference is ser of a no-null SYM result, in the key column's attrs byte (0x40 gone), which is the fix itself.

One description nit. "The legacy routes flag a key only when one is null" is not what I see. With v2 off, a SYM or STR key column holding a null is not flagged at all (03 / 00). That is harmless, since text nulls are read from the payload whatever the flag says (ray_vec_is_null), but the sentence overstates it. The two engines still differ in the flag when a key is null.

Docs

The new sentence in pipeline.md matches the code:

  • the dense and direct routes fall back to the hash route;
  • a failed exec_group_per_partition returns NULL and falls through to the concatenated table (group.c ~14322-14335), which sums in another order;
  • otherwise the groups and values are the same.

It sits next to the existing line that the order without asc:/desc: is the strategy's.

Nothing else changed

The diff against 3c30dbd is agg_engine.c (the 10-line branch above), the docs sentence and the two tests in test_aware.c.

Runs (gcc)

  • make -j4 test TEST_ARGS="-j 4": 5897 of 5901 passed, 4 skipped, 0 failed.
  • ASan+UBSan test binary: group_sym_key_nulls_flag 1/1, group_fallback_order 1/1, -f group/ 98/98, no sanitizer report.

Not covered (about 20 minutes)

  • The cost claim (+1.4 instructions per group) was not re-measured.
  • mkdocs.
  • Parted tables for the flag.
  • I did not confirm that aw/group_fallback_order reaches the fallback on every build. I relied on the test's own "at least one position takes the hash fallback" assertion.

@singaraiona
singaraiona added this pull request to the merge queue Nov 27, 2026
Merged via the queue into dev with commit 84408f0 Nov 27, 2026
11 checks passed
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.

A group whose dense route fails to allocate falls back to the hash route and answers its groups in another order

1 participant