Repository navigation
Derive literal, cast and lower stats through registered stat derivations - #11817
robert3005 wants to merge 10 commits into
Conversation
`stat_expr` special-cased literals, `cast` and `lower` by name when it expressed a statistic of a sub-expression. A function defined outside vortex-expr had no way to say how its statistics follow from its inputs'. `StatsRewriteRule` gains `rewrite_stat`, which expresses a statistic of an expression rooted at the rule's function. `stat_expr` asks the rules registered for the expression's function before falling back to a stat reference on the expression itself, and the literal, cast and lower cases become rules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Polar Signals Profiling ResultsLatest Run
Powered by Polar Signals Cloud |
Predicate rules and the derivation of a function's result statistics are different concerns: derivations apply to any sub-expression, not only to a predicate's root, and at most one decides each function's statistics. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Benchmarks: FineWeb NVMe 📖Commits: PR How to read Verdict and Engines
Hot runs: geomean 0.980x ➖ · 9 queries × 4 targets (1↑ 0↓)Each cell is develop
Cold runs (first iteration): geomean 1.014x ➖ · 9 queries × 4 targets (0↑ 3↓)
No file size changes detected. |
Benchmarks: TPC-H SF=1 on S3 📖Commits: PR How to read Verdict and Engines
Hot runs: geomean 1.028x ➖ · 22 queries × 4 targets (3↑ 4↓)Each cell is develop
Cold runs (first iteration): geomean 0.996x ➖ · 22 queries × 4 targets (4↑ 8↓)
|
Benchmarks: TPC-H SF=10 on NVME 📖Commits: PR How to read Verdict and Engines
Hot runs: geomean 1.000x ➖ · 22 queries × 4 targets (0↑ 0↓)Each cell is develop
Cold runs (first iteration): geomean 1.046x ➖ · 22 queries × 4 targets (0↑ 13↓)
No file size changes detected. |
Benchmarks: TPC-H SF=1 on NVME 📖Commits: PR How to read Verdict and Engines
Hot runs: geomean 0.998x ➖ · 22 queries × 4 targets (1↑ 1↓)Each cell is develop
Cold runs (first iteration): geomean 1.016x ➖ · 22 queries × 4 targets (0↑ 1↓)
No file size changes detected. |
Benchmarks: PolarSignals Profiling 📖Commits: PR Hot runs: geomean 0.999x ➖ · 13 queries × 1 target (0↑ 0↓)Each cell is develop
Cold runs (first iteration): geomean 1.114x 🔴 · 13 queries × 1 target (0↑ 7↓)
No file size changes detected. |
Benchmarks: Clickbench on NVME 📖Commits: PR How to read Verdict and Engines
Hot runs: geomean 0.995x ➖ · 43 queries × 4 targets (0↑ 0↓)Each cell is develop
Cold runs (first iteration): geomean 0.979x ➖ · 43 queries × 4 targets (6↑ 2↓)
No file size changes detected. |
Benchmarks: TPC-DS SF=10 on NVME 📖Commits: PR How to read Verdict and Engines
Hot runs: geomean 0.997x ➖ · 99 queries × 4 targets (2↑ 2↓)Each cell is develop
Cold runs (first iteration): geomean 0.990x ➖ · 99 queries × 4 targets (8↑ 3↓)
File Size Changes (1 files changed, -0.0% overall, 0↑ 1↓)
Totals:
|
Benchmarks: TPC-DS SF=1 on NVME 📖Commits: PR How to read Verdict and Engines
Hot runs: geomean 0.997x ➖ · 99 queries × 4 targets (4↑ 4↓)Each cell is develop
Cold runs (first iteration): geomean 1.010x ➖ · 99 queries × 4 targets (1↑ 5↓)
No file size changes detected. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Derivations describe fixed properties of a scalar function and carry no state, so the session holds `&'static dyn StatDerivation` like aggregate kernels rather than reference-counting them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
There was a problem hiding this comment.
Clean refactor — StatDerivation separates sub-expression stat derivation from predicate-root StatsRewriteRule nicely and preserves existing behavior for Literal, Cast, and Lower. One small API note inline about making stat_expr reachable to external StatDerivation implementers.
A stat derivation registered outside vortex-expr needs stat_expr to bound its inputs, as the lower derivation does with the min and max of its input. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Merging this PR will not alter performance2 warnings
Comparing Footnotes
|
lwwmanning
left a comment
There was a problem hiding this comment.
Posted by Claude Opus 5.5 on Will's behalf.
This looks right. The three derivations are the old literal_stat, cast_stat and lower_stat bodies moved behind StatDerivation. stat_expr consults a registered derivation before it falls back to a stat reference, as the old Literal, Cast and Lower branches did, so pruning output stays the same. The other changes only thread session through. Keeping derivations apart from StatsRewriteRule is justified, because rewrite rules run only on a predicate's root. No findings.
lwwmanning
left a comment
There was a problem hiding this comment.
Posted by Claude Opus 5.5 on Will's behalf.
A second, independent review agrees that pruning output is unchanged, including nested cases such as cast(lower(col)). The vortex-expr tests and the vortex-file lower-pruning tests pass, and clippy is clean. It did find points that my earlier comment missed. They concern the new public API, so they are worth settling before it merges and other crates start to use it. Auto-merge is on, so I am holding my approval until then.
- Registration takes
&'static dyn StatDerivation(inline). - The trait doc promises more than the code does (inline).
- The Cast derivation forwards
SumandUncompressedSizeInBytesunchanged (inline). - Nothing tests registering a derivation from outside the crate, or replacing one.
register_stat_derivationsilently replaces an earlier derivation, so a plugin can override a builtin without notice. Also,stat_derivation_forispubwhilerewrite_rules_forispub(crate). The publicstat_exprseems to be enough for callers outside the crate.
|
|
||
| /// Register how the statistics of a scalar function's result derive from its inputs', | ||
| /// replacing any derivation already registered for that function. | ||
| pub fn register_stat_derivation(&self, derivation: &'static dyn StatDerivation) { |
There was a problem hiding this comment.
Rewrite rules are stored as Arc<dyn StatsRewriteRule>, but derivations must be 'static. A derivation with runtime state, for example one built from a plugin's configuration, cannot be registered without Box::leak. An Arc would cost one atomic increment per lookup, on the planning path only, where falsify runs once per conjunct and the result is cached. This is the extension point that #10768 builds on, so switching to Arc now avoids a breaking change later.
`stat_expr` and `StatDerivation::derive_stat` take the aggregate function whose result is wanted, so the stats rewrite API is in terms of aggregate functions and does not expose the `Stat` enum. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
lwwmanning
left a comment
There was a problem hiding this comment.
Posted by Claude Opus 5.5 on Will's behalf.
fb5a344 keys derivations on the aggregate function rather than Stat. Pruning does not change: the new min and max helpers bind skip_nans(), as Stat::aggregate_fn does, so they look up the same stored statistics.
This helps with point 2 of my last review: a derivation can now describe AllNull or AllNonNan. But all_null, all_non_null, all_nan and all_non_nan in builtins.rs still call stat() directly, so a registered derivation is never consulted for them. The StatDerivation doc still says a registered derivation decides every statistic of such an expression. Routing those helpers through stat_expr would make that true.
Points 1, 3 and 4 are still open: registration still takes &'static dyn StatDerivation, the Cast derivation still forwards Sum and UncompressedSizeInBytes (inline), and nothing tests outside registration or replacement. I am still holding my approval until these are settled.
The all-null and all-NaN helpers built stat references directly, so a registered derivation was never asked for them. They now go through `stat_expr`, and the literal derivation answers them from its value. The cast derivation no longer forwards `Sum` and `UncompressedSizeInBytes`: a cast changes the sum's dtype and the size of each value. `stat_derivation_for` is crate-private like `rewrite_rules_for`, and tests cover registering and replacing a derivation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
stat_exprnames literals,castandlowerdirectly when it expresses a statistic of a sub-expression, such as the min oflower(col)inlower(col) = 'abc', so a function defined outside vortex-expr cannot say how its statistics follow from its inputs'. This blocks moving expressions out of vortex-array, which flips the dependency so that vortex-expr can no longer seeLowerin vortex-functions.A new
StatDerivationtrait, registered per scalar function onStatsSession, expresses an aggregate of a function's result in terms of aggregates of its inputs, and the now-publicstat_exprconsults it before falling back to a reference to the aggregate of the expression itself. It is separate fromStatsRewriteRulebecause predicate rules only run on a predicate's root, while derivations apply to any sub-expression. Both are described by aggregate functions rather than theStatenum. Literals,castandlowerbecome derivations with the same results, so pruning behaviour is unchanged.This PR was written with Claude Code.