Repository navigation
fix(query): a free vector is a column only beside one in an elementwise call - #1492
Open
singaraiona wants to merge 6 commits into
Open
singaraiona wants to merge 6 commits into
singaraiona wants to merge 6 commits into
Conversation
…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.
…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
force-pushed
the
fix/1462-free-vector-rule
branch
from
November 23, 2026 13:33
77ec76e to
f0b0c7f
Compare
Collaborator
Author
|
Pushed f0b0c7f, on the same base, dev 0f819e3.
Plain differential: 23,500 queries, 0 differences from dev. Suites: |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Wincluded, unless its head names a column), a literal[...], a lambda parameter, alet-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)}underwhere:orby:reads a table-length vector aligned to the rows (as on dev).(+ v W)withWas long as the table is sliced per group underby:and filtered bywhere:. 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 forselectandupdate, keyless, underwhere:and underby:. A helper of several statements is not inlined, andWinside it stays whole. A lambda parameter orletnamed like a column hides it, as before, and is read the way that column is: sliced per group and filtered bywhere:, never whole, inselectandupdatealike.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 bywhere:, whilecounthad 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 refolddomainerror where a global was sliced (#1462).updateevaluates on gathered sub-tables (the groups, the kept rows), where a vector is never table length, so it readWwhole everywhere and raisedlengthfor(+ v W)underby:or beside an aggregate underwhere:.The fix
All in
src/ops/query.c.select_plan_free_vectorsreplacesselect_plan_row_globalsand runs on every select route. Underwhere:orby:, each free vector an output reads as a column, through an elementwise chain with a row-valued operand (anifand a binary aggregate's pairing included), becomes a hidden column__fvNof the table, which thewhere:filters and the group slices on every route; helpers are inlined first, so a helper's global counts. Under a bareby: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 underwhere:needs it).fv_fold) with the builtin, sofirst/lastare 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).(max (+ W (count v))), is evaluated alone over the rows thewhere:keeps and spliced in, so its siblings keep the DAG (sending the whole select to the evaluator broke siblings that readWas a column, andwavgthere).c: (+ a b), reads the spliced value (substituted outside aggregates), so the placeholder never shows; a chain of such outputs too.(wsum 2 W),(cov W 1), spreads the atom over the vector before it is computed once.schemaerror.ifwhose 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 istype, 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.updatewrites 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 underwhere:andby:).updateslices it by the group and by the kept rows, asselectreads it;(fn [v] (update {m: (min v) ... by: k}))gives the per-group min.limiterror), and a select whose outputs read no free vector and run no helper skips the planning after one cheap pass.(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 readsWwhole), andexpr_reads_rows_outside_aggcounts it as rows to fold, so a lambda parameter no longer refolds.updateadds the hidden columns only for the column-beside-vector reads, beforeupdate_eval_keptgathers the kept rows and before the per-group sub-tables are built. The hidden columns never reach the result.where:route now asks only whether an output or theby:(a by-dict's values included) reads a free vector as a column.expr_vconst_on_rows,scalar_edge_keep_rowsand thesrc_tbl/selparameters of the keyless evaluator (the first/last read of a vector beside a column before thewhere:),expr_holds_vector_constandselect_outputs_hold_vector_const, therow_globals_*helpers,head_reads_rows_elementwise, and theOP_COUNT && op_tree_is_scalarfallback (a count of a constant collection is folded before the DAG).Docs:
select.md"Aggregate sources" states the rule with examples and theupdateside;functions.mdupdates theupdate ... 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 andletgive 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/letdomain→ [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] ordomain→ [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]).length→ its whole value ((sum W4) 100, (max W4) 40, (avg W4) 25.0, ...).(sum (+ x W))by klength→ [33 77 121];(count (+ x W))by klength→ [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;percentilethe 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 ofW;(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; withwhere: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.(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 klength→ [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
test/rfl/query/free_vector_rule.rfl(@also-unfused): both issues' examples; the matrix of 15 output shapes (every aggregate, elementwise, nested, projection,at) under select and update, keyless, where: and by:, for global, literal, parameter, let, inlined helper, a vector of another length and two-statement helpers; one-element vectors; other aggregates (med, dev, var, count distinct, wavg); a by-dict key; in-place update; hidden columns absent from results; parted tables. Fails on dev (first at the 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 parameter example,domain).(sum (+ v W)),(avg (* v W)),(max (+ v W)),(sum (if (> v 3) W v)),(wavg W v),(count (distinct (+ v W))),(first (asc (+ v W)))beside(max (+ W (count v))), through literals, parameters, an inlined helper andtake:, each pinned to dev's answer.query_sort_take_coverage.rflfor a parameter named like a column, read per group):by_nested_aggregate.rfl(whole-vector oracle beside the sliced one),select_whole_column_projection.rfl(fix(query): sorted, taken and parted routes honour a lambda local that hides a column #889's block),keyless_first_last_nulls.rfl,query_evalgroup_coverage.rfl,ops/group_coverage.rfl;width_matrix.rflnow reads the table's columns, as its 33/64/65-aggregate checks meant (it read globals of the same values).Mutations
Each applied alone, rebuilt,
rfl/queryrun; 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 inupdate_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; noifas elementwise; no atom fold; no parted flatten; no binary-aggregate pairing; no elementwise-chain propagation; hidden columns underwhere: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
tasksetto one core withRAYFORCE_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.(sum W)by k(sum (+ v W))by k(sum (+ v W))by k with where:(sum (+ v W))with where:(sum W)beside(sum v)with where:(+ v W)(max (+ W (count v)))by j (7 groups)(sum v)by k(sum v)with where:(max (+ v (count v)))by j(sum (+ v W))by j with where:(sum W)by k is not faster on one core. Dev summedWinside its single group pass. NowWis summed once over the whole vector, which is one more pass over it; that pass is the 1.4 ms at 10M.(sum W)beside(sum v)sums all 10M values ofW, the whole value. Dev summed only the rows thewhere:kept.(max (+ W (count v)))underby:is inherently groups × N: each group adds its count to all ofW. 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.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)
updatewith a paired aggregate of an atom and a vector,(wsum 2 W)by k, raisestype: 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 raiseslength, 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))withtake:evaluates per row.(count (+ v W4))by: with a vector of another length answers the group sizes instead oflength.A keyless select runs a two-statement helper per row (select 61 per row against update's 63).
updatewith bothby:andwhere:is stillnyi.