Skip to content
Open
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
42 changes: 34 additions & 8 deletions vortex-array/src/expr/exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;

use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_panic;
use vortex_utils::iter::ReduceBalancedIterExt;
Expand Down Expand Up @@ -254,33 +255,58 @@ pub fn bound_case_when_no_else(
}

/// Creates an n-ary CASE WHEN expression from WHEN/THEN pairs and an optional ELSE value.
///
/// # Panics
///
/// Panics if `when_then_pairs` is empty or has more than `u32::MAX` pairs. Use
/// [`try_nested_case_when`] when the pairs come from input the caller does not control.
pub fn nested_case_when(
when_then_pairs: Vec<(Expression, Expression)>,
else_value: Option<Expression>,
) -> Expression {
assert!(
try_nested_case_when(when_then_pairs, else_value)
.vortex_expect("nested_case_when requires between 1 and u32::MAX when/then pairs")
}

/// Creates an n-ary CASE WHEN expression from WHEN/THEN pairs and an optional ELSE value.
///
/// Returns an error if `when_then_pairs` is empty or has more than `u32::MAX` pairs.
///
/// ```rust
/// # use vortex_array::expr::{lit, try_nested_case_when};
/// assert!(try_nested_case_when(vec![(lit(true), lit(1_i32))], Some(lit(0_i32))).is_ok());
/// assert!(try_nested_case_when(vec![], Some(lit(0_i32))).is_err());
/// ```
pub fn try_nested_case_when(
when_then_pairs: Vec<(Expression, Expression)>,
else_value: Option<Expression>,
) -> VortexResult<Expression> {
vortex_ensure!(
!when_then_pairs.is_empty(),
"nested_case_when requires at least one when/then pair"
);
let Ok(num_when_then_pairs) = u32::try_from(when_then_pairs.len()) else {
vortex_bail!(
"nested_case_when has too many when/then pairs: {}",
when_then_pairs.len()
);
};

let has_else = else_value.is_some();
let mut children = Vec::with_capacity(when_then_pairs.len() * 2 + usize::from(has_else));
for (condition, then_value) in &when_then_pairs {
children.push(condition.clone());
children.push(then_value.clone());
for (condition, then_value) in when_then_pairs {
children.push(condition);
children.push(then_value);
}
if let Some(else_expr) = else_value {
children.push(else_expr);
}

let Ok(num_when_then_pairs) = u32::try_from(when_then_pairs.len()) else {
vortex_panic!("nested_case_when has too many when/then pairs");
};
let options = CaseWhenOptions {
num_when_then_pairs,
has_else,
};
CaseWhen.new_expr(options, children)
Ok(CaseWhen.new_expr(options, children))
}

/// Creates a bound n-ary CASE WHEN expression from WHEN/THEN pairs and an optional ELSE value.
Expand Down
1 change: 1 addition & 0 deletions vortex-array/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub use exprs::root;
pub use exprs::select;
pub use exprs::select_exclude;
pub use exprs::strpos;
pub use exprs::try_nested_case_when;
pub use exprs::var;
pub use exprs::variant_get;
pub use exprs::zip_expr;
Expand Down
28 changes: 28 additions & 0 deletions vortex-array/src/scalar_fn/fns/case_when.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ mod tests {
use crate::expr::proto::ExprSerializeProtoExt;
use crate::expr::root;
use crate::expr::test_harness;
use crate::expr::try_nested_case_when;
use crate::scalar::Scalar;
use crate::scalar_fn::ScalarFnVTableExt;
use crate::scalar_fn::VecExecutionArgs;
Expand Down Expand Up @@ -1135,6 +1136,33 @@ mod tests {
assert_arrays_eq!(result, buffer![10i32, 0, 30, 0, 0].into_array(), &mut ctx);
}

#[test]
fn try_nested_case_when_rejects_empty_pairs() {
let err = try_nested_case_when(vec![], Some(lit(0i32))).unwrap_err();
assert!(
err.to_string().contains("at least one when/then pair"),
"unexpected error: {err}"
);
assert!(try_nested_case_when(vec![], None).is_err());
}

#[test]
fn try_nested_case_when_matches_nested_case_when() -> VortexResult<()> {
let pairs = vec![
(eq(get_item("value", root()), lit(1i32)), lit(10i32)),
(eq(get_item("value", root()), lit(3i32)), lit(30i32)),
];
let expr = try_nested_case_when(pairs.clone(), Some(lit(0i32)))?;
assert_eq!(expr, nested_case_when(pairs, Some(lit(0i32))));
Ok(())
}

#[test]
#[should_panic(expected = "at least one when/then pair")]
fn nested_case_when_panics_on_empty_pairs() {
drop(nested_case_when(vec![], None));
}

#[test]
fn test_evaluate_nary_first_match_wins() {
let mut ctx = SESSION.create_execution_ctx();
Expand Down