Skip to content

Commit 9b7880f

Browse files
committed
Expressions
Signed-off-by: Nicholas Gates <[email protected]>
1 parent 71d8418 commit 9b7880f

File tree

8 files changed

+50
-50
lines changed

8 files changed

+50
-50
lines changed

vortex-array/src/arrays/scalar_fn/array.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use vortex_error::vortex_ensure;
88
use crate::Array;
99
use crate::ArrayRef;
1010
use crate::arrays::ScalarFnVTable;
11-
use crate::expr::BoundExpression;
11+
use crate::expr::ScalarFn;
1212
use crate::stats::ArrayStats;
1313
use crate::vtable::ArrayVTable;
1414
use crate::vtable::ArrayVTableExt;
@@ -17,7 +17,7 @@ use crate::vtable::ArrayVTableExt;
1717
pub struct ScalarFnArray {
1818
// NOTE(ngates): we should fix vtables so we don't have to hold this
1919
pub(super) vtable: ArrayVTable,
20-
pub(super) bound: BoundExpression,
20+
pub(super) bound: ScalarFn,
2121
pub(super) dtype: DType,
2222
pub(super) len: usize,
2323
pub(super) children: Vec<ArrayRef>,
@@ -26,11 +26,7 @@ pub struct ScalarFnArray {
2626

2727
impl ScalarFnArray {
2828
/// Create a new ScalarFnArray from a scalar function and its children.
29-
pub fn try_new(
30-
bound: BoundExpression,
31-
children: Vec<ArrayRef>,
32-
len: usize,
33-
) -> VortexResult<Self> {
29+
pub fn try_new(bound: ScalarFn, children: Vec<ArrayRef>, len: usize) -> VortexResult<Self> {
3430
let arg_dtypes: Vec<_> = children.iter().map(|c| c.dtype().clone()).collect();
3531
let dtype = bound.return_dtype(&arg_dtypes)?;
3632

vortex-array/src/arrays/scalar_fn/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
use vortex_dtype::DType;
55

6-
use crate::expr::BoundExpression;
6+
use crate::expr::ScalarFn;
77

88
#[derive(Clone, Debug)]
99
pub struct ScalarFnMetadata {
10-
pub(super) bound: BoundExpression,
10+
pub(super) bound: ScalarFn,
1111
pub(super) child_dtypes: Vec<DType>,
1212
}

vortex-array/src/arrays/scalar_fn/vtable/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use crate::arrays::scalar_fn::array::ScalarFnArray;
2929
use crate::arrays::scalar_fn::metadata::ScalarFnMetadata;
3030
use crate::execution::ExecutionCtx;
3131
use crate::expr;
32-
use crate::expr::BoundExpression;
3332
use crate::expr::ExecutionArgs;
3433
use crate::expr::ExprVTable;
34+
use crate::expr::ScalarFn;
3535
use crate::optimizer::rules::MatchKey;
3636
use crate::optimizer::rules::Matcher;
3737
use crate::serde::ArrayChildren;
@@ -162,7 +162,7 @@ pub trait ScalarFnArrayExt: expr::VTable {
162162
options: Self::Options,
163163
children: impl Into<Vec<ArrayRef>>,
164164
) -> VortexResult<ArrayRef> {
165-
let bound = BoundExpression::new_static(self, options);
165+
let bound = ScalarFn::new_static(self, options);
166166

167167
let children = children.into();
168168
vortex_ensure!(

vortex-array/src/expr/display.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::fmt::Display;
55
use std::fmt::Formatter;
66
use std::ops::Deref;
77

8-
use crate::expr::BoundExpression;
98
use crate::expr::Expression;
9+
use crate::expr::ScalarFn;
1010

1111
pub enum DisplayFormat {
1212
Compact,
@@ -19,7 +19,7 @@ impl Display for DisplayTreeExpr<'_> {
1919
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2020
pub use termtree::Tree;
2121
fn make_tree(expr: &Expression) -> Result<Tree<String>, std::fmt::Error> {
22-
let bound: &BoundExpression = expr.deref();
22+
let bound: &ScalarFn = expr.deref();
2323
let node_name = format!("{}", bound);
2424

2525
// Get child names for display purposes

vortex-array/src/expr/expression.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use vortex_error::vortex_ensure;
1717

1818
use crate::ArrayRef;
1919
use crate::expr::Root;
20+
use crate::expr::ScalarFn;
2021
use crate::expr::StatsCatalog;
2122
use crate::expr::VTable;
22-
use crate::expr::bound::BoundExpression;
2323
use crate::expr::display::DisplayTreeExpr;
2424
use crate::expr::stats::Stat;
2525

@@ -29,36 +29,39 @@ use crate::expr::stats::Stat;
2929
/// expression consists of an encoding (vtable), heap-allocated metadata, and child expressions.
3030
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
3131
pub struct Expression {
32-
/// The bound expression for this node.
33-
bound: BoundExpression,
32+
/// The scalar fn for this node.
33+
scalar_fn: ScalarFn,
3434
/// Any children of this expression.
3535
children: Arc<[Expression]>,
3636
}
3737

3838
impl Deref for Expression {
39-
type Target = BoundExpression;
39+
type Target = ScalarFn;
4040

4141
fn deref(&self) -> &Self::Target {
42-
&self.bound
42+
&self.scalar_fn
4343
}
4444
}
4545

4646
impl Expression {
47-
/// Create a new expression node from a bound expression and its children.
47+
/// Create a new expression node from a scalar_fn expression and its children.
4848
pub fn try_new(
49-
bound: BoundExpression,
49+
scalar_fn: ScalarFn,
5050
children: impl Into<Arc<[Expression]>>,
5151
) -> VortexResult<Self> {
5252
let children: Arc<[Expression]> = children.into();
5353

5454
vortex_ensure!(
55-
bound.signature().arity().matches(children.len()),
55+
scalar_fn.signature().arity().matches(children.len()),
5656
"Expression arity mismatch: expected {} children but got {}",
57-
bound.signature().arity(),
57+
scalar_fn.signature().arity(),
5858
children.len()
5959
);
6060

61-
Ok(Self { bound, children })
61+
Ok(Self {
62+
scalar_fn,
63+
children,
64+
})
6265
}
6366

6467
/// Returns true if this expression is of the given vtable type.
@@ -77,6 +80,11 @@ impl Expression {
7780
.vortex_expect("Expression options type mismatch")
7881
}
7982

83+
/// Returns the scalar fn vtable for this expression.
84+
pub fn scalar_fn(&self) -> &ScalarFn {
85+
&self.scalar_fn
86+
}
87+
8088
/// Returns the children of this expression.
8189
pub fn children(&self) -> &Arc<[Expression]> {
8290
&self.children
@@ -111,15 +119,15 @@ impl Expression {
111119
.iter()
112120
.map(|c| c.return_dtype(scope))
113121
.try_collect()?;
114-
self.bound.return_dtype(&dtypes)
122+
self.scalar_fn.return_dtype(&dtypes)
115123
}
116124

117125
/// Evaluates the expression in the given scope, returning an array.
118126
pub fn evaluate(&self, scope: &ArrayRef) -> VortexResult<ArrayRef> {
119127
if self.is::<Root>() {
120128
return Ok(scope.clone());
121129
}
122-
self.bound.evaluate(self, scope)
130+
self.scalar_fn.evaluate(self, scope)
123131
}
124132

125133
/// An expression over zone-statistics which implies all records in the zone evaluate to false.

vortex-array/src/expr/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use std::hash::Hash;
1414
use std::hash::Hasher;
15-
use std::ptr;
15+
use std::sync::Arc;
1616

1717
use arcref::ArcRef;
1818
use vortex_dtype::FieldName;
@@ -26,7 +26,6 @@ pub mod aliases;
2626
pub mod analysis;
2727
#[cfg(feature = "arbitrary")]
2828
pub mod arbitrary;
29-
mod bound;
3029
pub mod display;
3130
mod expression;
3231
mod exprs;
@@ -35,6 +34,7 @@ pub mod forms;
3534
mod options;
3635
pub mod proto;
3736
pub mod pruning;
37+
mod scalar_fn;
3838
pub mod session;
3939
mod signature;
4040
mod simplify;
@@ -44,10 +44,10 @@ pub mod traversal;
4444
mod vtable;
4545

4646
pub use analysis::*;
47-
pub use bound::*;
4847
pub use expression::*;
4948
pub use exprs::*;
5049
pub use pruning::StatsCatalog;
50+
pub use scalar_fn::*;
5151
pub use vtable::*;
5252

5353
pub type ExprId = ArcRef<str>;
@@ -85,14 +85,13 @@ fn split_inner(expr: &Expression, exprs: &mut Vec<Expression>) {
8585
}
8686
}
8787

88-
/// An expression wrapper that performs pointer equality.
88+
/// An expression wrapper that performs pointer equality on child expressions.
8989
#[derive(Clone)]
9090
pub struct ExactExpr(pub Expression);
91-
9291
impl PartialEq for ExactExpr {
9392
fn eq(&self, other: &Self) -> bool {
94-
self.0.id() == other.0.id()
95-
&& ptr::addr_eq(self.0.options().as_any(), other.0.options().as_any())
93+
self.0.scalar_fn() == other.0.scalar_fn()
94+
&& Arc::ptr_eq(self.0.children(), other.0.children())
9695
}
9796
}
9897
impl Eq for ExactExpr {}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ use crate::expr::options::ExpressionOptions;
2424
use crate::expr::signature::ExpressionSignature;
2525

2626
/// An instance of an expression bound to some invocation options.
27-
pub struct BoundExpression {
27+
pub struct ScalarFn {
2828
vtable: ExprVTable,
2929
options: Box<dyn Any + Send + Sync>,
3030
}
3131

32-
impl BoundExpression {
32+
impl ScalarFn {
3333
/// Create a new bound expression from raw vtable and options.
3434
///
3535
/// # Safety
@@ -103,16 +103,16 @@ impl BoundExpression {
103103
}
104104
}
105105

106-
impl Clone for BoundExpression {
106+
impl Clone for ScalarFn {
107107
fn clone(&self) -> Self {
108-
BoundExpression {
108+
ScalarFn {
109109
vtable: self.vtable.clone(),
110110
options: self.vtable.as_dyn().options_clone(self.options.deref()),
111111
}
112112
}
113113
}
114114

115-
impl Debug for BoundExpression {
115+
impl Debug for ScalarFn {
116116
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
117117
f.debug_struct("BoundExpression")
118118
.field("vtable", &self.vtable)
@@ -128,7 +128,7 @@ impl Debug for BoundExpression {
128128
}
129129
}
130130

131-
impl Display for BoundExpression {
131+
impl Display for ScalarFn {
132132
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
133133
write!(f, "{}(", self.vtable.id())?;
134134
self.vtable
@@ -138,7 +138,7 @@ impl Display for BoundExpression {
138138
}
139139
}
140140

141-
impl PartialEq for BoundExpression {
141+
impl PartialEq for ScalarFn {
142142
fn eq(&self, other: &Self) -> bool {
143143
self.vtable == other.vtable
144144
&& self
@@ -147,9 +147,9 @@ impl PartialEq for BoundExpression {
147147
.options_eq(self.options.deref(), other.options.deref())
148148
}
149149
}
150-
impl Eq for BoundExpression {}
150+
impl Eq for ScalarFn {}
151151

152-
impl Hash for BoundExpression {
152+
impl Hash for ScalarFn {
153153
fn hash<H: Hasher>(&self, state: &mut H) {
154154
self.vtable.hash(state);
155155
self.vtable

vortex-array/src/expr/vtable.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use vortex_vector::VectorOps;
2222
use crate::ArrayRef;
2323
use crate::expr::ExprId;
2424
use crate::expr::StatsCatalog;
25-
use crate::expr::bound::BoundExpression;
2625
use crate::expr::expression::Expression;
26+
use crate::expr::scalar_fn::ScalarFn;
2727
use crate::expr::stats::Stat;
2828

2929
/// This trait defines the interface for expression vtables, including methods for
@@ -247,9 +247,9 @@ impl Display for EmptyOptions {
247247

248248
/// Factory functions for static vtables.
249249
pub trait VTableExt: VTable {
250-
/// Bind this vtable with the given options into a [`BoundExpression`].
251-
fn bind(&'static self, options: Self::Options) -> BoundExpression {
252-
BoundExpression::new_static(self, options)
250+
/// Bind this vtable with the given options into a [`ScalarFn`].
251+
fn bind(&'static self, options: Self::Options) -> ScalarFn {
252+
ScalarFn::new_static(self, options)
253253
}
254254

255255
/// Create a new expression with this vtable and the given options and children.
@@ -535,12 +535,9 @@ impl ExprVTable {
535535
}
536536

537537
/// Deserialize an options of this expression vtable from metadata.
538-
pub fn deserialize(&self, metadata: &[u8]) -> VortexResult<BoundExpression> {
538+
pub fn deserialize(&self, metadata: &[u8]) -> VortexResult<ScalarFn> {
539539
Ok(unsafe {
540-
BoundExpression::new_unchecked(
541-
self.clone(),
542-
self.as_dyn().options_deserialize(metadata)?,
543-
)
540+
ScalarFn::new_unchecked(self.clone(), self.as_dyn().options_deserialize(metadata)?)
544541
})
545542
}
546543
}

0 commit comments

Comments
 (0)