datafusion-match-recognize 0.55.0

SQL MATCH_RECOGNIZE row-pattern matching for Apache DataFusion
Documentation
// Licensed under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2025-Present Datadog, Inc.

//! SQL null checks on individual selected rows.

use datafusion::arrow::array::{Array, AsArray};
use datafusion::arrow::datatypes::{
    DataType, Int16Type, Int32Type, Int64Type, RunEndIndexType,
};
use datafusion::arrow::downcast_dictionary_array;

/// Resolve the current row of an encoded COUNT input without expanding its NULL bitmap.
pub(crate) fn is_logically_null(array: &dyn Array, row: usize) -> bool {
    downcast_dictionary_array!(
        array => array.key(row).is_none_or(|key| is_logically_null(array.values().as_ref(), key)),
        DataType::RunEndEncoded(run_ends, _) => match run_ends.data_type() {
            DataType::Int16 => run_value_is_null::<Int16Type>(array, row),
            DataType::Int32 => run_value_is_null::<Int32Type>(array, row),
            DataType::Int64 => run_value_is_null::<Int64Type>(array, row),
            data_type => unreachable!("invalid Arrow run-end index type: {data_type}"),
        },
        DataType::Union(..) => {
            let union = array.as_union();
            is_logically_null(union.child(union.type_id(row)).as_ref(), union.value_offset(row))
        },
        DataType::Null => true,
        _ => array.is_null(row),
    )
}

fn run_value_is_null<R: RunEndIndexType>(array: &dyn Array, row: usize) -> bool {
    let array = array.as_run::<R>();
    is_logically_null(array.values().as_ref(), array.get_physical_index(row))
}

#[cfg(test)]
mod tests {
    use datafusion::arrow::array::{NullArray, RunArray};

    use super::*;

    #[test]
    fn logical_null_check_follows_sliced_run_offsets() {
        let array = RunArray::<Int16Type>::from_iter([Some("a"), Some("a"), None, None]);
        let sliced = array.slice(1, 3);
        assert!(!is_logically_null(&sliced, 0));
        assert!(is_logically_null(&sliced, 1));
        assert!(is_logically_null(&sliced, 2));
        assert!(is_logically_null(&NullArray::new(1), 0));
    }
    #[test]
    fn union_rows_use_the_active_child_across_dense_and_sparse_slices() {
        use datafusion::arrow::array::{ArrayRef, Int32Array, UnionArray};
        use datafusion::arrow::datatypes::{Field, UnionFields};
        use std::sync::Arc;
        let fields: UnionFields = [
            (2, Arc::new(Field::new("a", DataType::Int32, true))),
            (5, Arc::new(Field::new("b", DataType::Int32, true))),
        ]
        .into_iter()
        .collect();
        let types = vec![5_i8, 2, 5, 2];
        let child = |v: Vec<Option<i32>>| -> ArrayRef { Arc::new(Int32Array::from(v)) };
        for (offsets, a, b) in [
            (
                Some(vec![0, 0, 1, 1].into()),
                vec![Some(1), None],
                vec![None, Some(2)],
            ),
            (
                None,
                vec![None, Some(1), None, None],
                vec![None, None, Some(2), None],
            ),
        ] {
            let union = UnionArray::try_new(
                fields.clone(),
                types.clone().into(),
                offsets,
                vec![child(a), child(b)],
            )
            .unwrap()
            .slice(1, 3);
            let arrow = union.logical_nulls().unwrap();
            for row in 0..union.len() {
                assert_eq!(is_logically_null(&union, row), arrow.is_null(row));
            }
        }
    }
}