|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Expression planning against an input scope. |
| 5 | +
|
| 6 | +use vortex_error::VortexResult; |
| 7 | +use vortex_error::vortex_bail; |
| 8 | + |
| 9 | +use crate::dtype::DType; |
| 10 | +use crate::expr::Expression; |
| 11 | +use crate::expr::transform::coerce_expression; |
| 12 | + |
| 13 | +/// Plan an expression against an input [`DType`]. |
| 14 | +/// |
| 15 | +/// Planning typeifies the expression by inserting casts required by scalar functions, simplifies |
| 16 | +/// the resulting tree, and verifies that the planned expression has a valid return type for the |
| 17 | +/// provided scope. |
| 18 | +pub fn plan_expression(expr: Expression, scope: &DType) -> VortexResult<Expression> { |
| 19 | + let expr = coerce_expression(expr, scope)?; |
| 20 | + let expr = expr.optimize_recursive(scope)?; |
| 21 | + expr.return_dtype(scope)?; |
| 22 | + Ok(expr) |
| 23 | +} |
| 24 | + |
| 25 | +/// Plan a filter expression against an input [`DType`]. |
| 26 | +/// |
| 27 | +/// This performs the same planning pass as [`plan_expression`] and then requires the expression to |
| 28 | +/// return a Boolean value. |
| 29 | +pub fn plan_filter_expression(expr: Expression, scope: &DType) -> VortexResult<Expression> { |
| 30 | + let expr = plan_expression(expr, scope)?; |
| 31 | + let dtype = expr.return_dtype(scope)?; |
| 32 | + if !matches!(dtype, DType::Bool(_)) { |
| 33 | + vortex_bail!("filter expression must return bool, got {}", dtype); |
| 34 | + } |
| 35 | + Ok(expr) |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod tests { |
| 40 | + use vortex_error::VortexResult; |
| 41 | + |
| 42 | + use crate::dtype::DType; |
| 43 | + use crate::dtype::Nullability::NonNullable; |
| 44 | + use crate::dtype::Nullability::Nullable; |
| 45 | + use crate::dtype::PType; |
| 46 | + use crate::dtype::StructFields; |
| 47 | + use crate::expr::col; |
| 48 | + use crate::expr::lit; |
| 49 | + use crate::expr::plan_expression; |
| 50 | + use crate::expr::plan_filter_expression; |
| 51 | + use crate::scalar::Scalar; |
| 52 | + use crate::scalar_fn::ScalarFnVTableExt; |
| 53 | + use crate::scalar_fn::fns::binary::Binary; |
| 54 | + use crate::scalar_fn::fns::cast::Cast; |
| 55 | + use crate::scalar_fn::fns::operators::Operator; |
| 56 | + |
| 57 | + fn scope() -> DType { |
| 58 | + DType::Struct( |
| 59 | + StructFields::new( |
| 60 | + ["i32", "i64", "u8", "flag"].into(), |
| 61 | + vec![ |
| 62 | + DType::Primitive(PType::I32, NonNullable), |
| 63 | + DType::Primitive(PType::I64, NonNullable), |
| 64 | + DType::Primitive(PType::U8, NonNullable), |
| 65 | + DType::Bool(NonNullable), |
| 66 | + ], |
| 67 | + ), |
| 68 | + NonNullable, |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn mixed_numeric_comparison_inserts_cast() -> VortexResult<()> { |
| 74 | + let scope = scope(); |
| 75 | + let expr = Binary.new_expr(Operator::Lt, [col("i32"), col("i64")]); |
| 76 | + |
| 77 | + let planned = plan_filter_expression(expr, &scope)?; |
| 78 | + |
| 79 | + assert!(planned.child(0).is::<Cast>()); |
| 80 | + assert_eq!( |
| 81 | + planned.child(0).return_dtype(&scope)?, |
| 82 | + DType::Primitive(PType::I64, NonNullable) |
| 83 | + ); |
| 84 | + assert!(!planned.child(1).is::<Cast>()); |
| 85 | + Ok(()) |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn mixed_numeric_arithmetic_inserts_casts() -> VortexResult<()> { |
| 90 | + let scope = scope(); |
| 91 | + let expr = Binary.new_expr(Operator::Add, [col("u8"), col("i32")]); |
| 92 | + |
| 93 | + let planned = plan_expression(expr, &scope)?; |
| 94 | + |
| 95 | + assert!(planned.child(0).is::<Cast>()); |
| 96 | + assert_eq!( |
| 97 | + planned.return_dtype(&scope)?, |
| 98 | + DType::Primitive(PType::I64, NonNullable) |
| 99 | + ); |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn literal_values_are_coerced_against_column_types() -> VortexResult<()> { |
| 105 | + let scope = scope(); |
| 106 | + let expr = Binary.new_expr(Operator::Eq, [col("i32"), lit(1i64)]); |
| 107 | + |
| 108 | + let planned = plan_filter_expression(expr, &scope)?; |
| 109 | + |
| 110 | + assert!(!planned.child(0).is::<Cast>()); |
| 111 | + assert!(planned.child(1).is::<Cast>()); |
| 112 | + assert_eq!( |
| 113 | + planned.child(1).return_dtype(&scope)?, |
| 114 | + DType::Primitive(PType::I32, NonNullable) |
| 115 | + ); |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn null_literals_are_typed_from_context() -> VortexResult<()> { |
| 121 | + let scope = scope(); |
| 122 | + let expr = Binary.new_expr(Operator::Eq, [col("i32"), lit(Scalar::null(DType::Null))]); |
| 123 | + |
| 124 | + let planned = plan_filter_expression(expr, &scope)?; |
| 125 | + |
| 126 | + assert!(planned.child(1).is::<Cast>()); |
| 127 | + assert_eq!( |
| 128 | + planned.child(1).return_dtype(&scope)?, |
| 129 | + DType::Primitive(PType::I32, Nullable) |
| 130 | + ); |
| 131 | + Ok(()) |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn boolean_and_preserves_boolean_inputs() -> VortexResult<()> { |
| 136 | + let scope = scope(); |
| 137 | + let expr = Binary.new_expr(Operator::And, [col("flag"), col("flag")]); |
| 138 | + |
| 139 | + let planned = plan_filter_expression(expr, &scope)?; |
| 140 | + |
| 141 | + assert_eq!(planned.return_dtype(&scope)?, DType::Bool(NonNullable)); |
| 142 | + assert!(!planned.child(0).is::<Cast>()); |
| 143 | + assert!(!planned.child(1).is::<Cast>()); |
| 144 | + Ok(()) |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn filter_planning_rejects_non_boolean_outputs() { |
| 149 | + let scope = scope(); |
| 150 | + let expr = Binary.new_expr(Operator::Add, [col("i32"), lit(1i32)]); |
| 151 | + |
| 152 | + let err = plan_filter_expression(expr, &scope).unwrap_err(); |
| 153 | + |
| 154 | + assert!( |
| 155 | + err.to_string() |
| 156 | + .contains("filter expression must return bool") |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn logical_operators_reject_non_boolean_inputs() { |
| 162 | + let scope = scope(); |
| 163 | + let expr = Binary.new_expr(Operator::And, [col("i32"), col("i64")]); |
| 164 | + |
| 165 | + let err = plan_filter_expression(expr, &scope).unwrap_err(); |
| 166 | + |
| 167 | + assert!( |
| 168 | + err.to_string() |
| 169 | + .contains("logical operation requires boolean operands") |
| 170 | + ); |
| 171 | + } |
| 172 | +} |
0 commit comments