vortex_functions/between/
decimal.rs1use vortex_array::ArrayRef;
5use vortex_array::ArrayView;
6use vortex_array::ExecutionCtx;
7use vortex_array::IntoArray;
8use vortex_array::arrays::BoolArray;
9use vortex_array::arrays::Decimal;
10use vortex_array::dtype::NativeDecimalType;
11use vortex_array::match_each_decimal_value_type;
12use vortex_array::scalar::DecimalValue;
13use vortex_buffer::BitBuffer;
14use vortex_buffer::BufferAllocatorRef;
15use vortex_error::VortexExpect;
16use vortex_error::VortexResult;
17
18use crate::between::BetweenKernel;
19use crate::between::BetweenOptions;
20use crate::between::StrictComparison;
21
22impl BetweenKernel for Decimal {
23 fn between(
24 arr: ArrayView<'_, Decimal>,
25 lower: &ArrayRef,
26 upper: &ArrayRef,
27 options: &BetweenOptions,
28 ctx: &mut ExecutionCtx,
29 ) -> VortexResult<Option<ArrayRef>> {
30 let (Some(lower), Some(upper)) = (lower.as_constant(), upper.as_constant()) else {
31 return Ok(None);
32 };
33
34 if !arr.dtype().eq_ignore_nullability(lower.dtype())
35 || !arr.dtype().eq_ignore_nullability(upper.dtype())
36 {
37 return Ok(None);
38 }
39 let nullability =
40 arr.dtype().nullability() | lower.dtype().nullability() | upper.dtype().nullability();
41 let (Some(lower), Some(upper)) = (
42 lower.as_decimal().decimal_value(),
43 upper.as_decimal().decimal_value(),
44 ) else {
45 return Ok(None);
47 };
48
49 let bits = match_each_decimal_value_type!(arr.values_type(), |T| {
50 between_values(
51 &arr.buffer::<T>(),
52 lower,
53 upper,
54 options,
55 ctx.allocator().clone(),
56 )
57 });
58 let validity = arr.validity()?.union_nullability(nullability);
59
60 Ok(Some(BoolArray::new(bits, validity).into_array()))
61 }
62}
63
64fn between_values<T: NativeDecimalType>(
65 values: &[T],
66 lower: DecimalValue,
67 upper: DecimalValue,
68 options: &BetweenOptions,
69 allocator: BufferAllocatorRef,
70) -> BitBuffer {
71 let lower = lower
72 .cast::<T>()
73 .vortex_expect("a decimal bound fits the canonical width of its precision");
74 let upper = upper
75 .cast::<T>()
76 .vortex_expect("a decimal bound fits the canonical width of its precision");
77
78 match (options.lower_strict, options.upper_strict) {
79 (StrictComparison::Strict, StrictComparison::Strict) => {
80 between_bits(values, |v| (lower < v) & (v < upper), allocator)
81 }
82 (StrictComparison::Strict, StrictComparison::NonStrict) => {
83 between_bits(values, |v| (lower < v) & (v <= upper), allocator)
84 }
85 (StrictComparison::NonStrict, StrictComparison::Strict) => {
86 between_bits(values, |v| (lower <= v) & (v < upper), allocator)
87 }
88 (StrictComparison::NonStrict, StrictComparison::NonStrict) => {
89 between_bits(values, |v| (lower <= v) & (v <= upper), allocator)
90 }
91 }
92}
93
94fn between_bits<T: Copy>(
95 values: &[T],
96 predicate: impl Fn(T) -> bool,
97 allocator: BufferAllocatorRef,
98) -> BitBuffer {
99 BitBuffer::collect_bool_multiversioned_in(
100 values.len(),
101 |index| {
102 let value = unsafe { *values.get_unchecked(index) };
104 predicate(value)
105 },
106 allocator,
107 )
108}