Skip to content

Commit 1b34a5e

Browse files
authored
Expression enum and Root variant (#9247)
Makes `Root` an `Expression` variant instead of a `ScalarFnVTable` implementation. This is groundwork for lambdas and variables — those are also nodes whose dtype comes from the scope rather than from their children, and they need the same treatment. `Root` is not a scalar function. Its dtype comes from the scope rather than from its children (it has no children) and it is not executable. This PR makes `Expression` an enum instead of a struct. `Root` becomes a variant, and upcoming `Lambda` and `Variable` become variants as well. ```rust pub enum Expression { Scalar { scalar_fn: ScalarFnRef, children: Arc<Vec<Expression>> }, Root, } ``` This is a small breaking change. --------- Signed-off-by: Matt Katz <[email protected]>
1 parent 402a5ac commit 1b34a5e

25 files changed

Lines changed: 303 additions & 222 deletions

File tree

‎vortex-array/src/arrays/scalar_fn/vtable/validity.rs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::legacy_session;
2121
use crate::scalar_fn::TypedScalarFnInstance;
2222
use crate::scalar_fn::VecExecutionArgs;
2323
use crate::scalar_fn::fns::literal::Literal;
24-
use crate::scalar_fn::fns::root::Root;
2524
use crate::validity::Validity;
2625

2726
/// Execute an expression tree recursively.
@@ -32,10 +31,10 @@ fn execute_expr(
3231
row_count: usize,
3332
ctx: &mut ExecutionCtx,
3433
) -> VortexResult<ArrayRef> {
35-
// Handle Root expression - this should not happen in validity expressions
36-
if expr.is::<Root>() {
37-
vortex_bail!("Root expression cannot be executed in validity context");
38-
}
34+
// Only Expression::Scalar is executable
35+
let Some(scalar_fn) = expr.as_scalar() else {
36+
vortex_bail!("Only Expression::Scalar is executable");
37+
};
3938

4039
// Handle Literal expression - create a constant array
4140
if expr.is::<Literal>() {
@@ -52,7 +51,7 @@ fn execute_expr(
5251

5352
let args = VecExecutionArgs::new(inputs, row_count);
5453

55-
Ok(expr.scalar_fn().execute(&args, ctx)?.into_array())
54+
Ok(scalar_fn.execute(&args, ctx)?.into_array())
5655
}
5756

5857
impl ValidityVTable<ScalarFn> for ScalarFn {

‎vortex-array/src/expr/analysis/fallible.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ use crate::expr::label_tree;
88
pub fn label_is_fallible(expr: &Expression) -> BooleanLabels<'_> {
99
label_tree(
1010
expr,
11-
|expr| expr.signature().is_fallible(),
11+
|expr| match expr {
12+
Expression::Scalar { scalar_fn, .. } => scalar_fn.signature().is_fallible(),
13+
// The scope itself cannot fail.
14+
Expression::Root => false,
15+
},
1216
|acc, &child| acc | child,
1317
)
1418
}

‎vortex-array/src/expr/analysis/immediate_access.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::expr::BoundExpression;
99
use crate::expr::Expression;
1010
use crate::expr::analysis::AnnotationFn;
1111
use crate::scalar_fn::fns::get_item::GetItem;
12-
use crate::scalar_fn::fns::root::Root;
1312
use crate::scalar_fn::fns::select::Select;
1413

1514
/// Returns the "free fields" for this expression node.
@@ -26,9 +25,10 @@ use crate::scalar_fn::fns::select::Select;
2625
///
2726
/// # Annotation Rules
2827
///
29-
/// - **[`Select`]**: Returns the included field names if the child is [`Root`].
30-
/// - **[`GetItem`] on [`Root`]**: Returns `[field_name]` if the child is [`Root`].
31-
/// - **[`Root`]**: Returns all field names from `scope` (conservative over-approximation).
28+
/// - **[`Select`]**: Returns the included field names if the child is [`Expression::Root`].
29+
/// - **[`GetItem`] on the root**: Returns `[field_name]` if the child is [`Expression::Root`].
30+
/// - **[`Expression::Root`]**: Returns all field names from `scope` (conservative
31+
/// over-approximation).
3232
/// - **Everything else**: Returns empty (annotations aggregate from children automatically).
3333
///
3434
/// # Example
@@ -42,18 +42,18 @@ pub fn make_free_field_annotator(
4242
) -> impl AnnotationFn<Expression, Annotation = FieldName> {
4343
move |expr: &Expression| {
4444
if let Some(selection) = expr.as_opt::<Select>() {
45-
if expr.child(0).is::<Root>() {
45+
if expr.child(0).is_root() {
4646
return selection
4747
.normalize_to_included_fields(scope.names())
4848
.vortex_expect("Select fields must be valid for scope")
4949
.into_iter()
5050
.collect();
5151
}
5252
} else if let Some(field_name) = expr.as_opt::<GetItem>() {
53-
if expr.child(0).is::<Root>() {
53+
if expr.child(0).is_root() {
5454
return vec![field_name.clone()];
5555
}
56-
} else if expr.is::<Root>() {
56+
} else if expr.is_root() {
5757
return scope.names().iter().cloned().collect();
5858
}
5959

‎vortex-array/src/expr/analysis/strict.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ use crate::expr::Expression;
1212
pub fn label_strict(expr: &Expression) -> BooleanLabels<'_> {
1313
label_tree(
1414
expr,
15-
|expr| expr.signature().is_strict(),
15+
|expr| match expr {
16+
Expression::Scalar { scalar_fn, .. } => scalar_fn.signature().is_strict(),
17+
// Vacuously strict.
18+
Expression::Root => true,
19+
},
1620
|acc, &child| acc & child,
1721
)
1822
}

‎vortex-array/src/expr/bound_expression.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use crate::expr::traversal::TraversalOrder;
2222
use crate::expr::traversal::pre_order_visit_down;
2323
use crate::scalar_fn::ScalarFnRef;
2424
use crate::scalar_fn::ScalarFnVTable;
25-
use crate::scalar_fn::fns::root::Root;
2625
use crate::stats::rewrite::StatsRewriteCtx;
2726

2827
/// An [`Expression`] that has been type-checked against a [`Scope`].
@@ -264,7 +263,7 @@ impl Expression {
264263

265264
/// Bind this expression against an explicit [`Scope`].
266265
pub fn bind_scope(&self, scope: &Scope) -> VortexResult<BoundExpression> {
267-
if self.is::<Root>() {
266+
if self.is_root() {
268267
return Ok(BoundExpression::new_root(scope.root().clone()));
269268
}
270269

@@ -273,7 +272,10 @@ impl Expression {
273272
.iter()
274273
.map(|child| child.bind_scope(scope))
275274
.try_collect()?;
276-
BoundExpression::try_new(self.scalar_fn().clone(), children)
275+
let scalar_fn = self
276+
.as_scalar()
277+
.vortex_expect("root was handled above, so this is a scalar node");
278+
BoundExpression::try_new(scalar_fn.clone(), children)
277279
}
278280
}
279281

‎vortex-array/src/expr/display.rs‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use vortex_utils::tree::write_branch_tree;
1111
use crate::expr::BoundExpression;
1212
use crate::expr::BoundKind;
1313
use crate::expr::Expression;
14-
use crate::expr::root;
1514
use crate::scalar_fn::ChildName;
1615

1716
pub enum DisplayFormat {
@@ -59,17 +58,26 @@ trait DisplayTreeNode: Sized {
5958
fn fmt_tree_node(&self, f: &mut Formatter<'_>) -> fmt::Result;
6059
}
6160

61+
/// Tree-display label for the scope root.
62+
const ROOT_DISPLAY: &str = "vortex.root()";
63+
6264
impl DisplayTreeNode for Expression {
6365
fn tree_children(&self) -> &[Self] {
64-
Expression::children(self).as_slice()
66+
Expression::children(self)
6567
}
6668

6769
fn tree_child_name(&self, index: usize) -> ChildName {
68-
self.scalar_fn().signature().child_name(index)
70+
match self {
71+
Expression::Scalar { scalar_fn, .. } => scalar_fn.signature().child_name(index),
72+
Expression::Root => unreachable!("the scope root has no children"),
73+
}
6974
}
7075

7176
fn fmt_tree_node(&self, f: &mut Formatter<'_>) -> fmt::Result {
72-
Display::fmt(self.scalar_fn(), f)
77+
match self {
78+
Expression::Scalar { scalar_fn, .. } => Display::fmt(scalar_fn, f),
79+
Expression::Root => write!(f, "{ROOT_DISPLAY}"),
80+
}
7381
}
7482
}
7583

@@ -88,7 +96,7 @@ impl DisplayTreeNode for BoundExpression {
8896
fn fmt_tree_node(&self, f: &mut Formatter<'_>) -> fmt::Result {
8997
match self.kind() {
9098
BoundKind::Scalar { scalar_fn, .. } => Display::fmt(scalar_fn, f),
91-
BoundKind::Root => Display::fmt(root().scalar_fn(), f),
99+
BoundKind::Root => write!(f, "{ROOT_DISPLAY}"),
92100
}
93101
}
94102
}

0 commit comments

Comments
 (0)