vortex-array 0.54.0

Vortex in memory columnar data format
Documentation
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::any::Any;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::sync::Arc;

use itertools::Itertools;
use vortex_dtype::{DType, NativePType, match_each_native_ptype};
use vortex_error::{VortexExpect, VortexResult, vortex_bail};

use crate::arrays::ConstantArray;
use crate::compute::Operator as Op;
use crate::operator::{LengthBounds, Operator, OperatorEq, OperatorHash, OperatorId, OperatorRef};
use crate::pipeline::bits::BitView;
use crate::pipeline::vec::Selection;
use crate::pipeline::view::ViewMut;
use crate::pipeline::{
    BindContext, Element, Kernel, KernelContext, PipelinedOperator, RowSelection, VectorId,
};

#[derive(Debug)]
pub struct CompareOperator {
    children: [OperatorRef; 2],
    op: Op,
    dtype: DType,
}

impl CompareOperator {
    pub fn try_new(lhs: OperatorRef, rhs: OperatorRef, op: Op) -> VortexResult<CompareOperator> {
        if lhs.dtype() != rhs.dtype() {
            vortex_bail!(
                "Cannot compare arrays with different dtypes: {} and {}",
                lhs.dtype(),
                rhs.dtype()
            );
        }

        let lhs_const = lhs.as_any().downcast_ref::<ConstantArray>();
        let rhs_const = rhs.as_any().downcast_ref::<ConstantArray>();
        if lhs_const.is_some() && rhs_const.is_some() {
            // TODO(ngates): we should return the Constant result!
        }

        let nullability = lhs.dtype().nullability() | rhs.dtype().nullability();
        let dtype = DType::Bool(nullability);

        Ok(CompareOperator {
            children: [lhs, rhs],
            op,
            dtype,
        })
    }

    pub fn op(&self) -> Op {
        self.op
    }
}

impl OperatorHash for CompareOperator {
    fn operator_hash<H: Hasher>(&self, state: &mut H) {
        self.op.hash(state);
        self.dtype.hash(state);
        self.children.iter().for_each(|c| c.operator_hash(state));
    }
}

impl OperatorEq for CompareOperator {
    fn operator_eq(&self, other: &Self) -> bool {
        self.op == other.op
            && self.dtype == other.dtype
            && self
                .children
                .iter()
                .zip(other.children.iter())
                .all(|(a, b)| a.operator_eq(b))
    }
}

impl Operator for CompareOperator {
    fn id(&self) -> OperatorId {
        OperatorId::from("vortex.compare")
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn dtype(&self) -> &DType {
        &self.dtype
    }

    fn bounds(&self) -> LengthBounds {
        self.children[0].bounds() & self.children[1].bounds()
    }

    fn children(&self) -> &[OperatorRef] {
        &self.children
    }

    fn with_children(self: Arc<Self>, children: Vec<OperatorRef>) -> VortexResult<OperatorRef> {
        let (lhs, rhs) = children
            .into_iter()
            .tuples()
            .next()
            .vortex_expect("missing");
        Ok(Arc::new(CompareOperator {
            children: [lhs, rhs],
            op: self.op,
            dtype: self.dtype.clone(),
        }))
    }

    fn as_pipelined(&self) -> Option<&dyn PipelinedOperator> {
        // If both children support pipelining, but have different row selections, then we cannot
        // pipeline without an alignment step (which we currently do not support).
        if let Some((left, right)) = self.children[0]
            .as_pipelined()
            .zip(self.children[1].as_pipelined())
            && left.row_selection() != right.row_selection()
        {
            return None;
        }

        Some(self)
    }
}

macro_rules! match_each_compare_op {
    ($self:expr, | $enc:ident | $body:block) => {{
        match $self {
            Op::Eq => {
                type $enc = Eq;
                $body
            }
            Op::NotEq => {
                type $enc = NotEq;
                $body
            }
            Op::Gt => {
                type $enc = Gt;
                $body
            }
            Op::Gte => {
                type $enc = Gte;
                $body
            }
            Op::Lt => {
                type $enc = Lt;
                $body
            }
            Op::Lte => {
                type $enc = Lte;
                $body
            }
        }
    }};
}

impl PipelinedOperator for CompareOperator {
    fn row_selection(&self) -> RowSelection {
        self.children[0]
            .as_pipelined()
            .map(|p| p.row_selection())
            .unwrap_or(RowSelection::All)
    }

    #[allow(clippy::cognitive_complexity)]
    fn bind(&self, ctx: &dyn BindContext) -> VortexResult<Box<dyn Kernel>> {
        debug_assert_eq!(self.children[0].dtype(), self.children[1].dtype());

        let DType::Primitive(ptype, _) = self.children[0].dtype() else {
            vortex_bail!(
                "Unsupported type for comparison: {}",
                self.children[0].dtype()
            )
        };

        let lhs_const = self.children[0].as_any().downcast_ref::<ConstantArray>();
        if let Some(lhs_const) = lhs_const {
            // LHS is constant, use ScalarComparePrimitiveKernel
            return match_each_native_ptype!(ptype, |T| {
                match_each_compare_op!(self.op.swap(), |Op| {
                    Ok(Box::new(ScalarComparePrimitiveKernel::<T, Op> {
                        lhs: ctx.children()[1],
                        rhs: lhs_const
                            .scalar()
                            .as_primitive()
                            .typed_value::<T>()
                            .vortex_expect("scalar value not of type T"),
                        _phantom: PhantomData,
                    }) as Box<dyn Kernel>)
                })
            });
        }

        let rhs_const = self.children[1].as_any().downcast_ref::<ConstantArray>();
        if let Some(rhs_const) = rhs_const {
            // RHS is constant, use ScalarComparePrimitiveKernel
            return match_each_native_ptype!(ptype, |T| {
                match_each_compare_op!(self.op, |Op| {
                    Ok(Box::new(ScalarComparePrimitiveKernel::<T, Op> {
                        lhs: ctx.children()[0],
                        rhs: rhs_const
                            .scalar()
                            .as_primitive()
                            .typed_value::<T>()
                            .vortex_expect("scalar value not of type T"),
                        _phantom: PhantomData,
                    }) as Box<dyn Kernel>)
                })
            });
        }

        match_each_native_ptype!(ptype, |T| {
            match_each_compare_op!(self.op, |Op| {
                Ok(Box::new(ComparePrimitiveKernel::<T, Op> {
                    lhs: ctx.children()[0],
                    rhs: ctx.children()[1],
                    _phantom: PhantomData,
                }) as Box<dyn Kernel>)
            })
        })
    }

    fn vector_children(&self) -> Vec<usize> {
        vec![0, 1]
    }

    fn batch_children(&self) -> Vec<usize> {
        vec![]
    }
}

/// A compare operator for primitive types that compares two vectors element-wise using a binary
/// operation.
/// Kernel that performs primitive type comparisons between two input vectors.
pub struct ComparePrimitiveKernel<T, Op> {
    lhs: VectorId,
    rhs: VectorId,
    _phantom: PhantomData<(T, Op)>,
}

impl<T: Element + NativePType, Op: CompareOp<T> + Send> Kernel for ComparePrimitiveKernel<T, Op> {
    fn step(
        &self,
        ctx: &KernelContext,
        _chunk_idx: usize,
        selection: &BitView,
        out: &mut ViewMut,
    ) -> VortexResult<()> {
        let lhs_vec = ctx.vector(self.lhs);
        let lhs = lhs_vec.as_array::<T>();
        let rhs_vec = ctx.vector(self.rhs);
        let rhs = rhs_vec.as_array::<T>();
        let bools = out.as_array_mut::<bool>();

        match (lhs_vec.selection(), rhs_vec.selection()) {
            (Selection::Prefix, Selection::Prefix) => {
                for i in 0..selection.true_count() {
                    bools[i] = Op::compare(&lhs[i], &rhs[i]);
                }
                out.set_selection(Selection::Prefix)
            }
            (Selection::Mask, Selection::Mask) => {
                // TODO(ngates): check density to decide if we should iterate indices or do
                //  a full scan
                let mut pos = 0;
                selection.iter_ones(|idx| {
                    bools[pos] = Op::compare(&lhs[idx], &rhs[idx]);
                    pos += 1;
                });
                out.set_selection(Selection::Prefix)
            }
            (Selection::Mask, Selection::Prefix) => {
                let mut pos = 0;
                selection.iter_ones(|idx| {
                    bools[pos] = Op::compare(&lhs[idx], &rhs[pos]);
                    pos += 1;
                });
                out.set_selection(Selection::Prefix)
            }
            (Selection::Prefix, Selection::Mask) => {
                let mut pos = 0;
                selection.iter_ones(|idx| {
                    bools[pos] = Op::compare(&lhs[pos], &rhs[idx]);
                    pos += 1;
                });
                out.set_selection(Selection::Prefix)
            }
        }

        Ok(())
    }
}

struct ScalarComparePrimitiveKernel<T: Element + NativePType, Op: CompareOp<T>> {
    lhs: VectorId,
    rhs: T,
    _phantom: PhantomData<Op>,
}

impl<T: Element + NativePType, Op: CompareOp<T> + Send> Kernel
    for ScalarComparePrimitiveKernel<T, Op>
{
    fn step(
        &self,
        ctx: &KernelContext,
        _chunk_idx: usize,
        selection: &BitView,
        out: &mut ViewMut,
    ) -> VortexResult<()> {
        let lhs_vec = ctx.vector(self.lhs);
        let lhs = lhs_vec.as_array::<T>();
        let bools = out.as_array_mut::<bool>();

        match lhs_vec.selection() {
            Selection::Prefix => {
                for i in 0..selection.true_count() {
                    bools[i] = Op::compare(&lhs[i], &self.rhs);
                }
                out.set_selection(Selection::Prefix)
            }
            Selection::Mask => {
                // TODO(ngates): decide at what true count we should iter indices...
                selection.iter_ones(|idx| {
                    bools[idx] = Op::compare(&lhs[idx], &self.rhs);
                });
                out.set_selection(Selection::Mask)
            }
        }

        Ok(())
    }
}

pub(crate) trait CompareOp<T> {
    fn compare(lhs: &T, rhs: &T) -> bool;
}

/// Equality comparison operation.
pub struct Eq;
impl<T: PartialEq> CompareOp<T> for Eq {
    #[inline(always)]
    fn compare(lhs: &T, rhs: &T) -> bool {
        lhs == rhs
    }
}

/// Not equal comparison operation.
pub struct NotEq;
impl<T: PartialEq> CompareOp<T> for NotEq {
    #[inline(always)]
    fn compare(lhs: &T, rhs: &T) -> bool {
        lhs != rhs
    }
}

/// Greater than comparison operation.
pub struct Gt;
impl<T: PartialOrd> CompareOp<T> for Gt {
    #[inline(always)]
    fn compare(lhs: &T, rhs: &T) -> bool {
        lhs > rhs
    }
}

/// Greater than or equal comparison operation.
pub struct Gte;
impl<T: PartialOrd> CompareOp<T> for Gte {
    #[inline(always)]
    fn compare(lhs: &T, rhs: &T) -> bool {
        lhs >= rhs
    }
}

/// Less than comparison operation.
pub struct Lt;
impl<T: PartialOrd> CompareOp<T> for Lt {
    #[inline(always)]
    fn compare(lhs: &T, rhs: &T) -> bool {
        lhs < rhs
    }
}

/// Less than or equal comparison operation.
pub struct Lte;
impl<T: PartialOrd> CompareOp<T> for Lte {
    #[inline(always)]
    fn compare(lhs: &T, rhs: &T) -> bool {
        lhs <= rhs
    }
}

// TODO(ngates): bring these back!
// #[cfg(test)]
// mod tests {
//     use std::rc::Rc;
//
//     use vortex_buffer::BufferMut;
//     use vortex_dtype::Nullability;
//     use vortex_scalar::Scalar;
//
//     use crate::arrays::PrimitiveArray;
//     use crate::operator::bits::BitView;
//
//     #[test]
//     fn test_scalar_compare_stacked_on_primitive() {
//         // Create input data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
//         let size = 16;
//         let primitive_array = (0..i32::try_from(size).unwrap()).collect::<PrimitiveArray>();
//         let primitive_op = primitive_array.as_ref().to_operator().unwrap().unwrap();
//
//         // Create scalar compare operator: primitive_value > 10
//         let compare_value = Scalar::primitive(10i32, Nullability::NonNullable);
//         let scalar_compare_op = Rc::new(ScalarCompareOperator::new(
//             primitive_op,
//             BinaryOperator::Gt,
//             compare_value,
//         ));
//
//         // Create query plan from the stacked operators
//         let plan = QueryPlan::new(scalar_compare_op.as_ref()).unwrap();
//         let mut operator = plan.executable_plan().unwrap();
//
//         // Create all-true mask for simplicity
//         let mask_data = [usize::MAX; N_WORDS];
//         let mask_view = BitView::new(&mask_data);
//
//         // Create output buffer for boolean results
//         let mut output = BufferMut::<bool>::with_capacity(N);
//         unsafe { output.set_len(N) };
//         let mut output_view = ViewMut::new(&mut output[..], None);
//
//         // Execute the operator
//         let result = operator._step(mask_view, &mut output_view);
//         assert!(result.is_ok());
//
//         // Verify results: values 0-10 should be false, values 11-15 should be true
//         for i in 0..size {
//             let expected = i > 10;
//             assert_eq!(
//                 output[i], expected,
//                 "Position {}: expected {}, got {}",
//                 i, expected, output[i]
//             );
//         }
//     }
//
//     #[test]
//     fn test_scalar_compare_different_operators() {
//         // Test with different comparison operators
//         let size = 8;
//         let primitive_array = (0..i32::try_from(size).unwrap()).collect::<PrimitiveArray>();
//
//         let primitive_op = primitive_array.as_ref().to_operator().unwrap().unwrap();
//
//         // Test Eq: values == 3
//         let compare_value = Scalar::primitive(3i32, Nullability::NonNullable);
//         let eq_op = Rc::new(ScalarCompareOperator::new(
//             primitive_op,
//             BinaryOperator::Eq,
//             compare_value,
//         ));
//
//         let plan = QueryPlan::new(eq_op.as_ref()).unwrap();
//         let mut operator = plan.executable_plan().unwrap();
//
//         let mask_data = [usize::MAX; N_WORDS];
//         let mask_view = BitView::new(&mask_data);
//
//         let mut output = BufferMut::<bool>::with_capacity(N);
//         unsafe { output.set_len(N) };
//         let mut output_view = ViewMut::new(&mut output[..], None);
//
//         let result = operator._step(mask_view, &mut output_view);
//         assert!(result.is_ok());
//
//         // Only position 3 should be true
//         for i in 0..size {
//             let expected = i == 3;
//             assert_eq!(
//                 output[i], expected,
//                 "Eq test - Position {}: expected {}, got {}",
//                 i, expected, output[i]
//             );
//         }
//     }
//
//     #[test]
//     fn test_scalar_compare_with_f32() {
//         // Test with floating-point values
//         let size = 8;
//         let values: Vec<f32> = (0..size).map(|i| i as f32 + 0.5).collect();
//         let primitive_array = values.into_iter().collect::<PrimitiveArray>();
//
//         let primitive_op = primitive_array.as_ref().to_operator().unwrap().unwrap();
//
//         // Test Lt: values < 3.5
//         let compare_value = Scalar::primitive(3.5f32, Nullability::NonNullable);
//         let lt_op = Rc::new(ScalarCompareOperator::new(
//             primitive_op,
//             BinaryOperator::Lt,
//             compare_value,
//         ));
//
//         let plan = QueryPlan::new(lt_op.as_ref()).unwrap();
//         let mut operator = plan.executable_plan().unwrap();
//
//         let mask_data = [usize::MAX; N_WORDS];
//         let mask_view = BitView::new(&mask_data);
//
//         let mut output = BufferMut::<bool>::with_capacity(N);
//         unsafe { output.set_len(N) };
//         let mut output_view = ViewMut::new(&mut output[..], None);
//
//         let result = operator._step(mask_view, &mut output_view);
//         assert!(result.is_ok());
//
//         // Values 0.5, 1.5, 2.5 should be < 3.5 (true), 3.5+ should be false
//         for i in 0..size {
//             let value = i as f32 + 0.5;
//             let expected = value < 3.5;
//             assert_eq!(
//                 output[i], expected,
//                 "Lt test - Position {}: value {} should be {}, got {}",
//                 i, value, expected, output[i]
//             );
//         }
//     }
// }