From 46230c321252df0685c06cf6212763f25fc8841f Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 27 Nov 2026 08:09:16 +0100 Subject: [PATCH] fix(group): a SYM or STR group key is flagged has-nulls only when a key 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 --- docs/docs/architecture/pipeline.md | 5 ++ src/ops/agg_engine.c | 12 +++- test/test_aware.c | 90 ++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) diff --git a/docs/docs/architecture/pipeline.md b/docs/docs/architecture/pipeline.md index c1d633db3..1510f2aeb 100644 --- a/docs/docs/architecture/pipeline.md +++ b/docs/docs/architecture/pipeline.md @@ -235,6 +235,11 @@ Grouped aggregates run on the parallel aggregation engine, which picks a strateg Without an explicit ordering clause, full grouped results can follow the selected strategy's group order. Use `asc:` or `desc:` when result positions matter. +A strategy that cannot allocate its tables falls back to another one (the +dense routes to the hash route, a per-partition group to the flattened +table), so a failed allocation can change the order of the groups and, by +summing in another order, the last bits of a float aggregate; the groups and +their values are otherwise the same. After a sort, groups that tie on the sort keys are meant to come in first-seen order. For integer, temporal and symbol group keys that is not guaranteed yet: the engine may emit such groups in key order diff --git a/src/ops/agg_engine.c b/src/ops/agg_engine.c index 7091741de..169aef898 100644 --- a/src/ops/agg_engine.c +++ b/src/ops/agg_engine.c @@ -1901,8 +1901,16 @@ ray_t* ray_group_gather(ray_t* src_col, const int64_t* first_row, int64_t n) { if (agg_cancelled()) { ray_release(out); return ray_error("cancel", NULL); } bool gathered_null = atomic_load_explicit(&c.any_null, memory_order_relaxed) && src_col->type != RAY_BOOL && src_col->type != RAY_U8; - if (gathered_null || ray_vec_may_have_nulls(src_col)) out->attrs |= RAY_ATTR_HAS_NULLS; - if (!gathered_null) ray_group_key_settle_nulls(out); + /* A text column may hold nulls whatever its attrs say, so its keys are + * scanned (one cell per group): flagged only when one is null, as the + * other routes' key columns are. */ + if (gathered_null) out->attrs |= RAY_ATTR_HAS_NULLS; + else if (src_col->type == RAY_SYM || src_col->type == RAY_STR) { + if (ray_vec_text_has_nulls(out)) out->attrs |= RAY_ATTR_HAS_NULLS; + } else if (ray_vec_may_have_nulls(src_col)) { + out->attrs |= RAY_ATTR_HAS_NULLS; + ray_group_key_settle_nulls(out); + } return out; } diff --git a/test/test_aware.c b/test/test_aware.c index e06d9df87..d4c6d37ff 100644 --- a/test/test_aware.c +++ b/test/test_aware.c @@ -3698,6 +3698,94 @@ static test_result_t test_radix_cancel_keeps_nothing(void) { * table init inside phase 2's tasks, on any thread. "Keeps nothing" is * the process's allocation trace (every heap) over the run, the error's * release and a GC. */ +/* Q equals R0: byte for byte, or by the expression `cmp`. */ +static bool aw_q_same(const char* cmp) { + ray_t* v = ray_eval_str(cmp ? cmp : "(and (== (count (ser Q)) (count (ser R0))) (all (== (ser Q) (ser R0))))"); + bool ok = v && !RAY_IS_ERR(v) && v->type == -RAY_BOOL && v->b8; + if (v && RAY_IS_ERR(v)) ray_error_free(v); else if (v) ray_release(v); + return ok; +} + +/* A SYM key column is flagged has-nulls only when a key is null, on the v2 + * serial routes as on the legacy ones: the two engines answer the same + * bytes, in key order (each emits its strategy's order), a null key + * included (#1819). */ +static test_result_t test_group_sym_key_nulls_flag(void) { + aw_eval_ok("(set KS (as 'SYM (at ['aa 'bb 'cc 'dd 'ee 'ff 'gg 'hh] (% (til 40000) 8))))"); + aw_eval_ok("(set KN (as 'SYM (at ['aa ' 'cc 'dd] (% (til 40000) 4))))"); + aw_eval_ok("(set TP (table [s n v] (list KS KN (til 40000))))"); + static const char* const fq[] = { + "(select {c: (count v) from: TP by: s})", + "(select {c: (count v) from: TP by: s where: (in s ['aa 'cc])})", + "(select {c: (sum v) from: TP by: s where: (> v 5)})", + "(select {c: (count v) from: TP by: n})", + }; + for (size_t q = 0; q < sizeof fq / sizeof *fq; q++) { + ray_t* r[2]; + for (int e = 0; e < 2; e++) { + ray_agg_engine_v2 = e == 0; + r[e] = aw_eval(fq[q]); + TEST_ASSERT_TRUE(r[e] && !RAY_IS_ERR(r[e]) && r[e]->type == RAY_TABLE); + } + ray_agg_engine_v2 = true; + ray_t* k = ray_table_col_idx(r[0], 0); + bool has_null = q == 3; + TEST_ASSERT_FMT(((k->attrs & RAY_ATTR_HAS_NULLS) != 0) == has_null, "%s: key attrs %x", fq[q], k->attrs); + ray_env_set(ray_sym_intern("Q", 1), r[0]); + ray_env_set(ray_sym_intern("R0", 2), r[1]); + ray_release(r[0]); ray_release(r[1]); + char cmp[300]; + snprintf(cmp, sizeof cmp, "(all (== (ser (select {from: Q asc: %s})) (ser (select {from: R0 asc: %s}))))", q == 3 ? "n" : "s", q == 3 ? "n" : "s"); + TEST_ASSERT_FMT(aw_q_same(cmp), "%s: v2 and the legacy engine answer different bytes", fq[q]); + } + aw_eval_ok("(set KS 0)"); aw_eval_ok("(set KN 0)"); aw_eval_ok("(set TP 0)"); aw_eval_ok("(set Q 0)"); aw_eval_ok("(set R0 0)"); + PASS(); +} + +/* A direct (dense) route whose allocation fails falls back to the hash + * route, which answers its groups in first-seen order: the order of a + * result without asc: or desc: follows the strategy (pipeline.md). The + * groups and their values are the unfailed ones (#1819). */ +static test_result_t test_group_fallback_order(void) { + aw_eval_ok("(set F (table [j v w] (list (% (til 70000) 7) (til 70000) (% (* (til 70000) 7919) 50000))))"); + static const char* const oq[] = { + "(select {s: (sum v) c: (count v) from: F where: (> v 5) by: j})", + "(select {s: (sum v) from: F by: w})", + }; + static const char* const ok[] = { "j", "w" }; + int reordered = 0; + for (int e = 0; e < 2; e++) { + ray_agg_engine_v2 = e == 0; + for (size_t q = 0; q < sizeof oq / sizeof *oq; q++) { + char setq[200], cmp[300]; + snprintf(setq, sizeof setq, "(set R0 %s)", oq[q]); + aw_eval_ok(setq); + snprintf(cmp, sizeof cmp, "(all (== (ser (select {from: Q asc: %s})) (ser (select {from: R0 asc: %s}))))", ok[q], ok[q]); + for (int64_t n = 1; n <= 4000; n++) { + ray_group_alloc_fail_for_test(n); + ray_t* got = aw_eval(oq[q]); + bool fired = ray_group_alloc_fail_fired_for_test(); + ray_group_alloc_fail_for_test(0); + if (got && RAY_IS_ERR(got)) { + TEST_ASSERT_FMT(strcmp(ray_err_code(got), "oom") == 0, "%s, allocation %lld fails: %s, not oom", oq[q], (long long)n, ray_err_code(got)); + ray_error_free(got); + continue; + } + TEST_ASSERT_NOT_NULL(got); + ray_env_set(ray_sym_intern("Q", 1), got); + ray_release(got); + if (!aw_q_same(NULL)) reordered++; + TEST_ASSERT_FMT(aw_q_same(cmp), "%s (v2 %d), allocation %lld fails: other groups or values", oq[q], e == 0, (long long)n); + if (!fired) break; + } + } + } + ray_agg_engine_v2 = true; + TEST_ASSERT_FMT(reordered > 0, "no failure took the hash fallback"); + aw_eval_ok("(set F 0)"); aw_eval_ok("(set Q 0)"); aw_eval_ok("(set R0 0)"); + PASS(); +} + static test_result_t test_group_alloc_fail(void) { ray_pool_destroy(); TEST_ASSERT_EQ_I(ray_pool_init_total(4), RAY_OK); @@ -6999,6 +7087,8 @@ const test_entry_t aware_entries[] = { { "aware/sort_head_late_chunked", test_sort_head_late_chunked, aw_setup, aw_teardown }, { "aware/topk_cancel_at_dispatch", test_topk_cancel_at_dispatch, aw_setup, aw_teardown }, { "aware/group_cancel_status", test_group_cancel_status, aw_setup, aw_teardown }, + { "aware/group_sym_key_nulls_flag", test_group_sym_key_nulls_flag, aw_setup, aw_teardown }, + { "aware/group_fallback_order", test_group_fallback_order, aw_setup, aw_teardown }, { "aware/group_alloc_fail", test_group_alloc_fail, aw_setup, aw_teardown }, { "aware/holistic_oom", test_holistic_oom, aw_setup, aw_teardown }, { "aware/radix_cancel_keeps_nothing", test_radix_cancel_keeps_nothing, aw_setup, aw_teardown },