-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathbetween.rs
More file actions
243 lines (218 loc) · 7.03 KB
/
between.rs
File metadata and controls
243 lines (218 loc) · 7.03 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt::Formatter;
use prost::Message;
use vortex_dtype::DType;
use vortex_dtype::DType::Bool;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_err;
use vortex_proto::expr as pb;
use vortex_session::VortexSession;
use crate::ArrayRef;
use crate::compute::BetweenOptions;
use crate::compute::between as between_compute;
use crate::expr::Arity;
use crate::expr::ChildName;
use crate::expr::ExecutionArgs;
use crate::expr::ExprId;
use crate::expr::StatsCatalog;
use crate::expr::VTable;
use crate::expr::VTableExt;
use crate::expr::expression::Expression;
use crate::expr::exprs::binary::Binary;
use crate::expr::exprs::operators::Operator;
/// An optimized scalar expression to compute whether values fall between two bounds.
///
/// This expression takes three children:
/// 1. The array of values to check.
/// 2. The lower bound.
/// 3. The upper bound.
///
/// The comparison strictness is controlled by the metadata.
///
/// NOTE: this expression will shortly be removed in favor of pipelined computation of two
/// separate comparisons combined with a logical AND.
pub struct Between;
impl VTable for Between {
type Options = BetweenOptions;
fn id(&self) -> ExprId {
ExprId::from("vortex.between")
}
fn serialize(&self, instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(
pb::BetweenOpts {
lower_strict: instance.lower_strict.is_strict(),
upper_strict: instance.upper_strict.is_strict(),
}
.encode_to_vec(),
))
}
fn deserialize(
&self,
_metadata: &[u8],
_session: &VortexSession,
) -> VortexResult<Self::Options> {
let opts = pb::BetweenOpts::decode(_metadata)?;
Ok(BetweenOptions {
lower_strict: if opts.lower_strict {
crate::compute::StrictComparison::Strict
} else {
crate::compute::StrictComparison::NonStrict
},
upper_strict: if opts.upper_strict {
crate::compute::StrictComparison::Strict
} else {
crate::compute::StrictComparison::NonStrict
},
})
}
fn arity(&self, _options: &Self::Options) -> Arity {
Arity::Exact(3)
}
fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName {
match child_idx {
0 => ChildName::from("array"),
1 => ChildName::from("lower"),
2 => ChildName::from("upper"),
_ => unreachable!("Invalid child index {} for Between expression", child_idx),
}
}
fn fmt_sql(
&self,
options: &Self::Options,
expr: &Expression,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let lower_op = if options.lower_strict.is_strict() {
"<"
} else {
"<="
};
let upper_op = if options.upper_strict.is_strict() {
"<"
} else {
"<="
};
write!(
f,
"({} {} {} {} {})",
expr.child(1),
lower_op,
expr.child(0),
upper_op,
expr.child(2)
)
}
fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
let arr_dt = &arg_dtypes[0];
let lower_dt = &arg_dtypes[1];
let upper_dt = &arg_dtypes[2];
if !arr_dt.eq_ignore_nullability(lower_dt) {
vortex_bail!(
"Array dtype {} does not match lower dtype {}",
arr_dt,
lower_dt
);
}
if !arr_dt.eq_ignore_nullability(upper_dt) {
vortex_bail!(
"Array dtype {} does not match upper dtype {}",
arr_dt,
upper_dt
);
}
Ok(Bool(
arr_dt.nullability() | lower_dt.nullability() | upper_dt.nullability(),
))
}
fn execute(&self, options: &Self::Options, args: ExecutionArgs) -> VortexResult<ArrayRef> {
let [arr, lower, upper]: [ArrayRef; _] = args
.inputs
.try_into()
.map_err(|_| vortex_err!("Expected 3 arguments for Between expression",))?;
between_compute(arr.as_ref(), lower.as_ref(), upper.as_ref(), options)
}
fn stat_falsification(
&self,
options: &Self::Options,
expr: &Expression,
catalog: &dyn StatsCatalog,
) -> Option<Expression> {
let arr = expr.child(0).clone();
let lower = expr.child(1).clone();
let upper = expr.child(2).clone();
let lhs = Binary.new_expr(
options.lower_strict.to_operator().into(),
[lower, arr.clone()],
);
let rhs = Binary.new_expr(options.upper_strict.to_operator().into(), [arr, upper]);
Binary
.new_expr(Operator::KleeneAnd, [lhs, rhs])
.stat_falsification(catalog)
}
fn is_null_sensitive(&self, _instance: &Self::Options) -> bool {
false
}
fn is_fallible(&self, _options: &Self::Options) -> bool {
false
}
}
/// Creates an expression that checks if values are between two bounds.
///
/// Returns a boolean array indicating which values fall within the specified range.
/// The comparison strictness is controlled by the options parameter.
///
/// ```rust
/// # use vortex_array::compute::BetweenOptions;
/// # use vortex_array::compute::StrictComparison;
/// # use vortex_array::expr::{between, lit, root};
/// let opts = BetweenOptions {
/// lower_strict: StrictComparison::NonStrict,
/// upper_strict: StrictComparison::NonStrict,
/// };
/// let expr = between(root(), lit(10), lit(20), opts);
/// ```
pub fn between(
arr: Expression,
lower: Expression,
upper: Expression,
options: BetweenOptions,
) -> Expression {
Between
.try_new_expr(options, [arr, lower, upper])
.vortex_expect("Failed to create Between expression")
}
#[cfg(test)]
mod tests {
use super::between;
use crate::compute::BetweenOptions;
use crate::compute::StrictComparison;
use crate::expr::exprs::get_item::get_item;
use crate::expr::exprs::literal::lit;
use crate::expr::exprs::root::root;
#[test]
fn test_display() {
let expr = between(
get_item("score", root()),
lit(10),
lit(50),
BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::Strict,
},
);
assert_eq!(expr.to_string(), "(10i32 <= $.score < 50i32)");
let expr2 = between(
root(),
lit(0),
lit(100),
BetweenOptions {
lower_strict: StrictComparison::Strict,
upper_strict: StrictComparison::NonStrict,
},
);
assert_eq!(expr2.to_string(), "(0i32 < $ <= 100i32)");
}
}