Skip to main content

arrow_data/equal/
fixed_list.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use crate::data::{ArrayData, contains_nulls};
6use arrow_schema::DataType;
7
8use super::equal_range;
9
10pub(super) fn fixed_list_equal(
11    lhs: &ArrayData,
12    rhs: &ArrayData,
13    lhs_start: usize,
14    rhs_start: usize,
15    len: usize,
16) -> bool {
17    let size = match lhs.data_type() {
18        DataType::FixedSizeList(_, i) => *i as usize,
19        _ => unreachable!(),
20    };
21
22    let lhs_values = &lhs.child_data()[0];
23    let rhs_values = &rhs.child_data()[0];
24
25    // Only checking one null mask here because by the time the control flow reaches
26    // this point, the equality of the two masks would have already been verified.
27    if !contains_nulls(lhs.nulls(), lhs_start, len) {
28        equal_range(
29            lhs_values,
30            rhs_values,
31            (lhs_start + lhs.offset()) * size,
32            (rhs_start + rhs.offset()) * size,
33            size * len,
34        )
35    } else {
36        // get a ref of the null buffer bytes, to use in testing for nullness
37        let lhs_nulls = lhs.nulls().unwrap();
38        let rhs_nulls = rhs.nulls().unwrap();
39        // with nulls, we need to compare item by item whenever it is not null
40        (0..len).all(|i| {
41            let lhs_pos = lhs_start + i;
42            let rhs_pos = rhs_start + i;
43
44            let lhs_is_null = lhs_nulls.is_null(lhs_pos);
45            let rhs_is_null = rhs_nulls.is_null(rhs_pos);
46
47            lhs_is_null
48                || (lhs_is_null == rhs_is_null)
49                    && equal_range(
50                        lhs_values,
51                        rhs_values,
52                        (lhs_pos + lhs.offset()) * size,
53                        (rhs_pos + rhs.offset()) * size,
54                        size, // 1 * size since we are comparing a single entry
55                    )
56        })
57    }
58}