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