MCPcopy Create free account
hub / github.com/apache/arrow-rs / list_equal

Function list_equal

arrow-data/src/equal/list.rs:46–149  ·  view source on GitHub ↗
(
    lhs: &ArrayData,
    rhs: &ArrayData,
    lhs_start: usize,
    rhs_start: usize,
    len: usize,
)

Source from the content-addressed store, hash-verified

44}
45
46pub(super) fn list_equal<T: ArrowNativeType + Integer>(
47 lhs: &ArrayData,
48 rhs: &ArrayData,
49 lhs_start: usize,
50 rhs_start: usize,
51 len: usize,
52) -> bool {
53 let lhs_offsets = lhs.buffer::<T>(0);
54 let rhs_offsets = rhs.buffer::<T>(0);
55
56 // There is an edge-case where a n-length list that has 0 children, results in panics.
57 // For example; an array with offsets [0, 0, 0, 0, 0] has 4 slots, but will have
58 // no valid children.
59 // Under logical equality, the child null bitmap will be an empty buffer, as there are
60 // no child values. This causes panics when trying to count set bits.
61 //
62 // We caught this by chance from an accidental test-case, but due to the nature of this
63 // crash only occurring on list equality checks, we are adding a check here, instead of
64 // on the buffer/bitmap utilities, as a length check would incur a penalty for almost all
65 // other use-cases.
66 //
67 // The solution is to check the number of child values from offsets, and return `true` if
68 // they = 0. Empty arrays are equal, so this is correct.
69 //
70 // It's unlikely that one would create a n-length list array with no values, where n > 0,
71 // however, one is more likely to slice into a list array and get a region that has 0
72 // child values.
73 // The test that triggered this behaviour had [4, 4] as a slice of 1 value slot.
74 // For the edge case that zero length list arrays are always equal.
75 if len == 0 {
76 return true;
77 }
78
79 let lhs_child_length = lhs_offsets[lhs_start + len].to_usize().unwrap()
80 - lhs_offsets[lhs_start].to_usize().unwrap();
81
82 let rhs_child_length = rhs_offsets[rhs_start + len].to_usize().unwrap()
83 - rhs_offsets[rhs_start].to_usize().unwrap();
84
85 if lhs_child_length == 0 && lhs_child_length == rhs_child_length {
86 return true;
87 }
88
89 let lhs_values = &lhs.child_data()[0];
90 let rhs_values = &rhs.child_data()[0];
91
92 let lhs_null_count = count_nulls(lhs.nulls(), lhs_start, len);
93 let rhs_null_count = count_nulls(rhs.nulls(), rhs_start, len);
94
95 if lhs_null_count != rhs_null_count {
96 return false;
97 }
98
99 if lhs_null_count == 0 && rhs_null_count == 0 {
100 lhs_child_length == rhs_child_length
101 && lengths_equal(
102 &lhs_offsets[lhs_start..lhs_start + len],
103 &rhs_offsets[rhs_start..rhs_start + len],

Callers

nothing calls this directly

Calls 8

count_nullsFunction · 0.85
lengths_equalFunction · 0.85
equal_rangeFunction · 0.85
to_usizeMethod · 0.80
child_dataMethod · 0.80
allMethod · 0.80
nullsMethod · 0.45
is_nullMethod · 0.45

Tested by

no test coverage detected