Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions vortex-array/src/expr/analysis/fallible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ pub fn label_is_fallible(expr: &Expression) -> BooleanLabels<'_> {
expr,
|expr| match expr {
Expression::Scalar { scalar_fn, .. } => scalar_fn.signature().is_fallible(),
// The scope itself cannot fail.
Expression::Root => false,
// These add no fallibility of their own. Note this is the *self* label: a lambda's
// body is one of its children, so the folded label at a lambda node is the body's
// fallibility. A higher-order function therefore picks the body up through the
// ordinary fold instead of walking it by hand.
Expression::Root | Expression::Variable(_) | Expression::Lambda(_) => false,
},
|acc, &child| acc | child,
)
Expand Down Expand Up @@ -82,3 +85,27 @@ mod tests {
assert_eq!(labels.get(&expr), Some(&false));
}
}

#[cfg(test)]
mod lambda_tests {
use super::*;
use crate::expr::checked_add;
use crate::expr::lambda;
use crate::expr::lit;
use crate::expr::var;

/// A lambda contributes no fallibility of its own, but its body is one of its children, so the
/// label at the lambda node is the body's. That is what lets a future higher-order function
/// pick the body up through the ordinary fold rather than walking it by hand.
#[test]
fn a_lambdas_label_is_its_bodys_fallibility() {
let fallible = Expression::from(lambda(["x"], checked_add(var("x"), lit(1i32))));
assert_eq!(label_is_fallible(&fallible).get(&fallible), Some(&true));

let infallible = Expression::from(lambda(["x"], var("x")));
assert_eq!(
label_is_fallible(&infallible).get(&infallible),
Some(&false)
);
}
}
44 changes: 43 additions & 1 deletion vortex-array/src/expr/analysis/immediate_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ pub fn make_bound_free_field_annotator(
) -> impl AnnotationFn<BoundExpression, Annotation = FieldName> {
move |expr: &BoundExpression| {
let Some(scalar_fn) = expr.as_scalar() else {
return scope.names().iter().cloned().collect();
// Only the scope root reads every field. A variable resolves against a frame, so it
// reads none of them, and saying otherwise would defeat column pruning.
return if expr.is_root() {
scope.names().iter().cloned().collect()
} else {
vec![]
};
};

if let Some(selection) = scalar_fn.as_opt::<Select>() {
Expand All @@ -87,3 +93,39 @@ pub fn make_bound_free_field_annotator(
vec![]
}
}

#[cfg(test)]
mod variable_tests {
use vortex_error::VortexResult;

use super::*;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::expr::Scope;
use crate::expr::lambda;
use crate::expr::test_harness::struct_dtype;
use crate::expr::var;

/// Only the scope root reads every field. A variable resolves against a frame, so treating it
/// like a root would mark a lambda body as reading the whole struct and defeat column pruning.
#[test]
fn a_bound_variable_accesses_no_root_fields() -> VortexResult<()> {
let scope_dtype = struct_dtype();
let fields = scope_dtype
.as_struct_fields_opt()
.vortex_expect("test scope is a struct");

let bound = lambda(["x"], var("x")).bind(
&Scope::new(scope_dtype.clone()),
[DType::Primitive(PType::I32, Nullability::NonNullable)],
)?;

let annotator = make_bound_free_field_annotator(fields);
assert!(
annotator(bound.body()).is_empty(),
"a variable should read no root fields"
);
Ok(())
}
}
2 changes: 1 addition & 1 deletion vortex-array/src/expr/analysis/referenced_field_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl NodeFolderContext for ReferencedFieldPaths {
{
let child_fields = node.children()[0]
.dtype()
.as_struct_fields_opt()
.and_then(|dtype| dtype.as_struct_fields_opt())
.ok_or_else(|| vortex_err!("Select child is not a struct"))?;
let included_fields = selection.normalize_to_included_fields(child_fields.names())?;

Expand Down
7 changes: 5 additions & 2 deletions vortex-array/src/expr/analysis/strict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ pub fn label_strict(expr: &Expression) -> BooleanLabels<'_> {
expr,
|expr| match expr {
Expression::Scalar { scalar_fn, .. } => scalar_fn.signature().is_strict(),
// Vacuously strict.
Expression::Root => true,
// Vacuously strict: nullary, so no argument can be null.
Expression::Root | Expression::Variable { .. } => true,
// A lambda is not a value, so strictness is not a meaningful question about it. Take
// the contract's conservative default rather than inventing an answer.
Expression::Lambda { .. } => false,
},
|acc, &child| acc & child,
)
Expand Down
Loading