Repository navigation
Derive literal, cast and lower stats through registered stat derivations #11817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robert3005
wants to merge
11
commits into
develop
Choose a base branch
from
rk/stat-expr-rewrite-rules
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+425
−168
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eaa227c
Derive literal, cast and lower stats through stats rewrite rules
robert3005 5fd88ba
Keep literal stats special-cased in stat_expr
robert3005 d53f5cd
Move stat derivations into their own StatDerivation trait
robert3005 c1ce313
Derive literal stats through a stat derivation
robert3005 122674c
Register stat derivations as static references
robert3005 83c85e7
Inline the literal, cast and lower stat helpers into their derivations
robert3005 441f366
Call stats::bound::stat directly instead of through stat_fn
robert3005 33914b1
Make stat_expr public in stats::derive
robert3005 fb5a344
Describe derived statistics by aggregate function rather than Stat
robert3005 46543d9
Route every derived statistic through stat_expr
robert3005 fda78bf
Register stat derivations behind an Arc
robert3005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| //! Session-registered derivations of a scalar function's result statistics from the statistics | ||
| //! of its inputs. | ||
| //! | ||
| //! Stats rewrite rules bound sub-expressions of a predicate, such as the min and max of | ||
| //! `cast(col)` in `cast(col) > 5`. Statistics are stored only for columns, so a statistic of | ||
| //! `cast(col)` must be expressed in terms of the statistics of `col`. A [`StatDerivation`] | ||
| //! registered for a scalar function says how. | ||
|
|
||
| use std::fmt::Debug; | ||
| use std::sync::Arc; | ||
|
|
||
| use vortex_array::aggregate_fn::AggregateFnRef; | ||
| use vortex_array::expr::BoundExpression; | ||
| use vortex_array::scalar_fn::ScalarFnId; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| use crate::stats::bound; | ||
| use crate::stats::session::StatsSessionExt; | ||
|
|
||
| /// Shared reference to a statistic derivation. | ||
| pub type StatDerivationRef = Arc<dyn StatDerivation>; | ||
|
|
||
| /// Expresses the statistics of a scalar function's result in terms of the statistics of its | ||
| /// inputs. | ||
| /// | ||
| /// At most one derivation is registered per scalar function. When one is registered, it decides | ||
| /// every statistic of expressions rooted at that function: stats rewrites never read a statistic | ||
| /// stored for such an expression itself. | ||
| pub trait StatDerivation: Debug + Send + Sync + 'static { | ||
| /// Returns the scalar function id whose statistics this derivation describes. | ||
| fn scalar_fn_id(&self) -> ScalarFnId; | ||
|
|
||
| /// Expresses the statistic `aggregate_fn` of `expr`, whose root scalar function id is | ||
| /// [`Self::scalar_fn_id`], as an expression over statistics of other expressions. | ||
| /// | ||
| /// Returns `None` when the statistic cannot be known from statistics. | ||
| fn derive_stat( | ||
| &self, | ||
| expr: &BoundExpression, | ||
| aggregate_fn: &AggregateFnRef, | ||
| session: &VortexSession, | ||
| ) -> Option<BoundExpression>; | ||
|
robert3005 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /// Expresses the statistic `aggregate_fn` of `expr` as an expression over the statistics that | ||
| /// can be read for the current scope. | ||
| /// | ||
| /// If a [`StatDerivation`] is registered for the root scalar function of `expr`, it decides the | ||
| /// statistic. Otherwise this is a reference to `aggregate_fn` of `expr` itself. Returns `None` | ||
| /// when the statistic cannot be known, for example when the aggregate does not support the dtype | ||
| /// of `expr`. | ||
| pub fn stat_expr( | ||
| expr: &BoundExpression, | ||
| aggregate_fn: &AggregateFnRef, | ||
| session: &VortexSession, | ||
| ) -> Option<BoundExpression> { | ||
| if let Some(scalar_fn) = expr.as_scalar() | ||
| && let Some(derivation) = session.stats().stat_derivation_for(scalar_fn.id()) | ||
| { | ||
| return derivation.derive_stat(expr, aggregate_fn, session); | ||
| } | ||
|
|
||
| // The aggregate may not support the expression's dtype, e.g. min/max over structs, | ||
| // even when the predicate itself is well-typed. Such stats cannot be lowered later, | ||
| // so do not reference them in the rewrite. | ||
| aggregate_fn | ||
| .return_dtype(expr.dtype()) | ||
| .is_some() | ||
| .then(|| bound::stat(expr.clone(), aggregate_fn.clone())) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use vortex_array::aggregate_fn::AggregateFnRef; | ||
| use vortex_array::aggregate_fn::AggregateFnVTableExt; | ||
| use vortex_array::aggregate_fn::NumericalAggregateOpts; | ||
| use vortex_array::aggregate_fn::fns::max::Max; | ||
| use vortex_array::aggregate_fn::fns::min::Min; | ||
| use vortex_array::dtype::DType; | ||
| use vortex_array::dtype::Nullability; | ||
| use vortex_array::dtype::StructFields; | ||
| use vortex_array::expr::BoundExpression; | ||
| use vortex_array::expr::bound::lit; | ||
| use vortex_array::expr::col; | ||
| use vortex_array::scalar_fn::ScalarFnId; | ||
| use vortex_array::scalar_fn::ScalarFnVTable; | ||
| use vortex_error::VortexResult; | ||
| use vortex_functions::byte_length::ByteLength; | ||
| use vortex_functions::expr::byte_length; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| use super::StatDerivation; | ||
| use super::stat_expr; | ||
| use crate::stats::session::StatsSessionExt; | ||
|
|
||
| /// Bounds the max byte length of every value by a fixed length, and knows no other statistic. | ||
| #[derive(Debug)] | ||
| struct MaxByteLength(u64); | ||
|
|
||
| impl StatDerivation for MaxByteLength { | ||
| fn scalar_fn_id(&self) -> ScalarFnId { | ||
| ByteLength.id() | ||
| } | ||
|
|
||
| fn derive_stat( | ||
| &self, | ||
| _expr: &BoundExpression, | ||
| aggregate_fn: &AggregateFnRef, | ||
| _session: &VortexSession, | ||
| ) -> Option<BoundExpression> { | ||
| aggregate_fn.is::<Max>().then(|| lit(self.0)) | ||
| } | ||
| } | ||
|
|
||
| fn byte_length_of_column() -> VortexResult<BoundExpression> { | ||
| byte_length(col("s")).bind(&DType::Struct( | ||
| StructFields::from_iter([("s", DType::Utf8(Nullability::NonNullable))]), | ||
| Nullability::NonNullable, | ||
| )) | ||
| } | ||
|
|
||
| #[test] | ||
| fn registered_derivation_decides_every_statistic() -> VortexResult<()> { | ||
| let session = VortexSession::empty(); | ||
| session.stats().register_stat_derivation(MaxByteLength(8)); | ||
| let expr = byte_length_of_column()?; | ||
|
|
||
| assert_eq!( | ||
| stat_expr( | ||
| &expr, | ||
| &Max.bind(NumericalAggregateOpts::skip_nans()), | ||
| &session | ||
| ), | ||
| Some(lit(8u64)) | ||
| ); | ||
| // The derivation knows no min, and no stored min of `byte_length(s)` is read instead. | ||
| assert_eq!( | ||
| stat_expr( | ||
| &expr, | ||
| &Min.bind(NumericalAggregateOpts::skip_nans()), | ||
| &session | ||
| ), | ||
| None | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn later_registration_replaces_earlier() -> VortexResult<()> { | ||
| let session = VortexSession::empty(); | ||
| session.stats().register_stat_derivation(MaxByteLength(8)); | ||
| session.stats().register_stat_derivation(MaxByteLength(16)); | ||
|
|
||
| assert_eq!( | ||
| stat_expr( | ||
| &byte_length_of_column()?, | ||
| &Max.bind(NumericalAggregateOpts::skip_nans()), | ||
| &session | ||
| ), | ||
| Some(lit(16u64)) | ||
| ); | ||
| Ok(()) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.