Skip to content

fix(query): a free vector is a column only beside one in an elementwise call - #1492

Open
singaraiona wants to merge 6 commits into
devfrom
fix/1462-free-vector-rule
Open

singaraiona wants to merge 6 commits into
devfrom
fix/1462-free-vector-rule

Conversation

@singaraiona

@singaraiona singaraiona commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1462
Fixes #1471

The rule

A free vector is a vector a query reads that is no column of its table: a global (a dotted path such as u.W included, unless its head names a column), a literal [...], a lambda parameter, a let-bound local, or a global read inside a helper the planner inlines. It stands for a column in two places: where an elementwise call combines it with a row-valued operand, that is a column or an expression reading one outside any aggregate; and in a projection, an output with no aggregate, which is one value per row, so {r: W} or {r: (* W 2)} under where: or by: reads a table-length vector aligned to the rows (as on dev). (+ v W) with W as long as the table is sliced per group under by: and filtered by where:. Everywhere else a free vector is one whole value, whatever its length: an aggregate's direct argument ((sum W), (first W), (max W), (avg W), (count W), and two-argument aggregates such as (quantile W 0.5), (wavg W W), (top W 2)), an elementwise expression over free vectors and atoms only ((max (+ W 0))), a nested (count W), and an index source ((at W 0)). The rule is the same for every binding form and every aggregate, and for select and update, keyless, under where: and under by:. A helper of several statements is not inlined, and W inside it stays whole. A lambda parameter or let named like a column hides it, as before, and is read the way that column is: sliced per group and filtered by where:, never whole, in select and update alike.

The cause

The planner compiled every vector-bound name or literal to a vector constant, and the DAG reads a vector constant of the table's length as a column. So (sum W) was sliced per group and filtered by where:, while count had a special case that kept a constant's own length (#1471: (sum gl) filtered, (count gl) whole). The nested-aggregate route of #1459 turned only globals (not lexical locals) into a column, so a lambda parameter raised the refold domain error where a global was sliced (#1462). update evaluates on gathered sub-tables (the groups, the kept rows), where a vector is never table length, so it read W whole everywhere and raised length for (+ v W) under by: or beside an aggregate under where:.

The fix

All in src/ops/query.c.

  • select_plan_free_vectors replaces select_plan_row_globals and runs on every select route. Under where: or by:, each free vector an output reads as a column, through an elementwise chain with a row-valued operand (an if and a binary aggregate's pairing included), becomes a hidden column __fvN of the table, which the where: filters and the group slices on every route; helpers are inlined first, so a helper's global counts. Under a bare by: whose outputs the grouped DAG runs, the vector constant is left in place: the DAG already slices it by the group, and faster than it reads a column. Over a parted table the columns the select reads are flattened first, as before (a running call under where: needs it).
  • Each aggregate whose arguments read no column but a free vector, unary or not, is computed once (fv_fold) with the builtin, so first/last are positional as outside a query; a collection result such as (top W 2) becomes one cell per group. A unary source whose value is an atom keeps its per-row meaning, as (sum 1) does. A keyless select of aggregates answers one row, so a folded output there is planned as a row-count placeholder and its value spliced into the result (select_splice_whole).
  • Keyless, an aggregate reading a free vector whole beside the rows' aggregates, (max (+ W (count v))), is evaluated alone over the rows the where: keeps and spliced in, so its siblings keep the DAG (sending the whole select to the evaluator broke siblings that read W as a column, and wavg there).
  • An output after a spliced one that names it, c: (+ a b), reads the spliced value (substituted outside aggregates), so the placeholder never shows; a chain of such outputs too.
  • A paired aggregate of a free vector and an atom, (wsum 2 W), (cov W 1), spreads the atom over the vector before it is computed once.
  • A dotted global is written as its quoted value before planning, so it takes every route the plain name takes. An unbound name counts as a column in the column-free test, so a misspelt column beside a free vector is still the planner's schema error.
  • An aggregate computed once over a free vector raises the type error of an if whose arms mix two temporal types, as every other route does since fix(query): raise type for an if over two temporal types #1481: (first (if true WD WT)) by k is type, not the evaluator's pick of an arm. The keyless evaluator keeps fix(query): raise type for an if over two temporal types #1481's refusal (rw_if_mix_err) beside the free-vector change there.
  • update writes a dotted global as its quoted value before it evaluates an expression, so the dotted name takes every route the plain name takes: (update {s: (sum (if (> v 3) u.W v)) from: T}) gave 210, and now gives 156, as the plain global and select do (the same under where: and by:).
  • A local named like a column is gathered as a hidden column too, so update slices it by the group and by the kept rows, as select reads it; (fn [v] (update {m: (min v) ... by: k})) gives the per-group min.
  • The hidden columns have no fixed cap (256, then a limit error), and a select whose outputs read no free vector and run no helper skips the planning after one cheap pass.
  • A free vector read elementwise beside the group's aggregates only, (max (+ W (count v))), is whole: the arith-of-aggregates decomposition declines it (it would evaluate the wrapper over one row per group), the keyless DAG declines it (the evaluator reads W whole), and expr_reads_rows_outside_agg counts it as rows to fold, so a lambda parameter no longer refolds.
  • update adds the hidden columns only for the column-beside-vector reads, before update_eval_kept gathers the kept rows and before the per-group sub-tables are built. The hidden columns never reach the result.
  • The prefilter guard of the grouped where: route now asks only whether an output or the by: (a by-dict's values included) reads a free vector as a column.
  • Removed as dead: expr_vconst_on_rows, scalar_edge_keep_rows and the src_tbl/sel parameters of the keyless evaluator (the first/last read of a vector beside a column before the where:), expr_holds_vector_const and select_outputs_hold_vector_const, the row_globals_* helpers, head_reads_rows_elementwise, and the OP_COUNT && op_tree_is_scalar fallback (a count of a constant collection is folded before the DAG).

Docs: select.md "Aggregate sources" states the rule with examples and the update side; functions.md updates the update ... where: and constant-count sentences.

Answers that change

Each checked on dev 3663c93 and on this branch, T: k [a a b b c c], v [1..6], W [10..60], where: (> v 3). Global, literal, lambda parameter and let give the same answer now in every row below.

select:

  • (sum W) by k: [30 70 110] → [210 210 210]; keyless with where: [150] → [210]; by with where: [40 110] → [210 210].
  • (first W) by k: [10 30 50] → [10 10 10]; keyless with where: [40] → [10]. (max W), (max (+ W 0)) by k: [20 40 60] → [60 60 60]. (avg W) by k: [15.0 35.0 55.0] → [35.0 35.0 35.0]; keyless with where: [50.0] → [35.0].
  • (max (+ W (count v))) by k: global/literal/inlined [22 42 62], parameter/let domain → [62 62 62] everywhere (by: a table-length vector bound to a lambda parameter isn't sliced per group; lone count of a table-length global counts the whole vector #1462); with where: [41 62] or domain → [61 62].
  • (max (+ v (count W))) by k: global/literal/inlined [4 6 8] → [8 10 12], as parameter/let already gave; keyless with where: [9] → [12].
  • (+ (sum v) (count W)) keyless with where: 18 → 21, as update gives (select where: slices a table-length global for sum but not count; update treats it as whole #1471).
  • (sum [7]) by k: [14 14 14] → [7 7 7]; keyless [42] → [7].
  • (min (+ W (count v))) keyless with where: [43] → [13]; by k [12 32 52] → [12 12 12].
  • {r: (+ v W)} by k with where: length → (list [44] [55 66]).
  • A vector of another length (W4 = [10 20 30 40]) as an aggregate's argument: length → its whole value ((sum W4) 100, (max W4) 40, (avg W4) 25.0, ...).
  • Through an inlined helper: (sum (+ x W)) by k length → [33 77 121]; (count (+ x W)) by k length → [2 2 2]; (count W) keyless with where: [3 3 3] → [6 6 6].
  • (first (asc (+ v W))) by k: length → [11 33 55].
  • (quantile W 0.5) by k: [15 35 55] → [35.0 35.0 35.0]; with where: 45 → 35.0; percentile the same; (wavg W W) by k: [16.67 35.71 55.45] → [43.33 43.33 43.33], as update gives; (top W 2) by k: per-group slices → [60 50] in each group.
  • (sum u.W) by k: domain → [210 210 210]; (sum (+ v u.W)) by k with where: length → [77 121].
  • (wsum 2 W) by k: [60.0 140.0 220.0] → [420.0 420.0 420.0], a rule change: the pair reads no column, so it is one whole value, the atom standing for every row of W; (wavg 2 W) by k: [15.0 35.0 55.0] → [35.0 ×3]; (wsum W 2.0) keyless: 420.0 (unchanged); (wavg W 2) and (cov W 1) by k unchanged ([2.0 ×3], [0.0 ×3]).
  • (fn [v] (update {m: (min v) from: (table [k v] ...) by: k})): [1 1 1 1 1 1] → the per-group min, as select gives; with where: the min of the kept rows. (update {m: (+ v 1) ... by: k}) with such a local: length → per row.
  • count (distinct v) of a lambda parameter named like a column, (fn [tbl y] (select {c: (count (distinct y)) by: k from: tbl})): [4 4 4] → [2 1 1]. This is an answer change on every count-distinct route (flat, splayed, parted, the composite rewrite, the global hash): fix(query): sorted, taken and parted routes honour a lambda local that hides a column #889's docs said a bound vector counts whole "whatever its … name", and under this PR's rule such a local is read as the column it hides, per group. (count y) of it goes [6 6 6] → [2 2 2], and the high-cardinality case gives one value per group instead of 3; the same queries through a name no column has (z) stay whole. query_sort_take_coverage.rfl is repinned accordingly, citing by: a table-length vector bound to a lambda parameter isn't sliced per group; lone count of a table-length global counts the whole vector #1462.
  • Keyless (min (- W (sum v))) beside other outputs with where: 12 → -8; (max (+ W (count v))) over a where: that keeps no row: null → 60 (W is whole).

update:

  • (sum (+ v W)) by k: length → [33 33 77 77 121 121]; keyless with where: length → [0N 0N 0N 165 165 165].
  • (count (+ v W)) by k length → [2 ...]; with where: length → [0N 0N 0N 3 3 3].
  • {r: (+ v W)} by k: length → [11 22 33 44 55 66].
  • (wavg W v) by k: length → the groups' weighted averages, as select gives.
  • (update {r: (sum (+ v W)) from: 'T where: ...}) in place: length → written.

Unchanged: every other cell of the survey ((count W), (sum (+ v W)) in select, projections, at, two-statement helpers).

Tests

Mutations

Each applied alone, rebuilt, rfl/query run; every one fails a test: no hidden-column substitution; no fold; no arith-decomposition decline; no keyless DAG decline; no refold credit for a free vector; no hidden column in update_eval_kept; none in the update by: sub-tables; no splice of a keyless folded output; lexical locals not free vectors; no prefilter guard; no by-dict walk in the guard; no if as elementwise; no atom fold; no parted flatten; no binary-aggregate pairing; no elementwise-chain propagation; hidden columns under where: only; a cap of 8 hidden columns; no fold of two-argument aggregates; no local hiding a column; dotted names always columns; no keyless splice of a whole read; no one-cell fold of a collection.

Timings

Clean release builds of origin/dev 0f819e3 and this branch. Each query is the minimum of 5 runs, best of 3 alternating rounds. One core means taskset to one core with RAYFORCE_CORES=1. The machine was shared and loaded (load average 30-57), so all-core numbers swung by 2x even for queries this change does not touch; the one-core column is the reliable one. Times in ms.

query 1M dev 1M branch 10M dev 10M branch
(sum W) by k 1.15 1.20 13.0 14.4
(sum (+ v W)) by k 1.77 1.76 20.1 21.0
(sum (+ v W)) by k with where: 6.50 6.65 68.2 73.6
keyless (sum (+ v W)) with where: 13.3 13.3 139 145
keyless (sum W) beside (sum v) with where: 14.6 15.5 151 162
update where: (+ v W) 3.79 3.89 42.4 42.4
(max (+ W (count v))) by j (7 groups) 9.65 13.1 117 154
plain (sum v) by k 1.32 1.14 13.0 13.1
plain (sum v) with where: 12.4 12.5 128 127
plain nested (max (+ v (count v))) by j 7.91 7.82 92.4 106 (100 on rerun, dev 102)
parted (sum (+ v W)) by j with where: 8.19 7.88 82.0 81.1
20,000 selects on 6 rows, by: 81.2 81.7
20,000 selects on 6 rows, where: 73.3 76.3 (72.3 against 72.4 on the quiet rerun)
20,000 selects on 6 rows with a free vector 208 242
  • (sum W) by k is not faster on one core. Dev summed W inside its single group pass. Now W is summed once over the whole vector, which is one more pass over it; that pass is the 1.4 ms at 10M.
  • The keyless (sum W) beside (sum v) sums all 10M values of W, the whole value. Dev summed only the rows the where: kept.
  • (max (+ W (count v))) under by: is inherently groups × N: each group adds its count to all of W. It measured 7.6 → 114 ms with 1000 groups at 1M rows. The fix that would help is to evaluate it once per distinct value of the inner aggregate (all groups with the same count share one answer), but that is a new route in the per-group evaluator, not done here.
  • A select that reads no free vector pays one walk of its outputs: within noise on the small-table loops. A select that does read one pays inlining, substitution and the re-entered plan: about 1.7 µs per query on a 6-row table.

Suites

On dev 0f819e3:

  • make test: 5591 of 5595 passed (4 skipped, 0 failed).
  • make clean && make CC=gcc test: 5591 of 5595 passed (4 skipped, 0 failed).
  • make CC=gcc release: builds without warnings.
  • make release && make test-release: 7064 of 7068 passed (4 skipped, 0 failed).
  • make clean.

Random rigs:

Not changed (pre-existing on dev)

  • update with a paired aggregate of an atom and a vector, (wsum 2 W) by k, raises type: wsum expects two numeric vectors (the builtin wants two vectors), as on dev.

  • A keyless select mixing a projection that reads a free vector with an aggregate, {a: (* W j) b: (wavg W v)}, evaluates per row.

  • A free vector of another length that happens to match the rows a where: keeps: select raises length, update gives an answer (118 in the review's case).

  • LIST globals in queries are not free vectors.

  • A keyless select of aggregates over an empty table returns no row.

  • With a real column w, keyless (sum (+ (+ j w) 1)) beside (wavg w f) answers 0 (the free-vector forms answer correctly).

  • A helper (fn [x] W) called as (gW 0) inside a keyless (last (- (gW 0) f)) with take: evaluates per row.

  • (count (+ v W4)) by: with a vector of another length answers the group sizes instead of length.

  • A keyless select runs a two-statement helper per row (select 61 per row against update's 63).

  • update with both by: and where: is still nyi.

…se call (#1462, #1471)

A global, literal, lambda parameter, let or inlined helper's global
stands for a column only where an elementwise call combines it with a
row-valued operand: (+ v W) is sliced per group and filtered by where:.
Everywhere else it is one whole value: an aggregate's argument, an
elementwise expression over free vectors and atoms, a nested (count W),
an index source.  The rule holds for every binding form and aggregate,
in select and update, keyless, under where: and under by:.

select reads such a vector through a hidden column on every route
(except a bare by: the DAG runs, which slices the constant itself),
computes each aggregate of a column-free free vector once, and keeps a
vector beside the group's aggregates whole.  update gathers the hidden
column with the kept rows and the groups.
…ree aggregate (#1462)

A keyless aggregate reading a free vector whole beside the rows'
aggregates, (max (+ W (count v))), is evaluated alone and spliced in;
the other outputs keep the DAG, where before the whole select went to
the evaluator and a sibling reading W as a column raised.  Aggregates
of two arguments that read no column, (quantile W 0.5), (wavg W W),
(top W 2), are computed once as the unary ones are.  A dotted global is
a free vector unless its head names a column, and a local hides a
column of its name.  More than eight vectors read as columns get hidden
columns too.  A select reading no free vector and running no helper
skips the planning.
… an atom (#1462)

An output naming a keyless aggregate spliced in after the select, c in
{b: (max (+ W (count v))) c: (+ a b)}, read the row-count placeholder;
it reads the value now.  A paired aggregate of a free vector and an atom,
(wsum 2 W), spreads the atom over the vector before it is computed once,
as an atom stands for every row.  A misspelt column beside a free vector
is the planner's schema error again, and a dotted global reads as the
plain one on the keyless route.  A local named like a column is no free
vector: the query reads it as it reads that column, as before.
…date too (#1462)

A lambda parameter or let named like a column still hides it, and it is
read the way the column is: sliced per group and filtered by where:,
never whole.  update now gathers it with each group's rows and with the
rows its where: keeps, as select reads it.
…error (#1462)

An aggregate computed once over a free vector, (first (if true WD WT)),
went through the evaluator, which picks an arm without the planner's
type promotion; it raises the type error every other route raises for
an if over two temporal types (#1337).
…1462)

update wrote a dotted global as a column path to the planner, so
(sum (if (> v 3) u.W v)) evaluated it per row and gave 210 where the
plain global and select give 156.  It is written as its quoted value
before evaluation, as select does.  select.md states that a projection
reads a table-length free vector aligned to the rows, and that a local
named like a column is read as that column.
@singaraiona
singaraiona force-pushed the fix/1462-free-vector-rule branch from 77ec76e to f0b0c7f Compare November 23, 2026 13:33
@singaraiona

Copy link
Copy Markdown
Collaborator Author

Pushed f0b0c7f, on the same base, dev 0f819e3.

  • Dotted global in update: (update {s: (sum (if (> v 3) u.W v)) from: T}) now gives 156, the same as the plain global and select (it gave 210). update writes a dotted free vector as its quoted value before evaluating, on every update route. Pinned, and mutation-checked.
  • Projections: select.md now states that a projection (an output with no aggregate) is one value per row, so {r: W} and {r: (* W 2)} read a table-length vector aligned to the rows. That is the same answer as before. Pinned for select and update.
  • Same-named local: the paragraph and the by: key paragraph now agree. A parameter or let named like a column hides it and is read as that column is, never whole. The Rayforce / Rayfall — issues from building a Data Vault backend #394 behaviour is kept.
  • Commits: the history was rebuilt so that every commit message agrees with the same-named local rule. The code tree is unchanged apart from the two fixes above.

Plain differential: 23,500 queries, 0 differences from dev. Suites: make test 5591/5595, gcc 5591/5595, gcc release clean, make test-release 7064/7068.

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

1 participant