From 21d319cd24bfbe771b42719f925ab63c0bfe87c1 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Sat, 24 Oct 2026 00:14:56 +0000 Subject: [PATCH 1/7] Make vortex-functions depend on vortex-expr instead of the reverse Expressions are about to move out of vortex-array into vortex-expr, which means vortex-functions (whose scalar functions construct expressions) has to sit above vortex-expr rather than below it. This commit flips the dependency without moving the expression types yet: - `select` returns to vortex-array's core builtins. The field-path analyses in vortex-expr (`referenced_field_paths`, immediate access) narrow through it alongside `get_item`, so it is part of the projection machinery those analyses are built on, like `get_item` and `pack`. - `StatFn` moves from vortex-functions into `vortex_expr::stats::stat_fn`, registered by the new `vortex_expr::initialize`. - The stats rewrite rules for `between`, `is_nan`, `like`, `list_contains`, `dynamic` and `dynamic_expression` move into `vortex_functions::pruning`, registered by `vortex_functions::initialize`. The shared pieces they build on (stat references, NaN guards, literal and cast stat folding) become the public `vortex_expr::stats::rewrite::helpers` module. - `find_between` moves next to the `between` function it targets. Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Will Manning Co-Authored-By: Will Manning Signed-off-by: Robert Kruszewski --- Cargo.lock | 2 +- vortex-array/src/scalar_fn/builtin/mod.rs | 2 +- vortex-array/src/scalar_fn/erased.rs | 12 + vortex-array/src/scalar_fn/typed.rs | 16 + vortex-array/src/scalar_fn/vtable.rs | 19 + vortex-datafusion/src/v2/dynamic_filters.rs | 2 +- vortex-expr/Cargo.toml | 1 - vortex-expr/src/lib.rs | 12 +- vortex-expr/src/stats/bind.rs | 3 +- vortex-expr/src/stats/expr.rs | 5 +- vortex-expr/src/stats/mod.rs | 1 + vortex-expr/src/stats/rewrite.rs | 3 +- vortex-expr/src/stats/rewrite/builtins.rs | 836 +----------------- vortex-expr/src/stats/rewrite/fn_tests.rs | 60 -- vortex-expr/src/stats/rewrite/helpers.rs | 233 +++++ .../src/stats/stat_fn.rs | 0 vortex-expr/src/transform/bound_partition.rs | 36 - vortex-expr/src/transform/mod.rs | 2 - vortex-expr/src/transform/replace.rs | 19 - vortex-file/src/v2/file_stats_reader.rs | 6 +- vortex-functions/Cargo.toml | 1 + .../src/between/find.rs | 13 +- vortex-functions/src/between/mod.rs | 2 + vortex-functions/src/lib.rs | 20 +- vortex-functions/src/list_transform/tests.rs | 13 + vortex-functions/src/lower.rs | 31 + vortex-functions/src/merge.rs | 69 ++ .../src/pruning}/dynamic_expression.rs | 48 +- .../src/pruning}/like_variant.rs | 0 vortex-functions/src/pruning/mod.rs | 747 ++++++++++++++++ vortex-layout/src/scan/scan_builder.rs | 2 +- vortex-layout/src/test.rs | 6 +- vortex-scan/src/execution/filter.rs | 2 +- vortex-scan/src/plan/builtins/zoned/mod.rs | 2 +- vortex/src/lib.rs | 1 + 35 files changed, 1236 insertions(+), 991 deletions(-) create mode 100644 vortex-expr/src/stats/rewrite/helpers.rs rename vortex-functions/src/stat.rs => vortex-expr/src/stats/stat_fn.rs (100%) rename vortex-expr/src/transform/match_between.rs => vortex-functions/src/between/find.rs (98%) rename {vortex-expr/src/stats/rewrite => vortex-functions/src/pruning}/dynamic_expression.rs (89%) rename {vortex-expr/src/stats/rewrite => vortex-functions/src/pruning}/like_variant.rs (100%) create mode 100644 vortex-functions/src/pruning/mod.rs diff --git a/Cargo.lock b/Cargo.lock index bd9b71f7542..3a0f66eb82a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11660,7 +11660,6 @@ dependencies = [ "vortex-array", "vortex-buffer", "vortex-error", - "vortex-functions", "vortex-session", "vortex-utils", ] @@ -11816,6 +11815,7 @@ dependencies = [ "vortex-buffer", "vortex-compute", "vortex-error", + "vortex-expr", "vortex-mask", "vortex-session", "vortex-utils", diff --git a/vortex-array/src/scalar_fn/builtin/mod.rs b/vortex-array/src/scalar_fn/builtin/mod.rs index 2a3403bb7e5..7c77e8f0299 100644 --- a/vortex-array/src/scalar_fn/builtin/mod.rs +++ b/vortex-array/src/scalar_fn/builtin/mod.rs @@ -4,7 +4,7 @@ //! The scalar functions that vortex-array itself is built on. //! //! These are the functions that the core crate's own arrays, validity and expression machinery -//! construct: literals and struct projection and packing for expressions, `is_null`, +//! construct: literals and struct projection, selection and packing for expressions, `is_null`, //! `is_not_null`, `not` and the boolean operators for validity reduction, and `cast`, `mask`, //! `fill_null` and the comparison and arithmetic operators, which canonical arrays apply lazily to //! their own children (offsets, indices, codes and validity). [`ArrayBuiltins`] exposes them as diff --git a/vortex-array/src/scalar_fn/erased.rs b/vortex-array/src/scalar_fn/erased.rs index b2218b8ec3a..796b9aacdff 100644 --- a/vortex-array/src/scalar_fn/erased.rs +++ b/vortex-array/src/scalar_fn/erased.rs @@ -21,6 +21,7 @@ use crate::ExecutionCtx; use crate::dtype::DType; use crate::expr::BoundExpression; use crate::expr::display::ExprDisplay; +use crate::expr::stats::Stat; use crate::scalar_fn::ArrayReduceNode; use crate::scalar_fn::ExecutionArgs; use crate::scalar_fn::ExpressionReduceNode; @@ -195,6 +196,17 @@ impl ScalarFnRef { } /// Simplify a bound expression using the dtypes carried by its nodes. + /// Expresses statistic `stat` of `expr`, which is rooted at this function, in terms of + /// statistics of other expressions. See [`ScalarFnVTable::stat_expression`]. + pub fn stat_expression( + &self, + expr: &BoundExpression, + stat: Stat, + child_stat: &dyn Fn(&BoundExpression, Stat) -> Option, + ) -> Option { + self.0.stat_expression(expr, stat, child_stat) + } + pub(crate) fn simplify(&self, expr: &BoundExpression) -> VortexResult> { self.0.simplify(expr) } diff --git a/vortex-array/src/scalar_fn/typed.rs b/vortex-array/src/scalar_fn/typed.rs index 29a36a02722..4acc6eb77e8 100644 --- a/vortex-array/src/scalar_fn/typed.rs +++ b/vortex-array/src/scalar_fn/typed.rs @@ -28,6 +28,7 @@ use crate::arrays::ScalarFnArray; use crate::dtype::DType; use crate::expr::BoundExpression; use crate::expr::display::ExprDisplay; +use crate::expr::stats::Stat; use crate::scalar_fn::Arity; use crate::scalar_fn::ArrayReduceNode; use crate::scalar_fn::ChildName; @@ -130,6 +131,12 @@ pub(super) trait DynScalarFn: 'static + Send + Sync + super::sealed::Sealed { fn child_name(&self, child_idx: usize) -> ChildName; fn is_strict(&self) -> bool; fn is_infallible(&self) -> bool; + fn stat_expression( + &self, + expr: &BoundExpression, + stat: Stat, + child_stat: &dyn Fn(&BoundExpression, Stat) -> Option, + ) -> Option; // Expression methods — take expressions for tree traversal fn fmt_sql(&self, expression: &dyn ExprDisplay, f: &mut Formatter<'_>) -> fmt::Result; @@ -251,6 +258,15 @@ impl DynScalarFn for TypedScalarFnInstance { V::is_infallible(&self.vtable, &self.options) } + fn stat_expression( + &self, + expr: &BoundExpression, + stat: Stat, + child_stat: &dyn Fn(&BoundExpression, Stat) -> Option, + ) -> Option { + V::stat_expression(&self.vtable, &self.options, expr, stat, child_stat) + } + fn fmt_sql(&self, expression: &dyn ExprDisplay, f: &mut Formatter<'_>) -> fmt::Result { V::fmt_sql(&self.vtable, &self.options, expression, f) } diff --git a/vortex-array/src/scalar_fn/vtable.rs b/vortex-array/src/scalar_fn/vtable.rs index f5d9a52ec76..85dedf2bb26 100644 --- a/vortex-array/src/scalar_fn/vtable.rs +++ b/vortex-array/src/scalar_fn/vtable.rs @@ -28,6 +28,7 @@ use crate::expr::BoundExpression; use crate::expr::Expression; use crate::expr::bound; use crate::expr::display::ExprDisplay; +use crate::expr::stats::Stat; use crate::matcher::Matcher; use crate::scalar::Scalar; use crate::scalar_fn::ScalarFnId; @@ -329,6 +330,24 @@ pub trait ScalarFnVTable: 'static + Sized + Clone + Send + Sync { _ = options; false } + + /// Expresses statistic `stat` of this function's result in terms of statistics of its + /// children, for statistics-based pruning. + /// + /// `expr` is a bound expression rooted at this function, and `child_stat` derives a statistic + /// of any expression, such as one of `expr`'s children. Return `None` when no derivation is + /// known; pruning then reads a statistic stored for `expr` itself, if there is one. The default + /// is `None`. + fn stat_expression( + &self, + options: &Self::Options, + expr: &BoundExpression, + stat: Stat, + child_stat: &dyn Fn(&BoundExpression, Stat) -> Option, + ) -> Option { + _ = (options, expr, stat, child_stat); + None + } } /// A node used for implementing abstract reduction rules over a tree of scalar functions. diff --git a/vortex-datafusion/src/v2/dynamic_filters.rs b/vortex-datafusion/src/v2/dynamic_filters.rs index fcbc7a66046..964bef1246b 100644 --- a/vortex-datafusion/src/v2/dynamic_filters.rs +++ b/vortex-datafusion/src/v2/dynamic_filters.rs @@ -24,8 +24,8 @@ use vortex::expr::get_item; use vortex::expr::lit; use vortex::expr::pack; use vortex::expr::root; -use vortex::expr::transform::find_between; use vortex::expr::transform::replace; +use vortex::functions::between::find_between; use vortex::functions::dynamic_expression::DynamicExpressionSource; use vortex::functions::dynamic_expression::input_field_name; use vortex::scalar_fn::builtin::operators::Operator; diff --git a/vortex-expr/Cargo.toml b/vortex-expr/Cargo.toml index 1aa7e66bf2b..cc030485545 100644 --- a/vortex-expr/Cargo.toml +++ b/vortex-expr/Cargo.toml @@ -18,7 +18,6 @@ parking_lot = { workspace = true } tracing = { workspace = true } vortex-array = { workspace = true } vortex-error = { workspace = true } -vortex-functions = { workspace = true } vortex-session = { workspace = true } vortex-utils = { workspace = true } diff --git a/vortex-expr/src/lib.rs b/vortex-expr/src/lib.rs index b47aa684493..a16faf383b2 100644 --- a/vortex-expr/src/lib.rs +++ b/vortex-expr/src/lib.rs @@ -8,7 +8,8 @@ //! `vortex_array::expr`. This crate holds the tooling that scan planning builds on top of them: //! tree annotation and labeling ([`analysis`]), normal forms such as conjunct extraction //! ([`forms`]), partitioning and substitution rewrites ([`transform`]), and statistics-based -//! pruning ([`stats`]). +//! pruning ([`stats`]), including the [`StatFn`] scalar function that pruning predicates read +//! statistics through. Call [`initialize`] to register it with a session. pub mod analysis; pub mod forms; @@ -16,3 +17,12 @@ pub mod stats; pub mod transform; pub use analysis::*; +use vortex_array::scalar_fn::session::ScalarFnSessionExt; +use vortex_session::VortexSession; + +use crate::stats::stat_fn::StatFn; + +/// Register the scalar functions this crate defines with the session. +pub fn initialize(session: &VortexSession) { + session.scalar_fns().register(StatFn); +} diff --git a/vortex-expr/src/stats/bind.rs b/vortex-expr/src/stats/bind.rs index 107d69d8db7..7533ddc0a2d 100644 --- a/vortex-expr/src/stats/bind.rs +++ b/vortex-expr/src/stats/bind.rs @@ -21,7 +21,8 @@ use vortex_array::expr::traversal::NodeExt; use vortex_array::expr::traversal::Transformed; use vortex_array::scalar::Scalar; use vortex_error::VortexResult; -use vortex_functions::stat::StatFn; + +use crate::stats::stat_fn::StatFn; /// A target that can bind abstract statistics to concrete expressions. /// diff --git a/vortex-expr/src/stats/expr.rs b/vortex-expr/src/stats/expr.rs index c5f2d992e93..c9ece375c92 100644 --- a/vortex-expr/src/stats/expr.rs +++ b/vortex-expr/src/stats/expr.rs @@ -19,8 +19,9 @@ use vortex_array::expr::BoundExpression; use vortex_array::expr::Expression; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_error::VortexExpect; -pub use vortex_functions::stat::StatFn; -pub use vortex_functions::stat::StatOptions; + +pub use crate::stats::stat_fn::StatFn; +pub use crate::stats::stat_fn::StatOptions; /// Creates an expression that reads a stored aggregate statistic for `expr`. /// diff --git a/vortex-expr/src/stats/mod.rs b/vortex-expr/src/stats/mod.rs index 71d9ebb1c49..a978f8676dc 100644 --- a/vortex-expr/src/stats/mod.rs +++ b/vortex-expr/src/stats/mod.rs @@ -20,3 +20,4 @@ pub mod bind; pub mod expr; pub mod rewrite; pub mod session; +pub mod stat_fn; diff --git a/vortex-expr/src/stats/rewrite.rs b/vortex-expr/src/stats/rewrite.rs index 999da990634..79867105674 100644 --- a/vortex-expr/src/stats/rewrite.rs +++ b/vortex-expr/src/stats/rewrite.rs @@ -20,10 +20,9 @@ use vortex_utils::iter::ReduceBalancedIterExt; use crate::stats::session::StatsSessionExt; mod builtins; -mod dynamic_expression; #[cfg(test)] mod fn_tests; -mod like_variant; +pub mod helpers; pub(crate) use builtins::register_builtins; diff --git a/vortex-expr/src/stats/rewrite/builtins.rs b/vortex-expr/src/stats/rewrite/builtins.rs index e7b207c70ef..236b3b13008 100644 --- a/vortex-expr/src/stats/rewrite/builtins.rs +++ b/vortex-expr/src/stats/rewrite/builtins.rs @@ -1,73 +1,38 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors -use std::cmp::Ordering; - -use vortex_array::aggregate_fn::AggregateFnRef; -use vortex_array::aggregate_fn::AggregateFnVTableExt; -use vortex_array::aggregate_fn::EmptyOptions as AggregateEmptyOptions; -use vortex_array::aggregate_fn::fns::all_nan::AllNan; -use vortex_array::aggregate_fn::fns::all_non_nan::AllNonNan; -use vortex_array::aggregate_fn::fns::all_non_null::AllNonNull; -use vortex_array::aggregate_fn::fns::all_null::AllNull; -use vortex_array::dtype::DType; use vortex_array::expr::BoundExpression; use vortex_array::expr::bound::and; -use vortex_array::expr::bound::and_collect; -use vortex_array::expr::bound::binary; -use vortex_array::expr::bound::cast; use vortex_array::expr::bound::eq; use vortex_array::expr::bound::gt; use vortex_array::expr::bound::gt_eq; use vortex_array::expr::bound::lit; use vortex_array::expr::bound::lt; use vortex_array::expr::bound::lt_eq; -use vortex_array::expr::bound::or; use vortex_array::expr::bound::or_collect; -use vortex_array::expr::stats::Stat; -use vortex_array::scalar::Scalar; -use vortex_array::scalar::StringLike; use vortex_array::scalar_fn::EmptyOptions; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::binary::Binary; -use vortex_array::scalar_fn::builtin::cast::Cast; use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull; use vortex_array::scalar_fn::builtin::is_null::IsNull; use vortex_array::scalar_fn::builtin::literal::Literal; -use vortex_array::scalar_fn::builtin::operators::CompareOperator; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_array::scalar_fn::internal::row_count::RowCount; use vortex_error::VortexExpect; use vortex_error::VortexResult; -use vortex_functions::between::Between; -use vortex_functions::dynamic::DynamicComparison; -use vortex_functions::dynamic_expression::DynamicExpression; -use vortex_functions::expr::bound::dynamic_with_options; -use vortex_functions::is_nan::IsNan; -use vortex_functions::like::Like; -use vortex_functions::list_contains::ListContains; -use vortex_functions::list_contains::PreparedSet; -use vortex_functions::list_contains::PreparedSetData; -use vortex_functions::lower::Lower; -use vortex_functions::lower_bound::LowerBound; -use vortex_functions::lower_bound::LowerBoundKind; use vortex_session::VortexSession; -use crate::stats::bound::stat; use crate::stats::rewrite::StatsRewriteRule; -use crate::stats::rewrite::dynamic_expression::StatsProof; -use crate::stats::rewrite::dynamic_expression::dynamic_stats_proof; use crate::stats::rewrite::falsify; -use crate::stats::rewrite::like_variant::LikeVariant; +use crate::stats::rewrite::helpers::*; use crate::stats::session::StatsSession; -/// Register built-in stats rewrite rules. +/// Register the stats rewrite rules for the scalar functions vortex-array is built on. pub(crate) fn register_builtins(session: &StatsSession) { session.register_rewrite(BinaryNanCountStatsRewrite); session.register_rewrite(BinaryAllNonNanStatsRewrite); - session.register_rewrite(BetweenStatsRewrite); session.register_rewrite(LiteralStatsRewrite); session.register_rewrite(IsNullNullCountStatsRewrite); session.register_rewrite(IsNullAllNonNullStatsRewrite); @@ -75,15 +40,6 @@ pub(crate) fn register_builtins(session: &StatsSession) { session.register_rewrite(IsNotNullNullCountStatsRewrite); session.register_rewrite(IsNotNullAllNullStatsRewrite); session.register_rewrite(IsNotNullAllNonNullStatsRewrite); - session.register_rewrite(IsNanNaNCountStatsRewrite); - session.register_rewrite(IsNanAllNonNanStatsRewrite); - session.register_rewrite(IsNanAllNanStatsRewrite); - session.register_rewrite(LikeStatsRewrite); - session.register_rewrite(ListContainsNanCountStatsRewrite); - session.register_rewrite(ListContainsAllNonNanStatsRewrite); - session.register_rewrite(DynamicComparisonNanCountStatsRewrite); - session.register_rewrite(DynamicComparisonAllNonNanStatsRewrite); - session.register_rewrite(DynamicExpressionStatsRewrite); } fn row_count() -> BoundExpression { @@ -230,30 +186,6 @@ fn binary_falsify( }) } -#[derive(Debug)] -struct BetweenStatsRewrite; - -impl StatsRewriteRule for BetweenStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - Between.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - session: &VortexSession, - ) -> VortexResult> { - let options = expr.as_::(); - let arr = expr.child(0).clone(); - let lower = expr.child(1).clone(); - let upper = expr.child(2).clone(); - - let lhs = binary(options.lower_strict.to_operator(), lower, arr.clone()); - let rhs = binary(options.upper_strict.to_operator(), arr, upper); - falsify(&and(lhs, rhs), session) - } -} - #[derive(Debug)] struct IsNullNullCountStatsRewrite; @@ -372,494 +304,7 @@ impl StatsRewriteRule for IsNotNullAllNonNullStatsRewrite { } } -/// Rewrites `is_nan` using the `NaNCount` stat: the predicate is falsified when a zone -/// contains no NaN values, and satisfied when every row in the zone is NaN. -#[derive(Debug)] -struct IsNanNaNCountStatsRewrite; - -impl StatsRewriteRule for IsNanNaNCountStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - IsNan.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - Ok(nan_count(expr.child(0)).map(|nan_count| eq(nan_count, lit(0u64)))) - } - - fn satisfy( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - Ok(nan_count(expr.child(0)).map(|nan_count| eq(nan_count, row_count()))) - } -} - -/// Falsifies `is_nan` when the `AllNonNan` pruning stat proves that no value in the zone -/// is NaN. -#[derive(Debug)] -struct IsNanAllNonNanStatsRewrite; - -impl StatsRewriteRule for IsNanAllNonNanStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - IsNan.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - Ok(Some(all_non_nan(expr.child(0)))) - } -} - -/// Satisfies `is_nan` when the `AllNan` pruning stat proves that every value in the zone -/// is NaN. -#[derive(Debug)] -struct IsNanAllNanStatsRewrite; - -impl StatsRewriteRule for IsNanAllNanStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - IsNan.id() - } - - fn satisfy( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - Ok(Some(all_nan(expr.child(0)))) - } -} - -#[derive(Debug)] -struct LikeStatsRewrite; - -impl StatsRewriteRule for LikeStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - Like.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - let like_options = expr.as_::(); - if like_options.negated || like_options.case_insensitive { - return Ok(None); - } - - let Some(pattern) = expr.child(1).as_opt::() else { - return Ok(None); - }; - let Some(pattern) = pattern.as_utf8().value() else { - return Ok(None); - }; - - let source = expr.child(0); - Ok(match LikeVariant::from_str(pattern) { - Some(LikeVariant::Exact(text)) => { - min(source) - .zip(max(source)) - .map(|(source_min, source_max)| { - or( - gt(source_min, lit(text.as_ref())), - lt(source_max, lit(text.as_ref())), - ) - }) - } - Some(LikeVariant::Prefix(prefix)) => { - let Some(successor) = prefix.to_string().increment().ok() else { - return Ok(None); - }; - min(source) - .zip(max(source)) - .map(|(source_min, source_max)| { - or( - gt_eq(source_min, lit(successor)), - lt(source_max, lit(prefix.as_ref())), - ) - }) - } - None => None, - }) - } -} - -#[derive(Debug)] -struct ListContainsNanCountStatsRewrite; - -impl StatsRewriteRule for ListContainsNanCountStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - ListContains.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - list_contains_falsify::(expr) - } -} - -#[derive(Debug)] -struct ListContainsAllNonNanStatsRewrite; - -impl StatsRewriteRule for ListContainsAllNonNanStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - ListContains.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - list_contains_falsify::(expr) - } -} - -/// Lists with more elements falsify `needle IN list` with the bounds of the list rather than -/// with a test per element, which keeps the pruning expression, and its evaluation per zone, -/// small for the large lists of dynamic join filters. -const MAX_LIST_ELEMENT_FALSIFICATIONS: usize = 16; - -/// The smallest and largest non-null elements, or `None` when there are none or two elements -/// are unordered. -fn non_null_bounds(elements: &[Scalar]) -> Option<(&Scalar, &Scalar)> { - let mut non_null = elements.iter().filter(|element| !element.is_null()); - let first = non_null.next()?; - non_null.try_fold((first, first), |(low, high), element| { - let low = match element.partial_cmp(low)? { - Ordering::Less => element, - Ordering::Equal | Ordering::Greater => low, - }; - let high = match element.partial_cmp(high)? { - Ordering::Greater => element, - Ordering::Equal | Ordering::Less => high, - }; - Some((low, high)) - }) -} - -fn list_contains_falsify( - expr: &BoundExpression, -) -> VortexResult> { - let list = expr.child(0); - let needle = expr.child(1); - - // Reduction replaces a literal list with a set prepared from it. - let elements = list - .as_opt::() - .or_else(|| list.as_opt::().map(PreparedSetData::list)) - .and_then(|list| list.as_list_opt()) - .and_then(|list| list.elements()); - let Some(elements) = elements else { - return Ok(None); - }; - if elements.is_empty() { - return Ok(P::EMIT_UNGUARDED_REWRITES.then(|| lit(true))); - } - - let Some(value_max) = max(needle) else { - return Ok(None); - }; - let Some(value_min) = min(needle) else { - return Ok(None); - }; - - let value_predicate = if elements.len() > MAX_LIST_ELEMENT_FALSIFICATIONS - && let Some((low, high)) = non_null_bounds(&elements) - { - Some(or( - lt(value_max, lit(low.clone())), - gt(value_min, lit(high.clone())), - )) - } else { - and_collect(elements.iter().map(|value| { - or( - lt(value_max.clone(), lit(value.clone())), - gt(value_min.clone(), lit(value.clone())), - ) - })) - }; - value_predicate - .map(|value_predicate| with_non_nan_guards::

([needle], value_predicate)) - .transpose() - .map(Option::flatten) -} - -#[derive(Debug)] -struct DynamicComparisonNanCountStatsRewrite; - -impl StatsRewriteRule for DynamicComparisonNanCountStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - DynamicComparison.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - dynamic_comparison_falsify::(expr) - } -} - -#[derive(Debug)] -struct DynamicComparisonAllNonNanStatsRewrite; - -impl StatsRewriteRule for DynamicComparisonAllNonNanStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - DynamicComparison.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - _session: &VortexSession, - ) -> VortexResult> { - dynamic_comparison_falsify::(expr) - } -} - -fn dynamic_comparison_falsify( - expr: &BoundExpression, -) -> VortexResult> { - let dynamic = expr.as_::(); - let lhs = expr.child(0); - - let Some((operator, lhs_stat)) = (match dynamic.operator() { - CompareOperator::Eq | CompareOperator::NotEq => None, - CompareOperator::Gt => max(lhs).map(|lhs_stat| (CompareOperator::Lte, lhs_stat)), - CompareOperator::Gte => max(lhs).map(|lhs_stat| (CompareOperator::Lt, lhs_stat)), - CompareOperator::Lt => min(lhs).map(|lhs_stat| (CompareOperator::Gte, lhs_stat)), - CompareOperator::Lte => min(lhs).map(|lhs_stat| (CompareOperator::Gt, lhs_stat)), - }) else { - return Ok(None); - }; - - let value_predicate = dynamic_with_options( - dynamic.with_operator(operator, !dynamic.default()), - lhs_stat, - ); - with_non_nan_guards::

([lhs], value_predicate) -} - -fn min(expr: &BoundExpression) -> Option { - stat_expr(expr, Stat::Min) -} - -fn max(expr: &BoundExpression) -> Option { - stat_expr(expr, Stat::Max) -} - -fn null_count(expr: &BoundExpression) -> Option { - stat_expr(expr, Stat::NullCount) -} - -fn nan_count(expr: &BoundExpression) -> Option { - stat_expr(expr, Stat::NaNCount) -} - -fn all_null(expr: &BoundExpression) -> BoundExpression { - stat_fn(expr.clone(), AllNull.bind(AggregateEmptyOptions)) -} - -fn all_non_null(expr: &BoundExpression) -> BoundExpression { - stat_fn(expr.clone(), AllNonNull.bind(AggregateEmptyOptions)) -} - -fn all_nan(expr: &BoundExpression) -> BoundExpression { - stat_fn(expr.clone(), AllNan.bind(AggregateEmptyOptions)) -} - -fn all_non_nan(expr: &BoundExpression) -> BoundExpression { - stat_fn(expr.clone(), AllNonNan.bind(AggregateEmptyOptions)) -} - -enum NanCheck { - NotNeeded, - Check(BoundExpression), - Unavailable, -} - -trait NonNanProof { - const EMIT_UNGUARDED_REWRITES: bool; - - fn check(expr: &BoundExpression) -> VortexResult; -} - -struct NanCountProof; - -impl NonNanProof for NanCountProof { - const EMIT_UNGUARDED_REWRITES: bool = true; - - fn check(expr: &BoundExpression) -> VortexResult { - non_nan_check(expr, |expr| match stat_expr(expr, Stat::NaNCount) { - Some(nan_count) => NanCheck::Check(eq(nan_count, lit(0u64))), - None => NanCheck::Unavailable, - }) - } -} - -struct AllNonNanProof; - -impl NonNanProof for AllNonNanProof { - const EMIT_UNGUARDED_REWRITES: bool = false; - - fn check(expr: &BoundExpression) -> VortexResult { - non_nan_check(expr, |expr| { - NanCheck::Check(stat_fn(expr.clone(), AllNonNan.bind(AggregateEmptyOptions))) - }) - } -} - -// Min/max do not order NaN values, so comparison rewrites are only sound when every -// candidate value is known to be non-NaN. Cast result dtypes are not enough: a cast -// from float to non-float still needs a proof about the float source values. -fn non_nan_check( - expr: &BoundExpression, - proof: impl FnOnce(&BoundExpression) -> NanCheck, -) -> VortexResult { - if let Some(scalar) = expr.as_opt::() { - if !scalar.dtype().is_float() { - return Ok(NanCheck::NotNeeded); - } - return Ok(if scalar.as_primitive().is_nan() { - NanCheck::Check(lit(false)) - } else { - NanCheck::NotNeeded - }); - } - - if expr.is::() { - if !has_nans(expr.child(0).dtype()) { - return Ok(NanCheck::NotNeeded); - } - - return non_nan_check(expr.child(0), proof); - } - - if !has_nans(expr.dtype()) { - return Ok(NanCheck::NotNeeded); - } - - Ok(proof(expr)) -} - -fn has_nans(dtype: &DType) -> bool { - dtype.is_float() -} - -fn stat_expr(expr: &BoundExpression, stat: Stat) -> Option { - if let Some(literal) = literal_stat(expr, stat) { - return Some(literal); - } - - // `literal_stat` handled every stat that is defined for literals. If it returned - // `None`, the requested stat is not meaningful for this literal, such as - // `NaNCount` over a non-float value, so do not manufacture `stat(literal, ...)`. - if expr.is::() { - return None; - } - - if let Some(dtype) = expr.as_opt::() { - return cast_stat(expr.child(0), dtype, stat); - } - - if expr.is::() { - return lower_stat(expr.child(0), stat); - } - - let aggregate_fn = stat.aggregate_fn()?; - // 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(|| stat_fn(expr.clone(), aggregate_fn)) -} - -fn with_non_nan_guards<'a, P: NonNanProof>( - exprs: impl IntoIterator, - value_predicate: BoundExpression, -) -> VortexResult> { - let mut nan_checks = Vec::new(); - for expr in exprs { - match P::check(expr)? { - NanCheck::NotNeeded => {} - NanCheck::Check(check) => nan_checks.push(check), - NanCheck::Unavailable => return Ok(None), - } - } - let nan_predicate = and_collect(nan_checks); - - Ok(match nan_predicate { - Some(nan_check) => Some(and(nan_check, value_predicate)), - // No possible NaN-bearing expression remains, so the value predicate is - // already guarded. Only one registered rule emits this unguarded - // rewrite so non-float comparisons are not duplicated. - None if P::EMIT_UNGUARDED_REWRITES => Some(value_predicate), - None => None, - }) -} - -fn literal_stat(expr: &BoundExpression, stat: Stat) -> Option { - let scalar = expr.as_opt::()?; - match stat { - Stat::Min | Stat::Max => Some(lit(scalar.clone())), - Stat::NullCount => Some(lit(if scalar.is_null() { 1u64 } else { 0u64 })), - Stat::NaNCount => { - let value = scalar.as_primitive_opt()?; - if !value.ptype().is_float() { - return None; - } - - Some(lit(if value.is_nan() { 1u64 } else { 0u64 })) - } - Stat::IsConstant - | Stat::IsSorted - | Stat::IsStrictSorted - | Stat::Sum - | Stat::UncompressedSizeInBytes => None, - } -} - -fn cast_stat(expr: &BoundExpression, dtype: &DType, stat: Stat) -> Option { - match stat { - Stat::Min | Stat::Max => stat_expr(expr, stat).map(|stat| cast(stat, dtype.clone())), - Stat::NaNCount | Stat::Sum | Stat::UncompressedSizeInBytes => stat_expr(expr, stat), - Stat::NullCount | Stat::IsConstant | Stat::IsSorted | Stat::IsStrictSorted => None, - } -} - -/// Bounds `lower(input)` from the byte-ordered min/max of `input`. -fn lower_stat(input: &BoundExpression, stat: Stat) -> Option { - let kind = match stat { - Stat::Min => LowerBoundKind::Min, - Stat::Max => LowerBoundKind::Max, - // Lowercasing maps nulls to nulls and values to values. - Stat::NullCount => return null_count(input), - _ => return None, - }; - LowerBound - .try_new_bound_expr(kind, [min(input)?, max(input)?]) - .ok() -} - -fn stat_fn(expr: BoundExpression, aggregate_fn: AggregateFnRef) -> BoundExpression { - stat(expr, aggregate_fn) -} - +#[cfg(test)] #[cfg(test)] mod tests { use std::sync::Arc; @@ -881,7 +326,6 @@ mod tests { use vortex_array::expr::cast; use vortex_array::expr::col; use vortex_array::expr::eq; - use vortex_array::expr::get_item; use vortex_array::expr::gt; use vortex_array::expr::gt_eq; use vortex_array::expr::is_not_null; @@ -897,21 +341,8 @@ mod tests { use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::binary::Binary; - use vortex_array::scalar_fn::builtin::operators::CompareOperator; use vortex_array::scalar_fn::internal::row_count::RowCount; use vortex_error::VortexResult; - use vortex_functions::between::BetweenOptions; - use vortex_functions::between::StrictComparison; - use vortex_functions::dynamic::DynamicComparison; - use vortex_functions::expr::between; - use vortex_functions::expr::dynamic; - use vortex_functions::expr::is_nan; - use vortex_functions::expr::like; - use vortex_functions::expr::list_contains; - use vortex_functions::list_contains::ListContains; - use vortex_functions::list_contains::ListContainsOptions; - use vortex_functions::list_contains::PreparedSet; - use vortex_functions::list_contains::PreparedSetData; use vortex_session::VortexSession; use crate::stats::expr::StatFn; @@ -972,14 +403,6 @@ mod tests { crate::stats::all_non_null(expr.clone()) } - fn all_nan(expr: &Expression) -> Expression { - crate::stats::all_nan(expr.clone()) - } - - fn all_non_nan(expr: &Expression) -> Expression { - crate::stats::all_non_nan(expr.clone()) - } - macro_rules! assert_rewrite_eq { ($actual:expr, $expected:expr) => { assert_eq!($actual, bind_expected($expected)?) @@ -1087,28 +510,6 @@ mod tests { Ok(()) } - #[test] - fn rewrites_between_falsifier() -> VortexResult<()> { - let expr = between( - col("a"), - lit(10), - lit(50), - BetweenOptions { - lower_strict: StrictComparison::NonStrict, - upper_strict: StrictComparison::NonStrict, - }, - ); - - assert_rewrite_eq!( - falsify(&expr)?, - Some(or( - gt(lit(10), stat(col("a"), Stat::Max)), - gt(stat(col("a"), Stat::Min), lit(50)), - )) - ); - Ok(()) - } - #[test] fn rewrites_null_falsifiers() -> VortexResult<()> { assert_rewrite_eq!( @@ -1155,212 +556,6 @@ mod tests { Ok(()) } - #[test] - fn rewrites_nan_falsifiers() -> VortexResult<()> { - assert_rewrite_eq!( - falsify(&is_nan(col("f")))?, - Some(or( - eq(stat(col("f"), Stat::NaNCount), lit(0u64)), - all_non_nan(&col("f")), - )) - ); - - // Nullable floats prune on nan_count just the same. - assert_rewrite_eq!( - falsify(&is_nan(get_item("x", col("n"))))?, - Some(or( - eq(stat(get_item("x", col("n")), Stat::NaNCount), lit(0u64)), - all_non_nan(&get_item("x", col("n"))), - )) - ); - - // NaN literals fold through the nan_count stat. - assert_rewrite_eq!( - falsify(&is_nan(lit(f32::NAN)))?, - Some(or(eq(lit(1u64), lit(0u64)), all_non_nan(&lit(f32::NAN)))) - ); - Ok(()) - } - - #[test] - fn rewrites_nan_satisfiers() -> VortexResult<()> { - assert_rewrite_eq!( - satisfy(&is_nan(col("f")))?, - Some(or( - eq( - stat(col("f"), Stat::NaNCount), - RowCount.new_expr(EmptyOptions, []) - ), - all_nan(&col("f")), - )) - ); - - assert_rewrite_eq!( - satisfy(&is_nan(lit(f32::NAN)))?, - Some(or( - eq(lit(1u64), RowCount.new_expr(EmptyOptions, [])), - all_nan(&lit(f32::NAN)), - )) - ); - Ok(()) - } - - #[test] - fn rewrites_list_contains_falsifier() -> VortexResult<()> { - let list = Scalar::list( - Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)), - vec![1.into(), 2.into(), 3.into()], - Nullability::NonNullable, - ); - let expr = list_contains(lit(list), col("a")); - - assert_rewrite_eq!( - falsify(&expr)?, - Some(and( - and( - or( - lt(stat(col("a"), Stat::Max), lit(1i32)), - gt(stat(col("a"), Stat::Min), lit(1i32)), - ), - or( - lt(stat(col("a"), Stat::Max), lit(2i32)), - gt(stat(col("a"), Stat::Min), lit(2i32)), - ), - ), - or( - lt(stat(col("a"), Stat::Max), lit(3i32)), - gt(stat(col("a"), Stat::Min), lit(3i32)), - ), - )) - ); - Ok(()) - } - - #[test] - fn rewrites_large_list_contains_falsifier_to_bounds() -> VortexResult<()> { - let list = Scalar::list( - Arc::new(DType::Primitive(PType::I32, Nullability::Nullable)), - (0..20) - .rev() - .map(|value| Scalar::from(Some(value))) - .chain([Scalar::null(DType::Primitive( - PType::I32, - Nullability::Nullable, - ))]) - .collect(), - Nullability::NonNullable, - ); - let expr = list_contains(lit(list), col("a")); - - assert_rewrite_eq!( - falsify(&expr)?, - Some(or( - lt(stat(col("a"), Stat::Max), lit(Scalar::from(Some(0i32)))), - gt(stat(col("a"), Stat::Min), lit(Scalar::from(Some(19i32)))), - )) - ); - Ok(()) - } - - #[test] - fn rewrites_prepared_set_list_contains_falsifier() -> VortexResult<()> { - let list = Scalar::list( - Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)), - vec![1.into(), 2.into()], - Nullability::NonNullable, - ); - let set = PreparedSet.new_expr(PreparedSetData::try_new(list)?, Vec::::new()); - let expr = ListContains.new_expr(ListContainsOptions::default(), [set, col("a")]); - - assert_rewrite_eq!( - falsify(&expr)?, - Some(and( - or( - lt(stat(col("a"), Stat::Max), lit(1i32)), - gt(stat(col("a"), Stat::Min), lit(1i32)), - ), - or( - lt(stat(col("a"), Stat::Max), lit(2i32)), - gt(stat(col("a"), Stat::Min), lit(2i32)), - ), - )) - ); - Ok(()) - } - - #[test] - fn rewrites_like_falsifier() -> VortexResult<()> { - let expr = like(col("s"), lit("prefix%")); - assert_rewrite_eq!( - falsify(&expr)?, - Some(or( - gt_eq(stat(col("s"), Stat::Min), lit("prefiy")), - lt(stat(col("s"), Stat::Max), lit("prefix")), - )) - ); - - let expr = like(col("s"), lit(r"\%%")); - assert_rewrite_eq!( - falsify(&expr)?, - Some(or( - gt_eq(stat(col("s"), Stat::Min), lit("&")), - lt(stat(col("s"), Stat::Max), lit("%")), - )) - ); - - let expr = like(col("s"), lit("pref%ix%")); - assert_rewrite_eq!( - falsify(&expr)?, - Some(or( - gt_eq(stat(col("s"), Stat::Min), lit("preg")), - lt(stat(col("s"), Stat::Max), lit("pref")), - )) - ); - - let expr = like(col("s"), lit("pref_ix_")); - assert_rewrite_eq!( - falsify(&expr)?, - Some(or( - gt_eq(stat(col("s"), Stat::Min), lit("preg")), - lt(stat(col("s"), Stat::Max), lit("pref")), - )) - ); - - let expr = like(col("s"), lit("exact")); - assert_rewrite_eq!( - falsify(&expr)?, - Some(or( - gt(stat(col("s"), Stat::Min), lit("exact")), - lt(stat(col("s"), Stat::Max), lit("exact")), - )) - ); - - let expr = like(col("s"), lit("%suffix")); - assert_rewrite_eq!(falsify(&expr)?, None); - Ok(()) - } - - #[test] - fn rewrites_dynamic_comparison_falsifier() -> VortexResult<()> { - let expr = dynamic( - CompareOperator::Gt, - || Some(10i32.into()), - DType::Primitive(PType::I32, Nullability::NonNullable), - true, - col("a"), - ); - let dynamic = expr.as_::(); - - assert_rewrite_eq!( - falsify(&expr)?, - Some(DynamicComparison.new_expr( - dynamic.with_operator(CompareOperator::Lte, false), - [stat(col("a"), Stat::Max)], - )) - ); - Ok(()) - } - #[test] fn nan_guard_tracks_cast_source_dtype() -> VortexResult<()> { let dtype = DType::Primitive(PType::I32, Nullability::NonNullable); @@ -1404,28 +599,3 @@ mod tests { Ok(()) } } - -#[derive(Debug)] -struct DynamicExpressionStatsRewrite; - -impl StatsRewriteRule for DynamicExpressionStatsRewrite { - fn scalar_fn_id(&self) -> ScalarFnId { - DynamicExpression.id() - } - - fn falsify( - &self, - expr: &BoundExpression, - session: &VortexSession, - ) -> VortexResult> { - dynamic_stats_proof(expr, session, StatsProof::Falsify) - } - - fn satisfy( - &self, - expr: &BoundExpression, - session: &VortexSession, - ) -> VortexResult> { - dynamic_stats_proof(expr, session, StatsProof::Satisfy) - } -} diff --git a/vortex-expr/src/stats/rewrite/fn_tests.rs b/vortex-expr/src/stats/rewrite/fn_tests.rs index f14a8fada81..36d7b1af1b3 100644 --- a/vortex-expr/src/stats/rewrite/fn_tests.rs +++ b/vortex-expr/src/stats/rewrite/fn_tests.rs @@ -3,31 +3,19 @@ //! Falsification tests for individual built-in scalar functions. -use std::sync::Arc; use std::sync::LazyLock; -use vortex_array::dtype::DType; -use vortex_array::dtype::Nullability; -use vortex_array::dtype::PType::I32; -use vortex_array::dtype::StructFields; -use vortex_array::expr::Expression; -use vortex_array::expr::and; use vortex_array::expr::col; use vortex_array::expr::eq; -use vortex_array::expr::gt; use vortex_array::expr::is_not_null; use vortex_array::expr::is_null; use vortex_array::expr::lit; -use vortex_array::expr::lt; use vortex_array::expr::or; -use vortex_array::expr::stats::Stat; use vortex_array::expr::test_harness; -use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::EmptyOptions; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::internal::row_count::RowCount; use vortex_error::VortexResult; -use vortex_functions::expr::list_contains; use vortex_session::VortexSession; use crate::stats::StatsSession; @@ -35,15 +23,10 @@ use crate::stats::all_non_null; use crate::stats::all_null; use crate::stats::null_count; use crate::stats::rewrite::BoundExpressionPruneExt; -use crate::stats::stat as stat_expr; static STATS_SESSION: LazyLock = LazyLock::new(|| VortexSession::empty().with::()); -fn stat(expr: Expression, stat: Stat) -> Expression { - stat_expr(expr, stat.aggregate_fn().unwrap()) -} - #[test] fn test_is_null_falsification() -> VortexResult<()> { let expr = is_null(col("a")); @@ -73,46 +56,3 @@ fn test_is_not_null_falsification() -> VortexResult<()> { ); Ok(()) } - -#[test] -pub fn list_falsification() -> VortexResult<()> { - let expr = list_contains( - lit(Scalar::list( - Arc::new(DType::Primitive(I32, Nullability::NonNullable)), - vec![1.into(), 2.into(), 3.into()], - Nullability::NonNullable, - )), - col("a"), - ); - let scope = DType::Struct( - StructFields::new( - ["a"].into(), - vec![DType::Primitive(I32, Nullability::NonNullable)], - ), - Nullability::NonNullable, - ); - - assert_eq!( - expr.bind(&scope)?.falsify(&STATS_SESSION)?, - Some( - and( - and( - or( - lt(stat(col("a"), Stat::Max), lit(1i32)), - gt(stat(col("a"), Stat::Min), lit(1i32)), - ), - or( - lt(stat(col("a"), Stat::Max), lit(2i32)), - gt(stat(col("a"), Stat::Min), lit(2i32)), - ) - ), - or( - lt(stat(col("a"), Stat::Max), lit(3i32)), - gt(stat(col("a"), Stat::Min), lit(3i32)), - ) - ) - .bind(&scope)? - ) - ); - Ok(()) -} diff --git a/vortex-expr/src/stats/rewrite/helpers.rs b/vortex-expr/src/stats/rewrite/helpers.rs new file mode 100644 index 00000000000..6740a65e36a --- /dev/null +++ b/vortex-expr/src/stats/rewrite/helpers.rs @@ -0,0 +1,233 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! Building blocks for [`StatsRewriteRule`](crate::stats::rewrite::StatsRewriteRule) +//! implementations: stat references over bound expressions and the NaN guards that keep +//! min/max comparisons sound for floating-point values. + +use vortex_array::aggregate_fn::AggregateFnRef; +use vortex_array::aggregate_fn::AggregateFnVTableExt; +use vortex_array::aggregate_fn::EmptyOptions as AggregateEmptyOptions; +use vortex_array::aggregate_fn::fns::all_nan::AllNan; +use vortex_array::aggregate_fn::fns::all_non_nan::AllNonNan; +use vortex_array::aggregate_fn::fns::all_non_null::AllNonNull; +use vortex_array::aggregate_fn::fns::all_null::AllNull; +use vortex_array::dtype::DType; +use vortex_array::expr::BoundExpression; +use vortex_array::expr::bound::and; +use vortex_array::expr::bound::and_collect; +use vortex_array::expr::bound::cast; +use vortex_array::expr::bound::eq; +use vortex_array::expr::bound::lit; +use vortex_array::expr::stats::Stat; +use vortex_array::scalar_fn::EmptyOptions; +use vortex_array::scalar_fn::ScalarFnVTableExt; +use vortex_array::scalar_fn::builtin::cast::Cast; +use vortex_array::scalar_fn::builtin::literal::Literal; +use vortex_array::scalar_fn::internal::row_count::RowCount; +use vortex_error::VortexExpect; +use vortex_error::VortexResult; + +use crate::stats::bound::stat; + +pub fn row_count() -> BoundExpression { + RowCount + .try_new_bound_expr(EmptyOptions, []) + .vortex_expect("row-count expressions are always well-typed") +} + +pub fn min(expr: &BoundExpression) -> Option { + stat_expr(expr, Stat::Min) +} + +pub fn max(expr: &BoundExpression) -> Option { + stat_expr(expr, Stat::Max) +} + +pub fn null_count(expr: &BoundExpression) -> Option { + stat_expr(expr, Stat::NullCount) +} + +pub fn nan_count(expr: &BoundExpression) -> Option { + stat_expr(expr, Stat::NaNCount) +} + +pub fn all_null(expr: &BoundExpression) -> BoundExpression { + stat_fn(expr.clone(), AllNull.bind(AggregateEmptyOptions)) +} + +pub fn all_non_null(expr: &BoundExpression) -> BoundExpression { + stat_fn(expr.clone(), AllNonNull.bind(AggregateEmptyOptions)) +} + +pub fn all_nan(expr: &BoundExpression) -> BoundExpression { + stat_fn(expr.clone(), AllNan.bind(AggregateEmptyOptions)) +} + +pub fn all_non_nan(expr: &BoundExpression) -> BoundExpression { + stat_fn(expr.clone(), AllNonNan.bind(AggregateEmptyOptions)) +} + +pub enum NanCheck { + NotNeeded, + Check(BoundExpression), + Unavailable, +} + +pub trait NonNanProof { + const EMIT_UNGUARDED_REWRITES: bool; + + fn check(expr: &BoundExpression) -> VortexResult; +} + +pub struct NanCountProof; + +impl NonNanProof for NanCountProof { + const EMIT_UNGUARDED_REWRITES: bool = true; + + fn check(expr: &BoundExpression) -> VortexResult { + non_nan_check(expr, |expr| match stat_expr(expr, Stat::NaNCount) { + Some(nan_count) => NanCheck::Check(eq(nan_count, lit(0u64))), + None => NanCheck::Unavailable, + }) + } +} + +pub struct AllNonNanProof; + +impl NonNanProof for AllNonNanProof { + const EMIT_UNGUARDED_REWRITES: bool = false; + + fn check(expr: &BoundExpression) -> VortexResult { + non_nan_check(expr, |expr| { + NanCheck::Check(stat_fn(expr.clone(), AllNonNan.bind(AggregateEmptyOptions))) + }) + } +} + +// Min/max do not order NaN values, so comparison rewrites are only sound when every +// candidate value is known to be non-NaN. Cast result dtypes are not enough: a cast +// from float to non-float still needs a proof about the float source values. +pub fn non_nan_check( + expr: &BoundExpression, + proof: impl FnOnce(&BoundExpression) -> NanCheck, +) -> VortexResult { + if let Some(scalar) = expr.as_opt::() { + if !scalar.dtype().is_float() { + return Ok(NanCheck::NotNeeded); + } + return Ok(if scalar.as_primitive().is_nan() { + NanCheck::Check(lit(false)) + } else { + NanCheck::NotNeeded + }); + } + + if expr.is::() { + if !has_nans(expr.child(0).dtype()) { + return Ok(NanCheck::NotNeeded); + } + + return non_nan_check(expr.child(0), proof); + } + + if !has_nans(expr.dtype()) { + return Ok(NanCheck::NotNeeded); + } + + Ok(proof(expr)) +} + +fn has_nans(dtype: &DType) -> bool { + dtype.is_float() +} + +pub fn stat_expr(expr: &BoundExpression, stat: Stat) -> Option { + if let Some(literal) = literal_stat(expr, stat) { + return Some(literal); + } + + // `literal_stat` handled every stat that is defined for literals. If it returned + // `None`, the requested stat is not meaningful for this literal, such as + // `NaNCount` over a non-float value, so do not manufacture `stat(literal, ...)`. + if expr.is::() { + return None; + } + + if let Some(dtype) = expr.as_opt::() { + return cast_stat(expr.child(0), dtype, stat); + } + + if let Some(derived) = expr + .as_scalar() + .and_then(|scalar_fn| scalar_fn.stat_expression(expr, stat, &stat_expr)) + { + return Some(derived); + } + + let aggregate_fn = stat.aggregate_fn()?; + // 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(|| stat_fn(expr.clone(), aggregate_fn)) +} + +pub fn with_non_nan_guards<'a, P: NonNanProof>( + exprs: impl IntoIterator, + value_predicate: BoundExpression, +) -> VortexResult> { + let mut nan_checks = Vec::new(); + for expr in exprs { + match P::check(expr)? { + NanCheck::NotNeeded => {} + NanCheck::Check(check) => nan_checks.push(check), + NanCheck::Unavailable => return Ok(None), + } + } + let nan_predicate = and_collect(nan_checks); + + Ok(match nan_predicate { + Some(nan_check) => Some(and(nan_check, value_predicate)), + // No possible NaN-bearing expression remains, so the value predicate is + // already guarded. Only one registered rule emits this unguarded + // rewrite so non-float comparisons are not duplicated. + None if P::EMIT_UNGUARDED_REWRITES => Some(value_predicate), + None => None, + }) +} + +pub fn literal_stat(expr: &BoundExpression, stat: Stat) -> Option { + let scalar = expr.as_opt::()?; + match stat { + Stat::Min | Stat::Max => Some(lit(scalar.clone())), + Stat::NullCount => Some(lit(if scalar.is_null() { 1u64 } else { 0u64 })), + Stat::NaNCount => { + let value = scalar.as_primitive_opt()?; + if !value.ptype().is_float() { + return None; + } + + Some(lit(if value.is_nan() { 1u64 } else { 0u64 })) + } + Stat::IsConstant + | Stat::IsSorted + | Stat::IsStrictSorted + | Stat::Sum + | Stat::UncompressedSizeInBytes => None, + } +} + +pub fn cast_stat(expr: &BoundExpression, dtype: &DType, stat: Stat) -> Option { + match stat { + Stat::Min | Stat::Max => stat_expr(expr, stat).map(|stat| cast(stat, dtype.clone())), + Stat::NaNCount | Stat::Sum | Stat::UncompressedSizeInBytes => stat_expr(expr, stat), + Stat::NullCount | Stat::IsConstant | Stat::IsSorted | Stat::IsStrictSorted => None, + } +} + +pub fn stat_fn(expr: BoundExpression, aggregate_fn: AggregateFnRef) -> BoundExpression { + stat(expr, aggregate_fn) +} diff --git a/vortex-functions/src/stat.rs b/vortex-expr/src/stats/stat_fn.rs similarity index 100% rename from vortex-functions/src/stat.rs rename to vortex-expr/src/stats/stat_fn.rs diff --git a/vortex-expr/src/transform/bound_partition.rs b/vortex-expr/src/transform/bound_partition.rs index 3953989401a..91d538033df 100644 --- a/vortex-expr/src/transform/bound_partition.rs +++ b/vortex-expr/src/transform/bound_partition.rs @@ -378,7 +378,6 @@ mod tests { use vortex_array::expr::lit; use vortex_array::expr::pack; use vortex_array::expr::root; - use vortex_functions::expr::merge; use super::*; use crate::analysis::make_bound_free_field_annotator; @@ -570,41 +569,6 @@ mod tests { assert_eq!(partitioned.root, expected_root); } - #[rstest] - fn test_expr_merge(dtype: DType) { - let expr = merge([col("a"), pack([("b", col("b"))], NonNullable)]); - - let partitioned = partition_by_field(expr.bind(&dtype).unwrap(), &dtype).unwrap(); - let expected = merge([get_item("a_0", col("a")), get_item("b_0", col("b"))]); - let root_dtype = - partition_root_dtype(&partitioned.partition_names, &partitioned.partitions); - assert_eq!( - partitioned.root, - expected.bind(&root_dtype).unwrap(), - "{} {}", - partitioned.root, - expected - ); - - assert_eq!(partitioned.partitions.len(), 2); - - let part_a = partitioned.find_partition(&"a".into()).unwrap(); - let expected_a = pack([("a_0", col("a"))], NonNullable); - assert_eq!( - part_a, - &expected_a.bind(&dtype).unwrap(), - "{part_a} {expected_a}" - ); - - let part_b = partitioned.find_partition(&"b".into()).unwrap(); - let expected_b = pack([("b_0", pack([("b", col("b"))], NonNullable))], NonNullable); - assert_eq!( - part_b, - &expected_b.bind(&dtype).unwrap(), - "{part_b} {expected_b}" - ); - } - #[rstest] fn replacing_partitions_refreshes_root_dtype(dtype: DType) -> VortexResult<()> { let mut partitioned = partition_by_field(col("b").bind(&dtype)?, &dtype)?; diff --git a/vortex-expr/src/transform/mod.rs b/vortex-expr/src/transform/mod.rs index 2043ebdebf2..90101207785 100644 --- a/vortex-expr/src/transform/mod.rs +++ b/vortex-expr/src/transform/mod.rs @@ -4,9 +4,7 @@ //! Transformations for [`Expression`](vortex_array::expr::Expression) and //! [`BoundExpression`](vortex_array::expr::BoundExpression) trees. mod bound_partition; -mod match_between; mod replace; pub use bound_partition::*; -pub use match_between::find_between; pub use replace::*; diff --git a/vortex-expr/src/transform/replace.rs b/vortex-expr/src/transform/replace.rs index b03cd5e7c66..a990b601d0f 100644 --- a/vortex-expr/src/transform/replace.rs +++ b/vortex-expr/src/transform/replace.rs @@ -28,16 +28,9 @@ pub fn replace(expr: Expression, needle: &Expression, replacement: Expression) - #[cfg(test)] mod test { use vortex_array::dtype::Nullability::NonNullable; - use vortex_array::expr::Lambda; - use vortex_array::expr::checked_add; - use vortex_array::expr::col; use vortex_array::expr::get_item; use vortex_array::expr::lit; use vortex_array::expr::pack; - use vortex_array::expr::root; - use vortex_array::expr::var; - use vortex_error::VortexResult; - use vortex_functions::expr::list_transform; use super::replace; @@ -58,16 +51,4 @@ mod test { let replaced_expr = replace(e, &needle, replacement); assert_eq!(replaced_expr.to_string(), "pack(a: 1i32, b: 42i32)"); } - - #[test] - fn test_replace_leaves_a_lambda_alone() -> VortexResult<()> { - // The lambda is in `list_transform`'s options, not among its children. - let expr = list_transform(col("l"), Lambda::new(["x"], checked_add(var("x"), lit(1)))?); - assert_eq!(replace(expr.clone(), &var("x"), lit(0)), expr); - assert_eq!( - replace(expr, &root(), col("s")).to_string(), - "vortex.list.transform($.s.l, (x) -> (@x + 1i32))" - ); - Ok(()) - } } diff --git a/vortex-file/src/v2/file_stats_reader.rs b/vortex-file/src/v2/file_stats_reader.rs index 886ba72c614..30fa210e3e3 100644 --- a/vortex-file/src/v2/file_stats_reader.rs +++ b/vortex-file/src/v2/file_stats_reader.rs @@ -298,9 +298,11 @@ mod tests { /// Each test needs its own session: clones of a session share state, and `with_handle` /// stores the test's runtime handle in it. fn new_session() -> VortexSession { - VortexSession::empty() + let session = VortexSession::empty() .with::() - .with::() + .with::(); + vortex_functions::initialize(&session); + session } struct CountingSegments(Arc, AtomicUsize); diff --git a/vortex-functions/Cargo.toml b/vortex-functions/Cargo.toml index f331cd5b5e5..e2e3e2862f3 100644 --- a/vortex-functions/Cargo.toml +++ b/vortex-functions/Cargo.toml @@ -28,6 +28,7 @@ vortex-array = { workspace = true, features = ["unstable_row_fns"] } vortex-buffer = { workspace = true } vortex-compute = { workspace = true } vortex-error = { workspace = true } +vortex-expr = { workspace = true } vortex-mask = { workspace = true } vortex-session = { workspace = true } vortex-utils = { workspace = true } diff --git a/vortex-expr/src/transform/match_between.rs b/vortex-functions/src/between/find.rs similarity index 98% rename from vortex-expr/src/transform/match_between.rs rename to vortex-functions/src/between/find.rs index 0a80cdb24c0..cea8f18dbc5 100644 --- a/vortex-expr/src/transform/match_between.rs +++ b/vortex-functions/src/between/find.rs @@ -13,9 +13,10 @@ use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_error::VortexExpect; use vortex_error::VortexResult; -use vortex_functions::between::Between; -use vortex_functions::between::BetweenOptions; -use vortex_functions::between::StrictComparison; + +use crate::between::Between; +use crate::between::BetweenOptions; +use crate::between::StrictComparison; /// Merge pairs of range comparisons on the same column within any conjunction of `expr` into a /// single [`Between`]. @@ -238,12 +239,12 @@ mod tests { use vortex_array::scalar::Scalar; use vortex_buffer::buffer; use vortex_error::VortexResult; - use vortex_functions::between::BetweenOptions; - use vortex_functions::between::StrictComparison; - use vortex_functions::expr::between; use vortex_session::VortexSession; use super::find_between; + use crate::between::BetweenOptions; + use crate::between::StrictComparison; + use crate::expr::between; static SESSION: LazyLock = LazyLock::new(VortexSession::empty); diff --git a/vortex-functions/src/between/mod.rs b/vortex-functions/src/between/mod.rs index 806458b4f60..9f9f77f33fa 100644 --- a/vortex-functions/src/between/mod.rs +++ b/vortex-functions/src/between/mod.rs @@ -3,6 +3,7 @@ mod constant; mod decimal; +mod find; mod kernel; mod primitive; mod widen; @@ -10,6 +11,7 @@ mod widen; use std::fmt::Display; use std::fmt::Formatter; +pub use find::find_between; pub use kernel::*; use prost::Message; use vortex_array::AnyCanonical; diff --git a/vortex-functions/src/lib.rs b/vortex-functions/src/lib.rs index 9e8ff5acc8c..82eaf569bc8 100644 --- a/vortex-functions/src/lib.rs +++ b/vortex-functions/src/lib.rs @@ -4,13 +4,15 @@ //! Scalar functions built on the vortex-array scalar function framework that the core array //! crate does not itself depend on. //! -//! The scalar functions that canonical arrays, validity and the optimizer rely on (cast, -//! fill_null, mask, binary operators, get_item, pack, literal, ...) live in -//! `vortex_array::scalar_fn::builtin`. This crate holds struct reshaping ([`merge`]), -//! conditionals ([`case_when`]), list reductions ([`list_length`], [`list_sum`]), and -//! [`byte_length`], [`ext_storage`] and [`replace_time_zone`], and the scan-time functions -//! [`dynamic`] (comparisons against a value that changes during a scan) and [`stat`] (reads of -//! a statistic), along with their expression constructors in [`expr`]. +//! The scalar functions that canonical arrays, validity, the optimizer and expression analysis +//! rely on (cast, fill_null, mask, binary operators, get_item, pack, select, literal, ...) live in +//! `vortex_array::scalar_fn::builtin`. This crate holds struct merging ([`merge`]), conditionals +//! ([`case_when`], [`zip`]), list functions ([`list_length`], [`list_sum`], [`list_transform`]), +//! [`byte_length`], [`ext_storage`] and [`replace_time_zone`], string functions ([`lower`], +//! [`strpos`]), string and range predicates ([`like`], [`between`], [`list_contains`], +//! [`is_nan`]) and the scan-time [`dynamic`] and +//! [`dynamic_expression`] functions, whose value changes during a scan, along with their +//! expression constructors in [`expr`] and their statistics-based [`pruning`] rewrites. //! //! Call [`initialize`] to register them with a session. @@ -39,8 +41,8 @@ pub mod list_transform; pub mod lower; pub mod lower_bound; pub mod merge; +pub mod pruning; pub mod replace_time_zone; -pub mod stat; pub mod strpos; pub mod variant_get; pub mod zip; @@ -56,6 +58,7 @@ pub fn initialize(session: &VortexSession) { list_contains::initialize(session); variant_get::initialize(session); zip::initialize(session); + pruning::initialize(session); let scalar_fns = session.scalar_fns(); scalar_fns.register(byte_length::ByteLength); scalar_fns.register(case_when::CaseWhen); @@ -66,7 +69,6 @@ pub fn initialize(session: &VortexSession) { scalar_fns.register(lower::Lower); scalar_fns.register(merge::Merge); scalar_fns.register(replace_time_zone::ReplaceTimeZone); - scalar_fns.register(stat::StatFn); scalar_fns.register(strpos::Strpos); } diff --git a/vortex-functions/src/list_transform/tests.rs b/vortex-functions/src/list_transform/tests.rs index ea57e3136ed..f37d662a7a4 100644 --- a/vortex-functions/src/list_transform/tests.rs +++ b/vortex-functions/src/list_transform/tests.rs @@ -44,6 +44,7 @@ use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::transform::replace; use vortex_session::VortexSession; use super::ListTransform; @@ -632,3 +633,15 @@ fn lambdas_are_not_serializable_yet() -> VortexResult<()> { assert!(error.to_string().contains("is not serializable"), "{error}"); Ok(()) } + +#[test] +fn replace_leaves_a_lambda_alone() -> VortexResult<()> { + // The lambda is in `list_transform`'s options, not among its children. + let expr = list_transform(col("l"), Lambda::new(["x"], checked_add(var("x"), lit(1)))?); + assert_eq!(replace(expr.clone(), &var("x"), lit(0)), expr); + assert_eq!( + replace(expr, &root(), col("s")).to_string(), + "vortex.list.transform($.s.l, (x) -> (@x + 1i32))" + ); + Ok(()) +} diff --git a/vortex-functions/src/lower.rs b/vortex-functions/src/lower.rs index 44360ac0e82..40b038e289c 100644 --- a/vortex-functions/src/lower.rs +++ b/vortex-functions/src/lower.rs @@ -10,6 +10,8 @@ use vortex_array::arrays::VarBinViewArray; use vortex_array::builders::ArrayBuilder; use vortex_array::builders::VarBinViewBuilder; use vortex_array::dtype::DType; +use vortex_array::expr::BoundExpression; +use vortex_array::expr::stats::Stat; use vortex_array::require_input; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::Arity; @@ -19,11 +21,16 @@ use vortex_array::scalar_fn::ExecutionArgs; use vortex_array::scalar_fn::ScalarFnExecutionResult; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; +use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; use vortex_session::VortexSession; use vortex_session::registry::CachedId; + +use crate::lower_bound::LowerBound; +use crate::lower_bound::LowerBoundKind; + /// Unicode lowercase of each element of a Utf8 array. /// /// Each string is lowercased as a whole, so context-dependent mappings such as the Greek final @@ -104,6 +111,30 @@ impl ScalarFnVTable for Lower { fn is_infallible(&self, _options: &Self::Options) -> bool { true } + + /// Bounds `lower(input)` from the byte-ordered min/max of `input`. + fn stat_expression( + &self, + _options: &Self::Options, + expr: &BoundExpression, + stat: Stat, + child_stat: &dyn Fn(&BoundExpression, Stat) -> Option, + ) -> Option { + let input = expr.child(0); + let kind = match stat { + Stat::Min => LowerBoundKind::Min, + Stat::Max => LowerBoundKind::Max, + // Lowercasing maps nulls to nulls and values to values. + Stat::NullCount => return child_stat(input, Stat::NullCount), + _ => return None, + }; + LowerBound + .try_new_bound_expr( + kind, + [child_stat(input, Stat::Min)?, child_stat(input, Stat::Max)?], + ) + .ok() + } } fn lower(input: VarBinViewArray, ctx: &mut ExecutionCtx) -> VortexResult { diff --git a/vortex-functions/src/merge.rs b/vortex-functions/src/merge.rs index 65867da91fc..f45a217e883 100644 --- a/vortex-functions/src/merge.rs +++ b/vortex-functions/src/merge.rs @@ -262,18 +262,26 @@ mod tests { use vortex_array::arrays::struct_::StructArrayExt; use vortex_array::assert_arrays_eq; use vortex_array::dtype::DType; + use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability::NonNullable; use vortex_array::dtype::PType::I32; use vortex_array::dtype::PType::I64; use vortex_array::dtype::PType::U32; use vortex_array::dtype::PType::U64; + use vortex_array::dtype::StructFields; + use vortex_array::expr::BoundExpression; use vortex_array::expr::Expression; + use vortex_array::expr::col; use vortex_array::expr::get_item; + use vortex_array::expr::pack; use vortex_array::expr::root; use vortex_array::scalar_fn::builtin::pack::Pack; use vortex_buffer::buffer; + use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_bail; + use vortex_expr::analysis::make_bound_free_field_annotator; + use vortex_expr::transform::partition_bound; use vortex_session::VortexSession; use crate::expr::merge; @@ -598,4 +606,65 @@ mod tests { &DType::struct_([("a", I32), ("b", U32), ("c", U64)], NonNullable) ); } + + fn partition_root_dtype(names: &FieldNames, partitions: &[BoundExpression]) -> DType { + DType::Struct( + StructFields::new( + names.clone(), + partitions + .iter() + .map(|partition| partition.dtype().clone()) + .collect(), + ), + NonNullable, + ) + } + + #[test] + fn partitions_merge_by_field() -> VortexResult<()> { + let dtype = DType::Struct( + StructFields::from_iter([ + ( + "a", + DType::Struct( + StructFields::from_iter([("x", DType::from(I32)), ("y", DType::from(I32))]), + NonNullable, + ), + ), + ("b", DType::from(I32)), + ("c", DType::from(I32)), + ]), + NonNullable, + ); + let fields = dtype.as_struct_fields_opt().vortex_expect("struct"); + let expr = merge([col("a"), pack([("b", col("b"))], NonNullable)]); + + let partitioned = + partition_bound(expr.bind(&dtype)?, make_bound_free_field_annotator(fields))?; + let expected = merge([get_item("a_0", col("a")), get_item("b_0", col("b"))]); + let root_dtype = + partition_root_dtype(&partitioned.partition_names, &partitioned.partitions); + assert_eq!( + partitioned.root, + expected.bind(&root_dtype)?, + "{} {}", + partitioned.root, + expected + ); + + assert_eq!(partitioned.partitions.len(), 2); + + let part_a = partitioned + .find_partition(&"a".into()) + .vortex_expect("partition a"); + let expected_a = pack([("a_0", col("a"))], NonNullable); + assert_eq!(part_a, &expected_a.bind(&dtype)?, "{part_a} {expected_a}"); + + let part_b = partitioned + .find_partition(&"b".into()) + .vortex_expect("partition b"); + let expected_b = pack([("b_0", pack([("b", col("b"))], NonNullable))], NonNullable); + assert_eq!(part_b, &expected_b.bind(&dtype)?, "{part_b} {expected_b}"); + Ok(()) + } } diff --git a/vortex-expr/src/stats/rewrite/dynamic_expression.rs b/vortex-functions/src/pruning/dynamic_expression.rs similarity index 89% rename from vortex-expr/src/stats/rewrite/dynamic_expression.rs rename to vortex-functions/src/pruning/dynamic_expression.rs index 63f19e14335..b6f722a5a13 100644 --- a/vortex-expr/src/stats/rewrite/dynamic_expression.rs +++ b/vortex-functions/src/pruning/dynamic_expression.rs @@ -23,19 +23,22 @@ use vortex_array::expr::bound::get_item; use vortex_array::expr::bound::lit; use vortex_array::expr::stats::Stat; use vortex_array::scalar_fn::EmptyOptions; +use vortex_array::scalar_fn::ScalarFnId; +use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::internal::row_count::RowCount; use vortex_error::VortexResult; -use vortex_functions::dynamic_expression::DynamicExpression; -use vortex_functions::dynamic_expression::DynamicExpressionOptions; -use vortex_functions::dynamic_expression::DynamicExpressionSource; -use vortex_functions::dynamic_expression::input_field_name; -use vortex_functions::stat::StatFn; +use vortex_expr::stats::bound::stat; +use vortex_expr::stats::rewrite::BoundExpressionPruneExt; +use vortex_expr::stats::rewrite::StatsRewriteRule; +use vortex_expr::stats::stat_fn::StatFn; use vortex_session::VortexSession; -use crate::stats::bound::stat; -use crate::stats::rewrite::BoundExpressionPruneExt; +use crate::dynamic_expression::DynamicExpression; +use crate::dynamic_expression::DynamicExpressionOptions; +use crate::dynamic_expression::DynamicExpressionSource; +use crate::dynamic_expression::input_field_name; /// Which stats proof a [`StatsProofSource`] builds. #[derive(Clone, Copy, Debug)] @@ -201,6 +204,32 @@ impl DynamicExpressionSource for StatsProofSource { } } +/// Proves a [`DynamicExpression`] through its current source expression. +#[derive(Debug)] +pub(super) struct DynamicExpressionStatsRewrite; + +impl StatsRewriteRule for DynamicExpressionStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + DynamicExpression.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + session: &VortexSession, + ) -> VortexResult> { + dynamic_stats_proof(expr, session, StatsProof::Falsify) + } + + fn satisfy( + &self, + expr: &BoundExpression, + session: &VortexSession, + ) -> VortexResult> { + dynamic_stats_proof(expr, session, StatsProof::Satisfy) + } +} + #[cfg(test)] mod tests { use std::sync::atomic::AtomicU64; @@ -222,10 +251,10 @@ mod tests { use vortex_array::scalar::Scalar; use vortex_array::validity::Validity; use vortex_buffer::buffer; - use vortex_functions::expr::dynamic_expression; + use vortex_expr::stats::StatsSession; use super::*; - use crate::stats::StatsSession; + use crate::expr::dynamic_expression; #[derive(Debug, Default)] struct Threshold(AtomicU64); @@ -263,6 +292,7 @@ mod tests { #[test] fn existing_proofs_follow_new_generations() -> VortexResult<()> { let session = VortexSession::empty().with::(); + crate::initialize(&session); let threshold = Arc::new(Threshold::default()); let dtype = DType::struct_( [("a", DType::Primitive(PType::U64, Nullability::NonNullable))], diff --git a/vortex-expr/src/stats/rewrite/like_variant.rs b/vortex-functions/src/pruning/like_variant.rs similarity index 100% rename from vortex-expr/src/stats/rewrite/like_variant.rs rename to vortex-functions/src/pruning/like_variant.rs diff --git a/vortex-functions/src/pruning/mod.rs b/vortex-functions/src/pruning/mod.rs new file mode 100644 index 00000000000..fde2f324a7b --- /dev/null +++ b/vortex-functions/src/pruning/mod.rs @@ -0,0 +1,747 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! Statistics-based pruning rewrites for this crate's scalar functions. + +use std::cmp::Ordering; + +use vortex_array::expr::BoundExpression; +use vortex_array::expr::bound::and; +use vortex_array::expr::bound::and_collect; +use vortex_array::expr::bound::binary; +use vortex_array::expr::bound::eq; +use vortex_array::expr::bound::gt; +use vortex_array::expr::bound::gt_eq; +use vortex_array::expr::bound::lit; +use vortex_array::expr::bound::lt; +use vortex_array::expr::bound::or; +use vortex_array::expr::stats::Stat; +use vortex_array::scalar::Scalar; +use vortex_array::scalar::StringLike; +use vortex_array::scalar_fn::ScalarFnId; +use vortex_array::scalar_fn::ScalarFnVTable; +use vortex_array::scalar_fn::builtin::literal::Literal; +use vortex_array::scalar_fn::builtin::operators::CompareOperator; +use vortex_error::VortexResult; +use vortex_expr::stats::rewrite::StatsRewriteRule; +use vortex_expr::stats::rewrite::falsify; +use vortex_expr::stats::rewrite::helpers::*; +use vortex_expr::stats::session::StatsSessionExt; +use vortex_session::VortexSession; + +use self::dynamic_expression::DynamicExpressionStatsRewrite; +use self::like_variant::LikeVariant; +use crate::between::Between; +use crate::dynamic::DynamicComparison; +use crate::expr::bound::dynamic_with_options; +use crate::is_nan::IsNan; +use crate::like::Like; +use crate::list_contains::ListContains; +use crate::list_contains::PreparedSet; +use crate::list_contains::PreparedSetData; + +mod dynamic_expression; +mod like_variant; + +/// Register the stats rewrite rules for this crate's scalar functions. +pub fn initialize(session: &VortexSession) { + let stats = session.stats(); + stats.register_rewrite(BetweenStatsRewrite); + stats.register_rewrite(IsNanNaNCountStatsRewrite); + stats.register_rewrite(IsNanAllNonNanStatsRewrite); + stats.register_rewrite(IsNanAllNanStatsRewrite); + stats.register_rewrite(LikeStatsRewrite); + stats.register_rewrite(ListContainsNanCountStatsRewrite); + stats.register_rewrite(ListContainsAllNonNanStatsRewrite); + stats.register_rewrite(DynamicComparisonNanCountStatsRewrite); + stats.register_rewrite(DynamicComparisonAllNonNanStatsRewrite); + stats.register_rewrite(DynamicExpressionStatsRewrite); +} + +#[derive(Debug)] +struct BetweenStatsRewrite; + +impl StatsRewriteRule for BetweenStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + Between.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + session: &VortexSession, + ) -> VortexResult> { + let options = expr.as_::(); + let arr = expr.child(0).clone(); + let lower = expr.child(1).clone(); + let upper = expr.child(2).clone(); + + let lhs = binary(options.lower_strict.to_operator(), lower, arr.clone()); + let rhs = binary(options.upper_strict.to_operator(), arr, upper); + falsify(&and(lhs, rhs), session) + } +} + +/// Rewrites `is_nan` using the `NaNCount` stat: the predicate is falsified when a zone +/// contains no NaN values, and satisfied when every row in the zone is NaN. +#[derive(Debug)] +struct IsNanNaNCountStatsRewrite; + +impl StatsRewriteRule for IsNanNaNCountStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + IsNan.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + Ok(nan_count(expr.child(0)).map(|nan_count| eq(nan_count, lit(0u64)))) + } + + fn satisfy( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + Ok(nan_count(expr.child(0)).map(|nan_count| eq(nan_count, row_count()))) + } +} + +/// Falsifies `is_nan` when the `AllNonNan` pruning stat proves that no value in the zone +/// is NaN. +#[derive(Debug)] +struct IsNanAllNonNanStatsRewrite; + +impl StatsRewriteRule for IsNanAllNonNanStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + IsNan.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + Ok(Some(all_non_nan(expr.child(0)))) + } +} + +/// Satisfies `is_nan` when the `AllNan` pruning stat proves that every value in the zone +/// is NaN. +#[derive(Debug)] +struct IsNanAllNanStatsRewrite; + +impl StatsRewriteRule for IsNanAllNanStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + IsNan.id() + } + + fn satisfy( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + Ok(Some(all_nan(expr.child(0)))) + } +} + +#[derive(Debug)] +struct LikeStatsRewrite; + +impl StatsRewriteRule for LikeStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + Like.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + let like_options = expr.as_::(); + if like_options.negated || like_options.case_insensitive { + return Ok(None); + } + + let Some(pattern) = expr.child(1).as_opt::() else { + return Ok(None); + }; + let Some(pattern) = pattern.as_utf8().value() else { + return Ok(None); + }; + + let source = expr.child(0); + Ok(match LikeVariant::from_str(pattern) { + Some(LikeVariant::Exact(text)) => { + min(source) + .zip(max(source)) + .map(|(source_min, source_max)| { + or( + gt(source_min, lit(text.as_ref())), + lt(source_max, lit(text.as_ref())), + ) + }) + } + Some(LikeVariant::Prefix(prefix)) => { + let Some(successor) = prefix.to_string().increment().ok() else { + return Ok(None); + }; + min(source) + .zip(max(source)) + .map(|(source_min, source_max)| { + or( + gt_eq(source_min, lit(successor)), + lt(source_max, lit(prefix.as_ref())), + ) + }) + } + None => None, + }) + } +} + +#[derive(Debug)] +struct ListContainsNanCountStatsRewrite; + +impl StatsRewriteRule for ListContainsNanCountStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + ListContains.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + list_contains_falsify::(expr) + } +} + +#[derive(Debug)] +struct ListContainsAllNonNanStatsRewrite; + +impl StatsRewriteRule for ListContainsAllNonNanStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + ListContains.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + list_contains_falsify::(expr) + } +} + +/// Lists with more elements falsify `needle IN list` with the bounds of the list rather than +/// with a test per element, which keeps the pruning expression, and its evaluation per zone, +/// small for the large lists of dynamic join filters. +const MAX_LIST_ELEMENT_FALSIFICATIONS: usize = 16; + +/// The smallest and largest non-null elements, or `None` when there are none or two elements +/// are unordered. +fn non_null_bounds(elements: &[Scalar]) -> Option<(&Scalar, &Scalar)> { + let mut non_null = elements.iter().filter(|element| !element.is_null()); + let first = non_null.next()?; + non_null.try_fold((first, first), |(low, high), element| { + let low = match element.partial_cmp(low)? { + Ordering::Less => element, + Ordering::Equal | Ordering::Greater => low, + }; + let high = match element.partial_cmp(high)? { + Ordering::Greater => element, + Ordering::Equal | Ordering::Less => high, + }; + Some((low, high)) + }) +} + +fn list_contains_falsify( + expr: &BoundExpression, +) -> VortexResult> { + let list = expr.child(0); + let needle = expr.child(1); + + // Reduction replaces a literal list with a set prepared from it. + let elements = list + .as_opt::() + .or_else(|| list.as_opt::().map(PreparedSetData::list)) + .and_then(|list| list.as_list_opt()) + .and_then(|list| list.elements()); + let Some(elements) = elements else { + return Ok(None); + }; + if elements.is_empty() { + return Ok(P::EMIT_UNGUARDED_REWRITES.then(|| lit(true))); + } + + let Some(value_max) = max(needle) else { + return Ok(None); + }; + let Some(value_min) = min(needle) else { + return Ok(None); + }; + + let value_predicate = if elements.len() > MAX_LIST_ELEMENT_FALSIFICATIONS + && let Some((low, high)) = non_null_bounds(&elements) + { + Some(or( + lt(value_max, lit(low.clone())), + gt(value_min, lit(high.clone())), + )) + } else { + and_collect(elements.iter().map(|value| { + or( + lt(value_max.clone(), lit(value.clone())), + gt(value_min.clone(), lit(value.clone())), + ) + })) + }; + value_predicate + .map(|value_predicate| with_non_nan_guards::

([needle], value_predicate)) + .transpose() + .map(Option::flatten) +} + +#[derive(Debug)] +struct DynamicComparisonNanCountStatsRewrite; + +impl StatsRewriteRule for DynamicComparisonNanCountStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + DynamicComparison.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + dynamic_comparison_falsify::(expr) + } +} + +#[derive(Debug)] +struct DynamicComparisonAllNonNanStatsRewrite; + +impl StatsRewriteRule for DynamicComparisonAllNonNanStatsRewrite { + fn scalar_fn_id(&self) -> ScalarFnId { + DynamicComparison.id() + } + + fn falsify( + &self, + expr: &BoundExpression, + _session: &VortexSession, + ) -> VortexResult> { + dynamic_comparison_falsify::(expr) + } +} + +fn dynamic_comparison_falsify( + expr: &BoundExpression, +) -> VortexResult> { + let dynamic = expr.as_::(); + let lhs = expr.child(0); + + let Some((operator, lhs_stat)) = (match dynamic.operator() { + CompareOperator::Eq | CompareOperator::NotEq => None, + CompareOperator::Gt => max(lhs).map(|lhs_stat| (CompareOperator::Lte, lhs_stat)), + CompareOperator::Gte => max(lhs).map(|lhs_stat| (CompareOperator::Lt, lhs_stat)), + CompareOperator::Lt => min(lhs).map(|lhs_stat| (CompareOperator::Gte, lhs_stat)), + CompareOperator::Lte => min(lhs).map(|lhs_stat| (CompareOperator::Gt, lhs_stat)), + }) else { + return Ok(None); + }; + + let value_predicate = dynamic_with_options( + dynamic.with_operator(operator, !dynamic.default()), + lhs_stat, + ); + with_non_nan_guards::

([lhs], value_predicate) +} + +#[cfg(test)] +#[cfg(test)] +mod tests { + use std::sync::Arc; + use std::sync::LazyLock; + + use vortex_array::aggregate_fn::AggregateFnRef; + use vortex_array::dtype::DType; + use vortex_array::dtype::Nullability; + use vortex_array::dtype::PType; + use vortex_array::dtype::StructFields; + use vortex_array::expr::BoundExpression; + use vortex_array::expr::Expression; + use vortex_array::expr::and; + use vortex_array::expr::col; + use vortex_array::expr::eq; + use vortex_array::expr::get_item; + use vortex_array::expr::gt; + use vortex_array::expr::gt_eq; + use vortex_array::expr::lit; + use vortex_array::expr::lt; + use vortex_array::expr::or; + use vortex_array::expr::stats::Stat; + use vortex_array::scalar::Scalar; + use vortex_array::scalar_fn::EmptyOptions; + use vortex_array::scalar_fn::ScalarFnVTableExt; + use vortex_array::scalar_fn::builtin::operators::CompareOperator; + use vortex_array::scalar_fn::internal::row_count::RowCount; + use vortex_error::VortexResult; + use vortex_expr::stats::expr::StatFn; + use vortex_expr::stats::expr::StatOptions; + use vortex_expr::stats::rewrite::BoundExpressionPruneExt; + use vortex_session::VortexSession; + + use crate::between::BetweenOptions; + use crate::between::StrictComparison; + use crate::dynamic::DynamicComparison; + use crate::expr::between; + use crate::expr::dynamic; + use crate::expr::is_nan; + use crate::expr::like; + use crate::expr::list_contains; + + static SESSION: LazyLock = LazyLock::new(|| { + let session = VortexSession::empty(); + crate::initialize(&session); + session + }); + + fn stat(expr: Expression, stat: Stat) -> Expression { + let aggregate_fn = stat.aggregate_fn().expect("stat should have aggregate fn"); + stat_fn(expr, aggregate_fn) + } + + fn stat_fn(expr: Expression, aggregate_fn: AggregateFnRef) -> Expression { + StatFn.new_expr(StatOptions::new(aggregate_fn), [expr]) + } + + fn test_scope() -> DType { + DType::Struct( + StructFields::from_iter([ + ("a", DType::Primitive(PType::I32, Nullability::NonNullable)), + ("b", DType::Primitive(PType::I32, Nullability::NonNullable)), + ("f", DType::Primitive(PType::F32, Nullability::NonNullable)), + ("s", DType::Utf8(Nullability::NonNullable)), + ("t", DType::Utf8(Nullability::NonNullable)), + ("n", nested_struct_dtype()), + ]), + Nullability::NonNullable, + ) + } + + fn nested_struct_dtype() -> DType { + DType::Struct( + StructFields::from_iter([("x", DType::Primitive(PType::F32, Nullability::Nullable))]), + Nullability::NonNullable, + ) + } + + fn falsify(expr: &Expression) -> VortexResult> { + expr.bind(&test_scope())?.falsify(&SESSION) + } + + fn satisfy(expr: &Expression) -> VortexResult> { + expr.bind(&test_scope())?.satisfy(&SESSION) + } + + fn bind_expected(expr: Option) -> VortexResult> { + expr.map(|expr| expr.bind(&test_scope())).transpose() + } + + fn all_nan(expr: &Expression) -> Expression { + vortex_expr::stats::all_nan(expr.clone()) + } + + fn all_non_nan(expr: &Expression) -> Expression { + vortex_expr::stats::all_non_nan(expr.clone()) + } + + macro_rules! assert_rewrite_eq { + ($actual:expr, $expected:expr) => { + assert_eq!($actual, bind_expected($expected)?) + }; + } + + #[test] + fn rewrites_between_falsifier() -> VortexResult<()> { + let expr = between( + col("a"), + lit(10), + lit(50), + BetweenOptions { + lower_strict: StrictComparison::NonStrict, + upper_strict: StrictComparison::NonStrict, + }, + ); + + assert_rewrite_eq!( + falsify(&expr)?, + Some(or( + gt(lit(10), stat(col("a"), Stat::Max)), + gt(stat(col("a"), Stat::Min), lit(50)), + )) + ); + Ok(()) + } + + #[test] + fn rewrites_nan_falsifiers() -> VortexResult<()> { + assert_rewrite_eq!( + falsify(&is_nan(col("f")))?, + Some(or( + eq(stat(col("f"), Stat::NaNCount), lit(0u64)), + all_non_nan(&col("f")), + )) + ); + + // Nullable floats prune on nan_count just the same. + assert_rewrite_eq!( + falsify(&is_nan(get_item("x", col("n"))))?, + Some(or( + eq(stat(get_item("x", col("n")), Stat::NaNCount), lit(0u64)), + all_non_nan(&get_item("x", col("n"))), + )) + ); + + // NaN literals fold through the nan_count stat. + assert_rewrite_eq!( + falsify(&is_nan(lit(f32::NAN)))?, + Some(or(eq(lit(1u64), lit(0u64)), all_non_nan(&lit(f32::NAN)))) + ); + Ok(()) + } + + #[test] + fn rewrites_nan_satisfiers() -> VortexResult<()> { + assert_rewrite_eq!( + satisfy(&is_nan(col("f")))?, + Some(or( + eq( + stat(col("f"), Stat::NaNCount), + RowCount.new_expr(EmptyOptions, []) + ), + all_nan(&col("f")), + )) + ); + + assert_rewrite_eq!( + satisfy(&is_nan(lit(f32::NAN)))?, + Some(or( + eq(lit(1u64), RowCount.new_expr(EmptyOptions, [])), + all_nan(&lit(f32::NAN)), + )) + ); + Ok(()) + } + + #[test] + fn rewrites_list_contains_falsifier() -> VortexResult<()> { + let list = Scalar::list( + Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)), + vec![1.into(), 2.into(), 3.into()], + Nullability::NonNullable, + ); + let expr = list_contains(lit(list), col("a")); + + assert_rewrite_eq!( + falsify(&expr)?, + Some(and( + and( + or( + lt(stat(col("a"), Stat::Max), lit(1i32)), + gt(stat(col("a"), Stat::Min), lit(1i32)), + ), + or( + lt(stat(col("a"), Stat::Max), lit(2i32)), + gt(stat(col("a"), Stat::Min), lit(2i32)), + ), + ), + or( + lt(stat(col("a"), Stat::Max), lit(3i32)), + gt(stat(col("a"), Stat::Min), lit(3i32)), + ), + )) + ); + Ok(()) + } + + #[test] + fn rewrites_large_list_contains_falsifier_to_bounds() -> VortexResult<()> { + let list = Scalar::list( + Arc::new(DType::Primitive(PType::I32, Nullability::Nullable)), + (0..20) + .rev() + .map(|value| Scalar::from(Some(value))) + .chain([Scalar::null(DType::Primitive( + PType::I32, + Nullability::Nullable, + ))]) + .collect(), + Nullability::NonNullable, + ); + let expr = list_contains(lit(list), col("a")); + + assert_rewrite_eq!( + falsify(&expr)?, + Some(or( + lt(stat(col("a"), Stat::Max), lit(Scalar::from(Some(0i32)))), + gt(stat(col("a"), Stat::Min), lit(Scalar::from(Some(19i32)))), + )) + ); + Ok(()) + } + + #[test] + fn rewrites_like_falsifier() -> VortexResult<()> { + let expr = like(col("s"), lit("prefix%")); + assert_rewrite_eq!( + falsify(&expr)?, + Some(or( + gt_eq(stat(col("s"), Stat::Min), lit("prefiy")), + lt(stat(col("s"), Stat::Max), lit("prefix")), + )) + ); + + let expr = like(col("s"), lit(r"\%%")); + assert_rewrite_eq!( + falsify(&expr)?, + Some(or( + gt_eq(stat(col("s"), Stat::Min), lit("&")), + lt(stat(col("s"), Stat::Max), lit("%")), + )) + ); + + let expr = like(col("s"), lit("pref%ix%")); + assert_rewrite_eq!( + falsify(&expr)?, + Some(or( + gt_eq(stat(col("s"), Stat::Min), lit("preg")), + lt(stat(col("s"), Stat::Max), lit("pref")), + )) + ); + + let expr = like(col("s"), lit("pref_ix_")); + assert_rewrite_eq!( + falsify(&expr)?, + Some(or( + gt_eq(stat(col("s"), Stat::Min), lit("preg")), + lt(stat(col("s"), Stat::Max), lit("pref")), + )) + ); + + let expr = like(col("s"), lit("exact")); + assert_rewrite_eq!( + falsify(&expr)?, + Some(or( + gt(stat(col("s"), Stat::Min), lit("exact")), + lt(stat(col("s"), Stat::Max), lit("exact")), + )) + ); + + let expr = like(col("s"), lit("%suffix")); + assert_rewrite_eq!(falsify(&expr)?, None); + Ok(()) + } + + #[test] + fn rewrites_dynamic_comparison_falsifier() -> VortexResult<()> { + let expr = dynamic( + CompareOperator::Gt, + || Some(10i32.into()), + DType::Primitive(PType::I32, Nullability::NonNullable), + true, + col("a"), + ); + let dynamic = expr.as_::(); + + assert_rewrite_eq!( + falsify(&expr)?, + Some(DynamicComparison.new_expr( + dynamic.with_operator(CompareOperator::Lte, false), + [stat(col("a"), Stat::Max)], + )) + ); + Ok(()) + } + + #[test] + fn rewrites_prepared_set_list_contains_falsifier() -> VortexResult<()> { + let list = Scalar::list( + Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)), + vec![1.into(), 2.into()], + Nullability::NonNullable, + ); + let set = PreparedSet.new_expr( + PreparedSetData::try_new(list)?, + Vec::::new(), + ); + let expr = ListContains.new_expr( + crate::list_contains::ListContainsOptions::default(), + [set, col("a")], + ); + + assert_rewrite_eq!( + falsify(&expr)?, + Some(and( + or( + lt(stat(col("a"), Stat::Max), lit(1i32)), + gt(stat(col("a"), Stat::Min), lit(1i32)), + ), + or( + lt(stat(col("a"), Stat::Max), lit(2i32)), + gt(stat(col("a"), Stat::Min), lit(2i32)), + ), + )) + ); + Ok(()) + } + + #[test] + fn list_falsification() -> VortexResult<()> { + let expr = list_contains( + lit(Scalar::list( + Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)), + vec![1.into(), 2.into(), 3.into()], + Nullability::NonNullable, + )), + col("a"), + ); + let scope = DType::Struct( + StructFields::new( + ["a"].into(), + vec![DType::Primitive(PType::I32, Nullability::NonNullable)], + ), + Nullability::NonNullable, + ); + + assert_eq!( + expr.bind(&scope)?.falsify(&SESSION)?, + Some( + and( + and( + or( + lt(stat(col("a"), Stat::Max), lit(1i32)), + gt(stat(col("a"), Stat::Min), lit(1i32)), + ), + or( + lt(stat(col("a"), Stat::Max), lit(2i32)), + gt(stat(col("a"), Stat::Min), lit(2i32)), + ) + ), + or( + lt(stat(col("a"), Stat::Max), lit(3i32)), + gt(stat(col("a"), Stat::Min), lit(3i32)), + ) + ) + .bind(&scope)? + ) + ); + Ok(()) + } +} diff --git a/vortex-layout/src/scan/scan_builder.rs b/vortex-layout/src/scan/scan_builder.rs index b57162dde0b..eccd98b8750 100644 --- a/vortex-layout/src/scan/scan_builder.rs +++ b/vortex-layout/src/scan/scan_builder.rs @@ -23,7 +23,7 @@ use vortex_array::stream::ArrayStreamAdapter; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_expr::analysis::referenced_field_paths; -use vortex_expr::transform::find_between; +use vortex_functions::between::find_between; use vortex_io::runtime::BlockingRuntime; use vortex_io::runtime::Task; use vortex_io::session::RuntimeSessionExt; diff --git a/vortex-layout/src/test.rs b/vortex-layout/src/test.rs index 29eeb8c3eba..c2151edb5a1 100644 --- a/vortex-layout/src/test.rs +++ b/vortex-layout/src/test.rs @@ -12,9 +12,11 @@ use crate::session::LayoutSession; pub static SESSION: LazyLock = LazyLock::new(|| new_session().with_tokio()); pub fn new_session() -> VortexSession { - VortexSession::empty() + let session = VortexSession::empty() .with::() - .with::() + .with::(); + vortex_functions::initialize(&session); + session } /// A child strategy that fails with [`FailingStrategy::MESSAGE`] without reading its stream, as a diff --git a/vortex-scan/src/execution/filter.rs b/vortex-scan/src/execution/filter.rs index 16cc3db2292..b073dc7bb2b 100644 --- a/vortex-scan/src/execution/filter.rs +++ b/vortex-scan/src/execution/filter.rs @@ -13,7 +13,7 @@ use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_ensure_eq; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; -use vortex_expr::transform::find_between; +use vortex_functions::between::find_between; use vortex_io::request::IoBatch; use vortex_io::request::IoConsumer; use vortex_io::request::IoIntent; diff --git a/vortex-scan/src/plan/builtins/zoned/mod.rs b/vortex-scan/src/plan/builtins/zoned/mod.rs index c6c31f35f66..536283320d3 100644 --- a/vortex-scan/src/plan/builtins/zoned/mod.rs +++ b/vortex-scan/src/plan/builtins/zoned/mod.rs @@ -27,8 +27,8 @@ use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::stats::stat_fn::StatFn; use vortex_functions::dynamic::DynamicExprUpdates; -use vortex_functions::stat::StatFn; use vortex_mask::Mask; use vortex_session::VortexSession; use vortex_session::registry::CachedId; diff --git a/vortex/src/lib.rs b/vortex/src/lib.rs index b753fd5030e..28707736cfb 100644 --- a/vortex/src/lib.rs +++ b/vortex/src/lib.rs @@ -359,6 +359,7 @@ impl VortexSessionDefault for VortexSession { .with::() .with::() .with::(); + vortex_expr::initialize(&session); vortex_functions::initialize(&session); #[cfg(feature = "parquet-variant")] vortex_parquet_variant::initialize(&session); From c52e2d066ff031fcbd74d2246633b7cf00991b31 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Mon, 9 Nov 2026 02:24:57 +0000 Subject: [PATCH 2/7] Move expressions out of vortex-array into vortex-expr Unbound `Expression`, binding, traversal, lambdas, proto serialization and the expression constructors now live in `vortex-expr`; `vortex-array` keeps arrays, the scalar function machinery, the builtins it is built on, and `BoundExpression` with its reduce node and optimizer. The scalar function vtable reduces over bound expressions, so keeping them next to it lets every reduction rule stay monomorphized for expression trees as well as array trees. `vortex-expr` re-exports `BoundExpression`. The layering is `vortex-array -> vortex-expr -> vortex-functions`. - `ScalarFnVTable::simplify` is generic over `ReduceNode`, like `reduce`, but stays a separate hook that only the expression optimizer applies: a simplification may rewrite a node into one whose nullability differs, which the array optimizer cannot accept (it must keep the exact dtype and defer constant masks to the kernel rules). `ReduceNode::is_same` lets rules such as the COALESCE rewrite compare operands. - `new_expr`, `try_new_expr` and `try_new_bound_expr` move to `vortex_expr::ScalarFnExprExt`, `ArrayRef::apply`/`apply_bound` to `vortex_expr::ArrayExprExt`, and the expression half of the builtins to `vortex_expr::ExprBuiltins`. `ExprDisplay` moves to `vortex_array::scalar_fn::display`, and the tree renderer to `vortex_array::expr::display`. - `ArrayRef::take_slot_unchecked`/`put_slot_unchecked` become public, like `with_slots`, so row-count substitution in `vortex-expr` still rewrites lazy arrays in place. `vortex_array::expr` otherwise keeps only `stats`, which moves with the aggregate split. The core builtins' expression-level tests move to `vortex-expr`; the few array-level tests that used expressions now go through `ArrayBuiltins`. The `expr_*` benches move to `vortex-expr`, and their CodSpeed counts and durations move with them. Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Will Manning Co-Authored-By: Will Manning Signed-off-by: Robert Kruszewski --- AGENTS.md | 2 + docs/concepts/expressions.md | 6 +- encodings/fsst/Cargo.toml | 2 + encodings/fsst/benches/fsst_url_compare.rs | 5 +- encodings/fsst/src/kernel.rs | 3 +- encodings/parquet-variant/Cargo.toml | 1 + .../src/json_to_variant_tests.rs | 3 +- encodings/parquet-variant/src/kernel.rs | 3 +- encodings/sequence/Cargo.toml | 1 + .../sequence/src/compute/list_contains.rs | 5 +- fuzz/Cargo.toml | 1 + fuzz/fuzz_targets/file_io.rs | 5 +- fuzz/src/file/mod.rs | 6 +- fuzz/src/fsst_like.rs | 4 +- scripts/codspeed_bench_counts.json | 6 +- scripts/codspeed_bench_durations.json | 6 +- vortex-array/Cargo.toml | 10 - vortex-array/benches/dict_compare.rs | 13 +- vortex-array/src/array/erased.rs | 4 +- vortex-array/src/arrays/scalar_fn/rules.rs | 28 +- vortex-array/src/builtins.rs | 67 --- vortex-array/src/expr/aliases.rs | 7 - vortex-array/src/expr/bound_expression.rs | 257 +-------- vortex-array/src/expr/display.rs | 171 +----- vortex-array/src/expr/mod.rs | 126 +--- vortex-array/src/expr/optimize.rs | 138 +---- vortex-array/src/lib.rs | 1 - .../src/scalar_fn/builtin/binary/mod.rs | 545 ++---------------- .../src/scalar_fn/builtin/cast/mod.rs | 175 +----- .../src/{expr => scalar_fn/builtin}/field.rs | 0 .../src/scalar_fn/builtin/fill_null/mod.rs | 361 +----------- .../src/scalar_fn/builtin/get_item.rs | 444 +------------- .../src/scalar_fn/builtin/is_not_null.rs | 197 +------ vortex-array/src/scalar_fn/builtin/is_null.rs | 139 ----- vortex-array/src/scalar_fn/builtin/literal.rs | 57 +- .../src/scalar_fn/builtin/mask/mod.rs | 62 +- vortex-array/src/scalar_fn/builtin/mod.rs | 1 + vortex-array/src/scalar_fn/builtin/not/mod.rs | 108 ---- vortex-array/src/scalar_fn/builtin/pack.rs | 248 +------- vortex-array/src/scalar_fn/builtin/select.rs | 295 +--------- vortex-array/src/scalar_fn/display.rs | 18 + vortex-array/src/scalar_fn/erased.rs | 20 +- vortex-array/src/scalar_fn/internal/mod.rs | 4 - vortex-array/src/scalar_fn/mod.rs | 8 +- vortex-array/src/scalar_fn/typed.rs | 14 +- vortex-array/src/scalar_fn/vtable.rs | 69 +-- vortex-cuda/src/layout.rs | 3 +- vortex-datafusion/src/convert/exprs/mod.rs | 2 +- vortex-datafusion/src/convert/exprs/tests.rs | 1 + vortex-duckdb/src/convert/expr.rs | 2 +- vortex-duckdb/src/convert/table_filter.rs | 2 +- vortex-expr/Cargo.toml | 20 + .../benches}/large_struct_pack.rs | 6 +- .../benches/optimize.rs | 12 +- vortex-expr/src/analysis/annotation.rs | 11 +- vortex-expr/src/analysis/immediate_access.rs | 2 +- vortex-expr/src/analysis/labeling.rs | 20 +- .../src/analysis/referenced_field_paths.rs | 23 +- .../expression.rs => vortex-expr/src/apply.rs | 31 +- .../src/expr => vortex-expr/src}/arbitrary.rs | 22 +- vortex-expr/src/bind.rs | 227 ++++++++ vortex-expr/src/builtins.rs | 86 +++ vortex-expr/src/display.rs | 144 +++++ .../expr => vortex-expr/src}/expression.rs | 24 +- .../src/expr => vortex-expr/src}/exprs.rs | 105 ++-- vortex-expr/src/forms/extract_conjuncts.rs | 3 +- .../expr => vortex-expr/src}/lambda/mod.rs | 45 +- .../src/lambda}/row_count.rs | 39 +- .../expr => vortex-expr/src}/lambda/tests.rs | 63 +- vortex-expr/src/lib.rs | 78 ++- .../src/proto}/foreign.rs | 25 +- .../proto.rs => vortex-expr/src/proto/mod.rs | 36 +- vortex-expr/src/scalar_fn_ext.rs | 41 ++ .../src/expr => vortex-expr/src}/scope.rs | 11 +- vortex-expr/src/stats/bind.rs | 22 +- vortex-expr/src/stats/expr.rs | 16 +- vortex-expr/src/stats/rewrite.rs | 12 +- vortex-expr/src/stats/rewrite/builtins.rs | 58 +- vortex-expr/src/stats/rewrite/fn_tests.rs | 18 +- vortex-expr/src/stats/rewrite/helpers.rs | 16 +- vortex-expr/src/stats/stat_fn.rs | 2 +- vortex-expr/src/test_harness.rs | 25 + vortex-expr/src/tests/binary.rs | 465 +++++++++++++++ vortex-expr/src/tests/cast.rs | 144 +++++ vortex-expr/src/tests/expression.rs | 16 + vortex-expr/src/tests/fill_null.rs | 347 +++++++++++ vortex-expr/src/tests/get_item.rs | 441 ++++++++++++++ vortex-expr/src/tests/is_not_null.rs | 192 ++++++ vortex-expr/src/tests/is_null.rs | 136 +++++ vortex-expr/src/tests/literal.rs | 55 ++ vortex-expr/src/tests/mask.rs | 37 ++ vortex-expr/src/tests/mod.rs | 18 + vortex-expr/src/tests/not.rs | 108 ++++ vortex-expr/src/tests/optimize.rs | 110 ++++ vortex-expr/src/tests/pack.rs | 246 ++++++++ vortex-expr/src/tests/select.rs | 247 ++++++++ vortex-expr/src/transform/bound_partition.rs | 24 +- vortex-expr/src/transform/mod.rs | 4 +- vortex-expr/src/transform/replace.rs | 15 +- .../src}/traversal/fold.rs | 18 +- .../expr => vortex-expr/src}/traversal/mod.rs | 28 +- .../src}/traversal/visitor.rs | 8 +- vortex-ffi/src/array.rs | 1 + vortex-ffi/src/expression.rs | 2 +- vortex-file/src/file.rs | 2 +- vortex-file/src/pruning.rs | 7 +- vortex-file/src/tests.rs | 32 +- vortex-file/src/v2/file_stats_reader.rs | 17 +- vortex-functions/benches/case_when.rs | 13 +- vortex-functions/benches/list_contains_set.rs | 5 +- vortex-functions/benches/list_length.rs | 3 +- vortex-functions/benches/list_sum.rs | 3 +- vortex-functions/src/between/find.rs | 23 +- vortex-functions/src/between/mod.rs | 15 +- vortex-functions/src/builtins.rs | 6 +- vortex-functions/src/byte_length.rs | 3 +- vortex-functions/src/case_when.rs | 68 ++- vortex-functions/src/dynamic.rs | 13 +- vortex-functions/src/dynamic_expression.rs | 13 +- vortex-functions/src/expr.rs | 38 +- vortex-functions/src/ext_storage.rs | 7 +- vortex-functions/src/is_nan.rs | 7 +- vortex-functions/src/like/mod.rs | 8 +- vortex-functions/src/list_contains/mod.rs | 15 +- .../src/list_contains/prepared/function.rs | 2 +- .../src/list_contains/prepared/tests.rs | 5 +- vortex-functions/src/list_length.rs | 5 +- vortex-functions/src/list_sum.rs | 7 +- vortex-functions/src/list_transform/mod.rs | 8 +- vortex-functions/src/list_transform/tests.rs | 23 +- vortex-functions/src/lower.rs | 9 +- vortex-functions/src/merge.rs | 13 +- .../src/pruning/dynamic_expression.rs | 21 +- vortex-functions/src/pruning/mod.rs | 46 +- vortex-functions/src/replace_time_zone.rs | 29 +- vortex-functions/src/strpos.rs | 11 +- vortex-functions/src/tests.rs | 38 +- vortex-functions/src/variant_get/mod.rs | 9 +- vortex-functions/src/zip/mod.rs | 35 +- vortex-jni/src/expression.rs | 2 +- vortex-json/Cargo.toml | 1 + vortex-json/src/json_to_variant.rs | 7 +- vortex-layout/benches/zone_map_prune.rs | 16 +- vortex-layout/examples/reverse_index/main.rs | 8 +- .../examples/reverse_index/pruning_tests.rs | 10 +- vortex-layout/examples/reverse_index/tests.rs | 8 +- vortex-layout/src/layouts/chunked/reader.rs | 4 +- vortex-layout/src/layouts/dict/reader.rs | 30 +- vortex-layout/src/layouts/dict/writer.rs | 2 +- vortex-layout/src/layouts/flat/reader.rs | 9 +- vortex-layout/src/layouts/flat/writer.rs | 2 +- vortex-layout/src/layouts/indexed/index.rs | 2 +- vortex-layout/src/layouts/indexed/reader.rs | 2 +- vortex-layout/src/layouts/indexed/tests.rs | 24 +- vortex-layout/src/layouts/list/expr.rs | 4 +- vortex-layout/src/layouts/list/reader.rs | 17 +- vortex-layout/src/layouts/partitioned.rs | 3 +- vortex-layout/src/layouts/row_idx/mod.rs | 20 +- vortex-layout/src/layouts/struct_/reader.rs | 50 +- .../aggregates/bloom_filter/scalar_fn.rs | 12 +- vortex-layout/src/layouts/zoned/pruning.rs | 5 +- vortex-layout/src/layouts/zoned/reader.rs | 10 +- vortex-layout/src/layouts/zoned/schema.rs | 10 +- vortex-layout/src/layouts/zoned/zone_map.rs | 39 +- vortex-layout/src/reader.rs | 2 +- vortex-layout/src/scan/layout.rs | 4 +- vortex-layout/src/scan/multi.rs | 8 +- vortex-layout/src/scan/repeated_scan.rs | 2 +- vortex-layout/src/scan/scan_builder.rs | 24 +- vortex-layout/src/scan/split_by.rs | 2 +- vortex-layout/src/scan/test.rs | 2 +- vortex-layout/src/scan/v2/mod.rs | 2 +- vortex-layout/src/scan/v2/tests.rs | 20 +- vortex-python/src/arrays/mod.rs | 1 + vortex-python/src/expr/mod.rs | 2 +- vortex-scan/README.md | 6 +- vortex-scan/src/execution/cache.rs | 14 +- vortex-scan/src/execution/filter.rs | 2 +- vortex-scan/src/execution/pipeline/tests.rs | 8 +- vortex-scan/src/lib.rs | 4 +- vortex-scan/src/plan/builtins/eval/execute.rs | 3 +- vortex-scan/src/plan/builtins/eval/mod.rs | 8 +- vortex-scan/src/plan/builtins/pack/mod.rs | 18 +- vortex-scan/src/plan/builtins/row_idx/expr.rs | 12 +- vortex-scan/src/plan/builtins/row_idx/mod.rs | 4 +- vortex-scan/src/plan/builtins/take/mod.rs | 4 +- vortex-scan/src/plan/builtins/zoned/mod.rs | 8 +- vortex-scan/src/plan/tests.rs | 23 +- vortex-spatial/src/prune/contains.rs | 27 +- vortex-spatial/src/prune/distance.rs | 14 +- vortex-spatial/src/prune/intersects.rs | 10 +- vortex-spatial/src/prune/mod.rs | 18 +- vortex-test/compat-gen/src/adapter.rs | 2 +- vortex/src/editions/tests.rs | 6 +- vortex/src/lib.rs | 15 +- 195 files changed, 4416 insertions(+), 4276 deletions(-) delete mode 100644 vortex-array/src/expr/aliases.rs rename vortex-array/src/{expr => scalar_fn/builtin}/field.rs (100%) create mode 100644 vortex-array/src/scalar_fn/display.rs delete mode 100644 vortex-array/src/scalar_fn/internal/mod.rs rename {vortex-array/benches/expr => vortex-expr/benches}/large_struct_pack.rs (94%) rename vortex-array/benches/expr/optimize_bench.rs => vortex-expr/benches/optimize.rs (85%) rename vortex-array/src/expression.rs => vortex-expr/src/apply.rs (74%) rename {vortex-array/src/expr => vortex-expr/src}/arbitrary.rs (84%) create mode 100644 vortex-expr/src/bind.rs create mode 100644 vortex-expr/src/builtins.rs create mode 100644 vortex-expr/src/display.rs rename {vortex-array/src/expr => vortex-expr/src}/expression.rs (95%) rename {vortex-array/src/expr => vortex-expr/src}/exprs.rs (90%) rename {vortex-array/src/expr => vortex-expr/src}/lambda/mod.rs (91%) rename {vortex-array/src/scalar_fn/internal => vortex-expr/src/lambda}/row_count.rs (86%) rename {vortex-array/src/expr => vortex-expr/src}/lambda/tests.rs (91%) rename {vortex-array/src/scalar_fn => vortex-expr/src/proto}/foreign.rs (85%) rename vortex-array/src/expr/proto.rs => vortex-expr/src/proto/mod.rs (93%) create mode 100644 vortex-expr/src/scalar_fn_ext.rs rename {vortex-array/src/expr => vortex-expr/src}/scope.rs (74%) create mode 100644 vortex-expr/src/test_harness.rs create mode 100644 vortex-expr/src/tests/binary.rs create mode 100644 vortex-expr/src/tests/cast.rs create mode 100644 vortex-expr/src/tests/expression.rs create mode 100644 vortex-expr/src/tests/fill_null.rs create mode 100644 vortex-expr/src/tests/get_item.rs create mode 100644 vortex-expr/src/tests/is_not_null.rs create mode 100644 vortex-expr/src/tests/is_null.rs create mode 100644 vortex-expr/src/tests/literal.rs create mode 100644 vortex-expr/src/tests/mask.rs create mode 100644 vortex-expr/src/tests/mod.rs create mode 100644 vortex-expr/src/tests/not.rs create mode 100644 vortex-expr/src/tests/optimize.rs create mode 100644 vortex-expr/src/tests/pack.rs create mode 100644 vortex-expr/src/tests/select.rs rename {vortex-array/src/expr => vortex-expr/src}/traversal/fold.rs (93%) rename {vortex-array/src/expr => vortex-expr/src}/traversal/mod.rs (97%) rename {vortex-array/src/expr => vortex-expr/src}/traversal/visitor.rs (85%) diff --git a/AGENTS.md b/AGENTS.md index 003df93ec1f..f564de07b52 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,6 +22,8 @@ documentation in `docs/`, and benchmark tooling in `vortex-bench/` and `benchmar - `vortex-array` contains the core `Array` trait and the base encodings, including most Apache Arrow-style encodings. - `encodings/*` contains more specialized compressed encodings. +- `vortex-expr` contains expression trees, their optimizer, and the analyses and rewrites built on + them. `vortex-functions` contains the scalar functions that `vortex-array` itself is not built on. - `vortex-file` implements file IO. It uses `LayoutReader` from `vortex-layout`. - `vortex-io` holds the core async and blocking IO traits, plus the generic `object_store` adapters that implement them. diff --git a/docs/concepts/expressions.md b/docs/concepts/expressions.md index 81317a2b888..d6591ee80fb 100644 --- a/docs/concepts/expressions.md +++ b/docs/concepts/expressions.md @@ -9,8 +9,10 @@ to the returned data. Expressions are defined as abstract scalar functions. These vtables define the signature of the function, properties such as whether the function is strict, and the actual logic for executing the function over input arrays. -The built-in scalar functions can be found in the `vortex-array::expr` module, with additional use-case specific -functions provided by integration and plugin crates. +The scalar function framework and the handful of functions the array crate itself is built on live in +`vortex_array::scalar_fn`; expression trees, their optimizer and the analyses built on them live in the +`vortex-expr` crate; `vortex-functions` holds the rest of the built-in scalar functions, with additional +use-case specific functions provided by integration and plugin crates. In the future, we plan to add full support for spatial functions, date-time functions, and more. diff --git a/encodings/fsst/Cargo.toml b/encodings/fsst/Cargo.toml index 3d6313686dd..691842af042 100644 --- a/encodings/fsst/Cargo.toml +++ b/encodings/fsst/Cargo.toml @@ -22,6 +22,7 @@ rand = { workspace = true, optional = true } vortex-array = { workspace = true } vortex-buffer = { workspace = true } vortex-error = { workspace = true } +vortex-expr = { workspace = true } vortex-functions = { workspace = true } vortex-mask = { workspace = true } vortex-session = { workspace = true } @@ -36,6 +37,7 @@ rand = { workspace = true } rstest = { workspace = true } test-with = { workspace = true } vortex-array = { workspace = true, features = ["_test-harness"] } +vortex-expr = { workspace = true } [[bench]] name = "fsst_compress" diff --git a/encodings/fsst/benches/fsst_url_compare.rs b/encodings/fsst/benches/fsst_url_compare.rs index 388477ac0b0..26e272cccb2 100644 --- a/encodings/fsst/benches/fsst_url_compare.rs +++ b/encodings/fsst/benches/fsst_url_compare.rs @@ -16,14 +16,15 @@ use vortex_array::arrays::VarBinArray; use vortex_array::arrays::VarBinViewArray; use vortex_array::arrays::varbin::VarBinArrayExt; use vortex_array::builtins::ArrayBuiltins; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::memory::MemorySession; use vortex_array::optimizer::kernels::KernelSession; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_array::session::ArraySession; use vortex_array::test_harness::mark_optimized; +use vortex_expr::ArrayExprExt; +use vortex_expr::lit; +use vortex_expr::root; use vortex_fsst::fsst_compress; use vortex_fsst::fsst_train_compressor; use vortex_fsst::test_utils::HIGH_MATCH_DOMAIN; diff --git a/encodings/fsst/src/kernel.rs b/encodings/fsst/src/kernel.rs index 840594d7b17..c9184dc5eae 100644 --- a/encodings/fsst/src/kernel.rs +++ b/encodings/fsst/src/kernel.rs @@ -44,8 +44,9 @@ mod tests { use vortex_array::assert_arrays_eq; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; - use vortex_array::expr::root; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::root; use vortex_functions::expr::byte_length; use vortex_mask::Mask; use vortex_session::VortexSession; diff --git a/encodings/parquet-variant/Cargo.toml b/encodings/parquet-variant/Cargo.toml index ce8776645c7..acbe69151db 100644 --- a/encodings/parquet-variant/Cargo.toml +++ b/encodings/parquet-variant/Cargo.toml @@ -39,6 +39,7 @@ rstest = { workspace = true } tokio = { workspace = true, features = ["full"] } vortex-array = { workspace = true, features = ["_test-harness"] } vortex-arrow = { workspace = true, features = ["_test-harness-hegel"] } +vortex-expr = { workspace = true } vortex-file = { workspace = true, features = ["tokio"] } vortex-io = { workspace = true, features = ["tokio"] } vortex-layout = { workspace = true } diff --git a/encodings/parquet-variant/src/json_to_variant_tests.rs b/encodings/parquet-variant/src/json_to_variant_tests.rs index 16febc9d8ce..0399a46aa72 100644 --- a/encodings/parquet-variant/src/json_to_variant_tests.rs +++ b/encodings/parquet-variant/src/json_to_variant_tests.rs @@ -26,10 +26,11 @@ use vortex_array::assert_nth_scalar_is_null; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::root; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::ArrayExprExt; +use vortex_expr::root; use vortex_functions::expr::variant_get; use vortex_functions::variant_get::VariantPath; use vortex_json::Json; diff --git a/encodings/parquet-variant/src/kernel.rs b/encodings/parquet-variant/src/kernel.rs index a550161e878..2f157c9d084 100644 --- a/encodings/parquet-variant/src/kernel.rs +++ b/encodings/parquet-variant/src/kernel.rs @@ -326,7 +326,6 @@ mod tests { use vortex_array::dtype::DType as VortexDType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::validity::Validity; use vortex_arrow::ArrowSessionExt; @@ -334,6 +333,8 @@ mod tests { use vortex_error::vortex_bail; use vortex_error::vortex_ensure; use vortex_error::vortex_err; + use vortex_expr::ArrayExprExt; + use vortex_expr::root; use vortex_functions::expr::variant_get; use vortex_functions::variant_get::VariantPath; use vortex_functions::variant_get::VariantPathElement; diff --git a/encodings/sequence/Cargo.toml b/encodings/sequence/Cargo.toml index e8ce273483b..ce6ee9c1fb1 100644 --- a/encodings/sequence/Cargo.toml +++ b/encodings/sequence/Cargo.toml @@ -20,6 +20,7 @@ vortex-array = { workspace = true } vortex-buffer = { workspace = true } vortex-compute = { workspace = true } vortex-error = { workspace = true } +vortex-expr = { workspace = true } vortex-functions = { workspace = true } vortex-mask = { workspace = true } vortex-session = { workspace = true } diff --git a/encodings/sequence/src/compute/list_contains.rs b/encodings/sequence/src/compute/list_contains.rs index 926510981c1..881298a0008 100644 --- a/encodings/sequence/src/compute/list_contains.rs +++ b/encodings/sequence/src/compute/list_contains.rs @@ -97,11 +97,12 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType::I32; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::scalar::Scalar; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::lit; + use vortex_expr::root; use vortex_functions::expr::list_contains; use vortex_functions::expr::list_contains_opts; use vortex_functions::list_contains::ListContainsOptions; diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index ec54202f0ac..c38d028f3c5 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -48,6 +48,7 @@ vortex-array = { workspace = true, features = ["arbitrary", "_test-harness"] } vortex-btrblocks = { workspace = true } vortex-buffer = { workspace = true } vortex-error = { workspace = true } +vortex-expr = { workspace = true, features = ["arbitrary"] } vortex-fsst = { workspace = true, features = ["_test-harness"] } vortex-functions = { workspace = true } vortex-io = { workspace = true, features = ["wasm-bindgen"] } diff --git a/fuzz/fuzz_targets/file_io.rs b/fuzz/fuzz_targets/file_io.rs index 543ea76db0e..511755278a0 100644 --- a/fuzz/fuzz_targets/file_io.rs +++ b/fuzz/fuzz_targets/file_io.rs @@ -6,6 +6,8 @@ use itertools::Itertools; use libfuzzer_sys::Corpus; use libfuzzer_sys::fuzz_target; +use vortex::expr::lit; +use vortex::expr::root; use vortex_array::Canonical; use vortex_array::IntoArray; use vortex_array::VortexSessionExecute; @@ -15,10 +17,9 @@ use vortex_array::arrays::bool::BoolArrayExt; use vortex_array::assert_arrays_eq; use vortex_array::dtype::DType; use vortex_array::dtype::StructFields; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_btrblocks::BtrBlocksCompressorBuilder; use vortex_error::VortexExpect; +use vortex_expr::ArrayExprExt; use vortex_file::OpenOptionsSessionExt; use vortex_file::WriteOptionsSessionExt; use vortex_file::WriteStrategyBuilder; diff --git a/fuzz/src/file/mod.rs b/fuzz/src/file/mod.rs index f4fdc6d6ce1..f2254f99d7c 100644 --- a/fuzz/src/file/mod.rs +++ b/fuzz/src/file/mod.rs @@ -3,13 +3,13 @@ use arbitrary::Arbitrary; use arbitrary::Unstructured; +use vortex::expr::Expression; use vortex_array::ArrayRef; use vortex_array::arrays::arbitrary::ArbitraryArray; use vortex_array::arrays::arbitrary::ArbitraryArrayConfig; use vortex_array::arrays::arbitrary::ArbitraryWith; -use vortex_array::expr::Expression; -use vortex_array::expr::arbitrary::filter_expr; -use vortex_array::expr::arbitrary::projection_expr; +use vortex_expr::arbitrary::filter_expr; +use vortex_expr::arbitrary::projection_expr; use crate::FUZZ_FILE_ARRAY_MAX_LEN; use crate::array::CompressorStrategy; diff --git a/fuzz/src/fsst_like.rs b/fuzz/src/fsst_like.rs index 8ab863616a6..168c3d64615 100644 --- a/fuzz/src/fsst_like.rs +++ b/fuzz/src/fsst_like.rs @@ -9,6 +9,8 @@ use std::sync::LazyLock; use arbitrary::Arbitrary; use arbitrary::Unstructured; +use vortex::functions::like::Like; +use vortex::functions::like::LikeOptions; use vortex_array::ArrayRef; use vortex_array::Canonical; use vortex_array::IntoArray; @@ -23,8 +25,6 @@ use vortex_error::VortexResult; use vortex_fsst::FSSTArray; use vortex_fsst::fsst_compress; use vortex_fsst::fsst_train_compressor; -use vortex_functions::like::Like; -use vortex_functions::like::LikeOptions; use vortex_session::VortexSession; use crate::error::Backtrace; diff --git a/scripts/codspeed_bench_counts.json b/scripts/codspeed_bench_counts.json index 45e4be04a61..8204f263cad 100644 --- a/scripts/codspeed_bench_counts.json +++ b/scripts/codspeed_bench_counts.json @@ -30,8 +30,6 @@ "dict_compress": 31, "dict_mask": 16, "dict_unreferenced_mask": 10, - "expr_large_struct_pack": 4, - "expr_optimize": 1, "filter_bool": 51, "filter_fixed_width": 30, "filter_fsl": 4, @@ -107,6 +105,10 @@ "vortex-elias-fano": { "unary": 20 }, + "vortex-expr": { + "expr_large_struct_pack": 4, + "expr_optimize": 1 + }, "vortex-fastlanes": { "bitpack_blocked": 0, "bitpack_blocked_decompress": 0, diff --git a/scripts/codspeed_bench_durations.json b/scripts/codspeed_bench_durations.json index fb7b6ddb336..2fc9523a973 100644 --- a/scripts/codspeed_bench_durations.json +++ b/scripts/codspeed_bench_durations.json @@ -28,8 +28,6 @@ "dict_compress": 4.688, "dict_mask": 4.854, "dict_unreferenced_mask": 2.816, - "expr_large_struct_pack": 2.408, - "expr_optimize": 2.377, "filter_bool": 5.51, "filter_fixed_width": 3.863, "filter_fsl": 2.687, @@ -99,6 +97,10 @@ "vortex-elias-fano": { "unary": 3.413 }, + "vortex-expr": { + "expr_large_struct_pack": 2.408, + "expr_optimize": 2.377 + }, "vortex-fastlanes": { "bitpack_blocked": 0.367, "bitpack_blocked_decompress": 0.369, diff --git a/vortex-array/Cargo.toml b/vortex-array/Cargo.toml index 2cab252bb47..549c97b600a 100644 --- a/vortex-array/Cargo.toml +++ b/vortex-array/Cargo.toml @@ -195,16 +195,6 @@ harness = false name = "varbinview_compact" harness = false -[[bench]] -name = "expr_large_struct_pack" -path = "benches/expr/large_struct_pack.rs" -harness = false - -[[bench]] -name = "expr_optimize" -path = "benches/expr/optimize_bench.rs" -harness = false - [[bench]] name = "chunked_dict_builder" harness = false diff --git a/vortex-array/benches/dict_compare.rs b/vortex-array/benches/dict_compare.rs index a0233fcd4e0..cdffa2e602c 100644 --- a/vortex-array/benches/dict_compare.rs +++ b/vortex-array/benches/dict_compare.rs @@ -19,9 +19,6 @@ use vortex_array::arrays::dict_test::gen_varbin_words; use vortex_array::arrays::varbin::VarBinArrayExt; use vortex_array::builders::dict::dict_encode; use vortex_array::builtins::ArrayBuiltins; -use vortex_array::expr::eq; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::memory::MemorySession; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::optimizer::kernels::KernelSession; @@ -164,7 +161,10 @@ fn bench_compare_sliced_dict_primitive( .with_inputs(|| (&dict, SESSION.create_execution_ctx())) .bench_refs(|(dict, ctx)| { dict.clone() - .apply(&eq(root(), lit(value))) + .binary( + ConstantArray::new(value, codes_len).into_array(), + Operator::Eq, + ) .unwrap() .execute::(ctx) .unwrap() @@ -193,7 +193,10 @@ fn bench_compare_sliced_dict_varbinview( .with_inputs(|| (&dict, SESSION.create_execution_ctx())) .bench_refs(|(dict, ctx)| { dict.clone() - .apply(&eq(root(), lit(value))) + .binary( + ConstantArray::new(value, codes_len).into_array(), + Operator::Eq, + ) .unwrap() .execute::(ctx) .unwrap() diff --git a/vortex-array/src/array/erased.rs b/vortex-array/src/array/erased.rs index 320788b95cf..7b11815819f 100644 --- a/vortex-array/src/array/erased.rs +++ b/vortex-array/src/array/erased.rs @@ -544,7 +544,7 @@ impl ArrayRef { /// # Safety /// The caller must put back a slot with the same logical dtype and length before exposing the /// parent array, and must only use this for physical rewrites. - pub(crate) unsafe fn take_slot_unchecked( + pub unsafe fn take_slot_unchecked( mut self, slot_idx: usize, ) -> VortexResult<(ArrayRef, ArrayRef)> { @@ -578,7 +578,7 @@ impl ArrayRef { /// # Safety /// The replacement must have the same logical dtype and length as the taken slot, and this /// must only be used for physical rewrites. - pub(crate) unsafe fn put_slot_unchecked( + pub unsafe fn put_slot_unchecked( mut self, slot_idx: usize, replacement: ArrayRef, diff --git a/vortex-array/src/arrays/scalar_fn/rules.rs b/vortex-array/src/arrays/scalar_fn/rules.rs index 3520b1dd7ce..978f1d4efa0 100644 --- a/vortex-array/src/arrays/scalar_fn/rules.rs +++ b/vortex-array/src/arrays/scalar_fn/rules.rs @@ -167,10 +167,6 @@ mod tests { use crate::dtype::DType; use crate::dtype::Nullability; use crate::dtype::PType; - use crate::expr::cast; - use crate::expr::is_not_null; - use crate::expr::is_null; - use crate::expr::root; use crate::optimizer::rules::ArrayParentReduceRule; use crate::scalar_fn::TypedScalarFnInstance; use crate::scalar_fn::builtin::binary::Binary; @@ -234,10 +230,7 @@ mod tests { ConstantArray::new(Some(1u64), 0).into_array(), PrimitiveArray::from_iter(vec![2u64]) .into_array() - .apply(&cast( - root(), - DType::Primitive(PType::U64, Nullability::Nullable), - )) + .cast(DType::Primitive(PType::U64, Nullability::Nullable)) .vortex_expect("casted"), ], DType::Primitive(PType::U64, Nullability::Nullable), @@ -245,8 +238,7 @@ mod tests { .vortex_expect("construction") .into_array(); - let expr = is_null(root()); - let result = array.apply(&expr).vortex_expect("expr evaluation"); + let result = array.is_null().vortex_expect("is_null"); assert_arrays_eq!( result, BoolArray::from_iter([false]), @@ -259,22 +251,14 @@ mod tests { let ctx = &mut SESSION.create_execution_ctx(); let validity = BoolArray::from_iter([true, false, true]).into_array(); - assert_arrays_eq!( - validity.clone().apply(&is_null(root()))?, - ConstantArray::new(false, 3), - ctx - ); - assert_arrays_eq!( - validity.clone().apply(&is_not_null(root()))?, - ConstantArray::new(true, 3), - ctx - ); + assert_arrays_eq!(validity.is_null()?, ConstantArray::new(false, 3), ctx); + assert_arrays_eq!(validity.is_not_null()?, ConstantArray::new(true, 3), ctx); let buffer = buffer![1, 2, 3]; let nullable = PrimitiveArray::new(buffer, Validity::Array(validity.clone())).into_array(); - assert_arrays_eq!(nullable.clone().apply(&is_not_null(root()))?, validity, ctx); - assert_arrays_eq!(nullable.apply(&is_null(root()))?, validity.not()?, ctx); + assert_arrays_eq!(nullable.is_not_null()?, validity, ctx); + assert_arrays_eq!(nullable.is_null()?, validity.not()?, ctx); Ok(()) } diff --git a/vortex-array/src/builtins.rs b/vortex-array/src/builtins.rs index 0aacd73c88f..ad29a5d1eae 100644 --- a/vortex-array/src/builtins.rs +++ b/vortex-array/src/builtins.rs @@ -17,10 +17,7 @@ use crate::arrays::ConstantArray; use crate::arrays::InterleaveArray; use crate::dtype::DType; use crate::dtype::FieldName; -use crate::expr::Expression; use crate::scalar::Scalar; -use crate::scalar_fn::EmptyOptions; -use crate::scalar_fn::ScalarFnVTableExt; use crate::scalar_fn::builtin::binary::Binary; use crate::scalar_fn::builtin::cast::Cast; use crate::scalar_fn::builtin::fill_null::FillNull; @@ -32,70 +29,6 @@ use crate::scalar_fn::builtin::not::Not; use crate::scalar_fn::builtin::operators::Operator; /// A collection of built-in scalar functions that can be applied to expressions or arrays. -pub trait ExprBuiltins: Sized { - /// Cast to the given data type. - fn cast(&self, dtype: DType) -> VortexResult; - - /// Replace null values with the given fill value. - fn fill_null(&self, fill_value: Expression) -> VortexResult; - - /// Get item by field name (for struct types). - fn get_item(&self, field_name: impl Into) -> VortexResult; - - /// Is null check. - fn is_null(&self) -> VortexResult; - - /// Is not null check. - fn is_not_null(&self) -> VortexResult; - - /// Mask the expression using the given boolean mask. - /// The resulting expression's validity is the intersection of the original expression's - /// validity. - fn mask(&self, mask: Expression) -> VortexResult; - - /// Boolean negation. - fn not(&self) -> VortexResult; - - // TODO(joe): add an `interleave` expression builtin mirroring `ArrayBuiltins::interleave`. - - /// Apply a binary operator to this expression and another. - fn binary(&self, rhs: Expression, op: Operator) -> VortexResult; -} - -impl ExprBuiltins for Expression { - fn cast(&self, dtype: DType) -> VortexResult { - Cast.try_new_expr(dtype, [self.clone()]) - } - - fn fill_null(&self, fill_value: Expression) -> VortexResult { - FillNull.try_new_expr(EmptyOptions, [self.clone(), fill_value]) - } - - fn get_item(&self, field_name: impl Into) -> VortexResult { - GetItem.try_new_expr(field_name.into(), [self.clone()]) - } - - fn is_null(&self) -> VortexResult { - IsNull.try_new_expr(EmptyOptions, [self.clone()]) - } - - fn is_not_null(&self) -> VortexResult { - IsNotNull.try_new_expr(EmptyOptions, [self.clone()]) - } - - fn mask(&self, mask: Expression) -> VortexResult { - Mask.try_new_expr(EmptyOptions, [self.clone(), mask]) - } - - fn not(&self) -> VortexResult { - Not.try_new_expr(EmptyOptions, [self.clone()]) - } - - fn binary(&self, rhs: Expression, op: Operator) -> VortexResult { - Binary.try_new_expr(op, [self.clone(), rhs]) - } -} - pub trait ArrayBuiltins: Sized { /// Cast to the given data type. fn cast(&self, dtype: DType) -> VortexResult; diff --git a/vortex-array/src/expr/aliases.rs b/vortex-array/src/expr/aliases.rs deleted file mode 100644 index bf4694355d0..00000000000 --- a/vortex-array/src/expr/aliases.rs +++ /dev/null @@ -1,7 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: Copyright the Vortex contributors - -pub mod paste { - //! Re-export of [`paste`](https://docs.rs/paste/latest/paste/). - pub use paste::paste; -} diff --git a/vortex-array/src/expr/bound_expression.rs b/vortex-array/src/expr/bound_expression.rs index f6d6c2f78c0..b8f42bfb91a 100644 --- a/vortex-array/src/expr/bound_expression.rs +++ b/vortex-array/src/expr/bound_expression.rs @@ -16,12 +16,8 @@ use vortex_error::VortexResult; use vortex_error::vortex_ensure; use crate::dtype::DType; -use crate::expr::Expression; use crate::expr::display::DisplayTreeExpr; -use crate::expr::expression::unbound_variable; -use crate::expr::scope::Scope; -use crate::expr::traversal::TraversalOrder; -use crate::expr::traversal::pre_order_visit_down; +use crate::scalar::Scalar; use crate::scalar_fn::EmptyOptions; use crate::scalar_fn::ExpressionReduceNode; use crate::scalar_fn::ReduceNode; @@ -30,8 +26,10 @@ use crate::scalar_fn::ScalarFnRef; use crate::scalar_fn::ScalarFnVTable; use crate::scalar_fn::ScalarFnVTableExt; use crate::scalar_fn::builtin::is_not_null::IsNotNull; +use crate::scalar_fn::builtin::literal::Literal; +use crate::scalar_fn::display::ExprDisplay; -/// An [`Expression`] that has been type-checked against a [`Scope`]. +/// An expression that has been type-checked against a scope's root dtype. /// /// Every node carries its own dtype, so reading one is a field access rather than a walk of the /// subtree. Holding a `BoundExpression` is proof that the whole tree type-checked. @@ -379,15 +377,25 @@ impl BoundExpression { /// Return whether this expression tree contains a node using the given scalar-function vtable. pub fn contains(&self) -> VortexResult { - let mut contains = false; - pre_order_visit_down(self, |node| { - if node.is::() { - contains = true; - return Ok(TraversalOrder::Stop); + Ok(self.any_node(|node| node.is::())) + } + + /// Whether any node of this tree, visited depth-first, satisfies `predicate`. + fn any_node(&self, mut predicate: impl FnMut(&BoundExpression) -> bool) -> bool { + let mut stack = vec![self]; + while let Some(node) = stack.pop() { + if predicate(node) { + return true; } - Ok(TraversalOrder::Continue) - })?; - Ok(contains) + stack.extend(node.children().iter().rev()); + } + false + } + + /// Create a bound literal node. + pub fn literal(value: Scalar) -> Self { + Self::try_new(Literal.bind(value), []) + .vortex_expect("literal expressions are always well-typed") } /// Return the typed scalar-function options when this node uses the given vtable. @@ -414,16 +422,7 @@ impl BoundExpression { /// /// Expressions without a scope root, such as literals, match every dtype. pub fn is_root_bound_to(&self, dtype: &DType) -> bool { - let mut is_bound_to = true; - pre_order_visit_down(self, |node| { - if node.is_root() && node.dtype() != dtype { - is_bound_to = false; - return Ok(TraversalOrder::Stop); - } - Ok(TraversalOrder::Continue) - }) - .vortex_expect("bound expression traversal cannot not fail"); - is_bound_to + !self.any_node(|node| node.is_root() && node.dtype() != dtype) } /// Returns a new expression representing the validity mask output of this expression. @@ -442,7 +441,7 @@ impl BoundExpression { // IsNotNull(x) -> x.validity() symbolic reduction rule works only // when node's.validity is Reduced to avoid infinite recursion. ReduceNodeValidity::Irreducible => { - IsNotNull.try_new_bound_expr(EmptyOptions, [self.clone()])? + BoundExpression::try_new(IsNotNull.bind(EmptyOptions), [self.clone()])? } }) } @@ -464,37 +463,6 @@ impl Display for BoundExpression { } } -impl Expression { - /// Bind this expression against a root dtype, type-checking every node in a single walk. - /// - /// The returned tree carries a dtype on each node, so callers needing types at more than one - /// node should bind once and read fields rather than calling - /// [`return_dtype`](Expression::return_dtype) repeatedly. - pub fn bind(&self, dtype: &DType) -> VortexResult { - self.bind_scope(&Scope::new(dtype.clone())) - } - - /// Bind this expression against an explicit [`Scope`]. - pub fn bind_scope(&self, scope: &Scope) -> VortexResult { - if self.is_root() { - return Ok(BoundExpression::new_root(scope.root().clone())); - } - if let Expression::Variable(name) = self { - return Err(unbound_variable(name)); - } - - let children: Vec<_> = self - .children() - .iter() - .map(|child| child.bind_scope(scope)) - .try_collect()?; - let scalar_fn = self - .as_scalar() - .vortex_expect("leaves were handled above, so this is a scalar node"); - BoundExpression::try_new(scalar_fn.clone(), children) - } -} - /// Iterative drop to avoid stack overflows on deep trees. impl Drop for BoundExpression { fn drop(&mut self) { @@ -516,179 +484,12 @@ impl Drop for BoundExpression { } } -#[cfg(test)] -mod tests { - use vortex_error::VortexResult; - - use super::*; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::expr::col; - use crate::expr::eq; - use crate::expr::lit; - use crate::expr::root; - use crate::expr::test_harness::struct_dtype; - use crate::scalar_fn::builtin::literal::Literal; - - fn scope() -> Scope { - Scope::new(struct_dtype()) - } - - #[test] - fn root_binds_to_the_scope() -> VortexResult<()> { - let bound = root().bind_scope(&scope())?; - assert!(bound.is_root()); - assert_eq!(bound.dtype(), &struct_dtype()); - assert_eq!(bound, BoundExpression::new_root(struct_dtype())); - Ok(()) - } - - #[test] - fn every_node_carries_its_dtype() -> VortexResult<()> { - let expr = eq(col("a"), lit(1_i32)); - let bound = expr.bind_scope(&scope())?; - - assert_eq!(bound.dtype(), &DType::Bool(Nullability::NonNullable)); - - let lhs = &bound.children()[0]; - assert_eq!( - lhs.dtype(), - &DType::Primitive(PType::I32, Nullability::NonNullable) - ); - assert_eq!(lhs.children()[0].dtype(), &struct_dtype()); - Ok(()) - } - - #[test] - fn bind_agrees_with_return_dtype() -> VortexResult<()> { - for (expr, expected) in [ - (root(), struct_dtype()), - ( - col("a"), - DType::Primitive(PType::I32, Nullability::NonNullable), - ), - ( - eq(col("a"), lit(1_i32)), - DType::Bool(Nullability::NonNullable), - ), - (lit(true), DType::Bool(Nullability::NonNullable)), - ] { - assert_eq!( - expr.bind(&struct_dtype())?.dtype(), - &expected, - "binding {expr}" - ); - assert_eq!( - expr.return_dtype(&struct_dtype())?, - expected, - "return dtype of {expr}" - ); - } - Ok(()) - } - - #[test] - fn contains_scalar_function() -> VortexResult<()> { - let bound = eq(col("a"), lit(1_i32)).bind_scope(&scope())?; - assert!(bound.contains::()?); - assert!(!root().bind_scope(&scope())?.contains::()?); - Ok(()) - } - - #[test] - fn bound_to_checks_every_root() -> VortexResult<()> { - let dtype = struct_dtype(); - let bound = eq(col("a"), col("a")).bind(&dtype)?; - assert!(bound.is_root_bound_to(&dtype)); - assert!(!bound.is_root_bound_to(&DType::Bool(Nullability::NonNullable))); - assert!( - lit(true) - .bind(&dtype)? - .is_root_bound_to(&DType::Bool(Nullability::NonNullable)) - ); - Ok(()) - } - - #[test] - fn bound_display_matches_unbound() -> VortexResult<()> { - for expr in [root(), col("a"), eq(col("a"), lit(1_i32)), lit(true)] { - let bound = expr.bind_scope(&scope())?; - assert_eq!(bound.to_string(), expr.to_string()); - assert_eq!( - bound.display_tree().to_string(), - expr.display_tree().to_string() - ); - } - Ok(()) +impl ExprDisplay for BoundExpression { + fn display_child(&self, index: usize) -> &dyn ExprDisplay { + &self.children()[index] } - #[test] - fn clone_shares_children() -> VortexResult<()> { - let bound = eq(col("a"), lit(1_i32)).bind_scope(&scope())?; - let cloned = bound.clone(); - - let ( - BoundExpression::Scalar { children: a, .. }, - BoundExpression::Scalar { children: b, .. }, - ) = (&bound, &cloned) - else { - unreachable!("eq is a scalar node") - }; - assert!(Arc::ptr_eq(a, b)); - Ok(()) - } - - #[test] - fn repeated_subtree_is_bound_per_occurrence() -> VortexResult<()> { - let shared = col("a"); - let bound = eq(shared.clone(), shared).bind_scope(&scope())?; - let children = bound.children(); - assert_eq!(children.len(), 2); - for child in children { - assert_eq!( - child.dtype(), - &DType::Primitive(PType::I32, Nullability::NonNullable) - ); - } - Ok(()) - } - - #[test] - fn structural_and_exact_equality_are_distinct() -> VortexResult<()> { - let expr = eq(col("a"), lit(1_i32)); - let bound = expr.bind_scope(&scope())?; - let independently_bound = expr.bind_scope(&scope())?; - - assert_eq!(bound, independently_bound); - assert_eq!(ExactBoundExpr(bound.clone()), ExactBoundExpr(bound.clone())); - assert_ne!(ExactBoundExpr(bound), ExactBoundExpr(independently_bound)); - Ok(()) - } - - #[test] - fn weak_keys_match_only_their_live_expression() -> VortexResult<()> { - let expr = eq(col("a"), lit(1_i32)); - let bound = expr.bind_scope(&scope())?; - let independently_bound = expr.bind_scope(&scope())?; - let key = WeakBoundExprKey::new(&bound); - - assert!(key.is_live()); - assert!((&key as &dyn ExactKey) == (&bound as &dyn ExactKey)); - assert!((&key as &dyn ExactKey) != (&independently_bound as &dyn ExactKey)); - // A clone shares the children, so it is the same expression to the key. - let clone = bound.clone(); - assert_eq!(key, WeakBoundExprKey::new(&clone)); - - drop(bound); - assert!(key.is_live()); - drop(clone); - assert!(!key.is_live()); - Ok(()) - } - - #[test] - fn binding_reports_a_type_error() { - let expr = eq(col("a"), lit("nope")); - assert!(expr.bind_scope(&scope()).is_err()); + fn display_children_count(&self) -> usize { + self.children().len() } } diff --git a/vortex-array/src/expr/display.rs b/vortex-array/src/expr/display.rs index 74dcfd26a30..013b4d197ed 100644 --- a/vortex-array/src/expr/display.rs +++ b/vortex-array/src/expr/display.rs @@ -1,6 +1,8 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors +//! Tree rendering shared by bound and unbound expressions. + use std::fmt; use std::fmt::Display; use std::fmt::Formatter; @@ -9,78 +11,22 @@ use vortex_utils::tree::TreeDisplayAdapter; use vortex_utils::tree::write_branch_tree; use crate::expr::BoundExpression; -use crate::expr::Expression; use crate::scalar_fn::ChildName; -pub enum DisplayFormat { - Compact, - Tree, -} - -/// Read-only expression-tree interface used by scalar functions for SQL-style formatting. -/// -/// Both [`Expression`] and [`BoundExpression`] implement this interface, allowing scalar -/// functions to format either representation without converting between them. -pub trait ExprDisplay: Display { - /// Return the child at `index`. - fn display_child(&self, index: usize) -> &dyn ExprDisplay; - - /// Return the number of children in this node. - fn display_children_count(&self) -> usize; -} - -impl ExprDisplay for Expression { - fn display_child(&self, index: usize) -> &dyn ExprDisplay { - Expression::child(self, index) - } - - fn display_children_count(&self) -> usize { - self.children().len() - } -} - -impl ExprDisplay for BoundExpression { - fn display_child(&self, index: usize) -> &dyn ExprDisplay { - &self.children()[index] - } - - fn display_children_count(&self) -> usize { - self.children().len() - } -} - -trait DisplayTreeNode: Sized { +/// A node of an expression tree that [`DisplayTreeExpr`] can render. +pub trait DisplayTreeNode: Sized { + /// The children of this node, in argument order. fn tree_children(&self) -> &[Self]; + /// The display name of the child at `index`. fn tree_child_name(&self, index: usize) -> ChildName; + /// Write this node's own label. fn fmt_tree_node(&self, f: &mut Formatter<'_>) -> fmt::Result; } /// Tree-display label for the scope root. -const ROOT_DISPLAY: &str = "vortex.root()"; - -impl DisplayTreeNode for Expression { - fn tree_children(&self) -> &[Self] { - Expression::children(self) - } - - fn tree_child_name(&self, index: usize) -> ChildName { - match self { - Expression::Scalar { scalar_fn, .. } => scalar_fn.signature().child_name(index), - Expression::Root => unreachable!("the scope root has no children"), - Expression::Variable(_) => unreachable!("a variable has no children"), - } - } - - fn fmt_tree_node(&self, f: &mut Formatter<'_>) -> fmt::Result { - match self { - Expression::Scalar { scalar_fn, .. } => Display::fmt(scalar_fn, f), - Expression::Root => write!(f, "{ROOT_DISPLAY}"), - Expression::Variable(name) => write!(f, "@{name}"), - } - } -} +pub const ROOT_DISPLAY: &str = "vortex.root()"; impl DisplayTreeNode for BoundExpression { fn tree_children(&self) -> &[Self] { @@ -102,7 +48,8 @@ impl DisplayTreeNode for BoundExpression { } } -pub struct DisplayTreeExpr<'a, T: ?Sized = Expression>(pub &'a T); +/// Renders an expression tree with one node per line. +pub struct DisplayTreeExpr<'a, T: ?Sized>(pub &'a T); impl TreeDisplayAdapter for DisplayTreeExpr<'_, T> { type Context = (); @@ -136,101 +83,3 @@ impl Display for DisplayTreeExpr<'_, T> { write_branch_tree(self, self.0, &mut (), f) } } - -#[cfg(test)] -mod tests { - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::expr::and; - use crate::expr::cast; - use crate::expr::eq; - use crate::expr::get_item; - use crate::expr::gt; - use crate::expr::lit; - use crate::expr::not; - use crate::expr::root; - - #[test] - fn test_display_tree_root() { - use insta::assert_snapshot; - let root_expr = root(); - assert_snapshot!(root_expr.display_tree().to_string(), @"vortex.root()"); - } - - #[test] - fn test_display_tree_literal() { - use insta::assert_snapshot; - let lit_expr = lit(42); - assert_snapshot!(lit_expr.display_tree().to_string(), @"vortex.literal(42i32)"); - } - - #[test] - fn test_display_tree_get_item() { - use insta::assert_snapshot; - let get_item_expr = get_item("my_field", root()); - assert_snapshot!(get_item_expr.display_tree().to_string(), @r" - vortex.get_item(my_field) - └── input: vortex.root() - "); - } - - #[test] - fn test_display_tree_binary() { - use insta::assert_snapshot; - let binary_expr = gt(get_item("x", root()), lit(10)); - assert_snapshot!(binary_expr.display_tree().to_string(), @r" - vortex.binary(>) - ├── lhs: vortex.get_item(x) - │ └── input: vortex.root() - └── rhs: vortex.literal(10i32) - "); - } - - #[test] - fn test_display_tree_complex_binary() { - use insta::assert_snapshot; - let complex_binary = and( - eq(get_item("name", root()), lit("alice")), - gt(get_item("age", root()), lit(18)), - ); - assert_snapshot!(complex_binary.display_tree().to_string(), @r#" - vortex.binary(and) - ├── lhs: vortex.binary(=) - │ ├── lhs: vortex.get_item(name) - │ │ └── input: vortex.root() - │ └── rhs: vortex.literal("alice") - └── rhs: vortex.binary(>) - ├── lhs: vortex.get_item(age) - │ └── input: vortex.root() - └── rhs: vortex.literal(18i32) - "#); - } - - #[test] - fn test_display_tree_cast() { - use insta::assert_snapshot; - let cast_expr = cast( - get_item("value", root()), - DType::Primitive(PType::I64, Nullability::NonNullable), - ); - assert_snapshot!(cast_expr.display_tree().to_string(), @r" - vortex.cast(i64) - └── input: vortex.get_item(value) - └── input: vortex.root() - "); - } - - #[test] - fn test_display_tree_not() { - use insta::assert_snapshot; - let not_expr = not(eq(get_item("active", root()), lit(true))); - assert_snapshot!(not_expr.display_tree().to_string(), @r" - vortex.not() - └── input: vortex.binary(=) - ├── lhs: vortex.get_item(active) - │ └── input: vortex.root() - └── rhs: vortex.literal(true) - "); - } -} diff --git a/vortex-array/src/expr/mod.rs b/vortex-array/src/expr/mod.rs index bf8b427003f..a9c7e0de407 100644 --- a/vortex-array/src/expr/mod.rs +++ b/vortex-array/src/expr/mod.rs @@ -1,130 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors -//! Vortex's expression language: scalar operations over [arrays](crate::ArrayRef). +//! Bound expression trees and their optimizer, and the statistics vocabulary arrays expose. //! -//! An [`Expression`] is a tree of scalar operations rooted at a scope (see [`root`]). Expressions -//! are the common currency of scans: a scan takes a *filter* expression that resolves to a boolean -//! and a *projection* expression that shapes the output. All expressions are serializable and own -//! their own wire format, so they can be pushed down to remote sources and reconstructed on workers. -//! -//! # Scalar functions -//! -//! Each node references a scalar function defined by a -//! [`ScalarFnVTable`](crate::scalar_fn::ScalarFnVTable). The vtable declares the function signature, -//! properties such as strictness, and the logic that executes it over input arrays. Built-in -//! functions live in [`crate::scalar_fn`]; integration and plugin crates supply additional, -//! use-case-specific functions. -//! -//! # Deferred execution -//! -//! Applying an expression to an array does not compute the result eagerly. Instead it builds a -//! [`ScalarFnArray`](crate::arrays::ScalarFnArray) representing the deferred application, letting -//! downstream encodings push the computation into compressed data, or fuse several expressions -//! together, before any data is materialized. The deferred tree is executed toward canonical form -//! only when a result is actually required. -//! -//! # Type checking -//! -//! Expressions are strictly typed: an input array's dtype must match the function signature exactly, -//! so callers perform any required casts themselves before building the expression. The one -//! relaxation is nullability—for example, equality may compare a `u32` against a `u32?`, but never -//! a `u32` against an `i32`. -//! -//! Scans decompose bound filter expressions into independent conjuncts so that they can evaluate -//! and reorder the most selective predicates first. -//! -//! The implementation takes inspiration from [Postgres] and [Apache Datafusion]. -//! -//! [Postgres]: https://www.postgresql.org/docs/current/sql-expressions.html -//! [Apache Datafusion]: https://github.com/apache/datafusion/tree/5fac581efbaffd0e6a9edf931182517524526afd/datafusion/expr +//! [`BoundExpression`] lives here because the scalar function vtable reduces over it: keeping it +//! next to [`ScalarFnVTable`](crate::scalar_fn::ScalarFnVTable) lets every reduction rule be +//! monomorphized for expression trees as well as array trees. Unbound expressions, binding, +//! constructors and the analyses built on them live in the `vortex-expr` crate. -pub mod aliases; -#[cfg(feature = "arbitrary")] -pub mod arbitrary; -pub mod bound_expression; +mod bound_expression; pub mod display; -pub(crate) mod expression; -mod exprs; -pub(crate) mod field; -pub mod lambda; mod optimize; -pub mod proto; -pub mod scope; pub mod stats; -pub mod traversal; pub use bound_expression::*; -pub use expression::*; -pub use exprs::and; -pub use exprs::and_collect; -pub use exprs::binary; -pub use exprs::bound; -pub use exprs::bound_select; -pub use exprs::bound_select_exclude; -pub use exprs::cast; -pub use exprs::checked_add; -pub use exprs::col; -pub use exprs::eq; -pub use exprs::fill_null; -pub use exprs::get_item; -pub use exprs::gt; -pub use exprs::gt_eq; -pub use exprs::is_not_null; -pub use exprs::is_null; -pub use exprs::is_root; -pub use exprs::lit; -pub use exprs::lt; -pub use exprs::lt_eq; -pub use exprs::mask; -pub use exprs::not; -pub use exprs::not_eq; -pub use exprs::or; -pub use exprs::or_collect; -pub use exprs::pack; -pub use exprs::root; -pub use exprs::select; -pub use exprs::select_exclude; -pub use exprs::var; -pub use lambda::*; -pub use scope::*; - -#[cfg(feature = "_test-harness")] -pub mod test_harness { - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::dtype::StructFields; - - pub fn struct_dtype() -> DType { - DType::Struct( - StructFields::new( - ["a", "col1", "col2", "bool1", "bool2"].into(), - vec![ - DType::Primitive(PType::I32, Nullability::NonNullable), - DType::Primitive(PType::U16, Nullability::Nullable), - DType::Primitive(PType::U16, Nullability::Nullable), - DType::Bool(Nullability::NonNullable), - DType::Bool(Nullability::NonNullable), - ], - ), - Nullability::NonNullable, - ) - } -} - -#[cfg(test)] -mod tests { - use crate::expr::eq; - use crate::expr::lit; - use crate::expr::root; - use crate::scalar_fn::builtin::literal::Literal; - - #[test] - fn expr_contains() { - let expression = &eq(root(), lit(3u64)); - assert!(expression.contains::().unwrap()); - let expression = root(); - assert!(!expression.contains::().unwrap()); - } -} diff --git a/vortex-array/src/expr/optimize.rs b/vortex-array/src/expr/optimize.rs index a96b3e7af32..411a8bc01a0 100644 --- a/vortex-array/src/expr/optimize.rs +++ b/vortex-array/src/expr/optimize.rs @@ -10,7 +10,7 @@ impl BoundExpression { /// Optimize the root expression node only, iterating to convergence. /// /// This applies optimization rules repeatedly until no more changes occur: - /// 1. `simplify` - scalar-function simplifications over the bound node + /// 1. `simplify` - scalar-function simplifications over the node /// 2. `reduce` - abstract reduction rules via `ReduceNode` pub fn optimize(&self) -> VortexResult { Ok(self.try_optimize()?.unwrap_or_else(|| self.clone())) @@ -21,18 +21,19 @@ impl BoundExpression { /// Non-scalar nodes carry no rules, so they never simplify. fn simplify_node(&self) -> VortexResult> { match self { - BoundExpression::Scalar { scalar_fn, .. } => scalar_fn.simplify(self), + BoundExpression::Scalar { scalar_fn, .. } => Ok(scalar_fn + .simplify_expression(&ExpressionReduceNode::new(self))? + .map(ExpressionReduceNode::into_expression)), BoundExpression::Root { .. } => Ok(None), } } /// Apply this node's own abstract reduction rule, if it has one. - fn reduce_node<'a>( - &self, - node: &ExpressionReduceNode<'a>, - ) -> VortexResult>> { + fn reduce_node(&self) -> VortexResult> { match self { - BoundExpression::Scalar { scalar_fn, .. } => scalar_fn.reduce_expression(node), + BoundExpression::Scalar { scalar_fn, .. } => Ok(scalar_fn + .reduce_expression(&ExpressionReduceNode::new(self))? + .map(ExpressionReduceNode::into_expression)), BoundExpression::Root { .. } => Ok(None), } } @@ -60,15 +61,9 @@ impl BoundExpression { changed = true; } - // Try reduce via ReduceNode. The node borrows the expression, so constructing it is - // free; the block scopes the borrow so `current` can be updated. - let reduced = { - let expr = current.as_ref().unwrap_or(self); - expr.reduce_node(&ExpressionReduceNode::new(expr))? - .map(ExpressionReduceNode::into_expression) - }; - if let Some(reduced_expr) = reduced { - current = Some(reduced_expr); + let expr = current.as_ref().unwrap_or(self); + if let Some(reduced) = expr.reduce_node()? { + current = Some(reduced); changed = true; } @@ -79,7 +74,6 @@ impl BoundExpression { Ok(current) } - /// Optimize the entire expression tree recursively. /// /// Optimizes children first (bottom-up), then optimizes the root. @@ -119,113 +113,3 @@ impl BoundExpression { Ok(current) } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use rstest::rstest; - use vortex_buffer::buffer; - use vortex_error::VortexResult; - use vortex_error::vortex_err; - use vortex_session::VortexSession; - - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::BoolArray; - use crate::arrays::StructArray; - use crate::assert_arrays_eq; - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::dtype::StructFields; - use crate::expr::Expression; - use crate::expr::and; - use crate::expr::cast; - use crate::expr::eq; - use crate::expr::get_item; - use crate::expr::lit; - use crate::expr::lt_eq; - use crate::expr::or; - use crate::expr::root; - use crate::scalar::Scalar; - use crate::scalar_fn::builtin::literal::Literal; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - #[test] - fn optimize_or_chain_correctness() -> VortexResult<()> { - let expr = or( - eq(get_item("x", root()), lit(1i32)), - eq(get_item("x", root()), lit(2i32)), - ); - let scope = DType::Struct( - StructFields::new( - ["x"].into(), - vec![DType::Primitive(PType::I32, Nullability::NonNullable)], - ), - Nullability::NonNullable, - ); - let optimized = expr.bind(&scope)?.optimize_recursive()?; - - let data = - StructArray::from_fields(&[("x", buffer![0i32, 1, 2, 3].into_array())])?.into_array(); - let actual = data.apply_bound(&optimized)?; - assert_arrays_eq!( - actual, - BoolArray::from_iter([false, true, true, false]), - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - #[test] - fn optimize_folds_cast_of_literal_in_comparison() -> VortexResult<()> { - let expr = lt_eq( - get_item("x", root()), - cast( - lit(3i32), - DType::Primitive(PType::F64, Nullability::NonNullable), - ), - ); - let scope = DType::Struct( - StructFields::new( - ["x"].into(), - vec![DType::Primitive(PType::F64, Nullability::NonNullable)], - ), - Nullability::NonNullable, - ); - let optimized = expr.bind(&scope)?.optimize_recursive()?; - - // Prune rules pattern-match a bare Literal on the comparison RHS; a cast wrapper - // silently disables pruning. - let rhs = optimized - .child(1) - .as_opt::() - .ok_or_else(|| vortex_err!("expected a bare literal RHS, got {optimized}"))?; - assert_eq!(rhs, &Scalar::primitive(3.0f64, Nullability::NonNullable)); - Ok(()) - } - - #[rstest] - #[case::and_annihilator(and(root(), lit(false)), DType::Bool(Nullability::Nullable))] - #[case::or_annihilator(or(root(), lit(true)), DType::Bool(Nullability::Nullable))] - #[case::and_nullable_identity( - and(root(), lit(Scalar::from(Some(true)))), - DType::Bool(Nullability::NonNullable) - )] - #[case::or_nullable_identity( - or(root(), lit(Scalar::from(Some(false)))), - DType::Bool(Nullability::NonNullable) - )] - fn literal_simplification_preserves_result_dtype( - #[case] expr: Expression, - #[case] scope: DType, - ) -> VortexResult<()> { - let original = expr.bind(&scope)?; - let optimized = expr.bind(&scope)?.optimize_recursive()?; - assert_eq!(optimized.dtype(), original.dtype()); - - Ok(()) - } -} diff --git a/vortex-array/src/lib.rs b/vortex-array/src/lib.rs index 331208c2e66..9f8046f35b4 100644 --- a/vortex-array/src/lib.rs +++ b/vortex-array/src/lib.rs @@ -115,7 +115,6 @@ pub mod display; pub mod dtype; mod executor; pub mod expr; -mod expression; pub mod extension; pub mod flatbuffers; mod hash; diff --git a/vortex-array/src/scalar_fn/builtin/binary/mod.rs b/vortex-array/src/scalar_fn/builtin/binary/mod.rs index fb51375253d..571c80a4d83 100644 --- a/vortex-array/src/scalar_fn/builtin/binary/mod.rs +++ b/vortex-array/src/scalar_fn/builtin/binary/mod.rs @@ -27,9 +27,6 @@ use crate::arrays::Constant; use crate::arrays::ScalarFnArray; use crate::dtype::DType; use crate::dtype::Nullability; -use crate::expr::BoundExpression; -use crate::expr::bound; -use crate::expr::display::ExprDisplay; use crate::matcher::Matcher; use crate::proto::expr as pb; use crate::require_input; @@ -41,9 +38,10 @@ use crate::scalar_fn::ScalarFnExecutionResult; use crate::scalar_fn::ScalarFnId; use crate::scalar_fn::ScalarFnVTable; use crate::scalar_fn::ScalarFnVTableExt; -use crate::scalar_fn::builtin::literal::Literal; +use crate::scalar_fn::builtin::cast::Cast; use crate::scalar_fn::builtin::operators::CompareOperator; use crate::scalar_fn::builtin::operators::Operator; +use crate::scalar_fn::display::ExprDisplay; pub mod boolean; pub use boolean::BooleanExecuteAdaptor; @@ -202,30 +200,25 @@ impl ScalarFnVTable for Binary { } } - fn simplify( - &self, - operator: &Operator, - expr: &BoundExpression, - ) -> VortexResult> { - let lhs = expr.child(0); - let rhs = expr.child(1); + fn simplify(&self, operator: &Operator, node: &T) -> VortexResult> { + let lhs = node.child(0); + let rhs = node.child(1); - let is_literal_null = - |expr: &BoundExpression| expr.as_opt::().is_some_and(Scalar::is_null); + let is_constant_null = |node: &T| node.as_constant().is_some_and(|s| s.is_null()); // Binding already rejected ill-typed comparisons such as `int_col = null_utf8`, so a - // comparison against a null literal always evaluates to null. - if operator.is_comparison() && (is_literal_null(lhs) || is_literal_null(rhs)) { - return Ok(Some(bound::lit(Scalar::null(DType::Bool( - Nullability::Nullable, - ))))); + // comparison against a null constant always evaluates to null. + if operator.is_comparison() && (is_constant_null(&lhs) || is_constant_null(&rhs)) { + return Ok(Some( + node.new_constant(Scalar::null(DType::Bool(Nullability::Nullable))), + )); } - let bool_literal = |expr: &BoundExpression| { - expr.as_opt::()? - .as_bool_opt() - .map(|value| value.value()) - }; + if !matches!(operator, Operator::And | Operator::Or) { + return Ok(None); + } + + let bool_constant = |node: &T| node.as_constant()?.as_bool_opt().map(|value| value.value()); // AND/OR use Kleene three-valued logic. `None` below is a boolean null. // @@ -240,34 +233,37 @@ impl ScalarFnVTable for Binary { // - null OR null => null // // Other null cases either fall out of the identity/annihilator rules - // above (`null AND true`, `null OR false`) or cannot be simplified under - // Kleene semantics (`null AND x`, `null OR x` for non-literal `x`). - // Simplification must keep the result nullability binding inferred: the annihilator - // literal and the identity child can both be less nullable than `lhs OP rhs`. - let result_nullable = lhs.dtype().is_nullable() || rhs.dtype().is_nullable(); + // above (`null AND true`, `null OR false`) or cannot be reduced under + // Kleene semantics (`null AND x`, `null OR x` for non-constant `x`). + // Reduction must keep the result nullability binding inferred: the annihilator + // constant and the identity child can both be less nullable than `lhs OP rhs`. + let result_nullable = lhs.node_dtype()?.is_nullable() || rhs.node_dtype()?.is_nullable(); let nullability = Nullability::from(result_nullable); - let bool_lit = |value: bool| bound::lit(Scalar::bool(value, nullability)); - let widen = |child: &BoundExpression| { - if child.dtype().is_nullable() == result_nullable { - child.clone() + let bool_constant_node = |value: bool| node.new_constant(Scalar::bool(value, nullability)); + let widen = |child: T| -> VortexResult { + if child.node_dtype()?.is_nullable() == result_nullable { + Ok(child) } else { - bound::cast(child.clone(), DType::Bool(nullability)) + child.new_node( + Cast.bind(DType::Bool(nullability)), + std::slice::from_ref(&child), + ) } }; Ok(match operator { - Operator::And => match (bool_literal(lhs), bool_literal(rhs)) { - (Some(Some(false)), _) | (_, Some(Some(false))) => Some(bool_lit(false)), - (Some(Some(true)), _) => Some(widen(rhs)), - (_, Some(Some(true))) => Some(widen(lhs)), - (Some(None), Some(None)) => Some(widen(lhs)), + Operator::And => match (bool_constant(&lhs), bool_constant(&rhs)) { + (Some(Some(false)), _) | (_, Some(Some(false))) => Some(bool_constant_node(false)), + (Some(Some(true)), _) => Some(widen(rhs)?), + (_, Some(Some(true))) => Some(widen(lhs)?), + (Some(None), Some(None)) => Some(widen(lhs)?), _ => None, }, - Operator::Or => match (bool_literal(lhs), bool_literal(rhs)) { - (Some(Some(true)), _) | (_, Some(Some(true))) => Some(bool_lit(true)), - (Some(Some(false)), _) => Some(widen(rhs)), - (_, Some(Some(false))) => Some(widen(lhs)), - (Some(None), Some(None)) => Some(widen(lhs)), + Operator::Or => match (bool_constant(&lhs), bool_constant(&rhs)) { + (Some(Some(true)), _) | (_, Some(Some(true))) => Some(bool_constant_node(true)), + (Some(Some(false)), _) => Some(widen(rhs)?), + (_, Some(Some(false))) => Some(widen(lhs)?), + (Some(None), Some(None)) => Some(widen(lhs)?), _ => None, }, _ => None, @@ -370,466 +366,3 @@ impl ScalarFnVTable for Binary { ) } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use vortex_error::VortexExpect; - use vortex_error::VortexResult; - use vortex_session::VortexSession; - - use super::*; - use crate::Canonical; - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::Bool; - use crate::arrays::BoolArray; - use crate::arrays::Constant; - use crate::arrays::ConstantArray; - use crate::arrays::PrimitiveArray; - use crate::arrays::ScalarFn; - use crate::arrays::scalar_fn::ExactScalarFn; - use crate::arrays::scalar_fn::ScalarFnArrayExt; - use crate::assert_arrays_eq; - use crate::builtins::ArrayBuiltins; - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::expr::Expression; - use crate::expr::and; - use crate::expr::and_collect; - use crate::expr::col; - use crate::expr::eq; - use crate::expr::gt; - use crate::expr::gt_eq; - use crate::expr::lit; - use crate::expr::lt; - use crate::expr::lt_eq; - use crate::expr::not_eq; - use crate::expr::or; - use crate::expr::or_collect; - use crate::expr::test_harness; - use crate::optimizer::ArrayOptimizer; - use crate::scalar::Scalar; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - #[test] - fn and_collect_balanced() { - let values = vec![lit(1), lit(2), lit(3), lit(4), lit(5)]; - - insta::assert_snapshot!(and_collect(values.into_iter()).unwrap().display_tree(), @r" - vortex.binary(and) - ├── lhs: vortex.binary(and) - │ ├── lhs: vortex.literal(1i32) - │ └── rhs: vortex.literal(2i32) - └── rhs: vortex.binary(and) - ├── lhs: vortex.binary(and) - │ ├── lhs: vortex.literal(3i32) - │ └── rhs: vortex.literal(4i32) - └── rhs: vortex.literal(5i32) - "); - - // 4 elements: and(and(1, 2), and(3, 4)) - perfectly balanced - let values = vec![lit(1), lit(2), lit(3), lit(4)]; - insta::assert_snapshot!(and_collect(values.into_iter()).unwrap().display_tree(), @r" - vortex.binary(and) - ├── lhs: vortex.binary(and) - │ ├── lhs: vortex.literal(1i32) - │ └── rhs: vortex.literal(2i32) - └── rhs: vortex.binary(and) - ├── lhs: vortex.literal(3i32) - └── rhs: vortex.literal(4i32) - "); - - // 1 element: just the element - let values = vec![lit(1)]; - insta::assert_snapshot!(and_collect(values.into_iter()).unwrap().display_tree(), @"vortex.literal(1i32)"); - - // 0 elements: None - let values: Vec = vec![]; - assert!(and_collect(values.into_iter()).is_none()); - } - - #[test] - fn or_collect_balanced() { - // 4 elements: or(or(1, 2), or(3, 4)) - perfectly balanced - let values = vec![lit(1), lit(2), lit(3), lit(4)]; - insta::assert_snapshot!(or_collect(values.into_iter()).unwrap().display_tree(), @r" - vortex.binary(or) - ├── lhs: vortex.binary(or) - │ ├── lhs: vortex.literal(1i32) - │ └── rhs: vortex.literal(2i32) - └── rhs: vortex.binary(or) - ├── lhs: vortex.literal(3i32) - └── rhs: vortex.literal(4i32) - "); - } - - #[test] - fn dtype() { - let dtype = test_harness::struct_dtype(); - let bool1: Expression = col("bool1"); - let bool2: Expression = col("bool2"); - assert_eq!( - and(bool1.clone(), bool2.clone()) - .return_dtype(&dtype) - .unwrap(), - DType::Bool(Nullability::NonNullable) - ); - assert_eq!( - or(bool1, bool2).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::NonNullable) - ); - - let col1: Expression = col("col1"); - let col2: Expression = col("col2"); - - assert_eq!( - eq(col1.clone(), col2.clone()).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::Nullable) - ); - assert_eq!( - not_eq(col1.clone(), col2.clone()) - .return_dtype(&dtype) - .unwrap(), - DType::Bool(Nullability::Nullable) - ); - assert_eq!( - gt(col1.clone(), col2.clone()).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::Nullable) - ); - assert_eq!( - gt_eq(col1.clone(), col2.clone()) - .return_dtype(&dtype) - .unwrap(), - DType::Bool(Nullability::Nullable) - ); - assert_eq!( - lt(col1.clone(), col2.clone()).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::Nullable) - ); - assert_eq!( - lt_eq(col1.clone(), col2.clone()) - .return_dtype(&dtype) - .unwrap(), - DType::Bool(Nullability::Nullable) - ); - - assert_eq!( - or(lt(col1.clone(), col2.clone()), not_eq(col1, col2)) - .return_dtype(&dtype) - .unwrap(), - DType::Bool(Nullability::Nullable) - ); - } - - #[test] - fn comparison_with_typed_null_simplifies_after_type_check() -> VortexResult<()> { - let dtype = test_harness::struct_dtype(); - - let expr = eq( - col("col1"), - lit(Scalar::null(DType::Primitive( - PType::U16, - Nullability::Nullable, - ))), - ); - - assert_eq!( - expr.bind(&dtype)?.optimize_recursive()?, - lit(Scalar::null(DType::Bool(Nullability::Nullable))).bind(&dtype)? - ); - Ok(()) - } - - #[test] - fn comparison_with_incompatible_null_still_type_checks() { - let dtype = test_harness::struct_dtype(); - let expr = eq( - col("col1"), - lit(Scalar::null(DType::Utf8(Nullability::Nullable))), - ); - - assert!(expr.bind(&dtype).is_err()); - } - - #[test] - fn test_display_print() { - let expr = gt(lit(1), lit(2)); - assert_eq!(format!("{expr}"), "(1i32 > 2i32)"); - } - - /// Regression test for GitHub issue #5947: struct comparison in filter expressions should work - /// using `make_comparator` instead of Arrow's `cmp` functions which don't support nested types. - #[test] - fn test_struct_comparison() { - use crate::IntoArray; - use crate::arrays::StructArray; - - // Create a struct array with one element for testing. - let lhs_struct = StructArray::from_fields(&[ - ("a", PrimitiveArray::from_iter([1i32]).into_array()), - ("b", PrimitiveArray::from_iter([3i32]).into_array()), - ]) - .unwrap() - .into_array(); - - let rhs_struct_equal = StructArray::from_fields(&[ - ("a", PrimitiveArray::from_iter([1i32]).into_array()), - ("b", PrimitiveArray::from_iter([3i32]).into_array()), - ]) - .unwrap() - .into_array(); - - let rhs_struct_different = StructArray::from_fields(&[ - ("a", PrimitiveArray::from_iter([1i32]).into_array()), - ("b", PrimitiveArray::from_iter([4i32]).into_array()), - ]) - .unwrap() - .into_array(); - - // Test using binary method directly - let result_equal = lhs_struct.binary(rhs_struct_equal, Operator::Eq).unwrap(); - assert_eq!( - result_equal - .execute_scalar(0, &mut SESSION.create_execution_ctx()) - .vortex_expect("value"), - Scalar::bool(true, Nullability::NonNullable), - "Equal structs should be equal" - ); - - let result_different = lhs_struct - .binary(rhs_struct_different, Operator::Eq) - .unwrap(); - assert_eq!( - result_different - .execute_scalar(0, &mut SESSION.create_execution_ctx()) - .vortex_expect("value"), - Scalar::bool(false, Nullability::NonNullable), - "Different structs should not be equal" - ); - } - - #[test] - fn test_or_kleene_validity() { - let mut ctx = SESSION.create_execution_ctx(); - use crate::arrays::StructArray; - use crate::expr::col; - - let struct_arr = StructArray::from_fields(&[ - ("a", BoolArray::from_iter([Some(true)]).into_array()), - ( - "b", - BoolArray::from_iter([Option::::None]).into_array(), - ), - ]) - .unwrap() - .into_array(); - - let expr = or(col("a"), col("b")); - let result = struct_arr.apply(&expr).unwrap(); - - assert_arrays_eq!( - result, - BoolArray::from_iter([Some(true)]).into_array(), - &mut ctx - ) - } - - #[test] - fn test_and_reduce() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let left = BoolArray::from_iter([true, true, false]).into_array(); - let right = ConstantArray::new(true, 3).into_array(); - - let array = Binary::try_new(left.clone(), right.clone(), Operator::And)? - .into_array() - .optimize_ctx(&SESSION)?; // calls reduce() - assert!(array.is::()); // and(left, const) -> left - assert_arrays_eq!(array, left, &mut ctx); - - let array = Binary::try_new(right.clone(), left.clone(), Operator::And)? - .into_array() - .optimize_ctx(&SESSION)?; - assert!(array.is::()); // and(const, left) -> left - assert_arrays_eq!(array, left, &mut ctx); - - let array = Binary::try_new(left.clone(), right.clone(), Operator::Or)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_eq!(array.as_constant(), Some(&true.into())); // or(left, const) -> true - - let array = Binary::try_new(right.clone(), left, Operator::Or)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_eq!(array.as_constant(), Some(&true.into())); // or(const, left) -> true - - let array = Binary::try_new(right.clone(), right.clone(), Operator::Or)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_eq!(array.as_constant(), Some(&true.into())); // or(const, const) -> const - - let left_false = ConstantArray::new(false, 3).into_array(); - - let array = Binary::try_new(right.clone(), left_false.clone(), Operator::Or)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_eq!(array.as_constant(), Some(&true.into())); - let array = Binary::try_new(left_false, right, Operator::And)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_eq!(array.as_constant(), Some(&false.into())); - - Ok(()) - } - - #[test] - fn test_and_reduce_nullable() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let nullable_true = - ConstantArray::new(Scalar::bool(true, Nullability::Nullable), 3).into_array(); - let right = BoolArray::from_iter([true, false, true]).into_array(); - - let array = Binary::try_new(nullable_true, right, Operator::And)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_arrays_eq!( - array, - BoolArray::from_iter([Some(true), Some(false), Some(true)]), - &mut ctx - ); - Ok(()) - } - - /// Short-circuiting against a constant keeps the result's nullability: `x AND false` over a - /// nullable `x` is a nullable false, which a parent rebuilt around it requires. - #[test] - fn test_short_circuit_keeps_nullability() -> VortexResult<()> { - let nullable = BoolArray::from_iter([Some(true), None, Some(false)]).into_array(); - let non_nullable = BoolArray::from_iter([true, false, true]).into_array(); - let false_ = ConstantArray::new(false, 3).into_array(); - let nullable_false = - ConstantArray::new(Scalar::bool(false, Nullability::Nullable), 3).into_array(); - - let and_false = Binary::try_new(nullable, false_, Operator::And)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_eq!(and_false.dtype(), &DType::Bool(Nullability::Nullable)); - assert!(and_false.is::()); - - let or_false = Binary::try_new(non_nullable, nullable_false, Operator::Or)? - .into_array() - .optimize_ctx(&SESSION)?; - assert_eq!(or_false.dtype(), &DType::Bool(Nullability::Nullable)); - assert_arrays_eq!( - or_false, - BoolArray::from_iter([Some(true), Some(false), Some(true)]), - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - #[test] - fn test_and_reject_non_bool() { - let lhs = ConstantArray::new(7i32, 3).into_array(); - let rhs = PrimitiveArray::from_iter([1i32, 2, 3]).into_array(); - assert!(Binary::try_new(lhs, rhs, Operator::And).is_err()); - } - - #[test] - fn test_isnull_and_reduce() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let left = BoolArray::from_iter([Some(true), Some(true), None]).into_array(); - let right = BoolArray::from_iter([true, false, true]).into_array(); - - // IsNull(and(x, y)) -> and(IsNull(x), y) -> and(not(x.validity), y) - // ^ nullable - for (lhs, rhs) in [(left.clone(), right.clone()), (right.clone(), left)] { - let array = lhs - .binary(rhs, Operator::And)? - .is_null()? - .optimize_ctx(&SESSION)?; - assert_eq!(*array.as_::>().options, Operator::And); - assert_arrays_eq!( - array.as_::().get_child(0), // not(left.validity()) - BoolArray::from_iter([false, false, true]), - &mut ctx - ); - assert_arrays_eq!(array.as_::().get_child(1), right, &mut ctx); - assert_arrays_eq!(array, BoolArray::from_iter([false, false, true]), &mut ctx); - } - Ok(()) - } - - #[test] - fn test_isnotnull_or_reduce() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let left = BoolArray::from_iter([Some(true), Some(true), None]).into_array(); - let right = BoolArray::from_iter([true, false, true]).into_array(); - - // IsNotNull(or(x, y)) -> or(IsNotNull(x), y) -> or(x.validity, y) - // ^ nullable - for (lhs, rhs) in [(left.clone(), right.clone()), (right.clone(), left)] { - let array = lhs - .binary(rhs, Operator::Or)? - .is_not_null()? - .optimize_ctx(&SESSION)?; - assert_eq!(*array.as_::>().options, Operator::Or); - assert_arrays_eq!( - array.as_::().get_child(0), // left.validity() - BoolArray::from_iter([true, true, false]), - &mut ctx - ); - assert_arrays_eq!(array.as_::().get_child(1), right, &mut ctx); - assert_arrays_eq!(array, BoolArray::from_iter([true, true, true]), &mut ctx); - } - Ok(()) - } - - /// Comparisons over arithmetic, joined by `AND`: every input is a scalar function, so each - /// operator executes its inputs through the executor. - #[test] - fn executes_non_columnar_inputs_iteratively() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let a = PrimitiveArray::from_iter([1i32, 5, 9, 12]).into_array(); - let b = PrimitiveArray::from_iter([2i32, 2, 2, 2]).into_array(); - let sum = a.binary(b.clone(), Operator::Add)?; - let difference = a.binary(b, Operator::Sub)?; - let ten = ConstantArray::new(10i32, 4).into_array(); - let result = sum - .binary(ten.clone(), Operator::Gt)? - .binary(difference.binary(ten, Operator::Lt)?, Operator::And)?; - - assert_arrays_eq!( - result, - BoolArray::from_iter([false, false, true, false]), - &mut ctx - ); - Ok(()) - } - - /// A constant `false` decides `AND` without executing the other operand, which here fails - /// when executed. - #[test] - fn deciding_constant_skips_other_operand() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let failing = PrimitiveArray::from_iter([1i32, 2, 3]) - .into_array() - .binary(ConstantArray::new(0i32, 3).into_array(), Operator::Div)? - .binary(ConstantArray::new(1i32, 3).into_array(), Operator::Gt)?; - assert!(failing.clone().execute::(&mut ctx).is_err()); - - let result = ConstantArray::new(false, 3) - .into_array() - .binary(failing, Operator::And)?; - - assert_arrays_eq!( - result, - BoolArray::from_iter([false, false, false]), - &mut ctx - ); - Ok(()) - } -} diff --git a/vortex-array/src/scalar_fn/builtin/cast/mod.rs b/vortex-array/src/scalar_fn/builtin/cast/mod.rs index 9efd9257759..cb05fa652d5 100644 --- a/vortex-array/src/scalar_fn/builtin/cast/mod.rs +++ b/vortex-array/src/scalar_fn/builtin/cast/mod.rs @@ -37,9 +37,6 @@ use crate::arrays::struct_::compute::cast::struct_cast; use crate::builtins::ArrayBuiltins; use crate::dtype::DType; use crate::dtype::StructFields; -use crate::expr::BoundExpression; -use crate::expr::bound; -use crate::expr::display::ExprDisplay; use crate::proto::expr as pb; use crate::scalar_fn::Arity; use crate::scalar_fn::ChildName; @@ -49,7 +46,7 @@ use crate::scalar_fn::ReduceNodeValidity; use crate::scalar_fn::ScalarFnId; use crate::scalar_fn::ScalarFnVTable; use crate::scalar_fn::ScalarFnVTableExt; -use crate::scalar_fn::builtin::literal::Literal; +use crate::scalar_fn::display::ExprDisplay; use crate::scalar_fn::is_not_null_node; /// Checks that a cast involving an extension dtype changes only its top-level nullability or @@ -224,17 +221,21 @@ impl ScalarFnVTable for Cast { Ok(None) } - fn simplify( - &self, - target_dtype: &DType, - expr: &BoundExpression, - ) -> VortexResult> { - let Some(scalar) = expr.child(0).as_opt::() else { - return Ok(None); - }; - // A failing cast (e.g. null to a non-nullable dtype) is left in place so the error - // surfaces at execution time rather than during optimization. - Ok(scalar.cast(target_dtype).ok().map(bound::lit)) + fn simplify(&self, target_dtype: &DType, node: &T) -> VortexResult> { + let child = node.child(0); + if let Some(scalar) = child.as_constant() { + // A failing cast (e.g. null to a non-nullable dtype) is left in place so the error + // surfaces at execution time rather than during optimization. + return Ok(scalar + .cast(target_dtype) + .ok() + .map(|scalar| node.new_constant(scalar))); + } + // Collapse node if child is already the target type + if &child.node_dtype()? == target_dtype { + return Ok(Some(child)); + } + Ok(None) } fn validity( @@ -295,147 +296,3 @@ fn cast_canonical( fn cast_constant(array: ArrayView, dtype: &DType) -> VortexResult> { ::cast(array, dtype) } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use vortex_buffer::buffer; - use vortex_error::VortexResult; - use vortex_error::vortex_err; - use vortex_session::VortexSession; - - use super::Cast; - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::StructArray; - use crate::assert_arrays_eq; - use crate::dtype::DType; - use crate::dtype::DecimalDType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::expr::Expression; - use crate::expr::cast; - use crate::expr::get_item; - use crate::expr::lit; - use crate::expr::root; - use crate::expr::test_harness; - use crate::scalar::DecimalValue; - use crate::scalar::Scalar; - use crate::scalar_fn::builtin::literal::Literal; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - #[test] - fn dtype() { - let dtype = test_harness::struct_dtype(); - assert_eq!( - cast(root(), DType::Bool(Nullability::NonNullable)) - .return_dtype(&dtype) - .unwrap(), - DType::Bool(Nullability::NonNullable) - ); - } - - #[test] - fn replace_children() -> VortexResult<()> { - let dtype = DType::Bool(Nullability::Nullable); - let expr = cast(root(), dtype.clone()); - let replaced = expr.with_children(vec![get_item("a", root())])?; - assert_eq!(replaced, cast(get_item("a", root()), dtype)); - Ok(()) - } - - #[test] - fn evaluate() { - let test_array = StructArray::from_fields(&[ - ("a", buffer![0i32, 1, 2].into_array()), - ("b", buffer![4i64, 5, 6].into_array()), - ]) - .unwrap() - .into_array(); - - let expr: Expression = cast( - get_item("a", root()), - DType::Primitive(PType::I64, Nullability::NonNullable), - ); - let result = test_array.apply(&expr).unwrap(); - - assert_eq!( - result.dtype(), - &DType::Primitive(PType::I64, Nullability::NonNullable) - ); - assert_arrays_eq!( - result, - buffer![0i64, 1, 2].into_array(), - &mut SESSION.create_execution_ctx() - ); - } - - #[test] - fn simplify_folds_cast_of_literal() -> VortexResult<()> { - let expr = cast( - lit(3i32), - DType::Primitive(PType::F64, Nullability::NonNullable), - ); - let optimized = expr.bind(&test_harness::struct_dtype())?.optimize()?; - - let scalar = optimized - .as_opt::() - .ok_or_else(|| vortex_err!("expected a bare literal, got {optimized}"))?; - assert_eq!(scalar, &Scalar::primitive(3.0f64, Nullability::NonNullable)); - Ok(()) - } - - #[test] - fn simplify_folds_cast_of_decimal_literal() -> VortexResult<()> { - let decimal = Scalar::decimal( - DecimalValue::I128(319), - DecimalDType::new(3, 2), - Nullability::NonNullable, - ); - let expr = cast( - lit(decimal), - DType::Primitive(PType::F64, Nullability::NonNullable), - ); - let optimized = expr.bind(&test_harness::struct_dtype())?.optimize()?; - - let scalar = optimized - .as_opt::() - .ok_or_else(|| vortex_err!("expected a bare literal, got {optimized}"))?; - assert_eq!( - scalar, - &Scalar::primitive(3.19f64, Nullability::NonNullable) - ); - Ok(()) - } - - #[test] - fn simplify_leaves_failing_cast_unchanged() -> VortexResult<()> { - let target = DType::Primitive(PType::F64, Nullability::NonNullable); - let expr = cast( - lit(Scalar::null(DType::Primitive( - PType::I32, - Nullability::Nullable, - ))), - target.clone(), - ); - let optimized = expr.bind(&test_harness::struct_dtype())?.optimize()?; - - assert!(optimized.as_opt::().is_none()); - assert_eq!(optimized.as_opt::(), Some(&target)); - Ok(()) - } - - #[test] - fn test_display() { - let expr = cast( - get_item("value", root()), - DType::Primitive(PType::I64, Nullability::NonNullable), - ); - assert_eq!(expr.to_string(), "cast($.value as i64)"); - - let expr2 = cast(root(), DType::Bool(Nullability::Nullable)); - assert_eq!(expr2.to_string(), "cast($ as bool?)"); - } -} diff --git a/vortex-array/src/expr/field.rs b/vortex-array/src/scalar_fn/builtin/field.rs similarity index 100% rename from vortex-array/src/expr/field.rs rename to vortex-array/src/scalar_fn/builtin/field.rs diff --git a/vortex-array/src/scalar_fn/builtin/fill_null/mod.rs b/vortex-array/src/scalar_fn/builtin/fill_null/mod.rs index 4faae06f454..c9d15582795 100644 --- a/vortex-array/src/scalar_fn/builtin/fill_null/mod.rs +++ b/vortex-array/src/scalar_fn/builtin/fill_null/mod.rs @@ -26,7 +26,6 @@ use crate::arrays::Primitive; use crate::arrays::ScalarFnArray; use crate::arrays::VarBinView; use crate::dtype::DType; -use crate::expr::BoundExpression; use crate::optimizer::ArrayOptimizer; use crate::require_input; use crate::scalar::Scalar; @@ -184,15 +183,15 @@ impl ScalarFnVTable for FillNull { .map(ScalarFnExecutionResult::done) } - fn simplify( + fn simplify( &self, _options: &Self::Options, - expr: &BoundExpression, - ) -> VortexResult> { - if !expr.child(0).dtype().is_nullable() { - return Ok(Some(expr.child(0).clone())); + node: &T, + ) -> VortexResult> { + let child = node.child(0); + if !child.node_dtype()?.is_nullable() { + return Ok(Some(child)); } - Ok(None) } @@ -288,351 +287,3 @@ fn fill_null_canonical( } } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use rstest::rstest; - use vortex_buffer::buffer; - use vortex_error::VortexExpect; - use vortex_session::VortexSession; - - use super::*; - use crate::Canonical; - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::BoolArray; - use crate::arrays::Chunked; - use crate::arrays::ChunkedArray; - use crate::arrays::Constant; - use crate::arrays::ConstantArray; - use crate::arrays::Primitive; - use crate::arrays::PrimitiveArray; - use crate::arrays::ScalarFnArray; - use crate::arrays::StructArray; - use crate::arrays::VariantArray; - use crate::arrays::scalar_fn::ExactScalarFn; - use crate::assert_arrays_eq; - use crate::builders::builder_with_capacity_in; - use crate::builtins::ArrayBuiltins; - use crate::dtype::DType; - use crate::dtype::DecimalDType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::expr::fill_null; - use crate::expr::get_item; - use crate::expr::lit; - use crate::expr::root; - use crate::extension::datetime::Date; - use crate::extension::datetime::TimeUnit; - use crate::kernel::ExecuteParentKernel; - use crate::matcher::Matcher; - use crate::optimizer::rules::ArrayParentReduceRule; - use crate::scalar::Scalar; - use crate::scalar_fn::builtin::binary::Binary; - use crate::scalar_fn::builtin::operators::Operator; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - #[test] - fn test_fill_null_rejects_other_base_type_for_non_nullable_input() -> VortexResult<()> { - let input = buffer![1i32].into_array(); - let fill = Scalar::from(0i64); - - let expression_error = fill_null(root(), lit(fill.clone())) - .bind(input.dtype()) - .expect_err("the input and fill have different base types"); - let array_error = input - .fill_null(fill) - .expect_err("the array shortcut must enforce the expression's dtype rule"); - - // Compare only the message, because the error text includes a backtrace when - // `RUST_BACKTRACE` is set, and the two paths fail at different call sites. - let rule = "fill_null requires input and fill value to have the same base type"; - assert!( - expression_error.to_string().contains(rule), - "{expression_error}" - ); - assert!(array_error.to_string().contains(rule), "{array_error}"); - Ok(()) - } - - /// A fill of `40 + 2` that is not a [`ConstantArray`] until it executes. - fn lazy_fill_42(len: usize) -> VortexResult { - Ok(ScalarFnArray::try_new( - Binary.bind(Operator::Add), - vec![ - ConstantArray::new(40i32, len).into_array(), - ConstantArray::new(2i32, len).into_array(), - ], - )? - .into_array()) - } - - #[test] - fn dtype() { - let dtype = DType::Primitive(PType::I32, Nullability::Nullable); - assert_eq!( - fill_null(root(), lit(0i32)).return_dtype(&dtype).unwrap(), - DType::Primitive(PType::I32, Nullability::NonNullable) - ); - } - - #[test] - fn replace_children() { - let expr = fill_null(root(), lit(0i32)); - let replaced = expr - .with_children(vec![get_item("a", root()), lit(42i32)]) - .vortex_expect("operation should succeed in test"); - assert_eq!(replaced, fill_null(get_item("a", root()), lit(42i32))); - } - - #[test] - fn evaluate() { - let mut ctx = SESSION.create_execution_ctx(); - let test_array = - PrimitiveArray::from_option_iter([Some(1i32), None, Some(3), None, Some(5)]) - .into_array(); - - let expr = fill_null(root(), lit(42i32)); - let result = test_array.apply(&expr).unwrap(); - - assert_eq!( - result.dtype(), - &DType::Primitive(PType::I32, Nullability::NonNullable) - ); - assert_arrays_eq!( - result, - PrimitiveArray::from_iter([1i32, 42, 3, 42, 5]), - &mut ctx - ); - } - - #[test] - fn evaluate_lazy_constant_fill() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let input = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]).into_array(); - let fill = lazy_fill_42(input.len())?; - assert!(fill.as_constant().is_none()); - - let result = FillNull::try_new(input, fill)? - .into_array() - .execute::(&mut ctx)?; - - assert_eq!( - result.dtype(), - &DType::Primitive(PType::I32, Nullability::NonNullable) - ); - assert_arrays_eq!(result, PrimitiveArray::from_iter([1i32, 42, 3]), &mut ctx); - Ok(()) - } - - #[test] - fn evaluate_lazy_fill_preserves_chunked_input() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let input = ChunkedArray::try_new( - vec![ - PrimitiveArray::from_option_iter([Some(1i32), None]).into_array(), - PrimitiveArray::from_option_iter([Some(3), None]).into_array(), - ], - DType::Primitive(PType::I32, Nullability::Nullable), - )? - .into_array(); - let fill = lazy_fill_42(input.len())?; - - let result = FillNull::try_new(input, fill)? - .into_array() - .execute::(&mut ctx)?; - - assert!(result.is::()); - assert_arrays_eq!( - result, - PrimitiveArray::from_iter([1i32, 42, 3, 42]), - &mut ctx - ); - Ok(()) - } - - #[test] - fn non_constant_fill_declines_and_errors() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let varying = PrimitiveArray::from_iter([1i32, 2, 3]).into_array(); - - let constant_input = ConstantArray::new(Scalar::null_native::(), 3).into_array(); - let constant_parent = - FillNull::try_new(constant_input.clone(), varying.clone())?.into_array(); - let reduced = FillNullReduceAdaptor(Constant).reduce_parent( - constant_input - .as_opt::() - .vortex_expect("constant input"), - ExactScalarFn::::try_match(&constant_parent) - .vortex_expect("fill_null parent"), - 0, - )?; - assert!(reduced.is_none()); - - let primitive_input = - PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]).into_array(); - let primitive_parent = FillNull::try_new(primitive_input.clone(), varying)?.into_array(); - let executed = FillNullExecuteAdaptor(Primitive).execute_parent( - primitive_input - .as_opt::() - .vortex_expect("primitive input"), - ExactScalarFn::::try_match(&primitive_parent) - .vortex_expect("fill_null parent"), - 0, - &mut ctx, - )?; - assert!(executed.is_none()); - - let err = primitive_parent.execute::(&mut ctx).unwrap_err(); - assert!( - err.to_string() - .contains("fill_null fill_value must be a constant/scalar") - ); - Ok(()) - } - - /// Filling a chunked array slices the constant fill per chunk, and an empty slice is a - /// canonical array rather than a constant. Stacked fills over an empty chunk must still run. - #[test] - fn repeated_fill_over_empty_chunk() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let input = ChunkedArray::try_new( - vec![ - PrimitiveArray::from_option_iter(Vec::>::new()).into_array(), - PrimitiveArray::from_option_iter([Some(7u16), None]).into_array(), - ], - DType::Primitive(PType::U16, Nullability::Nullable), - )? - .into_array(); - - let result = input - .fill_null(Scalar::primitive(1u16, Nullability::Nullable))? - .fill_null(Scalar::primitive(2u16, Nullability::NonNullable))?; - - assert_arrays_eq!(result, PrimitiveArray::from_iter([7u16, 1]), &mut ctx); - Ok(()) - } - - #[test] - fn evaluate_struct_field() { - let mut ctx = SESSION.create_execution_ctx(); - let test_array = StructArray::from_fields(&[( - "a", - PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]).into_array(), - )]) - .unwrap() - .into_array(); - - let expr = fill_null(get_item("a", root()), lit(0i32)); - let result = test_array.apply(&expr).unwrap(); - - assert_eq!( - result.dtype(), - &DType::Primitive(PType::I32, Nullability::NonNullable) - ); - assert_arrays_eq!(result, PrimitiveArray::from_iter([1i32, 0, 3]), &mut ctx); - } - - #[test] - fn evaluate_non_nullable_input() { - let mut ctx = SESSION.create_execution_ctx(); - let test_array = buffer![1i32, 2, 3].into_array(); - let expr = fill_null(root(), lit(0i32)); - let result = test_array.apply(&expr).unwrap(); - assert_arrays_eq!(result, PrimitiveArray::from_iter([1i32, 2, 3]), &mut ctx); - } - - /// Inputs with both valid and null rows execute for every non-zip dtype that `supports_dtype` - /// accepts, with a nullable or a non-nullable fill value. - #[rstest] - #[case::bool(DType::Bool(Nullability::Nullable))] - #[case::primitive(DType::Primitive(PType::I32, Nullability::Nullable))] - #[case::decimal(DType::Decimal(DecimalDType::new(19, 2), Nullability::Nullable))] - #[case::utf8(DType::Utf8(Nullability::Nullable))] - #[case::binary(DType::Binary(Nullability::Nullable))] - #[case::date(DType::Extension(Date::new(TimeUnit::Days, Nullability::Nullable).erased()))] - fn supported_dtypes_fill_mixed_validity( - #[case] dtype: DType, - #[values(Nullability::NonNullable, Nullability::Nullable)] fill_nullability: Nullability, - ) -> VortexResult<()> { - assert!(FillNull::supports_dtype(&dtype), "{dtype}"); - - let mut ctx = SESSION.create_execution_ctx(); - let fill = Scalar::default_value(&dtype.as_nonnullable()) - .cast(&dtype.with_nullability(fill_nullability))?; - let mut builder = builder_with_capacity_in(&dtype, 2, ctx.allocator()); - builder.append_scalar(&fill.cast(&dtype)?)?; - builder.append_null(); - let filled = builder - .finish() - .fill_null(fill.clone())? - .execute::(&mut ctx)? - .into_array(); - - assert_eq!(filled.dtype(), fill.dtype(), "{dtype}"); - assert_eq!(filled.execute_scalar(1, &mut ctx)?, fill, "{dtype}"); - assert_arrays_eq!(filled, ConstantArray::new(fill, 2), &mut ctx); - Ok(()) - } - - #[rstest] - #[case::null(DType::Null)] - #[case::variant(DType::Variant(Nullability::Nullable))] - #[case::nested_variant(DType::struct_( - [("value", DType::Variant(Nullability::Nullable))], - Nullability::Nullable, - ))] - fn unsupported_dtypes(#[case] dtype: DType) { - assert!(!FillNull::supports_dtype(&dtype), "{dtype}"); - } - - /// A variant input with mixed validity fails with an error instead of a panic. - #[test] - fn variant_with_mixed_validity_errors() -> VortexResult<()> { - let dtype = DType::Variant(Nullability::Nullable); - let value = Scalar::variant(Scalar::primitive(1i32, Nullability::NonNullable)); - let core_storage = ChunkedArray::try_new( - vec![ - ConstantArray::new(value.cast(&dtype)?, 1).into_array(), - ConstantArray::new(Scalar::null(dtype.clone()), 1).into_array(), - ], - dtype, - )? - .into_array(); - let input = VariantArray::try_new(core_storage, None)?.into_array(); - - let mut ctx = SESSION.create_execution_ctx(); - let error = input - .fill_null(value)? - .execute::(&mut ctx) - .unwrap_err(); - assert!( - error.to_string().contains("fill_null is not supported"), - "{error}" - ); - Ok(()) - } - - #[test] - fn test_display() { - let expr = fill_null(get_item("value", root()), lit(0i32)); - assert_eq!(expr.to_string(), "vortex.fill_null($.value, 0i32)"); - } - - /// A scalar function input is executed through the executor before its nulls are filled. - #[test] - fn executes_non_columnar_input_iteratively() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let input = PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]) - .into_array() - .binary(ConstantArray::new(2i32, 3).into_array(), Operator::Gt)?; - - let result = input.fill_null(Scalar::from(false))?; - - assert_arrays_eq!(result, BoolArray::from_iter([false, false, true]), &mut ctx); - Ok(()) - } -} diff --git a/vortex-array/src/scalar_fn/builtin/get_item.rs b/vortex-array/src/scalar_fn/builtin/get_item.rs index 67aaa96c571..429865181c9 100644 --- a/vortex-array/src/scalar_fn/builtin/get_item.rs +++ b/vortex-array/src/scalar_fn/builtin/get_item.rs @@ -22,7 +22,6 @@ use crate::builtins::ArrayBuiltins; use crate::dtype::DType; use crate::dtype::FieldName; use crate::dtype::Nullability; -use crate::expr::display::ExprDisplay; use crate::proto::expr as pb; use crate::scalar::Scalar; use crate::scalar_fn::Arity; @@ -37,6 +36,7 @@ use crate::scalar_fn::builtin::mask::Mask; use crate::scalar_fn::builtin::pack::Pack; use crate::scalar_fn::builtin::select::FieldSelection; use crate::scalar_fn::builtin::select::Select; +use crate::scalar_fn::display::ExprDisplay; #[derive(Clone)] pub struct GetItem; @@ -241,445 +241,3 @@ impl ScalarFnVTable for GetItem { true } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use rstest::rstest; - use vortex_buffer::buffer; - use vortex_error::VortexResult; - use vortex_session::VortexSession; - - use super::GetItem; - use crate::ArrayRef; - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::Constant; - use crate::arrays::ConstantArray; - use crate::arrays::Filter; - use crate::arrays::Primitive; - use crate::arrays::PrimitiveArray; - use crate::arrays::ScalarFn; - use crate::arrays::ScalarFnArray; - use crate::arrays::Slice; - use crate::assert_arrays_eq; - use crate::builtins::ArrayBuiltins; - use crate::dtype::DType; - use crate::dtype::FieldName; - use crate::dtype::FieldNames; - use crate::dtype::Nullability; - use crate::dtype::Nullability::NonNullable; - use crate::dtype::PType; - use crate::dtype::StructFields; - use crate::expr::Expression; - use crate::expr::checked_add; - use crate::expr::get_item; - use crate::expr::lit; - use crate::expr::pack; - use crate::expr::proto::ExprSerializeProtoExt; - use crate::expr::root; - use crate::expr::select; - use crate::expr::select_exclude; - use crate::optimizer::ArrayOptimizer; - use crate::scalar::Scalar; - use crate::scalar_fn::ScalarFnVTableExt; - use crate::scalar_fn::builtin::get_item::StructArray; - use crate::scalar_fn::builtin::select::FieldSelection; - use crate::scalar_fn::builtin::select::Select; - use crate::validity::Validity; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - fn test_array() -> StructArray { - StructArray::from_fields(&[ - ("a", buffer![0i32, 1, 2].into_array()), - ("b", buffer![4i64, 5, 6].into_array()), - ]) - .unwrap() - } - - #[test] - fn test_roundtrip_preserves_field_names() -> VortexResult<()> { - let name = "quote\"slash\\line\n\t\r\0 ♡"; - let escaped_name = name.escape_debug().to_string(); - let expected = buffer![1i32, 2].into_array(); - let input = StructArray::from_fields(&[ - (name, expected.clone()), - (escaped_name.as_str(), buffer![3i32, 4].into_array()), - ])? - .into_array(); - let expression = get_item(name, root()); - let restored = Expression::from_proto(&expression.serialize_proto()?, &SESSION)?; - - assert_arrays_eq!( - input.apply(&restored)?, - expected, - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - #[test] - fn get_item_by_name() { - let st = test_array(); - let get_item = get_item("a", root()); - assert!( - get_item - .as_scalar() - .is_some_and(|f| f.signature().is_strict()) - ); - let item = st.into_array().apply(&get_item).unwrap(); - assert_eq!(item.dtype(), &DType::from(PType::I32)); - assert_arrays_eq!( - item, - buffer![0i32, 1, 2].into_array(), - &mut SESSION.create_execution_ctx() - ); - } - - #[test] - fn get_item_by_name_none() { - let st = test_array(); - let get_item = get_item("c", root()); - assert!(st.into_array().apply(&get_item).is_err()); - } - - #[test] - fn get_nullable_field() { - let st = StructArray::try_new( - FieldNames::from(["a"]), - vec![buffer![1i32].into_array()], - 1, - Validity::AllInvalid, - ) - .unwrap() - .into_array(); - - let get_item_expr = get_item("a", root()); - let item = st.apply(&get_item_expr).unwrap(); - // The dtype should be nullable since it inherits struct validity - assert_eq!( - item.dtype(), - &DType::Primitive(PType::I32, Nullability::Nullable) - ); - assert_arrays_eq!( - item, - PrimitiveArray::from_option_iter([None::]), - &mut SESSION.create_execution_ctx() - ); - } - - #[test] - fn get_non_nullable_field_from_all_valid_nullable_struct() -> VortexResult<()> { - let st = StructArray::try_new( - FieldNames::from(["a"]), - vec![buffer![1i32].into_array()], - 1, - Validity::AllValid, - )? - .into_array(); - - let item = st.apply(&get_item("a", root()))?; - assert_eq!( - item.dtype(), - &DType::Primitive(PType::I32, Nullability::Nullable) - ); - assert_arrays_eq!( - item, - PrimitiveArray::from_option_iter([Some(1i32)]), - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - #[test] - fn execute_constant_struct_stays_constant() -> VortexResult<()> { - let field_count = 128usize; - let selected_idx = 97i32; - let names = (0..field_count) - .map(|idx| FieldName::from(format!("f{idx}"))) - .collect::>(); - let dtypes = vec![DType::Primitive(PType::I32, NonNullable); field_count]; - let fields = StructFields::new(FieldNames::from(names), dtypes); - let scalar = Scalar::struct_( - DType::Struct(fields, NonNullable), - (0..128).map(|idx| Scalar::primitive(idx, NonNullable)), - ); - let array = ConstantArray::new(scalar, 1_000_000).into_array(); - let mut ctx = SESSION.create_execution_ctx(); - - let item: ArrayRef = GetItem::try_new(array, "f97")? - .into_array() - .execute(&mut ctx)?; - - let constant = item.as_::(); - assert_eq!(constant.len(), 1_000_000); - assert_eq!( - constant.scalar(), - &Scalar::primitive(selected_idx, NonNullable) - ); - Ok(()) - } - - #[rstest] - #[case::include(FieldSelection::Include(FieldNames::from(["a"])))] - #[case::exclude(FieldSelection::Exclude(FieldNames::from(["b"])))] - fn get_item_reduces_through_select( - #[case] selection: FieldSelection, - #[values( - Validity::NonNullable, - Validity::AllValid, - Validity::AllInvalid, - Validity::from_iter([true, false, true]) - )] - validity: Validity, - ) -> VortexResult<()> { - let array = StructArray::try_new( - FieldNames::from(["a", "b"]), - vec![ - ConstantArray::new(42i32, 3).into_array(), - buffer![1i32, 2, 3].into_array(), - ], - 3, - validity, - )? - .into_array(); - let expected = array.get_item("a")?.optimize_ctx(&SESSION)?; - let selected = ScalarFnArray::try_new(Select.bind(selection), [array])?.into_array(); - let item = GetItem::try_new(selected, "a")? - .into_array() - .optimize_ctx(&SESSION)?; - - assert_eq!(item.encoding_id(), expected.encoding_id()); - assert_eq!(item.dtype(), expected.dtype()); - assert_arrays_eq!(item, expected, &mut SESSION.create_execution_ctx()); - Ok(()) - } - - #[rstest] - #[case::include(select(["a"], root()))] - #[case::exclude(select_exclude(["b"], root()))] - fn get_item_select_expression_reduces(#[case] selected: Expression) -> VortexResult<()> { - let dtype = test_array().dtype().clone(); - let result = get_item("a", selected).bind(&dtype)?.optimize_recursive()?; - assert_eq!(result, get_item("a", root()).bind(&dtype)?); - Ok(()) - } - - #[rstest] - #[case::include(select(["b"], root()))] - #[case::exclude(select_exclude(["a"], root()))] - fn get_item_select_rejects_removed_field(#[case] selected: Expression) { - assert!( - test_array() - .into_array() - .apply(&get_item("a", selected)) - .is_err() - ); - } - - #[rstest] - fn get_item_constant_reduces( - #[values(false, true)] null: bool, - #[values(0, 3)] len: usize, - ) -> VortexResult<()> { - let dtype = DType::Struct( - StructFields::new(FieldNames::from(["a"]), vec![DType::from(PType::I32)]), - Nullability::Nullable, - ); - let scalar = if null { - Scalar::null(dtype) - } else { - Scalar::struct_(dtype, [Scalar::from(42i32)]) - }; - let item = ConstantArray::new(scalar, len) - .into_array() - .apply(&get_item("a", root()))? - .optimize_ctx(&SESSION)?; - let expected = if null { - Scalar::null(DType::Primitive(PType::I32, Nullability::Nullable)) - } else { - Scalar::primitive(42i32, Nullability::Nullable) - }; - - assert_eq!(item.as_::().scalar(), &expected); - assert_eq!(item.len(), len); - Ok(()) - } - - #[test] - fn get_item_pushes_through_filter() -> VortexResult<()> { - use vortex_mask::Mask; - - let filtered = test_array() - .into_array() - .filter(Mask::from_iter([true, false, true]))?; - - let item = filtered.get_item("a")?.optimize_ctx(&SESSION)?; - - assert!(item.is::()); - assert!(!item.is::()); - assert_arrays_eq!( - item, - buffer![0i32, 2].into_array(), - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - #[test] - fn get_item_pushes_through_slice() -> VortexResult<()> { - let sliced = test_array().into_array().slice(1..3)?; - - let item = sliced.get_item("a")?.optimize_ctx(&SESSION)?; - - assert!(item.is::()); - assert!(!item.is::()); - assert!(!item.is::()); - assert_arrays_eq!( - item, - buffer![1i32, 2].into_array(), - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - #[test] - fn get_item_pushes_through_masked() -> VortexResult<()> { - let masked = test_array() - .into_array() - .mask(Validity::from_iter([true, false, true]).to_array(3))? - .optimize_ctx(&SESSION)?; - - let item = masked.get_item("a")?.optimize_ctx(&SESSION)?; - - assert!(item.is::()); - assert_eq!( - item.dtype(), - &DType::Primitive(PType::I32, Nullability::Nullable) - ); - assert!(!item.is::()); - assert_arrays_eq!( - item, - PrimitiveArray::from_option_iter([Some(0i32), None, Some(2)]), - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - #[test] - fn test_pack_get_item_rule() { - // Create: pack(a: lit(1), b: lit(2)).get_item("b") - let pack_expr = pack([("a", lit(1)), ("b", lit(2))], NonNullable); - let get_item_expr = get_item("b", pack_expr); - - let dtype = DType::Struct(StructFields::empty(), NonNullable); - let result = get_item_expr - .bind(&dtype) - .and_then(|expr| expr.optimize_recursive()) - .unwrap(); - - assert_eq!(result, lit(2).bind(&dtype).unwrap()); - } - - #[test] - fn test_multi_level_pack_get_item_simplify() { - let inner_pack = pack([("a", lit(1)), ("b", lit(2))], NonNullable); - let get_a = get_item("a", inner_pack); - - let outer_pack = pack([("x", get_a), ("y", lit(3)), ("z", lit(4))], NonNullable); - let get_z = get_item("z", outer_pack); - - let dtype = DType::Primitive(PType::I32, NonNullable); - - let result = get_z.bind(&dtype).unwrap().optimize_recursive().unwrap(); - assert_eq!(result, lit(4).bind(&dtype).unwrap()); - } - - #[test] - fn test_deeply_nested_pack_get_item() { - let innermost = pack([("a", lit(42))], NonNullable); - let get_a = get_item("a", innermost); - - let level2 = pack([("b", get_a)], NonNullable); - let get_b = get_item("b", level2); - - let level3 = pack([("c", get_b)], NonNullable); - let get_c = get_item("c", level3); - - let outermost = pack([("final", get_c)], NonNullable); - let get_final = get_item("final", outermost); - - let dtype = DType::Primitive(PType::I32, NonNullable); - - let result = get_final - .bind(&dtype) - .unwrap() - .optimize_recursive() - .unwrap(); - assert_eq!(result, lit(42).bind(&dtype).unwrap()); - } - - #[test] - fn test_partial_pack_get_item_simplify() { - let inner_pack = pack([("x", lit(1)), ("y", lit(2))], NonNullable); - let get_x = get_item("x", inner_pack); - let add_expr = checked_add(get_x, lit(10)); - - let outer_pack = pack([("result", add_expr)], NonNullable); - let get_result = get_item("result", outer_pack); - - let dtype = DType::Primitive(PType::I32, NonNullable); - - let result = get_result - .bind(&dtype) - .unwrap() - .optimize_recursive() - .unwrap(); - let expected = checked_add(lit(1), lit(10)).bind(&dtype).unwrap(); - assert_eq!(&result, &expected); - } - - #[test] - fn get_item_filter_list_field() { - let mut ctx = SESSION.create_execution_ctx(); - use vortex_mask::Mask; - - use crate::arrays::BoolArray; - use crate::arrays::FilterArray; - use crate::arrays::ListArray; - - let list = ListArray::try_new( - buffer![0f32, 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.].into_array(), - buffer![2u64, 4, 6, 8, 10, 12].into_array(), - Validity::Array(BoolArray::from_iter([true, true, false, true, true]).into_array()), - &mut ctx, - ) - .unwrap(); - - let filtered = FilterArray::try_new( - list.into_array(), - Mask::from_iter([true, true, false, false, false]), - ) - .unwrap(); - - let st = StructArray::try_new( - FieldNames::from(["data"]), - vec![filtered.into_array()], - 2, - Validity::AllValid, - ) - .unwrap(); - - let result = st.into_array().apply(&get_item("data", root())).unwrap(); - let expected = ListArray::try_new( - buffer![2f32, 3., 4., 5.].into_array(), - buffer![0u64, 2, 4].into_array(), - Validity::AllValid, - &mut ctx, - ) - .unwrap(); - assert_arrays_eq!(result, expected, &mut SESSION.create_execution_ctx()); - } -} diff --git a/vortex-array/src/scalar_fn/builtin/is_not_null.rs b/vortex-array/src/scalar_fn/builtin/is_not_null.rs index 26caf1a9afb..85eba3eacbd 100644 --- a/vortex-array/src/scalar_fn/builtin/is_not_null.rs +++ b/vortex-array/src/scalar_fn/builtin/is_not_null.rs @@ -14,7 +14,6 @@ use crate::ExecutionCtx; use crate::arrays::ScalarFnArray; use crate::dtype::DType; use crate::dtype::Nullability; -use crate::expr::display::ExprDisplay; use crate::scalar_fn::Arity; use crate::scalar_fn::ChildName; use crate::scalar_fn::EmptyOptions; @@ -28,6 +27,7 @@ use crate::scalar_fn::builtin::binary::Binary; use crate::scalar_fn::builtin::is_null::IsNull; use crate::scalar_fn::builtin::not::Not; use crate::scalar_fn::builtin::operators::Operator; +use crate::scalar_fn::display::ExprDisplay; use crate::scalar_fn::is_not_null_node; /// In array context, reduce @@ -184,198 +184,3 @@ impl ScalarFnVTable for IsNotNull { true } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use vortex_buffer::buffer; - use vortex_error::VortexResult; - use vortex_session::VortexSession; - - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::PrimitiveArray; - use crate::arrays::StructArray; - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::dtype::StructFields; - use crate::expr::BoundExpression; - use crate::expr::Expression; - use crate::expr::and; - use crate::expr::col; - use crate::expr::get_item; - use crate::expr::is_not_null; - use crate::expr::is_null; - use crate::expr::lit; - use crate::expr::not; - use crate::expr::or; - use crate::expr::root; - use crate::expr::test_harness; - use crate::scalar::Scalar; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - #[test] - fn dtype() { - let dtype = test_harness::struct_dtype(); - assert_eq!( - is_not_null(root()).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::NonNullable) - ); - } - - fn bool_dtype() -> DType { - let names = ["a", "b"].into(); - let fields = vec![ - DType::Bool(Nullability::Nullable), - DType::Bool(Nullability::NonNullable), - ]; - DType::Struct(StructFields::new(names, fields), Nullability::NonNullable) - } - - fn optimized(expr: Expression, scope: &DType) -> VortexResult { - expr.bind(scope)?.optimize() - } - - #[test] - fn reduce_to_constant() -> VortexResult<()> { - let dtype = bool_dtype(); - assert_eq!( - optimized(is_not_null(col("b")), &dtype)?, - lit(true).bind(&dtype)? - ); - assert_eq!( - optimized(is_null(col("b")), &dtype)?, - lit(false).bind(&dtype)? - ); - Ok(()) - } - - #[test] - fn reduce_kleene() -> VortexResult<()> { - let dtype = bool_dtype(); - assert_eq!( - optimized(is_not_null(and(col("a"), col("b"))), &dtype)?, - or(is_not_null(col("a")), not(col("b"))).bind(&dtype)? - ); - assert_eq!( - optimized(is_not_null(or(col("a"), col("b"))), &dtype)?, - or(is_not_null(col("a")), col("b")).bind(&dtype)? - ); - assert_eq!( - optimized(is_null(and(col("a"), col("b"))), &dtype)?, - and(is_null(col("a")), col("b")).bind(&dtype)? - ); - assert_eq!( - optimized(is_null(or(col("a"), col("b"))), &dtype)?, - and(is_null(col("a")), not(col("b"))).bind(&dtype)? - ); - Ok(()) - } - - #[test] - fn evaluate_mask() { - let test_array = - PrimitiveArray::from_option_iter(vec![Some(1), None, Some(2), None, Some(3)]) - .into_array(); - let expected = [true, false, true, false, true]; - - let result = test_array.clone().apply(&is_not_null(root())).unwrap(); - - assert_eq!(result.len(), test_array.len()); - assert_eq!(result.dtype(), &DType::Bool(Nullability::NonNullable)); - - for (i, expected_value) in expected.iter().enumerate() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(*expected_value, Nullability::NonNullable) - ); - } - } - - #[test] - fn evaluate_all_true() { - let test_array = buffer![1, 2, 3, 4, 5].into_array(); - - let result = test_array.clone().apply(&is_not_null(root())).unwrap(); - - assert_eq!(result.len(), test_array.len()); - for i in 0..result.len() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(true, Nullability::NonNullable) - ); - } - } - - #[test] - fn evaluate_all_false() { - let test_array = - PrimitiveArray::from_option_iter(vec![None::, None, None, None, None]) - .into_array(); - - let result = test_array.clone().apply(&is_not_null(root())).unwrap(); - - assert_eq!(result.len(), test_array.len()); - for i in 0..result.len() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(false, Nullability::NonNullable) - ); - } - } - - #[test] - fn evaluate_struct() { - let test_array = StructArray::from_fields(&[( - "a", - PrimitiveArray::from_option_iter(vec![Some(1), None, Some(2), None, Some(3)]) - .into_array(), - )]) - .unwrap() - .into_array(); - let expected = [true, false, true, false, true]; - - let result = test_array - .clone() - .apply(&is_not_null(get_item("a", root()))) - .unwrap(); - - assert_eq!(result.len(), test_array.len()); - assert_eq!(result.dtype(), &DType::Bool(Nullability::NonNullable)); - - for (i, expected_value) in expected.iter().enumerate() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(*expected_value, Nullability::NonNullable) - ); - } - } - - #[test] - fn test_display() { - let expr = is_not_null(get_item("name", root())); - assert_eq!(expr.to_string(), "is_not_null($.name)"); - - let expr2 = is_not_null(root()); - assert_eq!(expr2.to_string(), "is_not_null($)"); - } - - #[test] - fn test_is_not_null_is_not_strict() { - assert!( - !is_not_null(col("a")) - .as_scalar() - .is_some_and(|f| f.signature().is_strict()) - ); - } -} diff --git a/vortex-array/src/scalar_fn/builtin/is_null.rs b/vortex-array/src/scalar_fn/builtin/is_null.rs index 05eceba24a9..693f0519a64 100644 --- a/vortex-array/src/scalar_fn/builtin/is_null.rs +++ b/vortex-array/src/scalar_fn/builtin/is_null.rs @@ -106,142 +106,3 @@ impl ScalarFnVTable for IsNull { true } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use vortex_buffer::buffer; - use vortex_session::VortexSession; - - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::PrimitiveArray; - use crate::arrays::StructArray; - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::expr::col; - use crate::expr::get_item; - use crate::expr::is_null; - use crate::expr::root; - use crate::expr::test_harness; - use crate::scalar::Scalar; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - #[test] - fn dtype() { - let dtype = test_harness::struct_dtype(); - assert_eq!( - is_null(root()).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::NonNullable) - ); - } - - #[test] - fn evaluate_mask() { - let test_array = - PrimitiveArray::from_option_iter(vec![Some(1), None, Some(2), None, Some(3)]) - .into_array(); - let expected = [false, true, false, true, false]; - - let result = test_array.clone().apply(&is_null(root())).unwrap(); - - assert_eq!(result.len(), test_array.len()); - assert_eq!(result.dtype(), &DType::Bool(Nullability::NonNullable)); - - for (i, expected_value) in expected.iter().enumerate() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(*expected_value, Nullability::NonNullable) - ); - } - } - - #[test] - fn evaluate_all_false() { - let test_array = buffer![1, 2, 3, 4, 5].into_array(); - - let result = test_array.clone().apply(&is_null(root())).unwrap(); - - assert_eq!(result.len(), test_array.len()); - // All values should be false (non-nullable input) - for i in 0..result.len() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(false, Nullability::NonNullable) - ); - } - } - - #[test] - fn evaluate_all_true() { - let test_array = - PrimitiveArray::from_option_iter(vec![None::, None, None, None, None]) - .into_array(); - - let result = test_array.clone().apply(&is_null(root())).unwrap(); - - assert_eq!(result.len(), test_array.len()); - // All values should be true (all nulls) - for i in 0..result.len() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(true, Nullability::NonNullable) - ); - } - } - - #[test] - fn evaluate_struct() { - let test_array = StructArray::from_fields(&[( - "a", - PrimitiveArray::from_option_iter(vec![Some(1), None, Some(2), None, Some(3)]) - .into_array(), - )]) - .unwrap() - .into_array(); - let expected = [false, true, false, true, false]; - - let result = test_array - .clone() - .apply(&is_null(get_item("a", root()))) - .unwrap(); - - assert_eq!(result.len(), test_array.len()); - assert_eq!(result.dtype(), &DType::Bool(Nullability::NonNullable)); - - for (i, expected_value) in expected.iter().enumerate() { - assert_eq!( - result - .execute_scalar(i, &mut SESSION.create_execution_ctx()) - .unwrap(), - Scalar::bool(*expected_value, Nullability::NonNullable) - ); - } - } - - #[test] - fn test_display() { - let expr = is_null(get_item("name", root())); - assert_eq!(expr.to_string(), "vortex.is_null($.name)"); - - let expr2 = is_null(root()); - assert_eq!(expr2.to_string(), "vortex.is_null($)"); - } - - #[test] - fn test_is_null_is_not_strict() { - assert!( - !is_null(col("a")) - .as_scalar() - .is_some_and(|f| f.signature().is_strict()) - ); - } -} diff --git a/vortex-array/src/scalar_fn/builtin/literal.rs b/vortex-array/src/scalar_fn/builtin/literal.rs index 356babd4472..dcfdae397bf 100644 --- a/vortex-array/src/scalar_fn/builtin/literal.rs +++ b/vortex-array/src/scalar_fn/builtin/literal.rs @@ -14,7 +14,6 @@ use crate::ExecutionCtx; use crate::IntoArray; use crate::arrays::ConstantArray; use crate::dtype::DType; -use crate::expr::display::ExprDisplay; use crate::proto::expr as pb; use crate::scalar::Scalar; use crate::scalar_fn::Arity; @@ -24,6 +23,7 @@ use crate::scalar_fn::ReduceNode; use crate::scalar_fn::ReduceNodeValidity; use crate::scalar_fn::ScalarFnId; use crate::scalar_fn::ScalarFnVTable; +use crate::scalar_fn::display::ExprDisplay; /// Expression that represents a literal scalar value. #[derive(Clone)] @@ -108,58 +108,3 @@ impl ScalarFnVTable for Literal { true } } - -#[cfg(test)] -mod tests { - use crate::dtype::DType; - use crate::dtype::Nullability; - use crate::dtype::PType; - use crate::dtype::StructFields; - use crate::expr::lit; - use crate::expr::test_harness; - use crate::scalar::Scalar; - - #[test] - fn dtype() { - let dtype = test_harness::struct_dtype(); - - assert_eq!( - lit(10).return_dtype(&dtype).unwrap(), - DType::Primitive(PType::I32, Nullability::NonNullable) - ); - assert_eq!( - lit(i64::MAX).return_dtype(&dtype).unwrap(), - DType::Primitive(PType::I64, Nullability::NonNullable) - ); - assert_eq!( - lit(true).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::NonNullable) - ); - assert_eq!( - lit(Scalar::null(DType::Bool(Nullability::Nullable))) - .return_dtype(&dtype) - .unwrap(), - DType::Bool(Nullability::Nullable) - ); - - let sdtype = DType::Struct( - StructFields::new( - ["dog", "cat"].into(), - vec![ - DType::Primitive(PType::U32, Nullability::NonNullable), - DType::Utf8(Nullability::NonNullable), - ], - ), - Nullability::NonNullable, - ); - assert_eq!( - lit(Scalar::struct_( - sdtype.clone(), - vec![Scalar::from(32_u32), Scalar::from("rufus".to_string())] - )) - .return_dtype(&dtype) - .unwrap(), - sdtype - ); - } -} diff --git a/vortex-array/src/scalar_fn/builtin/mask/mod.rs b/vortex-array/src/scalar_fn/builtin/mask/mod.rs index 6836d33a205..5fab8982e01 100644 --- a/vortex-array/src/scalar_fn/builtin/mask/mod.rs +++ b/vortex-array/src/scalar_fn/builtin/mask/mod.rs @@ -22,8 +22,6 @@ use crate::builtins::ArrayBuiltins; use crate::child_to_validity; use crate::dtype::DType; use crate::dtype::Nullability; -use crate::expr::BoundExpression; -use crate::expr::bound; use crate::scalar::Scalar; use crate::scalar_fn::Arity; use crate::scalar_fn::ChildName; @@ -35,7 +33,6 @@ use crate::scalar_fn::ScalarFnId; use crate::scalar_fn::ScalarFnVTable; use crate::scalar_fn::ScalarFnVTableExt; use crate::scalar_fn::builtin::binary::Binary; -use crate::scalar_fn::builtin::literal::Literal; use crate::scalar_fn::builtin::operators::Operator; use crate::scalar_fn::is_not_null_node; @@ -115,27 +112,30 @@ impl ScalarFnVTable for Mask { execute_canonical(input, mask_array, ctx) } - fn simplify( + fn simplify( &self, _options: &Self::Options, - expr: &BoundExpression, - ) -> VortexResult> { - let Some(mask_lit) = expr.child(1).as_opt::() else { + node: &T, + ) -> VortexResult> { + let mask = node.child(1); + let Some(mask_constant) = mask.as_constant() else { return Ok(None); }; - let mask_lit = mask_lit + let mask_constant = mask_constant .as_bool() .value() .vortex_expect("Mask must be non-nullable"); - if mask_lit { + if mask_constant { // Mask is all true, so the output is just the input. - Ok(Some(expr.child(0).clone())) + Ok(Some(node.child(0))) } else { // Mask is all false, so the output is all nulls. - let input_dtype = expr.child(0).dtype(); - Ok(Some(bound::lit(Scalar::null(input_dtype.as_nullable())))) + let input_dtype = node.child(0).node_dtype()?; + Ok(Some( + node.new_constant(Scalar::null(input_dtype.as_nullable())), + )) } } @@ -194,41 +194,3 @@ fn execute_canonical( let canonical = input.execute::(ctx)?; Ok(mask_validity_canonical(canonical, validity, ctx)?.into_array()) } - -#[cfg(test)] -mod test { - use vortex_error::VortexExpect; - - use crate::dtype::DType; - use crate::dtype::Nullability::Nullable; - use crate::dtype::PType; - use crate::expr::bound; - use crate::expr::lit; - use crate::expr::mask; - use crate::scalar::Scalar; - - #[test] - fn test_simplify() { - let input_expr = lit(42u32); - let true_mask_expr = lit(true); - let false_mask_expr = lit(false); - - let mask_true_expr = mask(input_expr.clone(), true_mask_expr); - let simplified_true = mask_true_expr - .bind(&DType::Null) - .and_then(|expr| expr.optimize()) - .vortex_expect("Simplification"); - assert_eq!( - &simplified_true, - &input_expr.bind(&DType::Null).vortex_expect("bind") - ); - - let mask_false_expr = mask(input_expr, false_mask_expr); - let simplified_false = mask_false_expr - .bind(&DType::Null) - .and_then(|expr| expr.optimize()) - .vortex_expect("Simplification"); - let expected_null_expr = bound::lit(Scalar::null(DType::Primitive(PType::U32, Nullable))); - assert_eq!(&simplified_false, &expected_null_expr); - } -} diff --git a/vortex-array/src/scalar_fn/builtin/mod.rs b/vortex-array/src/scalar_fn/builtin/mod.rs index 7c77e8f0299..f6cf7c38296 100644 --- a/vortex-array/src/scalar_fn/builtin/mod.rs +++ b/vortex-array/src/scalar_fn/builtin/mod.rs @@ -17,6 +17,7 @@ pub mod binary; pub mod cast; +mod field; pub mod fill_null; pub mod get_item; pub mod is_not_null; diff --git a/vortex-array/src/scalar_fn/builtin/not/mod.rs b/vortex-array/src/scalar_fn/builtin/not/mod.rs index 2ceb3983c45..f6433a2487b 100644 --- a/vortex-array/src/scalar_fn/builtin/not/mod.rs +++ b/vortex-array/src/scalar_fn/builtin/not/mod.rs @@ -135,111 +135,3 @@ fn not_columnar(child: &ArrayRef, row_count: usize) -> VortexResult = LazyLock::new(VortexSession::empty); - - #[test] - fn is_strict() { - assert!( - not(root()) - .as_scalar() - .is_some_and(|f| f.signature().is_strict()) - ); - } - - #[test] - fn executes_non_columnar_input_iteratively() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let chunks = vec![ - BoolArray::from_iter([Some(true), None]).into_array(), - BoolArray::from_iter([Some(false)]).into_array(), - ]; - let dtype = chunks[0].dtype().clone(); - let chunked = ChunkedArray::try_new(chunks, dtype)?.into_array(); - let nested = Not::try_new(Not::try_new(Not::try_new(chunked)?.into_array())?.into_array())?; - - let result = nested.into_array().execute::(&mut ctx)?; - - assert_arrays_eq!( - result.into_array(), - BoolArray::from_iter([Some(false), None, Some(true)]), - &mut ctx - ); - Ok(()) - } - - #[test] - fn preserves_nulls() -> VortexResult<()> { - let mut ctx = SESSION.create_execution_ctx(); - let input = BoolArray::from_iter([Some(false), None, Some(true)]).into_array(); - - let result = input.apply(¬(root()))?; - - assert_arrays_eq!( - result, - BoolArray::from_iter([Some(true), None, Some(false)]), - &mut ctx - ); - Ok(()) - } - - #[test] - fn invert_booleans() { - let mut ctx = SESSION.create_execution_ctx(); - let not_expr = not(root()); - let bools = BoolArray::from_iter([false, true, false, false, true, true]); - let result = bools.into_array().apply(¬_expr).unwrap(); - assert_arrays_eq!( - result, - BoolArray::from_iter([true, false, true, true, false, false]), - &mut ctx - ); - } - - #[test] - fn test_display_order_of_operations() { - let a = not(get_item("a", root())); - let b = get_item("a", not(root())); - assert_ne!(a.to_string(), b.to_string()); - assert_eq!(a.to_string(), "vortex.not($.a)"); - assert_eq!(b.to_string(), "vortex.not($).a"); - } - - #[test] - fn dtype() { - let not_expr = not(root()); - let dtype = DType::Bool(Nullability::NonNullable); - assert_eq!( - not_expr.return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::NonNullable) - ); - - let dtype = test_harness::struct_dtype(); - assert_eq!( - not(col("bool1")).return_dtype(&dtype).unwrap(), - DType::Bool(Nullability::NonNullable) - ); - } -} diff --git a/vortex-array/src/scalar_fn/builtin/pack.rs b/vortex-array/src/scalar_fn/builtin/pack.rs index 0399b026454..f01ff5297ba 100644 --- a/vortex-array/src/scalar_fn/builtin/pack.rs +++ b/vortex-array/src/scalar_fn/builtin/pack.rs @@ -21,7 +21,6 @@ use crate::dtype::FieldName; use crate::dtype::FieldNames; use crate::dtype::Nullability; use crate::dtype::StructFields; -use crate::expr::display::ExprDisplay; use crate::proto::expr as pb; use crate::scalar_fn::Arity; use crate::scalar_fn::ChildName; @@ -30,6 +29,7 @@ use crate::scalar_fn::ReduceNode; use crate::scalar_fn::ReduceNodeValidity; use crate::scalar_fn::ScalarFnId; use crate::scalar_fn::ScalarFnVTable; +use crate::scalar_fn::display::ExprDisplay; use crate::validity::Validity; /// Pack zero or more expressions into a structure with named fields. @@ -164,249 +164,3 @@ impl ScalarFnVTable for Pack { true } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use vortex_buffer::buffer; - use vortex_error::VortexResult; - use vortex_error::vortex_bail; - use vortex_session::VortexSession; - - use super::Pack; - use super::PackOptions; - use crate::ArrayRef; - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::PrimitiveArray; - use crate::arrays::struct_::StructArrayExt; - use crate::assert_arrays_eq; - use crate::dtype::Nullability; - use crate::expr::Expression; - use crate::expr::col; - use crate::expr::pack; - use crate::expr::proto::ExprSerializeProtoExt; - use crate::scalar_fn::ScalarFnVTableExt; - use crate::scalar_fn::builtin::pack::StructArray; - use crate::validity::Validity; - - static SESSION: LazyLock = LazyLock::new(VortexSession::empty); - - fn test_array() -> ArrayRef { - StructArray::from_fields(&[ - ("a", buffer![0, 1, 2].into_array()), - ("b", buffer![4, 5, 6].into_array()), - ]) - .unwrap() - .into_array() - } - - #[test] - fn test_roundtrip_preserves_field_names() -> VortexResult<()> { - let name = "quote\"slash\\line\n\t\r\0 ♡"; - let escaped_name = name.escape_debug().to_string(); - let input = test_array(); - let expression = pack( - [(name, col("a")), (escaped_name.as_str(), col("b"))], - Nullability::NonNullable, - ); - let restored = Expression::from_proto(&expression.serialize_proto()?, &SESSION)?; - let expected = StructArray::from_fields(&[ - (name, buffer![0i32, 1, 2].into_array()), - (escaped_name.as_str(), buffer![4i32, 5, 6].into_array()), - ])?; - - assert_arrays_eq!( - input.apply(&restored)?, - expected, - &mut SESSION.create_execution_ctx() - ); - Ok(()) - } - - fn field_array(array: &ArrayRef, field_path: &[&str]) -> VortexResult { - let mut ctx = SESSION.create_execution_ctx(); - let mut field_path = field_path.iter(); - - let Some(field) = field_path.next() else { - vortex_bail!("empty field path"); - }; - - let mut array = array - .clone() - .execute::(&mut ctx)? - .unmasked_field_by_name(field)? - .clone(); - for field in field_path { - let next = array - .clone() - .execute::(&mut ctx)? - .unmasked_field_by_name(field)? - .clone(); - array = next; - } - Ok(array) - } - - #[test] - pub fn test_empty_pack() { - let mut ctx = SESSION.create_execution_ctx(); - let expr = Pack.new_expr( - PackOptions { - names: Default::default(), - nullability: Default::default(), - }, - [], - ); - - let test_array = test_array(); - let actual_array = test_array.clone().apply(&expr).unwrap(); - assert_eq!(actual_array.len(), test_array.len()); - let nfields = actual_array - .execute::(&mut ctx) - .unwrap() - .struct_fields() - .nfields(); - assert_eq!(nfields, 0); - } - - #[test] - pub fn test_simple_pack() { - let mut ctx = SESSION.create_execution_ctx(); - let expr = Pack.new_expr( - PackOptions { - names: ["one", "two", "three"].into(), - nullability: Nullability::NonNullable, - }, - [col("a"), col("b"), col("a")], - ); - - let actual_array = test_array() - .apply(&expr) - .unwrap() - .execute::(&mut ctx) - .unwrap(); - - assert_eq!(actual_array.names(), ["one", "two", "three"]); - assert!(matches!(actual_array.validity(), Ok(Validity::NonNullable))); - - assert_arrays_eq!( - field_array(&actual_array.clone().into_array(), &["one"]).unwrap(), - PrimitiveArray::from_iter([0i32, 1, 2]), - &mut ctx - ); - assert_arrays_eq!( - field_array(&actual_array.clone().into_array(), &["two"]).unwrap(), - PrimitiveArray::from_iter([4i32, 5, 6]), - &mut ctx - ); - assert_arrays_eq!( - field_array(&actual_array.into_array(), &["three"]).unwrap(), - PrimitiveArray::from_iter([0i32, 1, 2]), - &mut ctx - ); - } - - #[test] - pub fn test_nested_pack() { - let mut ctx = SESSION.create_execution_ctx(); - let expr = Pack.new_expr( - PackOptions { - names: ["one", "two", "three"].into(), - nullability: Nullability::NonNullable, - }, - [ - col("a"), - Pack.new_expr( - PackOptions { - names: ["two_one", "two_two"].into(), - nullability: Nullability::NonNullable, - }, - [col("b"), col("b")], - ), - col("a"), - ], - ); - - let actual_array = test_array() - .apply(&expr) - .unwrap() - .execute::(&mut ctx) - .unwrap(); - - assert_eq!(actual_array.names(), ["one", "two", "three"]); - - assert_arrays_eq!( - field_array(&actual_array.clone().into_array(), &["one"]).unwrap(), - PrimitiveArray::from_iter([0i32, 1, 2]), - &mut ctx - ); - assert_arrays_eq!( - field_array(&actual_array.clone().into_array(), &["two", "two_one"]).unwrap(), - PrimitiveArray::from_iter([4i32, 5, 6]), - &mut ctx - ); - assert_arrays_eq!( - field_array(&actual_array.clone().into_array(), &["two", "two_two"]).unwrap(), - PrimitiveArray::from_iter([4i32, 5, 6]), - &mut ctx - ); - assert_arrays_eq!( - field_array(&actual_array.into_array(), &["three"]).unwrap(), - PrimitiveArray::from_iter([0i32, 1, 2]), - &mut ctx - ); - } - - #[test] - pub fn test_pack_nullable() { - let mut ctx = SESSION.create_execution_ctx(); - let expr = Pack.new_expr( - PackOptions { - names: ["one", "two", "three"].into(), - nullability: Nullability::Nullable, - }, - [col("a"), col("b"), col("a")], - ); - - let actual_array = test_array() - .apply(&expr) - .unwrap() - .execute::(&mut ctx) - .unwrap(); - - assert_eq!(actual_array.names(), ["one", "two", "three"]); - assert!(matches!(actual_array.validity(), Ok(Validity::AllValid))); - let expected = StructArray::try_new( - ["one", "two", "three"].into(), - vec![ - buffer![0i32, 1, 2].into_array(), - buffer![4i32, 5, 6].into_array(), - buffer![0i32, 1, 2].into_array(), - ], - 3, - Validity::AllValid, - ) - .unwrap(); - assert_arrays_eq!(actual_array, expected, &mut ctx); - } - - #[test] - pub fn test_display() { - let expr = pack( - [("id", col("user_id")), ("name", col("username"))], - Nullability::NonNullable, - ); - assert_eq!(expr.to_string(), "pack(id: $.user_id, name: $.username)"); - - let expr2 = Pack.new_expr( - PackOptions { - names: ["x", "y"].into(), - nullability: Nullability::Nullable, - }, - [col("a"), col("b")], - ); - assert_eq!(expr2.to_string(), "pack(x: $.a, y: $.b)?"); - } -} diff --git a/vortex-array/src/scalar_fn/builtin/select.rs b/vortex-array/src/scalar_fn/builtin/select.rs index fdcecfda1ad..a2fb6e098a1 100644 --- a/vortex-array/src/scalar_fn/builtin/select.rs +++ b/vortex-array/src/scalar_fn/builtin/select.rs @@ -25,20 +25,20 @@ use crate::arrays::struct_::StructArrayExt; use crate::dtype::DType; use crate::dtype::FieldName; use crate::dtype::FieldNames; -use crate::expr::BoundExpression; -use crate::expr::display::ExprDisplay; -use crate::expr::field::DisplayFieldNames; use crate::proto::expr::FieldNames as ProtoFieldNames; use crate::proto::expr::SelectOpts; use crate::proto::expr::select_opts::Opts; use crate::scalar_fn::Arity; use crate::scalar_fn::ChildName; use crate::scalar_fn::ExecutionArgs; +use crate::scalar_fn::ReduceNode; use crate::scalar_fn::ScalarFnId; use crate::scalar_fn::ScalarFnVTable; use crate::scalar_fn::ScalarFnVTableExt; +use crate::scalar_fn::builtin::field::DisplayFieldNames; use crate::scalar_fn::builtin::pack::Pack; use crate::scalar_fn::builtin::pack::PackOptions; +use crate::scalar_fn::display::ExprDisplay; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum FieldSelection { @@ -182,13 +182,13 @@ impl ScalarFnVTable for Select { result.into_array().execute(ctx) } - fn simplify( + fn simplify( &self, selection: &FieldSelection, - expr: &BoundExpression, - ) -> VortexResult> { - let child_struct = expr.child(0); - let struct_dtype = child_struct.dtype(); + node: &T, + ) -> VortexResult> { + let child_struct = node.child(0); + let struct_dtype = child_struct.node_dtype()?; let struct_nullability = struct_dtype.nullability(); let struct_fields = struct_dtype.as_struct_fields_opt().ok_or_else(|| { @@ -210,17 +210,17 @@ impl ScalarFnVTable for Select { .is_nullable() }); - // If no fields are included, we can trivially simplify to a pack expression. + // If no fields are included, we can trivially reduce to a pack node. // NOTE(ngates): we do this knowing that our layout expression partitioning logic has // special-casing for pack, but not for select. We will fix this up when we revisit the // layout APIs. if included_fields.is_empty() { - return Ok(Some(Pack.try_new_bound_expr( - PackOptions { + return Ok(Some(node.new_node( + Pack.bind(PackOptions { names: FieldNames::empty(), nullability: struct_nullability, - }, - [], + }), + &[], )?)); } @@ -228,22 +228,22 @@ impl ScalarFnVTable for Select { // because reading a field does a validity intersection of the struct validity with the // field, which is not the same as just "masking" out the unwanted fields (a selection). // - // We can, however, make this simplification when the child of the `select` is already a + // We can, however, make this reduction when the child of the `select` is already a // `pack` and we know that reading a field will do no validity intersections. // // The intersection only happens when the struct is nullable but the field is not. That - // would change the semantics of a `select`, so we can only simplify when it won't happen. + // would change the semantics of a `select`, so we can only reduce when it won't happen. let would_intersect_validity = struct_nullability.is_nullable() && !all_included_fields_are_nullable; - if let Some(child_pack) = child_struct.as_opt::() + if let Some(child_pack) = child_struct.scalar_fn().and_then(|f| f.as_opt::()) && !would_intersect_validity { - // Take the field expressions straight out of the child `pack` rather than wrap the + // Take the field nodes straight out of the child `pack` rather than wrap the // `pack` in `get_item`. The guard above proves the two are equivalent here, and the // optimizer visits children before their parent, so a `get_item` introduced now - // would never be simplified away. - let fields: Vec = included_fields + // would never be reduced away. + let fields: Vec = included_fields .iter() .map(|name| { let idx = child_pack.names.find(name).ok_or_else(|| { @@ -254,16 +254,16 @@ impl ScalarFnVTable for Select { ) })?; - Ok(child_struct.child(idx).clone()) + Ok(child_struct.child(idx)) }) .try_collect::<_, _, VortexError>()?; - return Ok(Some(Pack.try_new_bound_expr( - PackOptions { + return Ok(Some(node.new_node( + Pack.bind(PackOptions { names: included_fields, nullability: struct_nullability, - }, - fields, + }), + &fields, )?)); } @@ -343,250 +343,3 @@ impl Display for FieldSelection { } } } - -#[cfg(test)] -mod tests { - use std::sync::LazyLock; - - use vortex_buffer::buffer; - use vortex_error::VortexResult; - use vortex_session::VortexSession; - - use crate::ArrayRef; - use crate::IntoArray; - use crate::VortexSessionExecute; - use crate::arrays::Constant; - use crate::arrays::ConstantArray; - use crate::arrays::ScalarFnArray; - use crate::assert_arrays_eq; - use crate::dtype::DType; - use crate::dtype::FieldName; - use crate::dtype::FieldNames; - use crate::dtype::Nullability; - use crate::dtype::Nullability::Nullable; - use crate::dtype::PType::I32; - use crate::dtype::StructFields; - use crate::expr::Expression; - use crate::expr::proto::ExprSerializeProtoExt; - use crate::expr::root; - use crate::expr::select; - use crate::expr::select_exclude; - use crate::expr::test_harness; - use crate::scalar::Scalar; - use crate::scalar_fn::ScalarFnVTableExt; - use crate::scalar_fn::builtin::select::FieldSelection; - use crate::scalar_fn::builtin::select::Select; - use crate::scalar_fn::builtin::select::StructArray; - - static SESSION: LazyLock = LazyLock::new(|| { - let session = VortexSession::empty(); - - crate::initialize(&session); - - session - }); - - fn test_array() -> StructArray { - StructArray::from_fields(&[ - ("a", buffer![0, 1, 2].into_array()), - ("b", buffer![4, 5, 6].into_array()), - ]) - .unwrap() - } - - #[test] - fn test_roundtrip_preserves_field_names() -> VortexResult<()> { - let name = "quote\"slash\\line\n\t\r\0 ♡"; - let escaped_name = name.escape_debug().to_string(); - let input = StructArray::from_fields(&[ - (name, buffer![1i32, 2].into_array()), - (escaped_name.as_str(), buffer![3i32, 4].into_array()), - ])? - .into_array(); - for (expression, expected_name, expected_values) in [ - (select([name], root()), name, buffer![1i32, 2].into_array()), - ( - select_exclude([name], root()), - escaped_name.as_str(), - buffer![3i32, 4].into_array(), - ), - ] { - let restored = Expression::from_proto(&expression.serialize_proto()?, &SESSION)?; - let expected = StructArray::from_fields(&[(expected_name, expected_values)])?; - assert_arrays_eq!( - input.clone().apply(&restored)?, - expected, - &mut SESSION.create_execution_ctx() - ); - } - Ok(()) - } - - #[test] - pub fn include_columns() { - let mut ctx = SESSION.create_execution_ctx(); - let st = test_array(); - let select = select(vec![FieldName::from("a")], root()); - let selected = st.into_array().apply(&select).unwrap(); - let selected_names = selected.dtype().as_struct_fields().names(); - assert_eq!(selected_names.as_ref(), &["a"]); - assert_arrays_eq!( - selected, - StructArray::from_fields(&[("a", buffer![0i32, 1, 2].into_array())]).unwrap(), - &mut ctx - ); - } - - #[test] - pub fn exclude_columns() { - let mut ctx = SESSION.create_execution_ctx(); - let st = test_array(); - let select = select_exclude(vec![FieldName::from("a")], root()); - let selected = st.into_array().apply(&select).unwrap(); - let selected_names = selected.dtype().as_struct_fields().names(); - assert_eq!(selected_names.as_ref(), &["b"]); - assert_arrays_eq!( - selected, - StructArray::from_fields(&[("b", buffer![4i32, 5, 6].into_array())]).unwrap(), - &mut ctx - ); - } - - #[test] - fn dtype() { - let dtype = test_harness::struct_dtype(); - - let select_expr = select(vec![FieldName::from("a")], root()); - let expected_dtype = DType::Struct( - dtype - .as_struct_fields_opt() - .unwrap() - .project(&["a".into()]) - .unwrap(), - Nullability::NonNullable, - ); - assert_eq!(select_expr.return_dtype(&dtype).unwrap(), expected_dtype); - - let select_expr_exclude = select_exclude( - vec![ - FieldName::from("col1"), - FieldName::from("col2"), - FieldName::from("bool1"), - FieldName::from("bool2"), - ], - root(), - ); - assert_eq!( - select_expr_exclude.return_dtype(&dtype).unwrap(), - expected_dtype - ); - - let select_expr_exclude = select_exclude( - vec![FieldName::from("col1"), FieldName::from("col2")], - root(), - ); - assert_eq!( - select_expr_exclude.return_dtype(&dtype).unwrap(), - DType::Struct( - dtype - .as_struct_fields_opt() - .unwrap() - .project(&["a".into(), "bool1".into(), "bool2".into()]) - .unwrap(), - Nullability::NonNullable - ) - ); - } - - #[test] - fn test_as_include_names() { - let field_names = FieldNames::from(["a", "b", "c"]); - for expr in [select(["a"], root()), select_exclude(["b", "c"], root())] { - assert_eq!( - expr.as_::() + .normalize_to_included_fields(&field_names) + .unwrap(), + FieldNames::from(["a"]) + ); + } +} + +#[test] +fn execute_constant_struct_stays_constant() -> VortexResult<()> { + let field_count = 128usize; + let names = (0..field_count) + .map(|idx| FieldName::from(format!("f{idx}"))) + .collect::>(); + let dtypes = vec![I32.into(); field_count]; + let fields = StructFields::new(FieldNames::from(names), dtypes); + let scalar = Scalar::struct_( + DType::Struct(fields, Nullability::NonNullable), + (0..128).map(Scalar::from), + ); + let array = ConstantArray::new(scalar, 1_000_000).into_array(); + let mut ctx = SESSION.create_execution_ctx(); + + let item: ArrayRef = ScalarFnArray::try_new( + Select.bind(FieldSelection::Include(FieldNames::from(["f97", "f3"]))), + vec![array], + )? + .into_array() + .execute(&mut ctx)?; + + let constant = item.as_::(); + assert_eq!(constant.len(), 1_000_000); + assert_eq!( + constant.scalar(), + &Scalar::struct_( + DType::Struct( + StructFields::new( + FieldNames::from(["f97", "f3"]), + vec![I32.into(), I32.into()], + ), + Nullability::NonNullable, + ), + [Scalar::from(97), Scalar::from(3)], + ) + ); + Ok(()) +} + +#[test] +fn test_remove_select_rule() { + let dtype = DType::Struct( + StructFields::new(["a", "b"].into(), vec![I32.into(), I32.into()]), + Nullable, + ); + let e = select(["a", "b"], root()); + + let result = e.bind(&dtype).unwrap().optimize_recursive().unwrap(); + + assert_eq!(result.dtype(), &dtype); +} + +#[test] +fn test_remove_select_rule_exclude_fields() { + use crate::select_exclude; + + let dtype = DType::Struct( + StructFields::new( + ["a", "b", "c"].into(), + vec![I32.into(), I32.into(), I32.into()], + ), + Nullable, + ); + let e = select_exclude(["c"], root()); + + let result = e.bind(&dtype).unwrap().optimize_recursive().unwrap(); + + // Should exclude "c" and include "a" and "b" + let result_dtype = result.dtype(); + assert!(result_dtype.is_nullable()); + let fields = result_dtype.as_struct_fields_opt().unwrap(); + assert_eq!(fields.names().as_ref(), &["a", "b"]); + assert_eq!( + result_dtype, + &DType::struct_([("a", I32), ("b", I32)], Nullable) + ); +} + +#[test] +fn test_roundtrip_preserves_field_names() -> VortexResult<()> { + let name = "quote\"slash\\line\n\t\r\0 ♡"; + let escaped_name = name.escape_debug().to_string(); + let input = StructArray::from_fields(&[ + (name, buffer![1i32, 2].into_array()), + (escaped_name.as_str(), buffer![3i32, 4].into_array()), + ])? + .into_array(); + for (expression, expected_name, expected_values) in [ + (select([name], root()), name, buffer![1i32, 2].into_array()), + ( + select_exclude([name], root()), + escaped_name.as_str(), + buffer![3i32, 4].into_array(), + ), + ] { + let restored = Expression::from_proto(&expression.serialize_proto()?, &SESSION)?; + let expected = StructArray::from_fields(&[(expected_name, expected_values)])?; + assert_arrays_eq!( + input.clone().apply(&restored)?, + expected, + &mut SESSION.create_execution_ctx() + ); + } + Ok(()) +} diff --git a/vortex-expr/src/transform/bound_partition.rs b/vortex-expr/src/transform/bound_partition.rs index 91d538033df..57b230b76ab 100644 --- a/vortex-expr/src/transform/bound_partition.rs +++ b/vortex-expr/src/transform/bound_partition.rs @@ -13,12 +13,6 @@ use vortex_array::dtype::Nullability; use vortex_array::dtype::StructFields; use vortex_array::expr::BoundExpression; use vortex_array::expr::ExactBoundExpr; -use vortex_array::expr::bound::get_item; -use vortex_array::expr::bound::pack; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::NodeRewriter; -use vortex_array::expr::traversal::Transformed; -use vortex_array::expr::traversal::TraversalOrder; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::builtin::pack::Pack; use vortex_error::VortexExpect; @@ -30,6 +24,12 @@ use crate::analysis::Annotation; use crate::analysis::AnnotationFn; use crate::analysis::BoundAnnotations; use crate::analysis::descendent_bound_annotations; +use crate::bound::get_item; +use crate::bound::pack; +use crate::traversal::NodeExt; +use crate::traversal::NodeRewriter; +use crate::traversal::Transformed; +use crate::traversal::TraversalOrder; /// Partition an expression into sub-expressions that are uniquely associated with an annotation. /// A root expression is also returned that can be used to recombine the results of the partitions @@ -372,15 +372,15 @@ mod tests { use vortex_array::dtype::Nullability::Nullable; use vortex_array::dtype::PType::I32; use vortex_array::dtype::StructFields; - use vortex_array::expr::checked_add; - use vortex_array::expr::col; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::pack; - use vortex_array::expr::root; use super::*; use crate::analysis::make_bound_free_field_annotator; + use crate::checked_add; + use crate::col; + use crate::get_item; + use crate::lit; + use crate::pack; + use crate::root; #[fixture] fn dtype() -> DType { diff --git a/vortex-expr/src/transform/mod.rs b/vortex-expr/src/transform/mod.rs index 90101207785..151a8c8d5c5 100644 --- a/vortex-expr/src/transform/mod.rs +++ b/vortex-expr/src/transform/mod.rs @@ -1,8 +1,8 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors -//! Transformations for [`Expression`](vortex_array::expr::Expression) and -//! [`BoundExpression`](vortex_array::expr::BoundExpression) trees. +//! Transformations for [`Expression`](crate::Expression) and +//! [`BoundExpression`](crate::BoundExpression) trees. mod bound_partition; mod replace; diff --git a/vortex-expr/src/transform/replace.rs b/vortex-expr/src/transform/replace.rs index a990b601d0f..914cd58023c 100644 --- a/vortex-expr/src/transform/replace.rs +++ b/vortex-expr/src/transform/replace.rs @@ -1,12 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors -use vortex_array::expr::Expression; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::Transformed; -use vortex_array::expr::traversal::TraversalOrder; use vortex_error::VortexExpect; +use crate::Expression; +use crate::traversal::NodeExt; +use crate::traversal::Transformed; +use crate::traversal::TraversalOrder; + /// Replaces all occurrences of `needle` in the expression `expr` with `replacement`. pub fn replace(expr: Expression, needle: &Expression, replacement: Expression) -> Expression { expr.transform_down(|node| { @@ -28,11 +29,11 @@ pub fn replace(expr: Expression, needle: &Expression, replacement: Expression) - #[cfg(test)] mod test { use vortex_array::dtype::Nullability::NonNullable; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::pack; use super::replace; + use crate::get_item; + use crate::lit; + use crate::pack; #[test] fn test_replace_full_tree() { diff --git a/vortex-array/src/expr/traversal/fold.rs b/vortex-expr/src/traversal/fold.rs similarity index 93% rename from vortex-array/src/expr/traversal/fold.rs rename to vortex-expr/src/traversal/fold.rs index d7c2bcddeba..bbfa6a3a3bb 100644 --- a/vortex-array/src/expr/traversal/fold.rs +++ b/vortex-expr/src/traversal/fold.rs @@ -3,7 +3,7 @@ use vortex_error::VortexResult; -use crate::expr::traversal::Node; +use crate::traversal::Node; /// Use to indicate the control flow of the fold on the downwards pass. /// In the case of Continue, the context is passed on to the children nodes. @@ -72,18 +72,18 @@ pub trait NodeFolderContext { #[cfg(test)] mod tests { + use vortex_array::scalar_fn::builtin::binary::Binary; + use vortex_array::scalar_fn::builtin::literal::Literal; + use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_error::VortexExpect; use vortex_error::vortex_bail; use super::*; - use crate::expr::Expression; - use crate::expr::checked_add; - use crate::expr::gt; - use crate::expr::lit; - use crate::expr::traversal::NodeExt; - use crate::scalar_fn::builtin::binary::Binary; - use crate::scalar_fn::builtin::literal::Literal; - use crate::scalar_fn::builtin::operators::Operator; + use crate::Expression; + use crate::checked_add; + use crate::gt; + use crate::lit; + use crate::traversal::NodeExt; struct AddFold; impl NodeFolderContext for AddFold { diff --git a/vortex-array/src/expr/traversal/mod.rs b/vortex-expr/src/traversal/mod.rs similarity index 97% rename from vortex-array/src/expr/traversal/mod.rs rename to vortex-expr/src/traversal/mod.rs index 8129703216b..56739fa8c93 100644 --- a/vortex-array/src/expr/traversal/mod.rs +++ b/vortex-expr/src/traversal/mod.rs @@ -17,8 +17,8 @@ pub use fold::NodeFolderContext; pub use visitor::pre_order_visit_down; use vortex_error::VortexResult; -use crate::expr::BoundExpression; -use crate::expr::Expression; +use crate::BoundExpression; +use crate::Expression; /// Signal to control a traversal's flow #[derive(Debug, Clone, PartialEq, Eq)] @@ -549,6 +549,10 @@ impl Node for BoundExpression { #[cfg(test)] mod tests { + use vortex_array::scalar_fn::builtin::binary::Binary; + use vortex_array::scalar_fn::builtin::get_item::GetItem; + use vortex_array::scalar_fn::builtin::literal::Literal; + use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_error::VortexResult; use vortex_utils::aliases::hash_set::HashSet; @@ -558,18 +562,14 @@ mod tests { use super::Transformed; use super::TraversalOrder; use super::visitor::pre_order_visit_down; - use crate::expr::Expression; - use crate::expr::and; - use crate::expr::col; - use crate::expr::eq; - use crate::expr::is_root; - use crate::expr::lit; - use crate::expr::not_eq; - use crate::expr::root; - use crate::scalar_fn::builtin::binary::Binary; - use crate::scalar_fn::builtin::get_item::GetItem; - use crate::scalar_fn::builtin::literal::Literal; - use crate::scalar_fn::builtin::operators::Operator; + use crate::Expression; + use crate::and; + use crate::col; + use crate::eq; + use crate::is_root; + use crate::lit; + use crate::not_eq; + use crate::root; #[derive(Default)] pub struct ExprLitCollector<'a>(pub Vec<&'a Expression>); diff --git a/vortex-array/src/expr/traversal/visitor.rs b/vortex-expr/src/traversal/visitor.rs similarity index 85% rename from vortex-array/src/expr/traversal/visitor.rs rename to vortex-expr/src/traversal/visitor.rs index d5f6c669e87..1aa330df18f 100644 --- a/vortex-array/src/expr/traversal/visitor.rs +++ b/vortex-expr/src/traversal/visitor.rs @@ -5,10 +5,10 @@ use std::marker::PhantomData; use vortex_error::VortexResult; -use crate::expr::traversal::Node; -use crate::expr::traversal::NodeExt; -use crate::expr::traversal::NodeVisitor; -use crate::expr::traversal::TraversalOrder; +use crate::traversal::Node; +use crate::traversal::NodeExt; +use crate::traversal::NodeVisitor; +use crate::traversal::TraversalOrder; struct FnVisitor<'a, F, T: 'a> where diff --git a/vortex-ffi/src/array.rs b/vortex-ffi/src/array.rs index e339215c2e0..fd5ad916c8f 100644 --- a/vortex-ffi/src/array.rs +++ b/vortex-ffi/src/array.rs @@ -36,6 +36,7 @@ use vortex::error::vortex_ensure; use vortex::error::vortex_ensure_eq; use vortex::error::vortex_err; use vortex::error::vortex_panic; +use vortex::expr::ArrayExprExt; use vortex_arrow::ArrowSessionExt; use crate::box_wrapper; diff --git a/vortex-ffi/src/expression.rs b/vortex-ffi/src/expression.rs index 4b83b5d3d42..4126b390f59 100644 --- a/vortex-ffi/src/expression.rs +++ b/vortex-ffi/src/expression.rs @@ -9,6 +9,7 @@ use std::sync::Arc; use vortex::dtype::FieldName; use vortex::error::VortexExpect; use vortex::expr::Expression; +use vortex::expr::ScalarFnExprExt; use vortex::expr::and_collect; use vortex::expr::get_item; use vortex::expr::is_null; @@ -18,7 +19,6 @@ use vortex::expr::not; use vortex::expr::or_collect; use vortex::expr::root; use vortex::expr::select; -use vortex::scalar_fn::ScalarFnVTableExt; use vortex::scalar_fn::builtin::binary::Binary; use vortex::scalar_fn::builtin::operators::Operator; diff --git a/vortex-file/src/file.rs b/vortex-file/src/file.rs index 0b1c3ccdfcc..3d8d9822431 100644 --- a/vortex-file/src/file.rs +++ b/vortex-file/src/file.rs @@ -14,9 +14,9 @@ use itertools::Itertools; use vortex_array::ArrayRef; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::Expression; use vortex_buffer::ByteBuffer; use vortex_error::VortexResult; +use vortex_expr::Expression; use vortex_layout::LayoutReader; use vortex_layout::scan::layout::LayoutReaderDataSource; use vortex_layout::scan::metrics::ScanUsage; diff --git a/vortex-file/src/pruning.rs b/vortex-file/src/pruning.rs index 11d5efcd3b5..65ef4c2d23c 100644 --- a/vortex-file/src/pruning.rs +++ b/vortex-file/src/pruning.rs @@ -9,13 +9,14 @@ use vortex_array::arrays::ConstantArray; use vortex_array::arrays::NullArray; use vortex_array::dtype::DType; use vortex_array::dtype::FieldPath; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::lit; use vortex_array::scalar_fn::builtin::cast::Cast; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::builtin::literal::Literal; -use vortex_array::scalar_fn::internal::row_count::substitute_row_count; use vortex_error::VortexResult; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; +use vortex_expr::bound::lit; +use vortex_expr::lambda::row_count::substitute_row_count; use vortex_expr::stats::bind::StatBinder; use vortex_expr::stats::bind::bind_stats; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; diff --git a/vortex-file/src/tests.rs b/vortex-file/src/tests.rs index 5bc15506d7d..f14e499f5b9 100644 --- a/vortex-file/src/tests.rs +++ b/vortex-file/src/tests.rs @@ -56,21 +56,6 @@ use vortex_array::dtype::PType; use vortex_array::dtype::PType::I32; use vortex_array::dtype::StructFields; use vortex_array::dtype::i256; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::Expression; -use vortex_array::expr::and; -use vortex_array::expr::cast; -use vortex_array::expr::col; -use vortex_array::expr::eq; -use vortex_array::expr::get_item; -use vortex_array::expr::gt; -use vortex_array::expr::gt_eq; -use vortex_array::expr::lit; -use vortex_array::expr::lt; -use vortex_array::expr::lt_eq; -use vortex_array::expr::or; -use vortex_array::expr::root; -use vortex_array::expr::select; use vortex_array::expr::stats::Precision; use vortex_array::expr::stats::Stat; use vortex_array::extension::datetime::TimeUnit; @@ -81,7 +66,6 @@ use vortex_array::flatbuffers::WriteFlatBufferExt; use vortex_array::match_each_decimal_value_type; use vortex_array::scalar::Scalar; use vortex_array::scalar::ScalarValue; -use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::pack::Pack; use vortex_array::scalar_fn::builtin::pack::PackOptions; use vortex_array::stream::ArrayStreamAdapter; @@ -105,6 +89,22 @@ use vortex_edition::declarations::preview::PREVIEW_2026_08_0; use vortex_edition::declarations::unstable::UNSTABLE; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::Expression; +use vortex_expr::ScalarFnExprExt; +use vortex_expr::and; +use vortex_expr::cast; +use vortex_expr::col; +use vortex_expr::eq; +use vortex_expr::get_item; +use vortex_expr::gt; +use vortex_expr::gt_eq; +use vortex_expr::lit; +use vortex_expr::lt; +use vortex_expr::lt_eq; +use vortex_expr::or; +use vortex_expr::root; +use vortex_expr::select; use vortex_functions::expr::lower; use vortex_io::VortexWrite; use vortex_io::runtime::tokio::TokioRuntime; diff --git a/vortex-file/src/v2/file_stats_reader.rs b/vortex-file/src/v2/file_stats_reader.rs index 30fa210e3e3..e43bd2b9f62 100644 --- a/vortex-file/src/v2/file_stats_reader.rs +++ b/vortex-file/src/v2/file_stats_reader.rs @@ -13,8 +13,9 @@ use std::sync::Arc; use vortex_array::MaskFuture; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::BoundExpression; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::ExactBoundExpr; use vortex_functions::dynamic::DynamicExprUpdates; use vortex_layout::ArrayFuture; use vortex_layout::LayoutReader; @@ -258,18 +259,18 @@ mod tests { use vortex_array::dtype::FieldPath; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::checked_add; - use vortex_array::expr::get_item; - use vortex_array::expr::gt; - use vortex_array::expr::is_not_null; - use vortex_array::expr::is_null; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::expr::stats::Precision; use vortex_array::extension::datetime::TimeUnit; use vortex_array::scalar::Scalar; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::checked_add; + use vortex_expr::get_item; + use vortex_expr::gt; + use vortex_expr::is_not_null; + use vortex_expr::is_null; + use vortex_expr::lit; + use vortex_expr::root; use vortex_functions::dynamic_expression::DynamicExpressionSource; use vortex_functions::dynamic_expression::input_field_name; use vortex_functions::expr::dynamic_expression; diff --git a/vortex-functions/benches/case_when.rs b/vortex-functions/benches/case_when.rs index d359ca30628..b576c6ae86b 100644 --- a/vortex-functions/benches/case_when.rs +++ b/vortex-functions/benches/case_when.rs @@ -22,12 +22,6 @@ use vortex_array::arrays::VarBinViewArray; use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DecimalDType; use vortex_array::dtype::session::DTypeSession; -use vortex_array::expr::eq; -use vortex_array::expr::get_item; -use vortex_array::expr::gt; -use vortex_array::expr::lit; -use vortex_array::expr::lt; -use vortex_array::expr::root; use vortex_array::memory::MemorySession; use vortex_array::optimizer::kernels::KernelSession; use vortex_array::scalar::Scalar; @@ -37,6 +31,13 @@ use vortex_array::scalar_fn::session::ScalarFnSession; use vortex_array::session::ArraySession; use vortex_array::test_harness::mark_optimized; use vortex_buffer::Buffer; +use vortex_expr::ArrayExprExt; +use vortex_expr::eq; +use vortex_expr::get_item; +use vortex_expr::gt; +use vortex_expr::lit; +use vortex_expr::lt; +use vortex_expr::root; use vortex_functions::case_when::CaseWhen; use vortex_functions::case_when::CaseWhenOptions; use vortex_functions::expr::case_when; diff --git a/vortex-functions/benches/list_contains_set.rs b/vortex-functions/benches/list_contains_set.rs index 2a94a5334ad..ed3848dda34 100644 --- a/vortex-functions/benches/list_contains_set.rs +++ b/vortex-functions/benches/list_contains_set.rs @@ -29,12 +29,13 @@ use vortex_array::arrays::WidenArray; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::memory::MemorySession; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::optimizer::kernels::KernelSession; use vortex_array::scalar::Scalar; +use vortex_expr::ArrayExprExt; +use vortex_expr::lit; +use vortex_expr::root; use vortex_functions::expr::list_contains; use vortex_session::VortexSession; diff --git a/vortex-functions/benches/list_length.rs b/vortex-functions/benches/list_length.rs index 8a646df1c2f..896d088f34f 100644 --- a/vortex-functions/benches/list_length.rs +++ b/vortex-functions/benches/list_length.rs @@ -27,7 +27,6 @@ use vortex_array::arrays::ListArray; use vortex_array::arrays::ListViewArray; use vortex_array::arrays::PrimitiveArray; use vortex_array::dtype::session::DTypeSession; -use vortex_array::expr::root; use vortex_array::memory::MemorySession; use vortex_array::optimizer::kernels::KernelSession; use vortex_array::scalar_fn::session::ScalarFnSession; @@ -35,6 +34,8 @@ use vortex_array::session::ArraySession; use vortex_array::test_harness::mark_optimized; use vortex_array::validity::Validity; use vortex_buffer::Buffer; +use vortex_expr::ArrayExprExt; +use vortex_expr::root; use vortex_functions::expr::list_length; use vortex_session::VortexSession; diff --git a/vortex-functions/benches/list_sum.rs b/vortex-functions/benches/list_sum.rs index d4824235f1b..0c89da27d2c 100644 --- a/vortex-functions/benches/list_sum.rs +++ b/vortex-functions/benches/list_sum.rs @@ -26,7 +26,6 @@ use vortex_array::arrays::ListArray; use vortex_array::arrays::ListViewArray; use vortex_array::arrays::PrimitiveArray; use vortex_array::dtype::session::DTypeSession; -use vortex_array::expr::root; use vortex_array::memory::MemorySession; use vortex_array::optimizer::kernels::KernelSession; use vortex_array::scalar_fn::session::ScalarFnSession; @@ -34,6 +33,8 @@ use vortex_array::session::ArraySession; use vortex_array::test_harness::mark_optimized; use vortex_array::validity::Validity; use vortex_buffer::Buffer; +use vortex_expr::ArrayExprExt; +use vortex_expr::root; use vortex_functions::expr::list_sum; use vortex_session::VortexSession; diff --git a/vortex-functions/src/between/find.rs b/vortex-functions/src/between/find.rs index cea8f18dbc5..6e66580ffd6 100644 --- a/vortex-functions/src/between/find.rs +++ b/vortex-functions/src/between/find.rs @@ -5,14 +5,14 @@ use std::ptr; -use vortex_array::expr::BoundExpression; -use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::binary::Binary; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::ScalarFnExprExt; use crate::between::Between; use crate::between::BetweenOptions; @@ -227,18 +227,19 @@ mod tests { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; - use vortex_array::expr::Expression; - use vortex_array::expr::and; - use vortex_array::expr::col; - use vortex_array::expr::gt; - use vortex_array::expr::gt_eq; - use vortex_array::expr::lit; - use vortex_array::expr::lt; - use vortex_array::expr::lt_eq; - use vortex_array::expr::or; use vortex_array::scalar::Scalar; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::and; + use vortex_expr::col; + use vortex_expr::gt; + use vortex_expr::gt_eq; + use vortex_expr::lit; + use vortex_expr::lt; + use vortex_expr::lt_eq; + use vortex_expr::or; use vortex_session::VortexSession; use super::find_between; diff --git a/vortex-functions/src/between/mod.rs b/vortex-functions/src/between/mod.rs index 9f9f77f33fa..64e5fa2f705 100644 --- a/vortex-functions/src/between/mod.rs +++ b/vortex-functions/src/between/mod.rs @@ -29,7 +29,6 @@ use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DType; use vortex_array::dtype::DType::Bool; use vortex_array::dtype::NativePType; -use vortex_array::expr::display::ExprDisplay; use vortex_array::optimizer::kernels::ArrayKernelsExt; use vortex_array::proto::expr as pb; use vortex_array::require_input; @@ -42,6 +41,7 @@ use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::operators::Operator; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_array::scalar_fn::session::ScalarFnSessionExt; use vortex_error::VortexResult; use vortex_error::vortex_bail; @@ -415,17 +415,18 @@ mod tests { use vortex_array::dtype::DecimalDType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::Expression; - use vortex_array::expr::col; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::proto::ExprSerializeProtoExt; - use vortex_array::expr::root; use vortex_array::scalar::DecimalValue; use vortex_array::scalar::Scalar; use vortex_array::test_harness::to_int_indices; use vortex_array::validity::Validity; use vortex_buffer::buffer; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::col; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::proto::ExprSerializeProtoExt; + use vortex_expr::root; use super::*; use crate::expr::between; diff --git a/vortex-functions/src/builtins.rs b/vortex-functions/src/builtins.rs index 6c261a418dc..d6061db21c9 100644 --- a/vortex-functions/src/builtins.rs +++ b/vortex-functions/src/builtins.rs @@ -2,15 +2,15 @@ // SPDX-FileCopyrightText: Copyright the Vortex contributors //! Builder methods for this crate's scalar functions, complementing -//! [`ExprBuiltins`](vortex_array::builtins::ExprBuiltins) and +//! [`ExprBuiltins`](vortex_expr::ExprBuiltins) and //! [`ArrayBuiltins`](vortex_array::builtins::ArrayBuiltins). use vortex_array::ArrayRef; use vortex_array::IntoArray; -use vortex_array::expr::Expression; use vortex_array::scalar_fn::EmptyOptions; -use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_error::VortexResult; +use vortex_expr::Expression; +use vortex_expr::ScalarFnExprExt; use crate::between::Between; use crate::between::BetweenOptions; diff --git a/vortex-functions/src/byte_length.rs b/vortex-functions/src/byte_length.rs index 38c89e4ad80..b1a070638b4 100644 --- a/vortex-functions/src/byte_length.rs +++ b/vortex-functions/src/byte_length.rs @@ -184,9 +184,10 @@ mod tests { use vortex_array::assert_arrays_eq; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::byte_length; diff --git a/vortex-functions/src/case_when.rs b/vortex-functions/src/case_when.rs index f84f8f225cf..711f9a397b4 100644 --- a/vortex-functions/src/case_when.rs +++ b/vortex-functions/src/case_when.rs @@ -28,8 +28,6 @@ use vortex_array::arrays::bool::BoolArrayExt; use vortex_array::builders::builder_with_capacity_in; use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::display::ExprDisplay; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::proto::expr as pb; use vortex_array::scalar::Scalar; @@ -37,13 +35,15 @@ use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; use vortex_array::scalar_fn::EmptyOptions; use vortex_array::scalar_fn::ExecutionArgs; +use vortex_array::scalar_fn::ReduceNode; use vortex_array::scalar_fn::ScalarFnId; +use vortex_array::scalar_fn::ScalarFnRef; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::fill_null::FillNull; use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull; use vortex_array::scalar_fn::builtin::is_null::IsNull; -use vortex_array::scalar_fn::builtin::literal::Literal; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_buffer::BufferMut; use vortex_error::VortexResult; use vortex_error::vortex_bail; @@ -270,11 +270,11 @@ impl ScalarFnVTable for CaseWhen { merge_case_branches(&branches, &remaining, &else_value, &output_dtype, ctx) } - fn simplify( + fn simplify( &self, options: &Self::Options, - expr: &BoundExpression, - ) -> VortexResult> { + node: &T, + ) -> VortexResult> { // Rewrite the COALESCE-shaped CASE WHEN into `fill_null`, which references `x` // once and lowers to a single fill kernel instead of a `zip`/merge that resolves // `x` twice (once for the `is_null` predicate, once for the value branch). @@ -282,45 +282,47 @@ impl ScalarFnVTable for CaseWhen { // CASE WHEN is_null(x) THEN c ELSE x END ==> fill_null(x, c) // CASE WHEN is_not_null(x) THEN x ELSE c END ==> fill_null(x, c) // - // The fill `c` must be a `Literal`: `fill_null`'s kernel reads the fill value via - // `as_constant()`, so a non-constant fill would produce an unexecutable expression. + // The fill `c` must be a constant: `fill_null`'s kernel reads the fill value via + // `as_constant()`, so a non-constant fill would produce an unexecutable node. if options.num_when_then_pairs != 1 || !options.has_else { return Ok(None); } - let when = expr.child(0); - let then = expr.child(1); - let els = expr.child(2); + let when = node.child(0); + let then = node.child(1); + let els = node.child(2); + + let when_is = |f: &dyn Fn(&ScalarFnRef) -> bool| when.scalar_fn().is_some_and(f); // `is_null(x) ? c : x` — predicate operand and ELSE are the same `x`, fill is THEN. - let (x, fill) = if when.is::() && when.child(0) == els { + let (x, fill) = if when_is(&|f| f.is::()) && when.child(0).is_same(&els) { (els, then) // `is_not_null(x) ? x : c` — predicate operand and THEN are the same `x`, fill is ELSE. - } else if when.is::() && when.child(0) == then { + } else if when_is(&|f| f.is::()) && when.child(0).is_same(&then) { (then, els) } else { return Ok(None); }; - let Some(scalar) = fill.as_opt::() else { + let Some(scalar) = fill.as_constant() else { return Ok(None); }; if scalar.is_null() { // Filling the nulls of `x` with NULL is a no-op - return Ok(Some(x.clone())); + return Ok(Some(x)); } - if !FillNull::supports_dtype(x.dtype()) - || x.dtype().as_nullable() != expr.dtype().as_nullable() + let x_dtype = x.node_dtype()?; + if !FillNull::supports_dtype(&x_dtype) + || x_dtype.as_nullable() != node.node_dtype()?.as_nullable() { return Ok(None); } - Ok(Some(FillNull.try_new_bound_expr( - EmptyOptions, - [x.clone(), fill.clone()], - )?)) + Ok(Some( + node.new_node(FillNull.bind(EmptyOptions), &[x, fill])?, + )) } fn is_strict(&self, _options: &Self::Options) -> bool { @@ -495,17 +497,6 @@ mod tests { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; - use vortex_array::expr::Expression; - use vortex_array::expr::col; - use vortex_array::expr::eq; - use vortex_array::expr::get_item; - use vortex_array::expr::gt; - use vortex_array::expr::is_not_null; - use vortex_array::expr::is_null; - use vortex_array::expr::lit; - use vortex_array::expr::proto::ExprSerializeProtoExt; - use vortex_array::expr::root; - use vortex_array::expr::test_harness; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::VecExecutionArgs; @@ -514,6 +505,19 @@ mod tests { use vortex_buffer::Buffer; use vortex_buffer::buffer; use vortex_error::VortexExpect as _; + use vortex_expr::ArrayExprExt; + use vortex_expr::BoundExpression; + use vortex_expr::Expression; + use vortex_expr::col; + use vortex_expr::eq; + use vortex_expr::get_item; + use vortex_expr::gt; + use vortex_expr::is_not_null; + use vortex_expr::is_null; + use vortex_expr::lit; + use vortex_expr::proto::ExprSerializeProtoExt; + use vortex_expr::root; + use vortex_expr::test_harness; use vortex_session::VortexSession; use super::*; diff --git a/vortex-functions/src/dynamic.rs b/vortex-functions/src/dynamic.rs index 2d59db40a9f..9f074ee9863 100644 --- a/vortex-functions/src/dynamic.rs +++ b/vortex-functions/src/dynamic.rs @@ -14,11 +14,6 @@ use vortex_array::ExecutionCtx; use vortex_array::IntoArray; use vortex_array::arrays::ConstantArray; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::display::ExprDisplay; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::NodeVisitor; -use vortex_array::expr::traversal::TraversalOrder; use vortex_array::scalar::Scalar; use vortex_array::scalar::ScalarValue; use vortex_array::scalar_fn::Arity; @@ -31,9 +26,14 @@ use vortex_array::scalar_fn::VecExecutionArgs; use vortex_array::scalar_fn::builtin::binary::Binary; use vortex_array::scalar_fn::builtin::operators::CompareOperator; use vortex_array::scalar_fn::builtin::operators::Operator; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_bail; +use vortex_expr::BoundExpression; +use vortex_expr::traversal::NodeExt; +use vortex_expr::traversal::NodeVisitor; +use vortex_expr::traversal::TraversalOrder; use vortex_session::registry::CachedId; use crate::dynamic_expression::DynamicExpression; @@ -335,9 +335,10 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::root; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::root; use vortex_session::VortexSession; use super::*; diff --git a/vortex-functions/src/dynamic_expression.rs b/vortex-functions/src/dynamic_expression.rs index 969e8f6ab41..0f342daad84 100644 --- a/vortex-functions/src/dynamic_expression.rs +++ b/vortex-functions/src/dynamic_expression.rs @@ -21,15 +21,16 @@ use vortex_array::dtype::FieldName; use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; use vortex_array::expr::BoundExpression; -use vortex_array::expr::display::ExprDisplay; use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; use vortex_array::scalar_fn::ExecutionArgs; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_array::validity::Validity; use vortex_error::VortexResult; use vortex_error::vortex_ensure; +use vortex_expr::ArrayExprExt; use vortex_session::registry::CachedId; /// A source of a predicate that can change while a query runs, such as the build-side bounds of @@ -230,12 +231,12 @@ mod tests { use vortex_array::VortexSessionExecute; use vortex_array::arrays::BoolArray; use vortex_array::assert_arrays_eq; - use vortex_array::expr::col; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::lt; - use vortex_array::expr::root; use vortex_buffer::buffer; + use vortex_expr::col; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::lt; + use vortex_expr::root; use vortex_session::VortexSession; use super::*; diff --git a/vortex-functions/src/expr.rs b/vortex-functions/src/expr.rs index e16223150f1..9dd62fcb891 100644 --- a/vortex-functions/src/expr.rs +++ b/vortex-functions/src/expr.rs @@ -3,7 +3,7 @@ //! Expression constructors for this crate's scalar functions. //! -//! These complement the constructors in `vortex_array::expr`. +//! These complement the constructors in `vortex_expr`. use std::sync::Arc; @@ -11,18 +11,18 @@ use vortex_array::aggregate_fn::NumericalAggregateOpts; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::expr::BoundExpression; -use vortex_array::expr::Expression; -use vortex_array::expr::Lambda; -use vortex_array::expr::lit; use vortex_array::scalar::Scalar; use vortex_array::scalar::ScalarValue; use vortex_array::scalar_fn::EmptyOptions; -use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::operators::CompareOperator; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_ensure; use vortex_error::vortex_panic; +use vortex_expr::Expression; +use vortex_expr::Lambda; +use vortex_expr::ScalarFnExprExt; +use vortex_expr::lit; use crate::between::Between; use crate::between::BetweenOptions; @@ -178,7 +178,7 @@ pub fn bound_nested_case_when( /// ```rust /// # use vortex_array::dtype::Nullability; /// # use vortex_functions::expr::merge; -/// # use vortex_array::expr::{get_item, root}; +/// # use vortex_expr::{get_item, root}; /// let expr = merge([get_item("a", root()), get_item("b", root())]); /// ``` pub fn merge(elements: impl IntoIterator>) -> Expression { @@ -217,7 +217,7 @@ pub fn bound_merge_opts( /// /// ```rust /// # use vortex_functions::expr::byte_length; -/// # use vortex_array::expr::root; +/// # use vortex_expr::root; /// let expr = byte_length(root()); /// ``` pub fn byte_length(input: Expression) -> Expression { @@ -235,7 +235,7 @@ pub fn bound_byte_length(input: BoundExpression) -> BoundExpression { /// /// ```rust /// # use vortex_functions::expr::lower; -/// # use vortex_array::expr::root; +/// # use vortex_expr::root; /// let expr = lower(root()); /// ``` pub fn lower(input: Expression) -> Expression { @@ -247,7 +247,7 @@ pub fn lower(input: Expression) -> Expression { /// /// ```rust /// # use vortex_functions::expr::strpos; -/// # use vortex_array::expr::{lit, root}; +/// # use vortex_expr::{lit, root}; /// let expr = strpos(root(), lit("needle")); /// ``` pub fn strpos(input: Expression, search: Expression) -> Expression { @@ -258,7 +258,7 @@ pub fn strpos(input: Expression, search: Expression) -> Expression { /// /// ```rust /// # use vortex_functions::expr::ext_storage; -/// # use vortex_array::expr::root; +/// # use vortex_expr::root; /// let expr = ext_storage(root()); /// ``` pub fn ext_storage(input: Expression) -> Expression { @@ -278,7 +278,7 @@ pub fn bound_ext_storage(input: BoundExpression) -> BoundExpression { /// /// ```rust /// # use vortex_functions::expr::list_length; -/// # use vortex_array::expr::root; +/// # use vortex_expr::root; /// let expr = list_length(root()); /// ``` pub fn list_length(input: Expression) -> Expression { @@ -302,7 +302,7 @@ pub fn bound_list_length(input: BoundExpression) -> BoundExpression { /// /// ```rust /// # use vortex_functions::expr::list_sum; -/// # use vortex_array::expr::root; +/// # use vortex_expr::root; /// let expr = list_sum(root()); /// ``` pub fn list_sum(input: Expression) -> Expression { @@ -426,7 +426,7 @@ pub fn bound_dynamic( /// ```rust /// # use vortex_functions::between::BetweenOptions; /// # use vortex_functions::between::StrictComparison; -/// # use vortex_array::expr::{lit, root}; +/// # use vortex_expr::{lit, root}; /// # use vortex_functions::expr::between; /// let opts = BetweenOptions { /// lower_strict: StrictComparison::NonStrict, @@ -465,7 +465,7 @@ pub fn bound_between( /// SQL's three-valued `IN`, where a null element makes that `null`, use [`in_list`]. /// /// ```rust -/// # use vortex_array::expr::{lit, root}; +/// # use vortex_expr::{lit, root}; /// # use vortex_functions::expr::list_contains; /// let expr = list_contains(root(), lit(42)); /// ``` @@ -488,7 +488,7 @@ pub fn list_contains_opts( /// `not(in_list(..))` never admits such a row. /// /// ```rust -/// # use vortex_array::expr::{lit, root}; +/// # use vortex_expr::{lit, root}; /// # use vortex_functions::expr::in_list; /// let expr = in_list(root(), lit(vec![1, 2, 3])); /// ``` @@ -518,7 +518,7 @@ pub fn in_list(needle: Expression, list: Expression) -> Expression { /// /// ```rust /// # use vortex_array::dtype::{DType, Nullability, PType}; -/// # use vortex_array::expr::root; +/// # use vortex_expr::root; /// # use vortex_functions::expr::in_list_of; /// let dtype = DType::Primitive(PType::I32, Nullability::NonNullable); /// let expr = in_list_of(root(), dtype, vec![3i32.into(), 1i32.into(), 2i32.into()])?; @@ -734,7 +734,7 @@ fn bound_like_with_options( /// follows the input. Only primitive float inputs are supported. /// /// ```rust -/// # use vortex_array::expr::root; +/// # use vortex_expr::root; /// # use vortex_functions::expr::is_nan; /// let expr = is_nan(root()); /// ``` @@ -754,7 +754,7 @@ pub fn bound_is_nan(child: BoundExpression) -> BoundExpression { /// Creates a zip expression that conditionally selects between two arrays. /// /// ```rust -/// # use vortex_array::expr::{lit, root}; +/// # use vortex_expr::{lit, root}; /// # use vortex_functions::expr::zip_expr; /// let expr = zip_expr(lit(true), root(), lit(0i32)); /// ``` @@ -804,7 +804,7 @@ pub fn bound_dynamic_expression( /// The lambda takes one parameter, the element. /// /// ```rust -/// # use vortex_array::expr::{Lambda, checked_add, col, lit, var}; +/// # use vortex_expr::{Lambda, checked_add, col, lit, var}; /// # use vortex_functions::expr::list_transform; /// let lambda = Lambda::new(["x"], checked_add(var("x"), lit(1_i32))).unwrap(); /// let expr = list_transform(col("l"), lambda); diff --git a/vortex-functions/src/ext_storage.rs b/vortex-functions/src/ext_storage.rs index 770362b181d..6fea4f4d240 100644 --- a/vortex-functions/src/ext_storage.rs +++ b/vortex-functions/src/ext_storage.rs @@ -118,15 +118,16 @@ mod tests { use vortex_array::dtype::PType; use vortex_array::dtype::extension::ExtDTypeRef; use vortex_array::dtype::extension::ForeignExtDType; - use vortex_array::expr::get_item; - use vortex_array::expr::root; - use vortex_array::expr::select; use vortex_array::extension::datetime::TimeUnit; use vortex_array::extension::datetime::Timestamp; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::scalar::Scalar; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::get_item; + use vortex_expr::root; + use vortex_expr::select; use vortex_session::VortexSession; use vortex_session::registry::CachedId; diff --git a/vortex-functions/src/is_nan.rs b/vortex-functions/src/is_nan.rs index acba4b462f5..7e6672e648a 100644 --- a/vortex-functions/src/is_nan.rs +++ b/vortex-functions/src/is_nan.rs @@ -55,12 +55,13 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_array::validity::Validity; use vortex_buffer::buffer; + use vortex_expr::ArrayExprExt; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::is_nan; diff --git a/vortex-functions/src/like/mod.rs b/vortex-functions/src/like/mod.rs index 91e852ccfee..ca498720fed 100644 --- a/vortex-functions/src/like/mod.rs +++ b/vortex-functions/src/like/mod.rs @@ -25,7 +25,6 @@ use vortex_array::arrays::varbinview::BinaryView; use vortex_array::arrays::varbinview::ResolvedViews; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; -use vortex_array::expr::display::ExprDisplay; use vortex_array::optimizer::kernels::ArrayKernelsExt; use vortex_array::proto::expr as pb; use vortex_array::require_input; @@ -37,6 +36,7 @@ use vortex_array::scalar_fn::ScalarFnExecutionResult; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_array::scalar_fn::session::ScalarFnSessionExt; use vortex_buffer::BitBuffer; use vortex_buffer::BitBufferMut; @@ -465,10 +465,10 @@ mod tests { use vortex_array::assert_arrays_eq; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::like; diff --git a/vortex-functions/src/list_contains/mod.rs b/vortex-functions/src/list_contains/mod.rs index 9a8bd522095..5c37d2b0e91 100644 --- a/vortex-functions/src/list_contains/mod.rs +++ b/vortex-functions/src/list_contains/mod.rs @@ -870,12 +870,6 @@ mod tests { use vortex_array::dtype::PType; use vortex_array::dtype::PType::I32; use vortex_array::dtype::StructFields; - use vortex_array::expr::Expression; - use vortex_array::expr::bound; - use vortex_array::expr::col; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::scalar::PValue; use vortex_array::scalar::Scalar; @@ -886,6 +880,13 @@ mod tests { use vortex_buffer::buffer; use vortex_error::VortexExpect; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::bound; + use vortex_expr::col; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::root; use vortex_session::VortexSession; use super::process_matches; @@ -1582,7 +1583,7 @@ mod tests { [None, Some(true), None, Some(true)], )?; assert_result( - needles.apply(&vortex_array::expr::not(in_list(root(), lit(set)))), + needles.apply(&vortex_expr::not(in_list(root(), lit(set)))), [None, Some(false), None, Some(false)], ) } diff --git a/vortex-functions/src/list_contains/prepared/function.rs b/vortex-functions/src/list_contains/prepared/function.rs index 332de9780db..00c31fbb273 100644 --- a/vortex-functions/src/list_contains/prepared/function.rs +++ b/vortex-functions/src/list_contains/prepared/function.rs @@ -11,7 +11,6 @@ use vortex_array::arrays::ConstantArray; use vortex_array::arrays::ScalarFn; use vortex_array::arrays::ScalarFnArray; use vortex_array::dtype::DType; -use vortex_array::expr::display::ExprDisplay; use vortex_array::proto::scalar as pb; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::Arity; @@ -22,6 +21,7 @@ use vortex_array::scalar_fn::ReduceNodeValidity; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_session::VortexSession; diff --git a/vortex-functions/src/list_contains/prepared/tests.rs b/vortex-functions/src/list_contains/prepared/tests.rs index da2f6d3434e..cd241c81284 100644 --- a/vortex-functions/src/list_contains/prepared/tests.rs +++ b/vortex-functions/src/list_contains/prepared/tests.rs @@ -37,8 +37,6 @@ use vortex_array::dtype::extension::ExtDType; use vortex_array::dtype::extension::ExtId; use vortex_array::dtype::extension::ExtVTable; use vortex_array::dtype::i256; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::scalar::Scalar; use vortex_array::scalar::ScalarValue; @@ -49,6 +47,9 @@ use vortex_buffer::BitBuffer; use vortex_buffer::buffer; use vortex_error::VortexResult; use vortex_error::vortex_bail; +use vortex_expr::ArrayExprExt; +use vortex_expr::lit; +use vortex_expr::root; use vortex_mask::Mask; use vortex_session::VortexSession; diff --git a/vortex-functions/src/list_length.rs b/vortex-functions/src/list_length.rs index d8f134623c1..2960e62ef73 100644 --- a/vortex-functions/src/list_length.rs +++ b/vortex-functions/src/list_length.rs @@ -201,12 +201,13 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::cast; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::cast; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::list_length; diff --git a/vortex-functions/src/list_sum.rs b/vortex-functions/src/list_sum.rs index 738039167a3..f2aca539e53 100644 --- a/vortex-functions/src/list_sum.rs +++ b/vortex-functions/src/list_sum.rs @@ -159,15 +159,16 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::Expression; - use vortex_array::expr::proto::ExprSerializeProtoExt; - use vortex_array::expr::root; use vortex_array::proto::expr as pb; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::proto::ExprSerializeProtoExt; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::list_sum; diff --git a/vortex-functions/src/list_transform/mod.rs b/vortex-functions/src/list_transform/mod.rs index f2ebee5d69a..11ad4c66268 100644 --- a/vortex-functions/src/list_transform/mod.rs +++ b/vortex-functions/src/list_transform/mod.rs @@ -23,18 +23,18 @@ use vortex_array::arrays::listview::ListViewArraySlotsExt; use vortex_array::arrays::listview::ListViewRebuildMode; use vortex_array::dtype::DType; use vortex_array::dtype::FieldName; -use vortex_array::expr::BoundLambda; -use vortex_array::expr::Lambda; -use vortex_array::expr::display::ExprDisplay; use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; use vortex_array::scalar_fn::ExecutionArgs; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_ensure; use vortex_error::vortex_ensure_eq; +use vortex_expr::BoundLambda; +use vortex_expr::Lambda; use vortex_session::registry::CachedId; /// Apply a lambda to every element of each list, keeping each list's length and validity. @@ -48,7 +48,7 @@ pub struct ListTransform; /// The lambda of a [`ListTransform`], as written or bound to the element's dtype. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum ListTransformOptions { - /// The lambda as written, in an [`Expression`](vortex_array::expr::Expression). + /// The lambda as written, in an [`Expression`](vortex_expr::Expression). Lambda(Lambda), /// The lambda bound to the element's dtype, in a /// [`BoundExpression`](vortex_array::expr::BoundExpression) or an array. diff --git a/vortex-functions/src/list_transform/tests.rs b/vortex-functions/src/list_transform/tests.rs index f37d662a7a4..f8b3bb5047a 100644 --- a/vortex-functions/src/list_transform/tests.rs +++ b/vortex-functions/src/list_transform/tests.rs @@ -26,17 +26,6 @@ use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::expr::BoundExpression; -use vortex_array::expr::Expression; -use vortex_array::expr::Lambda; -use vortex_array::expr::checked_add; -use vortex_array::expr::col; -use vortex_array::expr::eq; -use vortex_array::expr::get_item; -use vortex_array::expr::is_not_null; -use vortex_array::expr::lit; -use vortex_array::expr::proto::ExprSerializeProtoExt; -use vortex_array::expr::root; -use vortex_array::expr::var; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::ScalarFnVTable; @@ -44,7 +33,19 @@ use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::ArrayExprExt; +use vortex_expr::Expression; +use vortex_expr::Lambda; +use vortex_expr::checked_add; +use vortex_expr::col; +use vortex_expr::eq; +use vortex_expr::get_item; +use vortex_expr::is_not_null; +use vortex_expr::lit; +use vortex_expr::proto::ExprSerializeProtoExt; +use vortex_expr::root; use vortex_expr::transform::replace; +use vortex_expr::var; use vortex_session::VortexSession; use super::ListTransform; diff --git a/vortex-functions/src/lower.rs b/vortex-functions/src/lower.rs index 40b038e289c..53c3545853b 100644 --- a/vortex-functions/src/lower.rs +++ b/vortex-functions/src/lower.rs @@ -21,10 +21,10 @@ use vortex_array::scalar_fn::ExecutionArgs; use vortex_array::scalar_fn::ScalarFnExecutionResult; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; -use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::ScalarFnExprExt; use vortex_session::VortexSession; use vortex_session::registry::CachedId; @@ -193,11 +193,12 @@ mod tests { use vortex_array::arrays::RunEnd; use vortex_array::arrays::VarBinViewArray; use vortex_array::assert_arrays_eq; - use vortex_array::expr::Expression; - use vortex_array::expr::proto::ExprSerializeProtoExt; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::proto::ExprSerializeProtoExt; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::lower; diff --git a/vortex-functions/src/merge.rs b/vortex-functions/src/merge.rs index f45a217e883..90468fc2410 100644 --- a/vortex-functions/src/merge.rs +++ b/vortex-functions/src/merge.rs @@ -269,18 +269,19 @@ mod tests { use vortex_array::dtype::PType::U32; use vortex_array::dtype::PType::U64; use vortex_array::dtype::StructFields; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::Expression; - use vortex_array::expr::col; - use vortex_array::expr::get_item; - use vortex_array::expr::pack; - use vortex_array::expr::root; use vortex_array::scalar_fn::builtin::pack::Pack; use vortex_buffer::buffer; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_bail; + use vortex_expr::ArrayExprExt; + use vortex_expr::BoundExpression; + use vortex_expr::Expression; use vortex_expr::analysis::make_bound_free_field_annotator; + use vortex_expr::col; + use vortex_expr::get_item; + use vortex_expr::pack; + use vortex_expr::root; use vortex_expr::transform::partition_bound; use vortex_session::VortexSession; diff --git a/vortex-functions/src/pruning/dynamic_expression.rs b/vortex-functions/src/pruning/dynamic_expression.rs index b6f722a5a13..a437011c357 100644 --- a/vortex-functions/src/pruning/dynamic_expression.rs +++ b/vortex-functions/src/pruning/dynamic_expression.rs @@ -19,16 +19,16 @@ use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; use vortex_array::dtype::StructFields; use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::get_item; -use vortex_array::expr::bound::lit; use vortex_array::expr::stats::Stat; use vortex_array::scalar_fn::EmptyOptions; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; -use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::get_item::GetItem; -use vortex_array::scalar_fn::internal::row_count::RowCount; use vortex_error::VortexResult; +use vortex_expr::ScalarFnExprExt; +use vortex_expr::bound::get_item; +use vortex_expr::bound::lit; +use vortex_expr::lambda::row_count::RowCount; use vortex_expr::stats::bound::stat; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_expr::stats::rewrite::StatsRewriteRule; @@ -242,15 +242,16 @@ mod tests { use vortex_array::arrays::StructArray; use vortex_array::assert_arrays_eq; use vortex_array::dtype::PType; - use vortex_array::expr::col; - use vortex_array::expr::get_item as unbound_get_item; - use vortex_array::expr::lit as unbound_lit; - use vortex_array::expr::lt; - use vortex_array::expr::pack; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_array::validity::Validity; use vortex_buffer::buffer; + use vortex_expr::ArrayExprExt; + use vortex_expr::col; + use vortex_expr::get_item as unbound_get_item; + use vortex_expr::lit as unbound_lit; + use vortex_expr::lt; + use vortex_expr::pack; + use vortex_expr::root; use vortex_expr::stats::StatsSession; use super::*; diff --git a/vortex-functions/src/pruning/mod.rs b/vortex-functions/src/pruning/mod.rs index fde2f324a7b..ce5ab4a6b4a 100644 --- a/vortex-functions/src/pruning/mod.rs +++ b/vortex-functions/src/pruning/mod.rs @@ -5,16 +5,6 @@ use std::cmp::Ordering; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::and; -use vortex_array::expr::bound::and_collect; -use vortex_array::expr::bound::binary; -use vortex_array::expr::bound::eq; -use vortex_array::expr::bound::gt; -use vortex_array::expr::bound::gt_eq; -use vortex_array::expr::bound::lit; -use vortex_array::expr::bound::lt; -use vortex_array::expr::bound::or; use vortex_array::expr::stats::Stat; use vortex_array::scalar::Scalar; use vortex_array::scalar::StringLike; @@ -23,6 +13,16 @@ use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::scalar_fn::builtin::operators::CompareOperator; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::bound::and; +use vortex_expr::bound::and_collect; +use vortex_expr::bound::binary; +use vortex_expr::bound::eq; +use vortex_expr::bound::gt; +use vortex_expr::bound::gt_eq; +use vortex_expr::bound::lit; +use vortex_expr::bound::lt; +use vortex_expr::bound::or; use vortex_expr::stats::rewrite::StatsRewriteRule; use vortex_expr::stats::rewrite::falsify; use vortex_expr::stats::rewrite::helpers::*; @@ -374,24 +374,24 @@ mod tests { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::Expression; - use vortex_array::expr::and; - use vortex_array::expr::col; - use vortex_array::expr::eq; - use vortex_array::expr::get_item; - use vortex_array::expr::gt; - use vortex_array::expr::gt_eq; - use vortex_array::expr::lit; - use vortex_array::expr::lt; - use vortex_array::expr::or; use vortex_array::expr::stats::Stat; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::EmptyOptions; - use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::operators::CompareOperator; - use vortex_array::scalar_fn::internal::row_count::RowCount; use vortex_error::VortexResult; + use vortex_expr::BoundExpression; + use vortex_expr::Expression; + use vortex_expr::ScalarFnExprExt; + use vortex_expr::and; + use vortex_expr::col; + use vortex_expr::eq; + use vortex_expr::get_item; + use vortex_expr::gt; + use vortex_expr::gt_eq; + use vortex_expr::lambda::row_count::RowCount; + use vortex_expr::lit; + use vortex_expr::lt; + use vortex_expr::or; use vortex_expr::stats::expr::StatFn; use vortex_expr::stats::expr::StatOptions; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; diff --git a/vortex-functions/src/replace_time_zone.rs b/vortex-functions/src/replace_time_zone.rs index 27b29e2e188..bfbc4424471 100644 --- a/vortex-functions/src/replace_time_zone.rs +++ b/vortex-functions/src/replace_time_zone.rs @@ -21,8 +21,6 @@ use vortex_array::arrays::extension::ExtensionArrayExt; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound; use vortex_array::extension::datetime::TimeUnit; use vortex_array::extension::datetime::Timestamp; use vortex_array::extension::datetime::TimestampOptions; @@ -32,9 +30,9 @@ use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; use vortex_array::scalar_fn::ExecutionArgs; +use vortex_array::scalar_fn::ReduceNode; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; -use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::validity::Validity; use vortex_buffer::BitBufferMut; use vortex_buffer::BufferMut; @@ -112,20 +110,20 @@ impl ScalarFnVTable for ReplaceTimeZone { } } - fn simplify( + fn simplify( &self, options: &Self::Options, - expr: &BoundExpression, - ) -> VortexResult> { - let (Some(value), Some(policy)) = ( - expr.child(0).as_opt::(), - expr.child(1).as_opt::(), - ) else { + node: &T, + ) -> VortexResult> { + let (value, policy) = (node.child(0), node.child(1)); + let (Some(value), Some(policy)) = (value.as_constant(), policy.as_constant()) else { return Ok(None); }; - // Fold to a literal so stats pruning sees a bare literal operand. A failing replacement + // Fold to a constant so stats pruning sees a bare literal operand. A failing replacement // (e.g. a raised ambiguity) is left in place so the error surfaces at execution time. - Ok(replace_scalar(options, value, policy).ok().map(bound::lit)) + Ok(replace_scalar(options, value, policy) + .ok() + .map(|scalar| node.new_constant(scalar))) } fn return_dtype(&self, options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult { @@ -333,9 +331,6 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::StructFields; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::extension::datetime::TimeUnit; use vortex_array::extension::datetime::Timestamp; use vortex_array::scalar::Scalar; @@ -343,6 +338,10 @@ mod tests { use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_error::VortexResult; use vortex_error::vortex_err; + use vortex_expr::ArrayExprExt; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::root; use vortex_session::VortexSession; use super::ReplaceTimeZone; diff --git a/vortex-functions/src/strpos.rs b/vortex-functions/src/strpos.rs index 293f7fd2a25..232d52e568a 100644 --- a/vortex-functions/src/strpos.rs +++ b/vortex-functions/src/strpos.rs @@ -166,12 +166,13 @@ mod tests { use vortex_array::arrays::StructArray; use vortex_array::arrays::VarBinViewArray; use vortex_array::assert_arrays_eq; - use vortex_array::expr::Expression; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::proto::ExprSerializeProtoExt; - use vortex_array::expr::root; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::proto::ExprSerializeProtoExt; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::strpos; diff --git a/vortex-functions/src/tests.rs b/vortex-functions/src/tests.rs index 30ed7dd5b3d..c6fdffce363 100644 --- a/vortex-functions/src/tests.rs +++ b/vortex-functions/src/tests.rs @@ -9,27 +9,27 @@ use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; -use vortex_array::expr::Expression; -use vortex_array::expr::and; -use vortex_array::expr::bound; -use vortex_array::expr::cast; -use vortex_array::expr::col; -use vortex_array::expr::eq; -use vortex_array::expr::get_item; -use vortex_array::expr::gt; -use vortex_array::expr::gt_eq; -use vortex_array::expr::lit; -use vortex_array::expr::lt; -use vortex_array::expr::lt_eq; -use vortex_array::expr::not; -use vortex_array::expr::not_eq; -use vortex_array::expr::or; -use vortex_array::expr::pack; -use vortex_array::expr::root; -use vortex_array::expr::select; -use vortex_array::expr::select_exclude; use vortex_array::scalar::Scalar; use vortex_error::VortexResult; +use vortex_expr::Expression; +use vortex_expr::and; +use vortex_expr::bound; +use vortex_expr::cast; +use vortex_expr::col; +use vortex_expr::eq; +use vortex_expr::get_item; +use vortex_expr::gt; +use vortex_expr::gt_eq; +use vortex_expr::lit; +use vortex_expr::lt; +use vortex_expr::lt_eq; +use vortex_expr::not; +use vortex_expr::not_eq; +use vortex_expr::or; +use vortex_expr::pack; +use vortex_expr::root; +use vortex_expr::select; +use vortex_expr::select_exclude; use crate::between::BetweenOptions; use crate::between::StrictComparison; diff --git a/vortex-functions/src/variant_get/mod.rs b/vortex-functions/src/variant_get/mod.rs index b49f0877ae8..ed41f6cda71 100644 --- a/vortex-functions/src/variant_get/mod.rs +++ b/vortex-functions/src/variant_get/mod.rs @@ -19,7 +19,6 @@ use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DType; use vortex_array::dtype::FieldName; use vortex_array::dtype::Nullability; -use vortex_array::expr::display::ExprDisplay; use vortex_array::proto::expr as pb; use vortex_array::proto::expr::variant_path_element; use vortex_array::scalar::Scalar; @@ -30,6 +29,7 @@ use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::mask::Mask; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_array::scalar_fn::session::ScalarFnSessionExt; use vortex_error::VortexResult; use vortex_error::vortex_ensure; @@ -444,9 +444,6 @@ mod tests { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; - use vortex_array::expr::Expression; - use vortex_array::expr::proto::ExprSerializeProtoExt; - use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::scalar::Scalar; use vortex_array::scalar::ScalarValue; @@ -455,6 +452,10 @@ mod tests { use vortex_error::vortex_bail; use vortex_error::vortex_ensure; use vortex_error::vortex_err; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::proto::ExprSerializeProtoExt; + use vortex_expr::root; use vortex_session::VortexSession; use crate::expr::variant_get; diff --git a/vortex-functions/src/zip/mod.rs b/vortex-functions/src/zip/mod.rs index ba0c9c53862..068fef92cb1 100644 --- a/vortex-functions/src/zip/mod.rs +++ b/vortex-functions/src/zip/mod.rs @@ -45,19 +45,18 @@ use vortex_array::builders::builder_with_capacity_in; use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DType; use vortex_array::dtype::StructFields; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound; -use vortex_array::expr::display::ExprDisplay; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::optimizer::kernels::ArrayKernelsExt; use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; use vortex_array::scalar_fn::EmptyOptions; use vortex_array::scalar_fn::ExecutionArgs; +use vortex_array::scalar_fn::ReduceNode; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::ScalarFnVTableExt; -use vortex_array::scalar_fn::builtin::literal::Literal; +use vortex_array::scalar_fn::builtin::cast::Cast; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_array::scalar_fn::session::ScalarFnSessionExt; use vortex_array::validity::Validity; use vortex_error::VortexResult; @@ -212,27 +211,29 @@ impl ScalarFnVTable for Zip { zip_impl(&if_true, &if_false, &mask, ctx) } - fn simplify( + fn simplify( &self, _options: &Self::Options, - expr: &BoundExpression, - ) -> VortexResult> { - let Some(mask_lit) = expr.child(2).as_opt::() else { + node: &T, + ) -> VortexResult> { + let mask = node.child(2); + let Some(mask_constant) = mask.as_constant() else { return Ok(None); }; - if let Some(mask_val) = mask_lit.as_bool().value() { - let selected = if mask_val { - expr.child(0).clone() + if let Some(mask_value) = mask_constant.as_bool().value() { + let selected = if mask_value { + node.child(0) } else { - expr.child(1).clone() + node.child(1) }; // The selected branch can be less nullable than the zip result, which unions // both branches' nullability, so keep the dtype that binding inferred. - return Ok(Some(if selected.dtype() == expr.dtype() { + let dtype = node.node_dtype()?; + return Ok(Some(if selected.node_dtype()? == dtype { selected } else { - bound::cast(selected, expr.dtype().clone()) + selected.new_node(Cast.bind(dtype), std::slice::from_ref(&selected))? })); } @@ -445,12 +446,12 @@ mod tests { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::UnionVariants; - use vortex_array::expr::Expression; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::Expression; + use vortex_expr::lit; + use vortex_expr::root; use vortex_mask::Mask; use vortex_session::VortexSession; diff --git a/vortex-jni/src/expression.rs b/vortex-jni/src/expression.rs index b4c0cf9fa4f..e1c12e728a4 100644 --- a/vortex-jni/src/expression.rs +++ b/vortex-jni/src/expression.rs @@ -47,6 +47,7 @@ use vortex::encodings::uuid::UuidMetadata; use vortex::error::VortexResult; use vortex::error::vortex_err; use vortex::expr::Expression; +use vortex::expr::ScalarFnExprExt; use vortex::expr::and_collect; use vortex::expr::between; use vortex::expr::get_item; @@ -76,7 +77,6 @@ use vortex::scalar::DecimalValue; use vortex::scalar::Scalar; use vortex::scalar::ScalarValue; use vortex::scalar_fn::EmptyOptions; -use vortex::scalar_fn::ScalarFnVTableExt; use vortex::scalar_fn::builtin::binary::Binary; use vortex::scalar_fn::builtin::literal::Literal; use vortex::scalar_fn::builtin::operators::Operator; diff --git a/vortex-json/Cargo.toml b/vortex-json/Cargo.toml index 38d383ac9b0..4e64137133b 100644 --- a/vortex-json/Cargo.toml +++ b/vortex-json/Cargo.toml @@ -24,6 +24,7 @@ vortex-array = { workspace = true, default-features = false } vortex-arrow = { workspace = true } vortex-edition = { workspace = true } vortex-error = { workspace = true, default-features = false } +vortex-expr = { workspace = true } vortex-functions = { workspace = true } vortex-session = { workspace = true } diff --git a/vortex-json/src/json_to_variant.rs b/vortex-json/src/json_to_variant.rs index 634c1c333ba..911af87c2d1 100644 --- a/vortex-json/src/json_to_variant.rs +++ b/vortex-json/src/json_to_variant.rs @@ -15,7 +15,6 @@ use vortex_array::arrays::Extension; use vortex_array::arrays::ExtensionArray; use vortex_array::arrays::ScalarFnArray; use vortex_array::dtype::DType; -use vortex_array::expr::Expression; use vortex_array::proto::expr as pb; use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; @@ -27,6 +26,8 @@ use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_ensure; use vortex_error::vortex_err; +use vortex_expr::Expression; +use vortex_expr::ScalarFnExprExt; use vortex_functions::variant_get::VariantPath; use vortex_functions::variant_get::VariantPathElement; use vortex_session::VortexSession; @@ -314,11 +315,11 @@ mod tests { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::session::DTypeSession; - use vortex_array::expr::proto::ExprSerializeProtoExt; - use vortex_array::expr::root; use vortex_array::scalar_fn::session::ScalarFnSession; use vortex_array::scalar_fn::session::ScalarFnSessionExt; use vortex_array::session::ArraySession; + use vortex_expr::proto::ExprSerializeProtoExt; + use vortex_expr::root; use super::*; diff --git a/vortex-layout/benches/zone_map_prune.rs b/vortex-layout/benches/zone_map_prune.rs index 8291c6e6f20..86d39988b5c 100644 --- a/vortex-layout/benches/zone_map_prune.rs +++ b/vortex-layout/benches/zone_map_prune.rs @@ -29,19 +29,19 @@ use vortex_array::arrays::StructArray; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::Expression; -use vortex_array::expr::eq; -use vortex_array::expr::gt; -use vortex_array::expr::is_not_null; -use vortex_array::expr::lit; -use vortex_array::expr::or; -use vortex_array::expr::root; use vortex_array::memory::MemorySession; use vortex_array::optimizer::kernels::KernelSession; use vortex_array::test_harness::mark_optimized; use vortex_array::validity::Validity; use vortex_buffer::Buffer; +use vortex_expr::BoundExpression; +use vortex_expr::Expression; +use vortex_expr::eq; +use vortex_expr::gt; +use vortex_expr::is_not_null; +use vortex_expr::lit; +use vortex_expr::or; +use vortex_expr::root; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_expr::stats::session::StatsSession; use vortex_layout::layouts::zoned::zone_map::ZoneMap; diff --git a/vortex-layout/examples/reverse_index/main.rs b/vortex-layout/examples/reverse_index/main.rs index 1ebf65f1455..0f9160051fe 100644 --- a/vortex-layout/examples/reverse_index/main.rs +++ b/vortex-layout/examples/reverse_index/main.rs @@ -38,10 +38,6 @@ use vortex_array::dtype::FieldNames; use vortex_array::dtype::FieldPath; use vortex_array::dtype::Nullability::NonNullable; use vortex_array::dtype::StructFields; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::col; -use vortex_array::expr::eq; -use vortex_array::expr::lit; use vortex_array::memory::BufferAllocatorRef; use vortex_array::memory::MemorySessionExt; use vortex_array::scalar::Scalar; @@ -63,6 +59,10 @@ use vortex_edition::EditionSessionExt; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::BoundExpression; +use vortex_expr::col; +use vortex_expr::eq; +use vortex_expr::lit; use vortex_file::OpenOptionsSessionExt; use vortex_file::WriteOptionsSessionExt; use vortex_file::WriteStrategyBuilder; diff --git a/vortex-layout/examples/reverse_index/pruning_tests.rs b/vortex-layout/examples/reverse_index/pruning_tests.rs index 26a95e3d2c5..146803245b8 100644 --- a/vortex-layout/examples/reverse_index/pruning_tests.rs +++ b/vortex-layout/examples/reverse_index/pruning_tests.rs @@ -23,11 +23,6 @@ use vortex_array::arrays::PrimitiveArray; use vortex_array::arrays::StructArray; use vortex_array::arrays::struct_::StructArrayExt; use vortex_array::dtype::FieldPath; -use vortex_array::expr::col; -use vortex_array::expr::eq; -use vortex_array::expr::lit; -use vortex_array::expr::root; -use vortex_array::expr::select; use vortex_array::stream::ArrayStreamExt; use vortex_arrow::ArrowSessionExt; use vortex_edition::Edition; @@ -36,6 +31,11 @@ use vortex_edition::EditionId; use vortex_edition::EditionMember; use vortex_edition::EditionSession; use vortex_edition::EditionSessionExt; +use vortex_expr::col; +use vortex_expr::eq; +use vortex_expr::lit; +use vortex_expr::root; +use vortex_expr::select; use vortex_file::OpenOptionsSessionExt; use vortex_file::WriteOptionsSessionExt; use vortex_file::WriteStrategyBuilder; diff --git a/vortex-layout/examples/reverse_index/tests.rs b/vortex-layout/examples/reverse_index/tests.rs index 13e2e43f341..9f12a738652 100644 --- a/vortex-layout/examples/reverse_index/tests.rs +++ b/vortex-layout/examples/reverse_index/tests.rs @@ -22,10 +22,6 @@ use vortex_array::dtype::DType; use vortex_array::dtype::FieldPath; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::col; -use vortex_array::expr::eq; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::stream::ArrayStreamExt; use vortex_buffer::ByteBuffer; use vortex_buffer::ByteBufferMut; @@ -37,6 +33,10 @@ use vortex_edition::EditionSession; use vortex_edition::EditionSessionExt; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::col; +use vortex_expr::eq; +use vortex_expr::lit; +use vortex_expr::root; use vortex_file::OpenOptionsSessionExt; use vortex_file::VortexFile; use vortex_file::WriteOptionsSessionExt; diff --git a/vortex-layout/src/layouts/chunked/reader.rs b/vortex-layout/src/layouts/chunked/reader.rs index c658a83bb46..96a639886d6 100644 --- a/vortex-layout/src/layouts/chunked/reader.rs +++ b/vortex-layout/src/layouts/chunked/reader.rs @@ -20,11 +20,11 @@ use vortex_array::MaskFuture; use vortex_array::arrays::ChunkedArray; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::BoundExpression; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_ensure; use vortex_error::vortex_panic; +use vortex_expr::BoundExpression; use vortex_mask::Mask; use vortex_session::VortexSession; @@ -453,8 +453,8 @@ mod test { use vortex_array::dtype::FieldMask; use vortex_array::dtype::Nullability::NonNullable; use vortex_array::dtype::PType; - use vortex_array::expr::root; use vortex_buffer::buffer; + use vortex_expr::root; use vortex_io::runtime::single::block_on; use vortex_io::session::RuntimeSessionExt; use vortex_mask::Mask; diff --git a/vortex-layout/src/layouts/dict/reader.rs b/vortex-layout/src/layouts/dict/reader.rs index 6a653f30d1e..609e79429ac 100644 --- a/vortex-layout/src/layouts/dict/reader.rs +++ b/vortex-layout/src/layouts/dict/reader.rs @@ -19,16 +19,17 @@ use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; use vortex_array::dtype::Nullability; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::ExactBoundExpr; -use vortex_array::expr::bound::pack as bound_pack; -use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_error::VortexError; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; +use vortex_expr::ExactBoundExpr; +use vortex_expr::bound::pack as bound_pack; use vortex_expr::direct_bound_annotations; use vortex_expr::label_bound_tree; +use vortex_expr::root; use vortex_expr::transform::partition_bound_annotations; use vortex_functions::is_negative_cost; use vortex_mask::Mask; @@ -368,19 +369,20 @@ mod tests { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::Expression; - use vortex_array::expr::bound::pack as bound_pack; - use vortex_array::expr::cast; - use vortex_array::expr::eq; - use vortex_array::expr::get_item; - use vortex_array::expr::is_not_null; - use vortex_array::expr::lit; - use vortex_array::expr::pack; - use vortex_array::expr::root; use vortex_array::validity::Validity; use vortex_btrblocks::BtrBlocksCompressorBuilder; use vortex_error::VortexResult; + use vortex_expr::ArrayExprExt; + use vortex_expr::BoundExpression; + use vortex_expr::Expression; + use vortex_expr::bound::pack as bound_pack; + use vortex_expr::cast; + use vortex_expr::eq; + use vortex_expr::get_item; + use vortex_expr::is_not_null; + use vortex_expr::lit; + use vortex_expr::pack; + use vortex_expr::root; use vortex_functions::expr::byte_length; use vortex_functions::expr::like; use vortex_io::runtime::Handle; diff --git a/vortex-layout/src/layouts/dict/writer.rs b/vortex-layout/src/layouts/dict/writer.rs index 07c1cff9f71..0870f006f06 100644 --- a/vortex-layout/src/layouts/dict/writer.rs +++ b/vortex-layout/src/layouts/dict/writer.rs @@ -680,12 +680,12 @@ mod tests { use vortex_array::dtype::DecimalDType; use vortex_array::dtype::Nullability::NonNullable; use vortex_array::dtype::PType; - use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_array::session::ArraySession; use vortex_buffer::buffer; use vortex_decimal_byte_parts::DecimalByteParts; use vortex_error::VortexResult; + use vortex_expr::root; use vortex_io::runtime::single::block_on; use vortex_io::session::RuntimeSessionExt; use vortex_session::VortexSession; diff --git a/vortex-layout/src/layouts/flat/reader.rs b/vortex-layout/src/layouts/flat/reader.rs index 38a9bf0218d..9d0b03f1e38 100644 --- a/vortex-layout/src/layouts/flat/reader.rs +++ b/vortex-layout/src/layouts/flat/reader.rs @@ -16,10 +16,11 @@ use vortex_array::VortexSessionExecute; use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::BoundExpression; use vortex_array::serde::SerializedArray; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; use vortex_mask::Mask; use vortex_session::VortexSession; @@ -245,12 +246,12 @@ mod test { use vortex_array::arrays::BoolArray; use vortex_array::arrays::PrimitiveArray; use vortex_array::assert_arrays_eq; - use vortex_array::expr::gt; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::gt; + use vortex_expr::lit; + use vortex_expr::root; use vortex_io::runtime::single::block_on; use vortex_io::session::RuntimeSessionExt; diff --git a/vortex-layout/src/layouts/flat/writer.rs b/vortex-layout/src/layouts/flat/writer.rs index 949c15e881f..af5274cbf44 100644 --- a/vortex-layout/src/layouts/flat/writer.rs +++ b/vortex-layout/src/layouts/flat/writer.rs @@ -195,7 +195,6 @@ mod tests { use vortex_array::dtype::FieldName; use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; - use vortex_array::expr::root; use vortex_array::expr::stats::Precision; use vortex_array::expr::stats::Stat; use vortex_array::expr::stats::StatsProviderExt; @@ -204,6 +203,7 @@ mod tests { use vortex_buffer::buffer; use vortex_error::VortexExpect; use vortex_error::VortexResult; + use vortex_expr::root; use vortex_io::runtime::single::block_on; use vortex_io::session::RuntimeSessionExt; use vortex_mask::AllOr; diff --git a/vortex-layout/src/layouts/indexed/index.rs b/vortex-layout/src/layouts/indexed/index.rs index ac1981b84f2..1a24a414e93 100644 --- a/vortex-layout/src/layouts/indexed/index.rs +++ b/vortex-layout/src/layouts/indexed/index.rs @@ -13,9 +13,9 @@ use roaring::RoaringBitmap; use vortex_array::ArrayRef; use vortex_array::ExecutionCtx; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; use vortex_buffer::BitBufferMut; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; use vortex_mask::Mask; use vortex_session::VortexSession; use vortex_session::registry::Id; diff --git a/vortex-layout/src/layouts/indexed/reader.rs b/vortex-layout/src/layouts/indexed/reader.rs index f2a28b80e77..b131b214936 100644 --- a/vortex-layout/src/layouts/indexed/reader.rs +++ b/vortex-layout/src/layouts/indexed/reader.rs @@ -21,11 +21,11 @@ use vortex_array::MaskFuture; use vortex_array::VortexSessionExecute; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::BoundExpression; use vortex_buffer::BitBufferMut; use vortex_error::SharedVortexResult; use vortex_error::VortexError; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; use vortex_mask::Mask; use vortex_session::VortexSession; use vortex_utils::aliases::dash_map::DashMap; diff --git a/vortex-layout/src/layouts/indexed/tests.rs b/vortex-layout/src/layouts/indexed/tests.rs index e8fdee1ec73..9597f054d16 100644 --- a/vortex-layout/src/layouts/indexed/tests.rs +++ b/vortex-layout/src/layouts/indexed/tests.rs @@ -26,15 +26,15 @@ use vortex_array::arrays::VarBinViewArray; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::eq; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::stream::ArrayStreamExt; use vortex_array::test_harness::check_metadata; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::BoundExpression; +use vortex_expr::eq; +use vortex_expr::lit; +use vortex_expr::root; use vortex_functions::expr::like; use vortex_mask::Mask; use vortex_session::VortexSession; @@ -1088,10 +1088,6 @@ mod exact_value { use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability::NonNullable; use vortex_array::dtype::StructFields; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::col; - use vortex_array::expr::eq; - use vortex_array::expr::lit; use vortex_array::scalar_fn::builtin::binary::Binary; use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::scalar_fn::builtin::operators::Operator; @@ -1099,6 +1095,10 @@ mod exact_value { use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; + use vortex_expr::BoundExpression; + use vortex_expr::col; + use vortex_expr::eq; + use vortex_expr::lit; use vortex_session::VortexSession; use vortex_session::registry::CachedId; @@ -1485,11 +1485,11 @@ mod fixed_superset { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::eq; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_error::VortexResult; + use vortex_expr::BoundExpression; + use vortex_expr::eq; + use vortex_expr::lit; + use vortex_expr::root; use vortex_session::VortexSession; use crate::layouts::indexed::IndexBuilder; diff --git a/vortex-layout/src/layouts/list/expr.rs b/vortex-layout/src/layouts/list/expr.rs index d6d795ee80d..e42f562b3d7 100644 --- a/vortex-layout/src/layouts/list/expr.rs +++ b/vortex-layout/src/layouts/list/expr.rs @@ -3,11 +3,11 @@ use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::not; use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull; use vortex_array::scalar_fn::builtin::is_null::IsNull; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::bound::not; use vortex_functions::list_length::ListLength; /// The minimal set of list children an expression needs for evaluation. diff --git a/vortex-layout/src/layouts/list/reader.rs b/vortex-layout/src/layouts/list/reader.rs index b2dd9daa7b0..a904c955bba 100644 --- a/vortex-layout/src/layouts/list/reader.rs +++ b/vortex-layout/src/layouts/list/reader.rs @@ -19,12 +19,13 @@ use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::root; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_array::validity::Validity; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; +use vortex_expr::root; use vortex_mask::Mask; use vortex_session::VortexSession; @@ -626,13 +627,13 @@ mod tests { use vortex_array::arrays::ListViewArray; use vortex_array::arrays::PrimitiveArray; use vortex_array::assert_arrays_eq; - use vortex_array::expr::Expression; - use vortex_array::expr::cast; - use vortex_array::expr::gt; - use vortex_array::expr::is_not_null; - use vortex_array::expr::is_null; - use vortex_array::expr::lit; use vortex_buffer::buffer; + use vortex_expr::Expression; + use vortex_expr::cast; + use vortex_expr::gt; + use vortex_expr::is_not_null; + use vortex_expr::is_null; + use vortex_expr::lit; use vortex_functions::expr::list_length; use vortex_io::session::RuntimeSession; use vortex_io::session::RuntimeSessionExt; diff --git a/vortex-layout/src/layouts/partitioned.rs b/vortex-layout/src/layouts/partitioned.rs index 5c0fcab49de..3f9c6c15d23 100644 --- a/vortex-layout/src/layouts/partitioned.rs +++ b/vortex-layout/src/layouts/partitioned.rs @@ -16,12 +16,13 @@ use vortex_array::dtype::DType; use vortex_array::dtype::FieldName; use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; -use vortex_array::expr::BoundExpression; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::builtin::pack::Pack; use vortex_array::validity::Validity; use vortex_error::VortexError; use vortex_error::VortexResult; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; use vortex_expr::analysis::Annotation; use vortex_expr::transform::BoundPartitionedExpr; use vortex_mask::Mask; diff --git a/vortex-layout/src/layouts/row_idx/mod.rs b/vortex-layout/src/layouts/row_idx/mod.rs index 3853b49d81a..d9033fe8dc9 100644 --- a/vortex-layout/src/layouts/row_idx/mod.rs +++ b/vortex-layout/src/layouts/row_idx/mod.rs @@ -21,14 +21,16 @@ use vortex_array::dtype::FieldMask; use vortex_array::dtype::FieldName; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::Transformed; -use vortex_array::expr::traversal::TraversalOrder; use vortex_array::scalar::PValue; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; +use vortex_expr::ExactBoundExpr; use vortex_expr::transform::partition_bound; +use vortex_expr::traversal::NodeExt; +use vortex_expr::traversal::Transformed; +use vortex_expr::traversal::TraversalOrder; use vortex_mask::Mask; pub use vortex_scan::row_idx::*; use vortex_sequence::Sequence; @@ -396,13 +398,13 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::eq; - use vortex_array::expr::gt; - use vortex_array::expr::lit; - use vortex_array::expr::or; - use vortex_array::expr::root; use vortex_array::optimizer::ArrayOptimizer; use vortex_buffer::buffer; + use vortex_expr::eq; + use vortex_expr::gt; + use vortex_expr::lit; + use vortex_expr::or; + use vortex_expr::root; use vortex_io::runtime::single::block_on; use vortex_io::session::RuntimeSessionExt; use vortex_mask::Mask; diff --git a/vortex-layout/src/layouts/struct_/reader.rs b/vortex-layout/src/layouts/struct_/reader.rs index 313899b1a25..f60829aa4af 100644 --- a/vortex-layout/src/layouts/struct_/reader.rs +++ b/vortex-layout/src/layouts/struct_/reader.rs @@ -17,14 +17,6 @@ use vortex_array::dtype::Nullability; use vortex_array::dtype::RESERVED_VALIDITY_FIELD_NAME; use vortex_array::dtype::StructFields; use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::get_item; -use vortex_array::expr::bound::is_not_null; -use vortex_array::expr::bound::mask; -use vortex_array::expr::bound::not; -use vortex_array::expr::bound::pack; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::Transformed; -use vortex_array::expr::traversal::TraversalOrder; use vortex_array::scalar_fn::builtin::binary::Binary; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull; @@ -36,8 +28,16 @@ use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_err; use vortex_expr::analysis::AnnotationFn; +use vortex_expr::bound::get_item; +use vortex_expr::bound::is_not_null; +use vortex_expr::bound::mask; +use vortex_expr::bound::not; +use vortex_expr::bound::pack; use vortex_expr::make_bound_free_field_annotator; use vortex_expr::transform::partition_bound; +use vortex_expr::traversal::NodeExt; +use vortex_expr::traversal::Transformed; +use vortex_expr::traversal::TraversalOrder; use vortex_mask::Mask; use vortex_session::VortexSession; use vortex_utils::aliases::hash_map::HashMap; @@ -699,28 +699,28 @@ mod tests { use vortex_array::dtype::PType; use vortex_array::dtype::RESERVED_VALIDITY_FIELD_NAME; use vortex_array::dtype::StructFields; - use vortex_array::expr::Expression; - use vortex_array::expr::and; - use vortex_array::expr::col; - use vortex_array::expr::eq; - use vortex_array::expr::get_item; - use vortex_array::expr::gt; - use vortex_array::expr::is_not_null; - use vortex_array::expr::is_null; - use vortex_array::expr::lit; - use vortex_array::expr::lt; - use vortex_array::expr::mask; - use vortex_array::expr::not; - use vortex_array::expr::or; - use vortex_array::expr::pack; - use vortex_array::expr::root; - use vortex_array::expr::select; - use vortex_array::expr::select_exclude; use vortex_array::scalar::Scalar; use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexExpect; use vortex_error::VortexResult; + use vortex_expr::Expression; + use vortex_expr::and; + use vortex_expr::col; + use vortex_expr::eq; + use vortex_expr::get_item; + use vortex_expr::gt; + use vortex_expr::is_not_null; + use vortex_expr::is_null; + use vortex_expr::lit; + use vortex_expr::lt; + use vortex_expr::mask; + use vortex_expr::not; + use vortex_expr::or; + use vortex_expr::pack; + use vortex_expr::root; + use vortex_expr::select; + use vortex_expr::select_exclude; use vortex_io::runtime::single::block_on; use vortex_io::session::RuntimeSessionExt; use vortex_mask::Mask; diff --git a/vortex-layout/src/layouts/zoned/aggregates/bloom_filter/scalar_fn.rs b/vortex-layout/src/layouts/zoned/aggregates/bloom_filter/scalar_fn.rs index 6d167540791..c1a7edf8874 100644 --- a/vortex-layout/src/layouts/zoned/aggregates/bloom_filter/scalar_fn.rs +++ b/vortex-layout/src/layouts/zoned/aggregates/bloom_filter/scalar_fn.rs @@ -23,15 +23,12 @@ use vortex_array::arrays::varbinview::BinaryView; use vortex_array::arrays::varbinview::VarBinViewArrayExt; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::not; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; use vortex_array::scalar_fn::ExecutionArgs; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; -use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::binary::Binary; use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::scalar_fn::builtin::operators::Operator; @@ -42,6 +39,9 @@ use vortex_error::VortexResult; use vortex_error::vortex_ensure; use vortex_error::vortex_ensure_eq; use vortex_error::vortex_err; +use vortex_expr::BoundExpression; +use vortex_expr::ScalarFnExprExt; +use vortex_expr::bound::not; use vortex_expr::stats::expr::bound::stat as bound_stat; use vortex_expr::stats::rewrite::StatsRewriteRule; use vortex_session::VortexSession; @@ -296,9 +296,6 @@ mod tests { use vortex_array::dtype::DecimalDType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::bound::eq; - use vortex_array::expr::bound::lit; - use vortex_array::expr::bound::root; use vortex_array::scalar::DecimalValue; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::ScalarFnVTable; @@ -307,6 +304,9 @@ mod tests { use vortex_array::scalar_fn::session::ScalarFnSessionExt; use vortex_array::validity::Validity; use vortex_error::VortexResult; + use vortex_expr::bound::eq; + use vortex_expr::bound::lit; + use vortex_expr::bound::root; use vortex_expr::stats::StatsSessionExt; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_expr::stats::rewrite::StatsRewriteRule; diff --git a/vortex-layout/src/layouts/zoned/pruning.rs b/vortex-layout/src/layouts/zoned/pruning.rs index b1e98e32646..8bb14aee4e1 100644 --- a/vortex-layout/src/layouts/zoned/pruning.rs +++ b/vortex-layout/src/layouts/zoned/pruning.rs @@ -18,11 +18,12 @@ use vortex_array::VortexSessionExecute; use vortex_array::aggregate_fn::AggregateFnRef; use vortex_array::arrays::StructArray; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::root; use vortex_error::SharedVortexResult; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::ExactBoundExpr; +use vortex_expr::root; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_functions::dynamic::DynamicExprUpdates; use vortex_mask::Mask; diff --git a/vortex-layout/src/layouts/zoned/reader.rs b/vortex-layout/src/layouts/zoned/reader.rs index 61e33a5d441..3373d43a68f 100644 --- a/vortex-layout/src/layouts/zoned/reader.rs +++ b/vortex-layout/src/layouts/zoned/reader.rs @@ -12,12 +12,12 @@ use vortex_array::ArrayRef; use vortex_array::MaskFuture; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::BoundExpression; use vortex_buffer::BitBufferMut; use vortex_error::VortexError; use vortex_error::VortexResult; use vortex_error::vortex_ensure; use vortex_error::vortex_ensure_eq; +use vortex_expr::BoundExpression; use vortex_mask::Mask; use vortex_session::VortexSession; @@ -257,14 +257,14 @@ mod test { use vortex_array::arrays::ChunkedArray; use vortex_array::arrays::PrimitiveArray; use vortex_array::assert_arrays_eq; - use vortex_array::expr::gt; - use vortex_array::expr::is_not_null; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexExpect; use vortex_error::VortexResult; + use vortex_expr::gt; + use vortex_expr::is_not_null; + use vortex_expr::lit; + use vortex_expr::root; use vortex_io::runtime::Handle; use vortex_io::runtime::single::block_on; use vortex_io::session::RuntimeSession; diff --git a/vortex-layout/src/layouts/zoned/schema.rs b/vortex-layout/src/layouts/zoned/schema.rs index 0e4d9db318a..ddcfb2db808 100644 --- a/vortex-layout/src/layouts/zoned/schema.rs +++ b/vortex-layout/src/layouts/zoned/schema.rs @@ -11,15 +11,15 @@ use vortex_array::aggregate_fn::session::AggregateFnSessionExt; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::StructFields; -use vortex_array::expr::Expression; -use vortex_array::expr::cast; -use vortex_array::expr::get_item; -use vortex_array::expr::pack; -use vortex_array::expr::root; use vortex_array::expr::stats::Stat; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::Expression; +use vortex_expr::cast; +use vortex_expr::get_item; +use vortex_expr::pack; +use vortex_expr::root; use vortex_session::VortexSession; pub const MAX_IS_TRUNCATED: &str = "max_is_truncated"; diff --git a/vortex-layout/src/layouts/zoned/zone_map.rs b/vortex-layout/src/layouts/zoned/zone_map.rs index 029bd9058e5..8558e919c17 100644 --- a/vortex-layout/src/layouts/zoned/zone_map.rs +++ b/vortex-layout/src/layouts/zoned/zone_map.rs @@ -25,23 +25,24 @@ use vortex_array::arrays::RunEnd; use vortex_array::arrays::StructArray; use vortex_array::arrays::struct_::StructArrayExt; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::Expression; -use vortex_array::expr::eq; -use vortex_array::expr::get_item; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::expr::stats::Stat; use vortex_array::scalar_fn::EmptyOptions; -use vortex_array::scalar_fn::ScalarFnVTableExt; -use vortex_array::scalar_fn::internal::row_count::RowCount; -use vortex_array::scalar_fn::internal::row_count::contains_row_count; -use vortex_array::scalar_fn::internal::row_count::substitute_row_count; use vortex_array::validity::Validity; use vortex_buffer::buffer; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_ensure_eq; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; +use vortex_expr::Expression; +use vortex_expr::ScalarFnExprExt; +use vortex_expr::eq; +use vortex_expr::get_item; +use vortex_expr::lambda::row_count::RowCount; +use vortex_expr::lambda::row_count::contains_row_count; +use vortex_expr::lambda::row_count::substitute_row_count; +use vortex_expr::lit; +use vortex_expr::root; use vortex_expr::stats::bind::StatBinder; use vortex_expr::stats::bind::bind_stats; use vortex_mask::Mask; @@ -134,7 +135,7 @@ impl ZoneMap { /// [`BoundExpressionPruneExt::falsify`]. The returned mask has one value per zone, where /// `true` means the zone cannot contain matching rows and can be skipped. /// - /// If the predicate contains [`row_count`][vortex_array::scalar_fn::internal::row_count] + /// If the predicate contains [`row_count`][vortex_expr::lambda::row_count] /// placeholders, they are replaced after [`ArrayRef::apply_bound`] with per-zone /// counts derived from `zone_len` and `row_count`. Uniform zones use a /// [`ConstantArray`]; a short final zone uses a run-end encoded array. @@ -403,19 +404,19 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::Expression; - use vortex_array::expr::cast; - use vortex_array::expr::is_not_null; - use vortex_array::expr::is_null; - use vortex_array::expr::lit; - use vortex_array::expr::not_eq; - use vortex_array::expr::root; use vortex_array::expr::stats::Stat; use vortex_array::validity::Validity; use vortex_buffer::Buffer; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::BoundExpression; + use vortex_expr::Expression; + use vortex_expr::cast; + use vortex_expr::is_not_null; + use vortex_expr::is_null; + use vortex_expr::lit; + use vortex_expr::not_eq; + use vortex_expr::root; use vortex_expr::stats::all_non_null; use vortex_expr::stats::all_null; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; diff --git a/vortex-layout/src/reader.rs b/vortex-layout/src/reader.rs index 9f3b5ea3794..fec4a799288 100644 --- a/vortex-layout/src/reader.rs +++ b/vortex-layout/src/reader.rs @@ -14,9 +14,9 @@ use vortex_array::MaskFuture; use vortex_array::builtins::ArrayBuiltins; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::BoundExpression; use vortex_error::VortexResult; use vortex_error::vortex_bail; +use vortex_expr::BoundExpression; use vortex_mask::Mask; use vortex_session::VortexSession; diff --git a/vortex-layout/src/scan/layout.rs b/vortex-layout/src/scan/layout.rs index 2c69f13b1e9..7d63ac0838b 100644 --- a/vortex-layout/src/scan/layout.rs +++ b/vortex-layout/src/scan/layout.rs @@ -20,7 +20,6 @@ use vortex_array::arrays::ConstantArray; use vortex_array::dtype::DType; use vortex_array::dtype::FieldPath; use vortex_array::dtype::Nullability; -use vortex_array::expr::BoundExpression; use vortex_array::expr::stats::Precision; use vortex_array::scalar::Scalar; use vortex_array::stats::StatsSet; @@ -30,6 +29,7 @@ use vortex_array::stream::SendableArrayStream; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::BoundExpression; use vortex_mask::Mask; use vortex_metrics::MetricsRegistry; use vortex_scan::DataSource; @@ -463,9 +463,9 @@ mod tests { use futures::StreamExt; use futures::TryStreamExt; use rstest::rstest; - use vortex_array::expr::root; use vortex_buffer::Buffer; use vortex_error::VortexResult; + use vortex_expr::root; use vortex_io::runtime::BlockingRuntime; use vortex_io::runtime::single::SingleThreadRuntime; use vortex_metrics::DefaultMetricsRegistry; diff --git a/vortex-layout/src/scan/multi.rs b/vortex-layout/src/scan/multi.rs index 1f1ee275688..f128ec84e62 100644 --- a/vortex-layout/src/scan/multi.rs +++ b/vortex-layout/src/scan/multi.rs @@ -38,7 +38,6 @@ use itertools::Itertools; use tracing::Instrument; use vortex_array::dtype::DType; use vortex_array::dtype::FieldPath; -use vortex_array::expr::BoundExpression; use vortex_array::expr::stats::Precision; use vortex_array::stats::StatsSet; use vortex_array::stream::ArrayStreamAdapter; @@ -48,6 +47,7 @@ use vortex_error::SharedVortexResult; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::BoundExpression; use vortex_io::session::RuntimeSessionExt; use vortex_mask::Mask; use vortex_scan::DataSource; @@ -632,10 +632,10 @@ mod tests { use rstest::rstest; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::eq; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_error::VortexResult; + use vortex_expr::eq; + use vortex_expr::lit; + use vortex_expr::root; use vortex_io::runtime::BlockingRuntime; use vortex_io::runtime::single::SingleThreadRuntime; use vortex_scan::DataSource; diff --git a/vortex-layout/src/scan/repeated_scan.rs b/vortex-layout/src/scan/repeated_scan.rs index 2a1c7026923..f53e3eb571d 100644 --- a/vortex-layout/src/scan/repeated_scan.rs +++ b/vortex-layout/src/scan/repeated_scan.rs @@ -21,7 +21,6 @@ use itertools::Either; use itertools::Itertools; use vortex_array::ArrayRef; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; use vortex_array::iter::ArrayIterator; use vortex_array::iter::ArrayIteratorAdapter; use vortex_array::optimizer::ArrayOptimizer; @@ -30,6 +29,7 @@ use vortex_array::stream::ArrayStreamAdapter; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_bail; +use vortex_expr::BoundExpression; use vortex_io::runtime::BlockingRuntime; use vortex_io::session::RuntimeSessionExt; use vortex_scan::row_mask::RowMask; diff --git a/vortex-layout/src/scan/scan_builder.rs b/vortex-layout/src/scan/scan_builder.rs index eccd98b8750..7e9e351dffc 100644 --- a/vortex-layout/src/scan/scan_builder.rs +++ b/vortex-layout/src/scan/scan_builder.rs @@ -15,13 +15,13 @@ use itertools::Itertools; use vortex_array::ArrayRef; use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; -use vortex_array::expr::BoundExpression; use vortex_array::iter::ArrayIterator; use vortex_array::iter::ArrayIteratorAdapter; use vortex_array::stream::ArrayStream; use vortex_array::stream::ArrayStreamAdapter; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; use vortex_expr::analysis::referenced_field_paths; use vortex_functions::between::find_between; use vortex_io::runtime::BlockingRuntime; @@ -552,21 +552,21 @@ mod test { use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::ExactBoundExpr; - use vortex_array::expr::and; - use vortex_array::expr::col; - use vortex_array::expr::eq; - use vortex_array::expr::get_item; - use vortex_array::expr::gt_eq; - use vortex_array::expr::is_not_null; - use vortex_array::expr::lit; - use vortex_array::expr::lt; - use vortex_array::expr::root; use vortex_buffer::Buffer; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; + use vortex_expr::BoundExpression; + use vortex_expr::ExactBoundExpr; + use vortex_expr::and; + use vortex_expr::col; + use vortex_expr::eq; + use vortex_expr::get_item; + use vortex_expr::gt_eq; + use vortex_expr::is_not_null; + use vortex_expr::lit; + use vortex_expr::lt; + use vortex_expr::root; use vortex_functions::between::BetweenOptions; use vortex_functions::between::StrictComparison; use vortex_functions::expr::between; diff --git a/vortex-layout/src/scan/split_by.rs b/vortex-layout/src/scan/split_by.rs index 3b4fdb5ab6e..28b6a343a31 100644 --- a/vortex-layout/src/scan/split_by.rs +++ b/vortex-layout/src/scan/split_by.rs @@ -275,9 +275,9 @@ mod test { use vortex_array::dtype::FieldPath; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::BoundExpression; use vortex_buffer::buffer; use vortex_error::vortex_bail; + use vortex_expr::BoundExpression; use vortex_io::runtime::single::block_on; use vortex_mask::Mask; diff --git a/vortex-layout/src/scan/test.rs b/vortex-layout/src/scan/test.rs index 85c4b25d8b1..4df2113df64 100644 --- a/vortex-layout/src/scan/test.rs +++ b/vortex-layout/src/scan/test.rs @@ -18,9 +18,9 @@ use vortex_array::dtype::DType; use vortex_array::dtype::FieldMask; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::BoundExpression; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::BoundExpression; use vortex_io::runtime::BlockingRuntime; use vortex_io::runtime::Handle; use vortex_io::runtime::single::SingleThreadRuntime; diff --git a/vortex-layout/src/scan/v2/mod.rs b/vortex-layout/src/scan/v2/mod.rs index a7e04c5bd3e..a07c52821b3 100644 --- a/vortex-layout/src/scan/v2/mod.rs +++ b/vortex-layout/src/scan/v2/mod.rs @@ -7,9 +7,9 @@ use std::ops::Range; use std::sync::Arc; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; use vortex_error::VortexExpect; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; use vortex_scan::plan::LazyPlanRef; pub use vortex_scan::prepare::ChunkPlanner; pub use vortex_scan::prepare::FilterPlans; diff --git a/vortex-layout/src/scan/v2/tests.rs b/vortex-layout/src/scan/v2/tests.rs index a8219d3014e..ea81db2bce1 100644 --- a/vortex-layout/src/scan/v2/tests.rs +++ b/vortex-layout/src/scan/v2/tests.rs @@ -26,16 +26,6 @@ use vortex_array::arrays::VarBinArray; use vortex_array::assert_arrays_eq; use vortex_array::dtype::DType; use vortex_array::dtype::FieldNames; -use vortex_array::expr::Expression; -use vortex_array::expr::and; -use vortex_array::expr::eq; -use vortex_array::expr::get_item; -use vortex_array::expr::gt; -use vortex_array::expr::is_null; -use vortex_array::expr::lit; -use vortex_array::expr::lt; -use vortex_array::expr::pack; -use vortex_array::expr::root; use vortex_array::validity::Validity; use vortex_arrow::ArrowSessionExt; use vortex_buffer::Alignment; @@ -43,6 +33,16 @@ use vortex_buffer::Buffer; use vortex_buffer::buffer; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::Expression; +use vortex_expr::and; +use vortex_expr::eq; +use vortex_expr::get_item; +use vortex_expr::gt; +use vortex_expr::is_null; +use vortex_expr::lit; +use vortex_expr::lt; +use vortex_expr::pack; +use vortex_expr::root; use vortex_io::session::RuntimeSessionExt; use vortex_mask::Mask; use vortex_scan::execution::SegmentLocation; diff --git a/vortex-python/src/arrays/mod.rs b/vortex-python/src/arrays/mod.rs index b0169e81d13..3c256d5082a 100644 --- a/vortex-python/src/arrays/mod.rs +++ b/vortex-python/src/arrays/mod.rs @@ -50,6 +50,7 @@ use vortex::buffer::ByteBuffer; use vortex::dtype::DType; use vortex::dtype::Nullability; use vortex::error::VortexResult; +use vortex::expr::ArrayExprExt; use vortex::flatbuffers::WriteFlatBufferExt; use vortex::ipc::messages::EncoderMessage; use vortex::ipc::messages::MessageEncoder; diff --git a/vortex-python/src/expr/mod.rs b/vortex-python/src/expr/mod.rs index 5cb5b305d93..a63a291499a 100644 --- a/vortex-python/src/expr/mod.rs +++ b/vortex-python/src/expr/mod.rs @@ -15,6 +15,7 @@ use vortex::dtype::FieldNames; use vortex::dtype::Nullability; use vortex::expr; use vortex::expr::Expression; +use vortex::expr::ScalarFnExprExt; use vortex::expr::lit; use vortex::expr::proto::ExprSerializeProtoExt; use vortex::functions::between::BetweenOptions; @@ -25,7 +26,6 @@ use vortex::functions::replace_time_zone::ReplaceTimeZoneOptions; use vortex::functions::variant_get::VariantPath; use vortex::functions::variant_get::VariantPathElement; use vortex::proto::expr as pb; -use vortex::scalar_fn::ScalarFnVTableExt; use vortex::scalar_fn::builtin::binary::Binary; use vortex::scalar_fn::builtin::operators::Operator; diff --git a/vortex-scan/README.md b/vortex-scan/README.md index 5d1ae8b61fa..8daae7d765b 100644 --- a/vortex-scan/README.md +++ b/vortex-scan/README.md @@ -37,7 +37,7 @@ The `vortex-scan` crate provides efficient scanning operations over Vortex array ```rust use vortex_scan::ScanBuilder; -use vortex_array::expr::lit; +use vortex_expr::lit; // Create a scan that reads specific columns with a filter let scan = ScanBuilder::new(layout_reader) @@ -167,8 +167,8 @@ This controls how many splits are processed concurrently. Core dependencies: -- `vortex-array`: Core array types and operations (includes expression evaluation framework) -- `vortex-expr`: Expression analysis and rewrites +- `vortex-array`: Core array types and operations +- `vortex-expr`: Expression trees and the analyses and rewrites built on them - `vortex-io`: Byte requests and runtime abstractions - `futures`: Async runtime abstractions - `vortex-session`: Shared execution context diff --git a/vortex-scan/src/execution/cache.rs b/vortex-scan/src/execution/cache.rs index 84e98d49804..1a373ea238e 100644 --- a/vortex-scan/src/execution/cache.rs +++ b/vortex-scan/src/execution/cache.rs @@ -10,7 +10,7 @@ use std::sync::Weak; use parking_lot::Mutex; use rustc_hash::FxHashMap; -use vortex_array::expr::BoundExpression; +use vortex_expr::BoundExpression; use super::io::SegmentRanges; use super::retain::RetainedSegments; @@ -203,13 +203,13 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::Expression; - use vortex_array::expr::get_item; - use vortex_array::expr::gt; - use vortex_array::expr::lit; - use vortex_array::expr::lt; - use vortex_array::expr::root; use vortex_error::VortexResult; + use vortex_expr::Expression; + use vortex_expr::get_item; + use vortex_expr::gt; + use vortex_expr::lit; + use vortex_expr::lt; + use vortex_expr::root; use super::ExprKey; diff --git a/vortex-scan/src/execution/filter.rs b/vortex-scan/src/execution/filter.rs index b073dc7bb2b..bf295ef3ff3 100644 --- a/vortex-scan/src/execution/filter.rs +++ b/vortex-scan/src/execution/filter.rs @@ -8,10 +8,10 @@ use bit_vec::BitVec; use rustc_hash::FxHashSet; use vortex_array::VortexSessionExecute; use vortex_array::builtins::ArrayBuiltins; -use vortex_array::expr::BoundExpression; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_ensure_eq; +use vortex_expr::BoundExpression; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_functions::between::find_between; use vortex_io::request::IoBatch; diff --git a/vortex-scan/src/execution/pipeline/tests.rs b/vortex-scan/src/execution/pipeline/tests.rs index 28b6c283e1e..e943723b568 100644 --- a/vortex-scan/src/execution/pipeline/tests.rs +++ b/vortex-scan/src/execution/pipeline/tests.rs @@ -21,14 +21,14 @@ use vortex_array::arrays::VarBinViewArray; use vortex_array::arrays::dict::DictArraySlotsExt; use vortex_array::assert_arrays_eq; use vortex_array::dtype::DType; -use vortex_array::expr::get_item; -use vortex_array::expr::gt; -use vortex_array::expr::lit; -use vortex_array::expr::root; use vortex_array::validity::Validity; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::get_item; +use vortex_expr::gt; +use vortex_expr::lit; +use vortex_expr::root; use vortex_mask::Mask; use crate::execution::pipeline::*; diff --git a/vortex-scan/src/lib.rs b/vortex-scan/src/lib.rs index 216c689f017..97347496482 100644 --- a/vortex-scan/src/lib.rs +++ b/vortex-scan/src/lib.rs @@ -45,8 +45,6 @@ use selection::Selection; use vortex_array::aggregate_fn::AggregateFnRef; use vortex_array::dtype::DType; use vortex_array::dtype::FieldPath; -use vortex_array::expr::Expression; -use vortex_array::expr::root; use vortex_array::expr::stats::Precision; use vortex_array::scalar::Scalar; use vortex_array::stats::StatsSet; @@ -55,6 +53,8 @@ use vortex_array::stream::ArrayStreamAdapter; use vortex_array::stream::SendableArrayStream; use vortex_error::VortexResult; use vortex_error::vortex_bail; +use vortex_expr::Expression; +use vortex_expr::root; use vortex_session::VortexSession; /// A sendable stream of partitions. diff --git a/vortex-scan/src/plan/builtins/eval/execute.rs b/vortex-scan/src/plan/builtins/eval/execute.rs index 361e068a31f..acec8c8beb4 100644 --- a/vortex-scan/src/plan/builtins/eval/execute.rs +++ b/vortex-scan/src/plan/builtins/eval/execute.rs @@ -3,8 +3,9 @@ //! Applying an expression to each batch. -use vortex_array::expr::BoundExpression; use vortex_error::VortexResult; +use vortex_expr::ArrayExprExt; +use vortex_expr::BoundExpression; use crate::execution::operator::Cx; use crate::execution::operator::Input; diff --git a/vortex-scan/src/plan/builtins/eval/mod.rs b/vortex-scan/src/plan/builtins/eval/mod.rs index 1375eba6b52..145252608bb 100644 --- a/vortex-scan/src/plan/builtins/eval/mod.rs +++ b/vortex-scan/src/plan/builtins/eval/mod.rs @@ -10,14 +10,14 @@ use std::ops::Range; use vortex_array::EmptyMetadata; use vortex_array::dtype::Nullability; use vortex_array::dtype::StructFields; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::Transformed; -use vortex_array::expr::traversal::TraversalOrder; use vortex_array::scalar_fn::builtin::pack::Pack; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_bail; +use vortex_expr::BoundExpression; +use vortex_expr::traversal::NodeExt; +use vortex_expr::traversal::Transformed; +use vortex_expr::traversal::TraversalOrder; use vortex_mask::Mask; use vortex_session::VortexSession; use vortex_session::registry::CachedId; diff --git a/vortex-scan/src/plan/builtins/pack/mod.rs b/vortex-scan/src/plan/builtins/pack/mod.rs index 763a355151a..1f4b431459e 100644 --- a/vortex-scan/src/plan/builtins/pack/mod.rs +++ b/vortex-scan/src/plan/builtins/pack/mod.rs @@ -18,13 +18,11 @@ use vortex_array::dtype::FieldName; use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; use vortex_array::dtype::StructFields; -use vortex_array::expr as unbound; use vortex_array::expr::BoundExpression; -use vortex_array::expr::Expression; use vortex_array::expr::bound; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::NodeRewriter; -use vortex_array::expr::traversal::Transformed; +use vortex_expr::traversal::NodeExt; +use vortex_expr::traversal::NodeRewriter; +use vortex_expr::traversal::Transformed; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull; use vortex_array::scalar_fn::builtin::is_null::IsNull; @@ -37,6 +35,8 @@ use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_ensure; use vortex_error::vortex_err; +use vortex_expr as unbound; +use vortex_expr::Expression; use vortex_mask::Mask; use vortex_session::VortexSession; use vortex_session::registry::CachedId; @@ -657,10 +657,10 @@ impl PartitionRule for IsNullRule { mod partition_tests { use insta::assert_snapshot; use vortex_array::dtype::PType; - use vortex_array::expr as unbound; - use vortex_array::expr::bound::get_item; - use vortex_array::expr::bound::is_not_null; - use vortex_array::expr::bound::pack; + use vortex_expr as unbound; + use vortex_expr::bound::get_item; + use vortex_expr::bound::is_not_null; + use vortex_expr::bound::pack; use super::*; use crate::plan::partition::PartitionedExpression; diff --git a/vortex-scan/src/plan/builtins/row_idx/expr.rs b/vortex-scan/src/plan/builtins/row_idx/expr.rs index dc58f1f3867..45a762172bc 100644 --- a/vortex-scan/src/plan/builtins/row_idx/expr.rs +++ b/vortex-scan/src/plan/builtins/row_idx/expr.rs @@ -7,17 +7,17 @@ use vortex_array::ArrayRef; use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::Expression; -use vortex_array::expr::display::ExprDisplay; use vortex_array::scalar_fn::Arity; use vortex_array::scalar_fn::ChildName; use vortex_array::scalar_fn::EmptyOptions; use vortex_array::scalar_fn::ExecutionArgs; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; -use vortex_array::scalar_fn::ScalarFnVTableExt; +use vortex_array::scalar_fn::display::ExprDisplay; use vortex_error::VortexResult; use vortex_error::vortex_bail; +use vortex_expr::Expression; +use vortex_expr::ScalarFnExprExt; use vortex_session::registry::CachedId; #[derive(Clone)] @@ -76,9 +76,9 @@ pub fn row_idx() -> Expression { #[cfg(test)] mod tests { - use vortex_array::expr::Lambda; - use vortex_array::expr::checked_add; - use vortex_array::expr::var; + use vortex_expr::Lambda; + use vortex_expr::checked_add; + use vortex_expr::var; use super::row_idx; diff --git a/vortex-scan/src/plan/builtins/row_idx/mod.rs b/vortex-scan/src/plan/builtins/row_idx/mod.rs index a4cfc568b9a..64ed8fcc750 100644 --- a/vortex-scan/src/plan/builtins/row_idx/mod.rs +++ b/vortex-scan/src/plan/builtins/row_idx/mod.rs @@ -11,10 +11,10 @@ use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::Expression; use vortex_buffer::Buffer; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::Expression; use vortex_mask::Mask; use vortex_session::VortexSession; use vortex_session::registry::CachedId; diff --git a/vortex-scan/src/plan/builtins/take/mod.rs b/vortex-scan/src/plan/builtins/take/mod.rs index 53812bb88f9..b07f827bd8d 100644 --- a/vortex-scan/src/plan/builtins/take/mod.rs +++ b/vortex-scan/src/plan/builtins/take/mod.rs @@ -10,10 +10,10 @@ use vortex_array::Canonical; use vortex_array::EmptyMetadata; use vortex_array::IntoArray; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::ExactBoundExpr; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; use vortex_expr::BoundLabels; +use vortex_expr::ExactBoundExpr; use vortex_expr::label_bound_tree; use vortex_functions::dynamic::DynamicComparison; use vortex_functions::is_negative_cost; diff --git a/vortex-scan/src/plan/builtins/zoned/mod.rs b/vortex-scan/src/plan/builtins/zoned/mod.rs index 536283320d3..97d69cbebe8 100644 --- a/vortex-scan/src/plan/builtins/zoned/mod.rs +++ b/vortex-scan/src/plan/builtins/zoned/mod.rs @@ -16,10 +16,6 @@ use vortex_array::arrays::BoolArray; use vortex_array::arrays::ConstantArray; use vortex_array::arrays::StructArray; use vortex_array::dtype::DType; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::traversal::NodeExt; -use vortex_array::expr::traversal::Transformed; -use vortex_array::expr::traversal::TraversalOrder; use vortex_array::scalar::Scalar; use vortex_array::validity::Validity; use vortex_buffer::BitBufferMut; @@ -27,7 +23,11 @@ use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_err; +use vortex_expr::BoundExpression; use vortex_expr::stats::stat_fn::StatFn; +use vortex_expr::traversal::NodeExt; +use vortex_expr::traversal::Transformed; +use vortex_expr::traversal::TraversalOrder; use vortex_functions::dynamic::DynamicExprUpdates; use vortex_mask::Mask; use vortex_session::VortexSession; diff --git a/vortex-scan/src/plan/tests.rs b/vortex-scan/src/plan/tests.rs index 94c521b4993..807a0783d2a 100644 --- a/vortex-scan/src/plan/tests.rs +++ b/vortex-scan/src/plan/tests.rs @@ -13,21 +13,22 @@ use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::dtype::StructFields; -use vortex_array::expr::Expression; -use vortex_array::expr::and; -use vortex_array::expr::bound::and as bound_and; -use vortex_array::expr::cast; -use vortex_array::expr::checked_add; -use vortex_array::expr::get_item; -use vortex_array::expr::gt; -use vortex_array::expr::is_null; -use vortex_array::expr::lit; -use vortex_array::expr::pack; -use vortex_array::expr::root; use vortex_array::scalar::ScalarValue; use vortex_array::scalar_fn::builtin::operators::CompareOperator; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::ArrayExprExt; +use vortex_expr::Expression; +use vortex_expr::and; +use vortex_expr::bound::and as bound_and; +use vortex_expr::cast; +use vortex_expr::checked_add; +use vortex_expr::get_item; +use vortex_expr::gt; +use vortex_expr::is_null; +use vortex_expr::lit; +use vortex_expr::pack; +use vortex_expr::root; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_functions::expr::byte_length; use vortex_functions::expr::dynamic; diff --git a/vortex-spatial/src/prune/contains.rs b/vortex-spatial/src/prune/contains.rs index f4ff037c6c8..06805059e83 100644 --- a/vortex-spatial/src/prune/contains.rs +++ b/vortex-spatial/src/prune/contains.rs @@ -3,15 +3,15 @@ //! `ST_Contains` pruning with a geometry column and a constant in either operand order. -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::get_item; -use vortex_array::expr::bound::gt; -use vortex_array::expr::bound::lit; -use vortex_array::expr::bound::lt; -use vortex_array::expr::bound::or; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::bound::get_item; +use vortex_expr::bound::gt; +use vortex_expr::bound::lit; +use vortex_expr::bound::lt; +use vortex_expr::bound::or; use vortex_expr::stats::rewrite::StatsRewriteRule; use vortex_session::VortexSession; @@ -83,15 +83,8 @@ mod tests { use vortex_array::aggregate_fn::AggregateFnVTableExt; use vortex_array::aggregate_fn::EmptyOptions as AggregateOptions; use vortex_array::arrays::StructArray; - use vortex_array::expr::Expression; - use vortex_array::expr::fill_null; - use vortex_array::expr::get_item; - use vortex_array::expr::lit; - use vortex_array::expr::not; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::EmptyOptions; - use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_buffer::ByteBuffer; use vortex_buffer::ByteBufferMut; use vortex_edition::EDITION_DECLARATIONS; @@ -101,6 +94,14 @@ mod tests { use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_err; + use vortex_expr::ArrayExprExt; + use vortex_expr::Expression; + use vortex_expr::ScalarFnExprExt; + use vortex_expr::fill_null; + use vortex_expr::get_item; + use vortex_expr::lit; + use vortex_expr::not; + use vortex_expr::root; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_file::OpenOptionsSessionExt; use vortex_file::WriteOptionsSessionExt; diff --git a/vortex-spatial/src/prune/distance.rs b/vortex-spatial/src/prune/distance.rs index 2725d8baf2e..6277bc88578 100644 --- a/vortex-spatial/src/prune/distance.rs +++ b/vortex-spatial/src/prune/distance.rs @@ -4,13 +4,13 @@ //! `ST_Distance(geom, const) radius` pruning. use geo::Rect as SpatialRect; -use vortex_array::expr::BoundExpression; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_array::scalar_fn::builtin::binary::Binary; use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; use vortex_expr::stats::rewrite::StatsRewriteRule; use vortex_session::VortexSession; @@ -130,17 +130,17 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::gt_eq; - use vortex_array::expr::lit; - use vortex_array::expr::lt_eq; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::EmptyOptions; - use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_array::scalar_fn::builtin::binary::Binary; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_error::VortexResult; + use vortex_expr::BoundExpression; + use vortex_expr::ScalarFnExprExt; + use vortex_expr::gt_eq; + use vortex_expr::lit; + use vortex_expr::lt_eq; + use vortex_expr::root; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_expr::stats::rewrite::StatsRewriteRule; diff --git a/vortex-spatial/src/prune/intersects.rs b/vortex-spatial/src/prune/intersects.rs index 71c1236255f..11799918da1 100644 --- a/vortex-spatial/src/prune/intersects.rs +++ b/vortex-spatial/src/prune/intersects.rs @@ -3,10 +3,10 @@ //! `ST_Intersects(geom, const)` pruning. -use vortex_array::expr::BoundExpression; use vortex_array::scalar_fn::ScalarFnId; use vortex_array::scalar_fn::ScalarFnVTable; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; use vortex_expr::stats::rewrite::StatsRewriteRule; use vortex_session::VortexSession; @@ -56,13 +56,13 @@ mod tests { use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; - use vortex_array::expr::BoundExpression; - use vortex_array::expr::lit; - use vortex_array::expr::root; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::EmptyOptions; - use vortex_array::scalar_fn::ScalarFnVTableExt; use vortex_error::VortexResult; + use vortex_expr::BoundExpression; + use vortex_expr::ScalarFnExprExt; + use vortex_expr::lit; + use vortex_expr::root; use vortex_expr::stats::rewrite::BoundExpressionPruneExt; use vortex_expr::stats::rewrite::StatsRewriteRule; diff --git a/vortex-spatial/src/prune/mod.rs b/vortex-spatial/src/prune/mod.rs index f17bab453e4..44d1e3cad42 100644 --- a/vortex-spatial/src/prune/mod.rs +++ b/vortex-spatial/src/prune/mod.rs @@ -23,20 +23,20 @@ pub use intersects::SpatialIntersectsPrune; use vortex_array::VortexSessionExecute; use vortex_array::aggregate_fn::AggregateFnVTableExt; use vortex_array::aggregate_fn::EmptyOptions; -use vortex_array::expr::BoundExpression; -use vortex_array::expr::bound::binary; -use vortex_array::expr::bound::checked_add; -use vortex_array::expr::bound::get_item; -use vortex_array::expr::bound::gt; -use vortex_array::expr::bound::gt_eq; -use vortex_array::expr::bound::lit; -use vortex_array::expr::bound::lt; -use vortex_array::expr::bound::lt_eq; use vortex_array::scalar::Scalar; use vortex_array::scalar_fn::builtin::get_item::GetItem; use vortex_array::scalar_fn::builtin::literal::Literal; use vortex_array::scalar_fn::builtin::operators::Operator; use vortex_error::VortexResult; +use vortex_expr::BoundExpression; +use vortex_expr::bound::binary; +use vortex_expr::bound::checked_add; +use vortex_expr::bound::get_item; +use vortex_expr::bound::gt; +use vortex_expr::bound::gt_eq; +use vortex_expr::bound::lit; +use vortex_expr::bound::lt; +use vortex_expr::bound::lt_eq; use vortex_expr::stats::bound::stat; use vortex_functions::expr::bound::case_when; use vortex_functions::expr::bound::ext_storage; diff --git a/vortex-test/compat-gen/src/adapter.rs b/vortex-test/compat-gen/src/adapter.rs index 34bf5e72ae2..2f16822c669 100644 --- a/vortex-test/compat-gen/src/adapter.rs +++ b/vortex-test/compat-gen/src/adapter.rs @@ -14,7 +14,7 @@ use tokio::runtime::Runtime; use vortex::VortexSessionDefault; use vortex::array::ArrayRef; use vortex::array::MaskFuture; -use vortex::array::expr::root; +use vortex::expr::root; use vortex::file::OpenOptionsSessionExt; use vortex::file::WriteOptionsSessionExt; use vortex::io::session::RuntimeSessionExt; diff --git a/vortex/src/editions/tests.rs b/vortex/src/editions/tests.rs index 79869cbd389..79543aa95fd 100644 --- a/vortex/src/editions/tests.rs +++ b/vortex/src/editions/tests.rs @@ -20,9 +20,6 @@ use vortex_array::dtype::FieldName; use vortex_array::dtype::FieldPath; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; -use vortex_array::expr::col; -use vortex_array::expr::gt_eq; -use vortex_array::expr::lit; use vortex_array::expr::stats::Stat; use vortex_array::extension::datetime::Date; use vortex_array::extension::datetime::TimeUnit; @@ -44,6 +41,9 @@ use vortex_edition::test_harness::validate_edition; use vortex_error::VortexExpect; use vortex_error::VortexResult; use vortex_error::vortex_err; +use vortex_expr::col; +use vortex_expr::gt_eq; +use vortex_expr::lit; use vortex_file::OpenOptionsSessionExt; use vortex_file::WriteOptionsSessionExt; use vortex_file::WriteStrategyBuilder; diff --git a/vortex/src/lib.rs b/vortex/src/lib.rs index 28707736cfb..10f98c91579 100644 --- a/vortex/src/lib.rs +++ b/vortex/src/lib.rs @@ -66,7 +66,7 @@ //! use vortex::VortexSessionDefault; //! use vortex::array::{IntoArray, stream::ArrayStreamExt}; //! use vortex::array::arrays::PrimitiveArray; -//! use vortex::array::expr::{gt, lit, root}; +//! use vortex::expr::{gt, lit, root}; //! use vortex::array::validity::Validity; //! use vortex::buffer::{ByteBufferMut, buffer}; //! use vortex::file::{OpenOptionsSessionExt, WriteOptionsSessionExt}; @@ -105,11 +105,8 @@ pub use vortex_array::aggregate_fn; use vortex_array::aggregate_fn::session::AggregateFnSession; pub use vortex_array::compute; use vortex_array::dtype::session::DTypeSession; -// vortex::expr is in the process of having its dependencies inverted, and will eventually be -// pulled back out into a vortex_expr crate. pub mod expr { //! Expression trees, their optimizer, and the analyses and rewrites built on them. - pub use vortex_array::expr::*; pub use vortex_expr::*; pub use vortex_functions::expr::*; @@ -121,7 +118,7 @@ pub mod expr { /// Constructors for expressions whose children have already been bound and type-checked. pub mod bound { - pub use vortex_array::expr::bound::*; + pub use vortex_expr::bound::*; pub use vortex_functions::expr::bound::*; } } @@ -398,15 +395,15 @@ mod test { use vortex_array::arrays::StructArray; use vortex_array::assert_arrays_eq; use vortex_array::dtype::FieldNames; - use vortex_array::expr::gt; - use vortex_array::expr::lit; - use vortex_array::expr::root; - use vortex_array::expr::select; use vortex_array::stream::ArrayStreamExt; use vortex_array::validity::Validity; use vortex_btrblocks::BtrBlocksCompressorBuilder; use vortex_buffer::buffer; use vortex_error::VortexResult; + use vortex_expr::gt; + use vortex_expr::lit; + use vortex_expr::root; + use vortex_expr::select; use vortex_file::OpenOptionsSessionExt; use vortex_file::WriteOptionsSessionExt; use vortex_file::WriteStrategyBuilder; From 38b08d576543515a5d73f630d56f963a76229c57 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Mon, 9 Nov 2026 14:07:26 +0000 Subject: [PATCH 3/7] Move select and is_null out of vortex-array into vortex-functions `select` and `is_null` are not needed by vortex-array's own arrays, so they move to vortex-functions with their constructors, builder methods (`ArrayFunctions::is_null`, `ExprFunctions::is_null`), tests and pruning rewrites (`vortex_functions::pruning`). `not` stays in vortex-array, because validity inversion and the `sum_v2` aggregate negate arrays lazily. - `get_item` read through `select` by naming it. It now asks its child through the new `ScalarFnVTable::forwarded_field` hook, which reports the child whose field an output field passes through unchanged. `select` implements it, so `get_item(select(x))` still reduces to `get_item(x)` for arrays and expressions. - `IsNull` and `IsNotNull` share the and/or null-check reduction through the public `reduce_null_check`; each passes its own nested check. - The field-access analyses `referenced_field_paths` and `make_bound_free_field_annotator`, which look through `select`, move from vortex-expr to `vortex_layout::analysis`, next to the scans that use them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Ps4mp7KF5c4p66aoSoXx5j Signed-off-by: Will Manning Co-Authored-By: Will Manning Signed-off-by: Robert Kruszewski --- encodings/parquet-variant/src/arrow.rs | 1 + vortex-array/src/arrays/scalar_fn/rules.rs | 8 +- vortex-array/src/builtins.rs | 8 - .../src/scalar_fn/builtin/get_item.rs | 18 +- .../src/scalar_fn/builtin/is_not_null.rs | 29 ++- vortex-array/src/scalar_fn/builtin/mod.rs | 3 - vortex-array/src/scalar_fn/session.rs | 4 - vortex-array/src/scalar_fn/signature.rs | 7 + vortex-array/src/scalar_fn/typed.rs | 6 + vortex-array/src/scalar_fn/vtable.rs | 13 + vortex-expr/src/analysis/mod.rs | 4 - vortex-expr/src/builtins.rs | 8 - vortex-expr/src/exprs.rs | 76 ------ vortex-expr/src/lib.rs | 3 - vortex-expr/src/stats/bind.rs | 6 +- vortex-expr/src/stats/rewrite/builtins.rs | 83 ------ vortex-expr/src/stats/rewrite/fn_tests.rs | 15 -- vortex-expr/src/tests/binary.rs | 25 -- vortex-expr/src/tests/get_item.rs | 63 ----- vortex-expr/src/tests/is_not_null.rs | 13 - vortex-expr/src/tests/is_null.rs | 136 ---------- vortex-expr/src/tests/mod.rs | 2 - vortex-expr/src/transform/bound_partition.rs | 22 +- vortex-file/src/tests.rs | 2 +- vortex-file/src/v2/file_stats_reader.rs | 2 +- vortex-functions/src/builtins.rs | 15 ++ vortex-functions/src/case_when.rs | 4 +- vortex-functions/src/expr.rs | 77 ++++++ vortex-functions/src/ext_storage.rs | 2 +- .../builtin => vortex-functions/src}/field.rs | 3 +- .../src}/is_null.rs | 43 ++-- vortex-functions/src/lib.rs | 5 + vortex-functions/src/merge.rs | 69 ----- vortex-functions/src/pruning/is_null.rs | 156 ++++++++++++ vortex-functions/src/pruning/mod.rs | 7 + .../src}/select.rs | 57 +++-- vortex-functions/src/tests/is_null.rs | 236 ++++++++++++++++++ .../src/{tests.rs => tests/mod.rs} | 7 +- .../src/tests/select.rs | 85 ++++++- .../examples/reverse_index/pruning_tests.rs | 2 +- .../src/analysis/immediate_access.rs | 7 +- vortex-layout/src/analysis/mod.rs | 16 ++ .../src/analysis/referenced_field_paths.rs | 25 +- vortex-layout/src/analysis/tests.rs | 78 ++++++ vortex-layout/src/layouts/list/expr.rs | 2 +- vortex-layout/src/layouts/list/reader.rs | 2 +- vortex-layout/src/layouts/struct_/reader.rs | 12 +- vortex-layout/src/layouts/zoned/zone_map.rs | 2 +- vortex-layout/src/lib.rs | 1 + vortex-layout/src/scan/scan_builder.rs | 2 +- vortex-layout/src/scan/v2/tests.rs | 2 +- vortex-scan/src/plan/builtins/pack/mod.rs | 12 +- vortex-scan/src/plan/tests.rs | 2 +- vortex/src/lib.rs | 2 +- 54 files changed, 844 insertions(+), 646 deletions(-) delete mode 100644 vortex-expr/src/tests/is_null.rs rename {vortex-array/src/scalar_fn/builtin => vortex-functions/src}/field.rs (91%) rename {vortex-array/src/scalar_fn/builtin => vortex-functions/src}/is_null.rs (75%) create mode 100644 vortex-functions/src/pruning/is_null.rs rename {vortex-array/src/scalar_fn/builtin => vortex-functions/src}/select.rs (88%) create mode 100644 vortex-functions/src/tests/is_null.rs rename vortex-functions/src/{tests.rs => tests/mod.rs} (99%) rename {vortex-expr => vortex-functions}/src/tests/select.rs (74%) rename {vortex-expr => vortex-layout}/src/analysis/immediate_access.rs (95%) create mode 100644 vortex-layout/src/analysis/mod.rs rename {vortex-expr => vortex-layout}/src/analysis/referenced_field_paths.rs (94%) create mode 100644 vortex-layout/src/analysis/tests.rs diff --git a/encodings/parquet-variant/src/arrow.rs b/encodings/parquet-variant/src/arrow.rs index 041686f007b..36287bff2b0 100644 --- a/encodings/parquet-variant/src/arrow.rs +++ b/encodings/parquet-variant/src/arrow.rs @@ -53,6 +53,7 @@ use vortex_arrow::to_arrow_null_buffer; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_ensure; +use vortex_functions::builtins::ArrayFunctions; use vortex_session::registry::CachedId; use vortex_session::registry::Id; diff --git a/vortex-array/src/arrays/scalar_fn/rules.rs b/vortex-array/src/arrays/scalar_fn/rules.rs index 978f1d4efa0..8f987586c00 100644 --- a/vortex-array/src/arrays/scalar_fn/rules.rs +++ b/vortex-array/src/arrays/scalar_fn/rules.rs @@ -238,27 +238,25 @@ mod tests { .vortex_expect("construction") .into_array(); - let result = array.is_null().vortex_expect("is_null"); + let result = array.is_not_null().vortex_expect("is_not_null"); assert_arrays_eq!( result, - BoolArray::from_iter([false]), + BoolArray::from_iter([true]), &mut SESSION.create_execution_ctx() ); } #[test] - fn test_reduce_isnull() -> VortexResult<()> { + fn test_reduce_is_not_null() -> VortexResult<()> { let ctx = &mut SESSION.create_execution_ctx(); let validity = BoolArray::from_iter([true, false, true]).into_array(); - assert_arrays_eq!(validity.is_null()?, ConstantArray::new(false, 3), ctx); assert_arrays_eq!(validity.is_not_null()?, ConstantArray::new(true, 3), ctx); let buffer = buffer![1, 2, 3]; let nullable = PrimitiveArray::new(buffer, Validity::Array(validity.clone())).into_array(); assert_arrays_eq!(nullable.is_not_null()?, validity, ctx); - assert_arrays_eq!(nullable.is_null()?, validity.not()?, ctx); Ok(()) } diff --git a/vortex-array/src/builtins.rs b/vortex-array/src/builtins.rs index ad29a5d1eae..60bc534884c 100644 --- a/vortex-array/src/builtins.rs +++ b/vortex-array/src/builtins.rs @@ -23,7 +23,6 @@ use crate::scalar_fn::builtin::cast::Cast; use crate::scalar_fn::builtin::fill_null::FillNull; use crate::scalar_fn::builtin::get_item::GetItem; use crate::scalar_fn::builtin::is_not_null::IsNotNull; -use crate::scalar_fn::builtin::is_null::IsNull; use crate::scalar_fn::builtin::mask::Mask; use crate::scalar_fn::builtin::not::Not; use crate::scalar_fn::builtin::operators::Operator; @@ -39,9 +38,6 @@ pub trait ArrayBuiltins: Sized { /// Get item by field name (for struct types). fn get_item(&self, field_name: impl Into) -> VortexResult; - /// Is null check. - fn is_null(&self) -> VortexResult; - /// Is not null check. fn is_not_null(&self) -> VortexResult; @@ -91,10 +87,6 @@ impl ArrayBuiltins for ArrayRef { Ok(GetItem::try_new(self.clone(), field_name)?.into_array()) } - fn is_null(&self) -> VortexResult { - Ok(IsNull::new(self.clone()).into_array()) - } - fn is_not_null(&self) -> VortexResult { Ok(IsNotNull::new(self.clone()).into_array()) } diff --git a/vortex-array/src/scalar_fn/builtin/get_item.rs b/vortex-array/src/scalar_fn/builtin/get_item.rs index 429865181c9..38d2f3b8d69 100644 --- a/vortex-array/src/scalar_fn/builtin/get_item.rs +++ b/vortex-array/src/scalar_fn/builtin/get_item.rs @@ -34,8 +34,6 @@ use crate::scalar_fn::ScalarFnVTable; use crate::scalar_fn::ScalarFnVTableExt; use crate::scalar_fn::builtin::mask::Mask; use crate::scalar_fn::builtin::pack::Pack; -use crate::scalar_fn::builtin::select::FieldSelection; -use crate::scalar_fn::builtin::select::Select; use crate::scalar_fn::display::ExprDisplay; #[derive(Clone)] @@ -71,7 +69,8 @@ impl GetItem { ) } - /// Extract a constant field, bypass a selection, or read a field directly from a pack. + /// Extract a constant field, read through a function that forwards the field, or read a field + /// directly from a pack. fn maybe_apply_now( struct_node: &T, field_name: &FieldName, @@ -82,18 +81,11 @@ impl GetItem { )); } - if let Some(selection) = struct_node + if let Some(child_idx) = struct_node .scalar_fn() - .and_then(|scalar_fn| scalar_fn.as_opt::