• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16759123577

05 Aug 2025 07:15PM UTC coverage: 83.847% (+0.3%) from 83.546%
16759123577

Pull #4110

github

web-flow
Merge 19ba4dfa6 into 03508f9eb
Pull Request #4110: chore: Reapply "feat: implement Cast Kernel everywhere"

996 of 1052 new or added lines in 14 files covered. (94.68%)

21 existing lines in 1 file now uncovered.

48093 of 57358 relevant lines covered (83.85%)

467601.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.81
/vortex-array/src/compute/conformance/consistency.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
//! # Array Consistency Tests
5
//!
6
//! This module contains tests that verify consistency between related compute operations
7
//! on Vortex arrays. These tests ensure that different ways of achieving the same result
8
//! produce identical outputs.
9
//!
10
//! ## Test Categories
11
//!
12
//! - **Filter/Take Consistency**: Verifies that filtering with a mask produces the same
13
//!   result as taking with the indices where the mask is true.
14
//! - **Mask Composition**: Ensures that applying multiple masks sequentially produces
15
//!   the same result as applying a combined mask.
16
//! - **Identity Operations**: Tests that operations with identity inputs (all-true masks,
17
//!   sequential indices) preserve the original array.
18
//! - **Null Handling**: Verifies consistent behavior when operations introduce or
19
//!   interact with null values.
20
//! - **Edge Cases**: Tests empty arrays, single elements, and boundary conditions.
21

22
use vortex_error::VortexUnwrap;
23
use vortex_mask::Mask;
24

25
use crate::arrays::{BoolArray, PrimitiveArray};
26
use crate::compute::{filter, mask, take};
27
use crate::{Array, IntoArray};
28

29
/// Tests that filter and take operations produce consistent results.
30
///
31
/// # Invariant
32
/// `filter(array, mask)` should equal `take(array, indices_where_mask_is_true)`
33
///
34
/// # Test Details
35
/// - Creates a mask that keeps elements where index % 3 != 1
36
/// - Applies filter with this mask
37
/// - Creates indices array containing positions where mask is true
38
/// - Applies take with these indices
39
/// - Verifies both results are identical
40
pub fn test_filter_take_consistency(array: &dyn Array) {
4,221✔
41
    let len = array.len();
4,221✔
42
    if len == 0 {
4,221✔
43
        return;
1✔
44
    }
4,220✔
45

46
    // Create a test mask (keep elements where index % 3 != 1)
47
    let mask_pattern: Vec<bool> = (0..len).map(|i| i % 3 != 1).collect();
1,651,964✔
48
    let mask = Mask::try_from(&BoolArray::from_iter(mask_pattern.clone())).vortex_unwrap();
4,220✔
49

50
    // Filter the array
51
    let filtered = filter(array, &mask).vortex_unwrap();
4,220✔
52

53
    // Create indices where mask is true
54
    let indices: Vec<u64> = mask_pattern
4,220✔
55
        .iter()
4,220✔
56
        .enumerate()
4,220✔
57
        .filter_map(|(i, &v)| v.then_some(i as u64))
1,651,964✔
58
        .collect();
4,220✔
59
    let indices_array = PrimitiveArray::from_iter(indices).into_array();
4,220✔
60

61
    // Take using those indices
62
    let taken = take(array, &indices_array).vortex_unwrap();
4,220✔
63

64
    // Results should be identical
65
    assert_eq!(
4,220✔
66
        filtered.len(),
4,220✔
67
        taken.len(),
4,220✔
68
        "Filter and take should produce arrays of the same length. \
×
69
         Filtered length: {}, Taken length: {}",
×
70
        filtered.len(),
×
71
        taken.len()
×
72
    );
73

74
    for i in 0..filtered.len() {
1,100,809✔
75
        let filtered_val = filtered.scalar_at(i).vortex_unwrap();
1,100,809✔
76
        let taken_val = taken.scalar_at(i).vortex_unwrap();
1,100,809✔
77
        assert_eq!(
1,100,809✔
78
            filtered_val, taken_val,
79
            "Filter and take produced different values at index {i}. \
×
80
             Filtered value: {filtered_val:?}, Taken value: {taken_val:?}"
×
81
        );
82
    }
83
}
4,221✔
84

85
/// Tests that double masking is consistent with combined mask.
86
///
87
/// # Invariant
88
/// `mask(mask(array, mask1), mask2)` should equal `mask(array, mask1 | mask2)`
89
///
90
/// # Test Details
91
/// - Creates two masks: mask1 (every 3rd element) and mask2 (every 2nd element)
92
/// - Applies masks sequentially: first mask1, then mask2 on the result
93
/// - Creates a combined mask using OR operation (element is masked if either mask is true)
94
/// - Applies the combined mask directly to the original array
95
/// - Verifies both approaches produce identical results
96
///
97
/// # Why This Matters
98
/// This test ensures that mask operations compose correctly, which is critical for
99
/// complex query operations that may apply multiple filters.
100
pub fn test_double_mask_consistency(array: &dyn Array) {
4,221✔
101
    let len = array.len();
4,221✔
102
    if len == 0 {
4,221✔
103
        return;
1✔
104
    }
4,220✔
105

106
    // Create two different mask patterns
107
    let mask1_pattern: Vec<bool> = (0..len).map(|i| i % 3 == 0).collect();
1,651,964✔
108
    let mask2_pattern: Vec<bool> = (0..len).map(|i| i % 2 == 0).collect();
1,651,964✔
109

110
    let mask1 = Mask::try_from(&BoolArray::from_iter(mask1_pattern.clone())).vortex_unwrap();
4,220✔
111
    let mask2 = Mask::try_from(&BoolArray::from_iter(mask2_pattern.clone())).vortex_unwrap();
4,220✔
112

113
    // Apply masks sequentially
114
    let first_masked = mask(array, &mask1).vortex_unwrap();
4,220✔
115
    let double_masked = mask(&first_masked, &mask2).vortex_unwrap();
4,220✔
116

117
    // Create combined mask (OR operation - element is masked if EITHER mask is true)
118
    let combined_pattern: Vec<bool> = mask1_pattern
4,220✔
119
        .iter()
4,220✔
120
        .zip(mask2_pattern.iter())
4,220✔
121
        .map(|(&a, &b)| a || b)
1,651,964✔
122
        .collect();
4,220✔
123
    let combined_mask = Mask::try_from(&BoolArray::from_iter(combined_pattern)).vortex_unwrap();
4,220✔
124

125
    // Apply combined mask directly
126
    let directly_masked = mask(array, &combined_mask).vortex_unwrap();
4,220✔
127

128
    // Results should be identical
129
    assert_eq!(
4,220✔
130
        double_masked.len(),
4,220✔
131
        directly_masked.len(),
4,220✔
132
        "Sequential masking and combined masking should produce arrays of the same length. \
×
133
         Sequential length: {}, Combined length: {}",
×
134
        double_masked.len(),
×
135
        directly_masked.len()
×
136
    );
137

138
    for i in 0..double_masked.len() {
1,651,964✔
139
        let double_val = double_masked.scalar_at(i).vortex_unwrap();
1,651,964✔
140
        let direct_val = directly_masked.scalar_at(i).vortex_unwrap();
1,651,964✔
141
        assert_eq!(
1,651,964✔
142
            double_val, direct_val,
143
            "Sequential masking and combined masking produced different values at index {i}. \
×
144
             Sequential masking value: {double_val:?}, Combined masking value: {direct_val:?}\n\
×
145
             This likely indicates an issue with how masks are composed in the array implementation."
×
146
        );
147
    }
148
}
4,221✔
149

150
/// Tests that filtering with an all-true mask preserves the array.
151
///
152
/// # Invariant
153
/// `filter(array, all_true_mask)` should equal `array`
154
///
155
/// # Test Details
156
/// - Creates a mask with all elements set to true
157
/// - Applies filter with this mask
158
/// - Verifies the result is identical to the original array
159
///
160
/// # Why This Matters
161
/// This is an identity operation that should be optimized in implementations
162
/// to avoid unnecessary copying.
163
pub fn test_filter_identity(array: &dyn Array) {
4,221✔
164
    let len = array.len();
4,221✔
165
    if len == 0 {
4,221✔
166
        return;
1✔
167
    }
4,220✔
168

169
    let all_true_mask = Mask::new_true(len);
4,220✔
170
    let filtered = filter(array, &all_true_mask).vortex_unwrap();
4,220✔
171

172
    // Filtered array should be identical to original
173
    assert_eq!(
4,220✔
174
        filtered.len(),
4,220✔
175
        array.len(),
4,220✔
176
        "Filtering with all-true mask should preserve array length. \
×
177
         Original length: {}, Filtered length: {}",
×
178
        array.len(),
×
179
        filtered.len()
×
180
    );
181

182
    for i in 0..len {
1,651,964✔
183
        let original_val = array.scalar_at(i).vortex_unwrap();
1,651,964✔
184
        let filtered_val = filtered.scalar_at(i).vortex_unwrap();
1,651,964✔
185
        assert_eq!(
1,651,964✔
186
            filtered_val, original_val,
187
            "Filtering with all-true mask should preserve all values. \
×
188
             Value at index {i} changed from {original_val:?} to {filtered_val:?}"
×
189
        );
190
    }
191
}
4,221✔
192

193
/// Tests that masking with an all-false mask preserves values while making them nullable.
194
///
195
/// # Invariant
196
/// `mask(array, all_false_mask)` should have same values as `array` but with nullable type
197
///
198
/// # Test Details
199
/// - Creates a mask with all elements set to false (no elements are nullified)
200
/// - Applies mask operation
201
/// - Verifies all values are preserved but the array type becomes nullable
202
///
203
/// # Why This Matters
204
/// Masking always produces a nullable array, even when no values are actually masked.
205
/// This test ensures the type system handles this correctly.
206
pub fn test_mask_identity(array: &dyn Array) {
4,221✔
207
    let len = array.len();
4,221✔
208
    if len == 0 {
4,221✔
209
        return;
1✔
210
    }
4,220✔
211

212
    let all_false_mask = Mask::new_false(len);
4,220✔
213
    let masked = mask(array, &all_false_mask).vortex_unwrap();
4,220✔
214

215
    // Masked array should have same values (just nullable)
216
    assert_eq!(
4,220✔
217
        masked.len(),
4,220✔
218
        array.len(),
4,220✔
219
        "Masking with all-false mask should preserve array length. \
×
220
         Original length: {}, Masked length: {}",
×
221
        array.len(),
×
222
        masked.len()
×
223
    );
224

225
    assert!(
4,220✔
226
        masked.dtype().is_nullable(),
4,220✔
227
        "Mask operation should always produce a nullable array, but dtype is {:?}",
×
228
        masked.dtype()
×
229
    );
230

231
    for i in 0..len {
1,651,964✔
232
        let original_val = array.scalar_at(i).vortex_unwrap();
1,651,964✔
233
        let masked_val = masked.scalar_at(i).vortex_unwrap();
1,651,964✔
234
        let expected_val = original_val.clone().into_nullable();
1,651,964✔
235
        assert_eq!(
1,651,964✔
236
            masked_val, expected_val,
237
            "Masking with all-false mask should preserve values (as nullable). \
×
238
             Value at index {i}: original = {original_val:?}, masked = {masked_val:?}, expected = {expected_val:?}"
×
239
        );
240
    }
241
}
4,221✔
242

243
/// Tests that slice and filter with contiguous mask produce same results.
244
///
245
/// # Invariant
246
/// `filter(array, contiguous_true_mask)` should equal `slice(array, start, end)`
247
///
248
/// # Test Details
249
/// - Creates a mask that is true only for indices 1, 2, and 3
250
/// - Filters the array with this mask
251
/// - Slices the array from index 1 to 4
252
/// - Verifies both operations produce identical results
253
///
254
/// # Why This Matters
255
/// When a filter mask represents a contiguous range, it should be equivalent to
256
/// a slice operation. Some implementations may optimize this case.
257
pub fn test_slice_filter_consistency(array: &dyn Array) {
4,221✔
258
    let len = array.len();
4,221✔
259
    if len < 4 {
4,221✔
260
        return; // Need at least 4 elements for meaningful test
663✔
261
    }
3,558✔
262

263
    // Create a contiguous mask (true from index 1 to 3)
264
    let mut mask_pattern = vec![false; len];
3,558✔
265
    mask_pattern[1..4.min(len)].fill(true);
3,558✔
266

267
    let mask = Mask::try_from(&BoolArray::from_iter(mask_pattern)).vortex_unwrap();
3,558✔
268
    let filtered = filter(array, &mask).vortex_unwrap();
3,558✔
269

270
    // Slice should produce the same result
271
    let sliced = array.slice(1, 4.min(len)).vortex_unwrap();
3,558✔
272

273
    assert_eq!(
3,558✔
274
        filtered.len(),
3,558✔
275
        sliced.len(),
3,558✔
276
        "Filter with contiguous mask and slice should produce same length. \
×
277
         Filtered length: {}, Sliced length: {}",
×
278
        filtered.len(),
×
279
        sliced.len()
×
280
    );
281

282
    for i in 0..filtered.len() {
10,674✔
283
        let filtered_val = filtered.scalar_at(i).vortex_unwrap();
10,674✔
284
        let sliced_val = sliced.scalar_at(i).vortex_unwrap();
10,674✔
285
        assert_eq!(
10,674✔
286
            filtered_val, sliced_val,
287
            "Filter with contiguous mask and slice produced different values at index {i}. \
×
288
             Filtered value: {filtered_val:?}, Sliced value: {sliced_val:?}"
×
289
        );
290
    }
291
}
4,221✔
292

293
/// Tests that take with sequential indices equals slice.
294
///
295
/// # Invariant
296
/// `take(array, [1, 2, 3, ...])` should equal `slice(array, 1, n)`
297
///
298
/// # Test Details
299
/// - Creates indices array with sequential values [1, 2, 3]
300
/// - Takes elements at these indices
301
/// - Slices array from index 1 to 4
302
/// - Verifies both operations produce identical results
303
///
304
/// # Why This Matters
305
/// Sequential takes are a common pattern that can be optimized to slice operations.
306
pub fn test_take_slice_consistency(array: &dyn Array) {
4,221✔
307
    let len = array.len();
4,221✔
308
    if len < 3 {
4,221✔
309
        return; // Need at least 3 elements
509✔
310
    }
3,712✔
311

312
    // Take indices [1, 2, 3]
313
    let end = 4.min(len);
3,712✔
314
    let indices = PrimitiveArray::from_iter((1..end).map(|i| i as u64)).into_array();
10,982✔
315
    let taken = take(array, &indices).vortex_unwrap();
3,712✔
316

317
    // Slice from 1 to end
318
    let sliced = array.slice(1, end).vortex_unwrap();
3,712✔
319

320
    assert_eq!(
3,712✔
321
        taken.len(),
3,712✔
322
        sliced.len(),
3,712✔
323
        "Take with sequential indices and slice should produce same length. \
×
324
         Taken length: {}, Sliced length: {}",
×
325
        taken.len(),
×
326
        sliced.len()
×
327
    );
328

329
    for i in 0..taken.len() {
10,982✔
330
        let taken_val = taken.scalar_at(i).vortex_unwrap();
10,982✔
331
        let sliced_val = sliced.scalar_at(i).vortex_unwrap();
10,982✔
332
        assert_eq!(
10,982✔
333
            taken_val, sliced_val,
334
            "Take with sequential indices and slice produced different values at index {i}. \
×
335
             Taken value: {taken_val:?}, Sliced value: {sliced_val:?}"
×
336
        );
337
    }
338
}
4,221✔
339

340
/// Tests that filter preserves relative ordering
341
pub fn test_filter_preserves_order(array: &dyn Array) {
4,221✔
342
    let len = array.len();
4,221✔
343
    if len < 4 {
4,221✔
344
        return;
663✔
345
    }
3,558✔
346

347
    // Create a mask that selects elements at indices 0, 2, 3
348
    let mask_pattern: Vec<bool> = (0..len).map(|i| i == 0 || i == 2 || i == 3).collect();
1,650,953✔
349
    let mask = Mask::try_from(&BoolArray::from_iter(mask_pattern)).vortex_unwrap();
3,558✔
350

351
    let filtered = filter(array, &mask).vortex_unwrap();
3,558✔
352

353
    // Verify the filtered array contains the right elements in order
354
    assert_eq!(filtered.len(), 3.min(len));
3,558✔
355
    if len >= 4 {
3,558✔
356
        assert_eq!(
3,558✔
357
            filtered.scalar_at(0).vortex_unwrap(),
3,558✔
358
            array.scalar_at(0).vortex_unwrap()
3,558✔
359
        );
360
        assert_eq!(
3,558✔
361
            filtered.scalar_at(1).vortex_unwrap(),
3,558✔
362
            array.scalar_at(2).vortex_unwrap()
3,558✔
363
        );
364
        assert_eq!(
3,558✔
365
            filtered.scalar_at(2).vortex_unwrap(),
3,558✔
366
            array.scalar_at(3).vortex_unwrap()
3,558✔
367
        );
368
    }
×
369
}
4,221✔
370

371
/// Tests that take with repeated indices works correctly
372
pub fn test_take_repeated_indices(array: &dyn Array) {
4,221✔
373
    let len = array.len();
4,221✔
374
    if len == 0 {
4,221✔
375
        return;
1✔
376
    }
4,220✔
377

378
    // Take the first element three times
379
    let indices = PrimitiveArray::from_iter([0u64, 0, 0]).into_array();
4,220✔
380
    let taken = take(array, &indices).vortex_unwrap();
4,220✔
381

382
    assert_eq!(taken.len(), 3);
4,220✔
383
    for i in 0..3 {
16,880✔
384
        assert_eq!(
12,660✔
385
            taken.scalar_at(i).vortex_unwrap(),
12,660✔
386
            array.scalar_at(0).vortex_unwrap()
12,660✔
387
        );
388
    }
389
}
4,221✔
390

391
/// Tests mask and filter interaction with nulls
392
pub fn test_mask_filter_null_consistency(array: &dyn Array) {
4,221✔
393
    let len = array.len();
4,221✔
394
    if len < 3 {
4,221✔
395
        return;
509✔
396
    }
3,712✔
397

398
    // First mask some elements
399
    let mask_pattern: Vec<bool> = (0..len).map(|i| i % 2 == 0).collect();
1,651,415✔
400
    let mask_array = Mask::try_from(&BoolArray::from_iter(mask_pattern)).vortex_unwrap();
3,712✔
401
    let masked = mask(array, &mask_array).vortex_unwrap();
3,712✔
402

403
    // Then filter to remove the nulls
404
    let filter_pattern: Vec<bool> = (0..len).map(|i| i % 2 != 0).collect();
1,651,415✔
405
    let filter_mask = Mask::try_from(&BoolArray::from_iter(filter_pattern)).vortex_unwrap();
3,712✔
406
    let filtered = filter(&masked, &filter_mask).vortex_unwrap();
3,712✔
407

408
    // This should be equivalent to directly filtering the original array
409
    let direct_filtered = filter(array, &filter_mask).vortex_unwrap();
3,712✔
410

411
    assert_eq!(filtered.len(), direct_filtered.len());
3,712✔
412
    for i in 0..filtered.len() {
824,528✔
413
        assert_eq!(
824,528✔
414
            filtered.scalar_at(i).vortex_unwrap(),
824,528✔
415
            direct_filtered.scalar_at(i).vortex_unwrap()
824,528✔
416
        );
417
    }
418
}
4,221✔
419

420
/// Tests that empty operations are consistent
421
pub fn test_empty_operations_consistency(array: &dyn Array) {
4,221✔
422
    let len = array.len();
4,221✔
423

424
    // Empty filter
425
    let empty_filter = filter(array, &Mask::new_false(len)).vortex_unwrap();
4,221✔
426
    assert_eq!(empty_filter.len(), 0);
4,221✔
427
    assert_eq!(empty_filter.dtype(), array.dtype());
4,221✔
428

429
    // Empty take
430
    let empty_indices =
4,221✔
431
        PrimitiveArray::empty::<u64>(vortex_dtype::Nullability::NonNullable).into_array();
4,221✔
432
    let empty_take = take(array, &empty_indices).vortex_unwrap();
4,221✔
433
    assert_eq!(empty_take.len(), 0);
4,221✔
434
    assert_eq!(empty_take.dtype(), array.dtype());
4,221✔
435

436
    // Empty slice (if array is non-empty)
437
    if len > 0 {
4,221✔
438
        let empty_slice = array.slice(0, 0).vortex_unwrap();
4,220✔
439
        assert_eq!(empty_slice.len(), 0);
4,220✔
440
        assert_eq!(empty_slice.dtype(), array.dtype());
4,220✔
441
    }
1✔
442
}
4,221✔
443

444
/// Tests that take preserves array properties
445
pub fn test_take_preserves_properties(array: &dyn Array) {
4,221✔
446
    let len = array.len();
4,221✔
447
    if len == 0 {
4,221✔
448
        return;
1✔
449
    }
4,220✔
450

451
    // Take all elements in original order
452
    let indices = PrimitiveArray::from_iter((0..len).map(|i| i as u64)).into_array();
1,651,964✔
453
    let taken = take(array, &indices).vortex_unwrap();
4,220✔
454

455
    // Should be identical to original
456
    assert_eq!(taken.len(), array.len());
4,220✔
457
    assert_eq!(taken.dtype(), array.dtype());
4,220✔
458
    for i in 0..len {
1,651,964✔
459
        assert_eq!(
1,651,964✔
460
            taken.scalar_at(i).vortex_unwrap(),
1,651,964✔
461
            array.scalar_at(i).vortex_unwrap()
1,651,964✔
462
        );
463
    }
464
}
4,221✔
465

466
/// Tests consistency with nullable indices.
467
///
468
/// # Invariant
469
/// `take(array, [Some(0), None, Some(2)])` should produce `[array[0], null, array[2]]`
470
///
471
/// # Test Details
472
/// - Creates an indices array with null at position 1: `[Some(0), None, Some(2)]`
473
/// - Takes elements using these indices
474
/// - Verifies that:
475
///   - Position 0 contains the value from array index 0
476
///   - Position 1 contains null
477
///   - Position 2 contains the value from array index 2
478
///   - The result array has nullable type
479
///
480
/// # Why This Matters
481
/// Nullable indices are a powerful feature that allows introducing nulls during
482
/// a take operation, which is useful for outer joins and similar operations.
483
pub fn test_nullable_indices_consistency(array: &dyn Array) {
4,221✔
484
    let len = array.len();
4,221✔
485
    if len < 3 {
4,221✔
486
        return; // Need at least 3 elements to test indices 0 and 2
509✔
487
    }
3,712✔
488

489
    // Create nullable indices where some indices are null
490
    let indices = PrimitiveArray::from_option_iter([Some(0u64), None, Some(2u64)]).into_array();
3,712✔
491

492
    let taken = take(array, &indices).vortex_unwrap();
3,712✔
493

494
    // Result should have nulls where indices were null
495
    assert_eq!(
3,712✔
496
        taken.len(),
3,712✔
497
        3,
498
        "Take with nullable indices should produce array of length 3, got {}",
×
UNCOV
499
        taken.len()
×
500
    );
501

502
    assert!(
3,712✔
503
        taken.dtype().is_nullable(),
3,712✔
504
        "Take with nullable indices should produce nullable array, but dtype is {:?}",
×
UNCOV
505
        taken.dtype()
×
506
    );
507

508
    // Check first element (from index 0)
509
    let expected_0 = array.scalar_at(0).vortex_unwrap().into_nullable();
3,712✔
510
    let actual_0 = taken.scalar_at(0).vortex_unwrap();
3,712✔
511
    assert_eq!(
3,712✔
512
        actual_0, expected_0,
513
        "Take with nullable indices: element at position 0 should be from array index 0. \
×
UNCOV
514
         Expected: {expected_0:?}, Actual: {actual_0:?}"
×
515
    );
516

517
    // Check second element (should be null)
518
    let actual_1 = taken.scalar_at(1).vortex_unwrap();
3,712✔
519
    assert!(
3,712✔
520
        actual_1.is_null(),
3,712✔
UNCOV
521
        "Take with nullable indices: element at position 1 should be null, but got {actual_1:?}"
×
522
    );
523

524
    // Check third element (from index 2)
525
    let expected_2 = array.scalar_at(2).vortex_unwrap().into_nullable();
3,712✔
526
    let actual_2 = taken.scalar_at(2).vortex_unwrap();
3,712✔
527
    assert_eq!(
3,712✔
528
        actual_2, expected_2,
529
        "Take with nullable indices: element at position 2 should be from array index 2. \
×
UNCOV
530
         Expected: {expected_2:?}, Actual: {actual_2:?}"
×
531
    );
532
}
4,221✔
533

534
/// Tests large array consistency
535
pub fn test_large_array_consistency(array: &dyn Array) {
4,221✔
536
    let len = array.len();
4,221✔
537
    if len < 1000 {
4,221✔
538
        return;
3,377✔
539
    }
844✔
540

541
    // Test with every 10th element
542
    let indices: Vec<u64> = (0..len).step_by(10).map(|i| i as u64).collect();
162,520✔
543
    let indices_array = PrimitiveArray::from_iter(indices).into_array();
844✔
544
    let taken = take(array, &indices_array).vortex_unwrap();
844✔
545

546
    // Create equivalent filter mask
547
    let mask_pattern: Vec<bool> = (0..len).map(|i| i % 10 == 0).collect();
1,624,592✔
548
    let mask = Mask::try_from(&BoolArray::from_iter(mask_pattern)).vortex_unwrap();
844✔
549
    let filtered = filter(array, &mask).vortex_unwrap();
844✔
550

551
    // Results should match
552
    assert_eq!(taken.len(), filtered.len());
844✔
553
    for i in 0..taken.len() {
162,520✔
554
        assert_eq!(
162,520✔
555
            taken.scalar_at(i).vortex_unwrap(),
162,520✔
556
            filtered.scalar_at(i).vortex_unwrap()
162,520✔
557
        );
558
    }
559
}
4,221✔
560

561
/// Tests that cast operations preserve array properties when sliced.
562
///
563
/// # Invariant
564
/// `cast(slice(array, start, end), dtype)` should equal `slice(cast(array, dtype), start, end)`
565
///
566
/// # Test Details
567
/// - Slices the array from index 2 to 7 (or len-2 if smaller)
568
/// - Casts the sliced array to a different type
569
/// - Compares against the canonical form of the array (without slicing or casting the canonical form)
570
/// - Verifies both approaches produce identical results
571
///
572
/// # Why This Matters
573
/// This test specifically catches bugs where encodings (like RunEndArray) fail to preserve
574
/// offset information during cast operations. Such bugs can lead to incorrect data being
575
/// returned after casting a sliced array.
576
pub fn test_cast_slice_consistency(array: &dyn Array) {
4,221✔
577
    use vortex_dtype::{DType, Nullability, PType};
578

579
    use crate::compute::cast;
580

581
    let len = array.len();
4,221✔
582
    if len < 5 {
4,221✔
583
        return; // Need at least 5 elements for meaningful slice
707✔
584
    }
3,514✔
585

586
    // Define slice bounds
587
    let start = 2;
3,514✔
588
    let end = 7.min(len - 2).max(start + 1); // Ensure we have at least 1 element
3,514✔
589

590
    // Get canonical form of the original array
591
    let canonical = array.to_canonical().vortex_unwrap();
3,514✔
592

593
    // Choose appropriate target dtype based on the array's type
594
    let target_dtypes = match array.dtype() {
3,514✔
595
        DType::Null => vec![],
3✔
596
        DType::Bool(nullability) => vec![
10✔
597
            DType::Primitive(PType::U8, *nullability),
10✔
598
            DType::Primitive(PType::I32, *nullability),
10✔
599
        ],
600
        DType::Primitive(ptype, nullability) => {
2,830✔
601
            let mut targets = vec![];
2,830✔
602
            // Test nullability changes
603
            let opposite_nullability = match nullability {
2,830✔
604
                Nullability::NonNullable => Nullability::Nullable,
2,216✔
605
                Nullability::Nullable => Nullability::NonNullable,
614✔
606
            };
607
            targets.push(DType::Primitive(*ptype, opposite_nullability));
2,830✔
608

609
            // Test widening casts
610
            match ptype {
2,830✔
611
                PType::U8 => {
152✔
612
                    targets.push(DType::Primitive(PType::U16, *nullability));
152✔
613
                    targets.push(DType::Primitive(PType::I16, *nullability));
152✔
614
                }
152✔
615
                PType::U16 => {
190✔
616
                    targets.push(DType::Primitive(PType::U32, *nullability));
190✔
617
                    targets.push(DType::Primitive(PType::I32, *nullability));
190✔
618
                }
190✔
619
                PType::U32 => {
267✔
620
                    targets.push(DType::Primitive(PType::U64, *nullability));
267✔
621
                    targets.push(DType::Primitive(PType::I64, *nullability));
267✔
622
                }
267✔
623
                PType::U64 => {
228✔
624
                    targets.push(DType::Primitive(PType::F64, *nullability));
228✔
625
                }
228✔
626
                PType::I8 => {
38✔
627
                    targets.push(DType::Primitive(PType::I16, *nullability));
38✔
628
                    targets.push(DType::Primitive(PType::F32, *nullability));
38✔
629
                }
38✔
630
                PType::I16 => {
115✔
631
                    targets.push(DType::Primitive(PType::I32, *nullability));
115✔
632
                    targets.push(DType::Primitive(PType::F32, *nullability));
115✔
633
                }
115✔
634
                PType::I32 => {
1,034✔
635
                    targets.push(DType::Primitive(PType::I64, *nullability));
1,034✔
636
                    targets.push(DType::Primitive(PType::F64, *nullability));
1,034✔
637
                }
1,034✔
638
                PType::I64 => {
269✔
639
                    targets.push(DType::Primitive(PType::F64, *nullability));
269✔
640
                }
269✔
NEW
UNCOV
641
                PType::F16 => {
×
NEW
UNCOV
642
                    targets.push(DType::Primitive(PType::F32, *nullability));
×
NEW
UNCOV
643
                }
×
644
                PType::F32 => {
344✔
645
                    targets.push(DType::Primitive(PType::F64, *nullability));
344✔
646
                    targets.push(DType::Primitive(PType::I32, *nullability));
344✔
647
                }
344✔
648
                PType::F64 => {
193✔
649
                    targets.push(DType::Primitive(PType::I64, *nullability));
193✔
650
                }
193✔
651
            }
652
            targets
2,830✔
653
        }
654
        DType::Utf8(nullability) => {
238✔
655
            let opposite = match nullability {
238✔
656
                Nullability::NonNullable => Nullability::Nullable,
160✔
657
                Nullability::Nullable => Nullability::NonNullable,
78✔
658
            };
659
            vec![DType::Utf8(opposite), DType::Binary(*nullability)]
238✔
660
        }
661
        DType::Binary(nullability) => {
3✔
662
            let opposite = match nullability {
3✔
663
                Nullability::NonNullable => Nullability::Nullable,
2✔
664
                Nullability::Nullable => Nullability::NonNullable,
1✔
665
            };
666
            vec![
3✔
667
                DType::Binary(opposite),
3✔
668
                DType::Utf8(*nullability), // May fail if not valid UTF-8
3✔
669
            ]
670
        }
671
        DType::Decimal(decimal_type, nullability) => {
268✔
672
            let opposite = match nullability {
268✔
673
                Nullability::NonNullable => Nullability::Nullable,
191✔
674
                Nullability::Nullable => Nullability::NonNullable,
77✔
675
            };
676
            vec![DType::Decimal(*decimal_type, opposite)]
268✔
677
        }
678
        DType::Struct(fields, nullability) => {
4✔
679
            let opposite = match nullability {
4✔
680
                Nullability::NonNullable => Nullability::Nullable,
4✔
NEW
UNCOV
681
                Nullability::Nullable => Nullability::NonNullable,
×
682
            };
683
            vec![DType::Struct(fields.clone(), opposite)]
4✔
684
        }
685
        DType::List(element_type, nullability) => {
3✔
686
            let opposite = match nullability {
3✔
687
                Nullability::NonNullable => Nullability::Nullable,
3✔
NEW
UNCOV
688
                Nullability::Nullable => Nullability::NonNullable,
×
689
            };
690
            vec![DType::List(element_type.clone(), opposite)]
3✔
691
        }
692
        DType::Extension(_) => vec![], // Extension types typically only cast to themselves
155✔
693
    };
694

695
    // Test each target dtype
696
    for target_dtype in target_dtypes {
12,091✔
697
        // Slice the array
698
        let sliced = array.slice(start, end).vortex_unwrap();
8,577✔
699

700
        // Try to cast the sliced array
701
        let slice_then_cast = match cast(&sliced, &target_dtype) {
8,577✔
702
            Ok(result) => result,
8,162✔
703
            Err(_) => continue, // Skip if cast fails
415✔
704
        };
705

706
        // Verify against canonical form
707
        assert_eq!(
8,162✔
708
            slice_then_cast.len(),
8,162✔
709
            end - start,
8,162✔
NEW
UNCOV
710
            "Sliced and casted array should have length {}, but has {}",
×
NEW
UNCOV
711
            end - start,
×
NEW
UNCOV
712
            slice_then_cast.len()
×
713
        );
714

715
        // Compare each value against the canonical form
716
        for i in 0..slice_then_cast.len() {
20,201✔
717
            let slice_cast_val = slice_then_cast.scalar_at(i).vortex_unwrap();
20,201✔
718

719
            // Get the corresponding value from the canonical array (adjusted for slice offset)
720
            let canonical_val = canonical.as_ref().scalar_at(start + i).vortex_unwrap();
20,201✔
721

722
            // Cast the canonical scalar to the target dtype
723
            let expected_val = match canonical_val.cast(&target_dtype) {
20,201✔
724
                Ok(val) => val,
19,702✔
725
                Err(_) => {
726
                    // If scalar cast fails, we can't compare - skip this target dtype
727
                    // This can happen for some type conversions that aren't supported at scalar level
728
                    break;
499✔
729
                }
730
            };
731

732
            assert_eq!(
19,702✔
733
                slice_cast_val,
734
                expected_val,
NEW
UNCOV
735
                "Cast of sliced array produced incorrect value at index {i}. \
×
NEW
UNCOV
736
                 Got: {slice_cast_val:?}, Expected: {expected_val:?} \
×
NEW
UNCOV
737
                 (canonical value at index {}: {canonical_val:?})\n\
×
NEW
UNCOV
738
                 This likely indicates the array encoding doesn't preserve offset information during cast.",
×
NEW
UNCOV
739
                start + i
×
740
            );
741
        }
742

743
        // Also test the other way: cast then slice
744
        let casted = match cast(array, &target_dtype) {
8,162✔
745
            Ok(result) => result,
7,507✔
746
            Err(_) => continue, // Skip if cast fails
655✔
747
        };
748
        let cast_then_slice = casted.slice(start, end).vortex_unwrap();
7,507✔
749

750
        // Verify the two approaches produce identical results
751
        assert_eq!(
7,507✔
752
            slice_then_cast.len(),
7,507✔
753
            cast_then_slice.len(),
7,507✔
NEW
UNCOV
754
            "Slice-then-cast and cast-then-slice should produce arrays of the same length"
×
755
        );
756

757
        for i in 0..slice_then_cast.len() {
19,853✔
758
            let slice_cast_val = slice_then_cast.scalar_at(i).vortex_unwrap();
19,853✔
759
            let cast_slice_val = cast_then_slice.scalar_at(i).vortex_unwrap();
19,853✔
760
            assert_eq!(
19,853✔
761
                slice_cast_val, cast_slice_val,
NEW
UNCOV
762
                "Slice-then-cast and cast-then-slice produced different values at index {i}. \
×
NEW
UNCOV
763
                 Slice-then-cast: {slice_cast_val:?}, Cast-then-slice: {cast_slice_val:?}"
×
764
            );
765
        }
766
    }
767
}
4,221✔
768

769
/// Run all consistency tests on an array.
770
///
771
/// This function executes a comprehensive suite of consistency tests that verify
772
/// the correctness of compute operations on Vortex arrays.
773
///
774
/// # Test Suite Overview
775
///
776
/// ## Core Operation Consistency
777
/// - **Filter/Take**: Verifies `filter(array, mask)` equals `take(array, true_indices)`
778
/// - **Mask Composition**: Ensures sequential masks equal combined masks
779
/// - **Slice/Filter**: Checks contiguous filters equal slice operations
780
/// - **Take/Slice**: Validates sequential takes equal slice operations
781
/// - **Cast/Slice**: Ensures cast operations preserve sliced array properties
782
///
783
/// ## Identity Operations
784
/// - **Filter Identity**: All-true mask preserves the array
785
/// - **Mask Identity**: All-false mask preserves values (as nullable)
786
/// - **Take Identity**: Taking all indices preserves the array
787
///
788
/// ## Edge Cases
789
/// - **Empty Operations**: Empty filters, takes, and slices behave correctly
790
/// - **Single Element**: Operations work with single-element arrays
791
/// - **Repeated Indices**: Take with duplicate indices works correctly
792
///
793
/// ## Null Handling
794
/// - **Nullable Indices**: Null indices produce null values
795
/// - **Mask/Filter Interaction**: Masking then filtering behaves predictably
796
///
797
/// ## Large Arrays
798
/// - **Performance**: Operations scale correctly to large arrays (1000+ elements)
799
/// ```
800
pub fn test_array_consistency(array: &dyn Array) {
4,221✔
801
    // Core operation consistency
802
    test_filter_take_consistency(array);
4,221✔
803
    test_double_mask_consistency(array);
4,221✔
804
    test_slice_filter_consistency(array);
4,221✔
805
    test_take_slice_consistency(array);
4,221✔
806
    test_cast_slice_consistency(array);
4,221✔
807

808
    // Identity operations
809
    test_filter_identity(array);
4,221✔
810
    test_mask_identity(array);
4,221✔
811
    test_take_preserves_properties(array);
4,221✔
812

813
    // Ordering and correctness
814
    test_filter_preserves_order(array);
4,221✔
815
    test_take_repeated_indices(array);
4,221✔
816

817
    // Null handling
818
    test_mask_filter_null_consistency(array);
4,221✔
819
    test_nullable_indices_consistency(array);
4,221✔
820

821
    // Edge cases
822
    test_empty_operations_consistency(array);
4,221✔
823
    test_large_array_consistency(array);
4,221✔
824
}
4,221✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc