vortex_fsst/compute/
compare.rs1use vortex_array::ArrayRef;
5use vortex_array::ExecutionCtx;
6use vortex_array::IntoArray;
7use vortex_array::arrays::BoolArray;
8use vortex_array::arrays::ConstantArray;
9use vortex_array::builtins::ArrayBuiltins;
10use vortex_array::dtype::DType;
11use vortex_array::scalar::Scalar;
12use vortex_array::scalar_fn::fns::binary::CompareKernel;
13use vortex_array::scalar_fn::fns::operators::CompareOperator;
14use vortex_array::scalar_fn::fns::operators::Operator;
15use vortex_array::validity::Validity;
16use vortex_buffer::BitBuffer;
17use vortex_buffer::ByteBuffer;
18use vortex_error::VortexExpect;
19use vortex_error::VortexResult;
20use vortex_error::vortex_bail;
21
22use crate::FSSTArray;
23use crate::FSSTVTable;
24
25impl CompareKernel for FSSTVTable {
26 fn compare(
27 lhs: &FSSTArray,
28 rhs: &ArrayRef,
29 operator: CompareOperator,
30 ctx: &mut ExecutionCtx,
31 ) -> VortexResult<Option<ArrayRef>> {
32 match rhs.as_constant() {
33 Some(constant) => compare_fsst_constant(lhs, &constant, operator, ctx),
34 _ => Ok(None),
36 }
37 }
38}
39
40fn compare_fsst_constant(
42 left: &FSSTArray,
43 right: &Scalar,
44 operator: CompareOperator,
45 ctx: &mut ExecutionCtx,
46) -> VortexResult<Option<ArrayRef>> {
47 let is_rhs_empty = match right.dtype() {
48 DType::Binary(_) => right
49 .as_binary()
50 .is_empty()
51 .vortex_expect("RHS should not be null"),
52 DType::Utf8(_) => right
53 .as_utf8()
54 .is_empty()
55 .vortex_expect("RHS should not be null"),
56 _ => vortex_bail!("VarBinArray can only have type of Binary or Utf8"),
57 };
58 if is_rhs_empty {
59 let buffer = match operator {
60 CompareOperator::Gte => BitBuffer::new_set(left.len()),
62 CompareOperator::Lt => BitBuffer::new_unset(left.len()),
64 _ => left
65 .uncompressed_lengths()
66 .to_array()
67 .binary(
68 ConstantArray::new(
69 Scalar::zero_value(left.uncompressed_lengths().dtype()),
70 left.uncompressed_lengths().len(),
71 )
72 .into_array(),
73 operator.into(),
74 )?
75 .execute(ctx)?,
76 };
77
78 return Ok(Some(
79 BoolArray::new(
80 buffer,
81 Validity::copy_from_array(&left.to_array())?
82 .union_nullability(right.dtype().nullability()),
83 )
84 .into_array(),
85 ));
86 }
87
88 if !matches!(operator, CompareOperator::Eq | CompareOperator::NotEq) {
90 return Ok(None);
91 }
92
93 let compressor = left.compressor();
94 let encoded_buffer = match left.dtype() {
95 DType::Utf8(_) => {
96 let value = right
97 .as_utf8()
98 .value()
99 .vortex_expect("Expected non-null scalar");
100 ByteBuffer::from(compressor.compress(value.as_bytes()))
101 }
102 DType::Binary(_) => {
103 let value = right
104 .as_binary()
105 .value()
106 .vortex_expect("Expected non-null scalar");
107 ByteBuffer::from(compressor.compress(value.as_slice()))
108 }
109 _ => unreachable!("FSSTArray can only have string or binary data type"),
110 };
111
112 let encoded_scalar = Scalar::binary(
113 encoded_buffer,
114 left.dtype().nullability() | right.dtype().nullability(),
115 );
116
117 let rhs = ConstantArray::new(encoded_scalar, left.len());
118 left.codes()
119 .to_array()
120 .binary(rhs.into_array(), Operator::from(operator))
121 .map(Some)
122}
123
124#[cfg(test)]
125mod tests {
126 use vortex_array::Array;
127 use vortex_array::ToCanonical;
128 use vortex_array::arrays::BoolArray;
129 use vortex_array::arrays::ConstantArray;
130 use vortex_array::arrays::VarBinArray;
131 use vortex_array::assert_arrays_eq;
132 use vortex_array::builtins::ArrayBuiltins;
133 use vortex_array::dtype::DType;
134 use vortex_array::dtype::Nullability;
135 use vortex_array::scalar::Scalar;
136 use vortex_array::scalar_fn::fns::operators::Operator;
137
138 use crate::fsst_compress;
139 use crate::fsst_train_compressor;
140
141 #[test]
142 #[cfg_attr(miri, ignore)]
143 fn test_compare_fsst() {
144 let lhs = VarBinArray::from_iter(
145 [
146 Some("hello"),
147 None,
148 Some("world"),
149 None,
150 Some("this is a very long string"),
151 ],
152 DType::Utf8(Nullability::Nullable),
153 );
154 let compressor = fsst_train_compressor(&lhs);
155 let lhs = fsst_compress(lhs, &compressor);
156
157 let rhs = ConstantArray::new("world", lhs.len());
158
159 let equals = lhs
161 .to_array()
162 .binary(rhs.to_array(), Operator::Eq)
163 .unwrap()
164 .to_bool();
165
166 assert_eq!(equals.dtype(), &DType::Bool(Nullability::Nullable));
167
168 assert_arrays_eq!(
169 &equals,
170 &BoolArray::from_iter([Some(false), None, Some(true), None, Some(false)])
171 );
172
173 let not_equals = lhs
175 .to_array()
176 .binary(rhs.to_array(), Operator::NotEq)
177 .unwrap()
178 .to_bool();
179
180 assert_eq!(not_equals.dtype(), &DType::Bool(Nullability::Nullable));
181 assert_arrays_eq!(
182 ¬_equals,
183 &BoolArray::from_iter([Some(true), None, Some(false), None, Some(true)])
184 );
185
186 let null_rhs =
188 ConstantArray::new(Scalar::null(DType::Utf8(Nullability::Nullable)), lhs.len());
189 let equals_null = lhs
190 .to_array()
191 .binary(null_rhs.to_array(), Operator::Eq)
192 .unwrap();
193 assert_arrays_eq!(
194 &equals_null,
195 &BoolArray::from_iter([None::<bool>, None, None, None, None])
196 );
197
198 let noteq_null = lhs
199 .to_array()
200 .binary(null_rhs.to_array(), Operator::NotEq)
201 .unwrap();
202 assert_arrays_eq!(
203 ¬eq_null,
204 &BoolArray::from_iter([None::<bool>, None, None, None, None])
205 );
206 }
207}