datafusion-match-recognize 0.56.1

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.

//! Typed aggregate results retained by matching and predicate evaluation.
//!
//! [`AggregateValue`] preserves each result's domain when converting to and
//! from DataFusion scalars.

use datafusion::arrow::datatypes::i256;
use datafusion::common::ScalarValue;

/// A primitive aggregate result retained with candidate state.
///
/// Numeric and Boolean variants retain their scalar domain. `Null` represents
/// a null or unavailable aggregate result.
///
/// # Equality
///
/// Equality follows Rust primitive comparisons within each variant: signed zeros
/// compare equal, while NaNs compare unequal even to themselves.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AggregateValue {
    Int8(i8),
    Int16(i16),
    Int32(i32),
    Int64(i64),
    UInt8(u8),
    UInt16(u16),
    UInt32(u32),
    UInt64(u64),
    Float32(f32),
    Float64(f64),
    Decimal128(i128, u8, i8),
    Decimal256(i256, u8, i8),
    Boolean(bool),
    Null,
}

impl std::hash::Hash for AggregateValue {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // Include discriminant so equal payloads in different variants hash differently.
        std::mem::discriminant(self).hash(state);
        match self {
            Self::Int8(v) => v.hash(state),
            Self::Int16(v) => v.hash(state),
            Self::Int32(v) => v.hash(state),
            Self::Int64(v) => v.hash(state),
            Self::UInt8(v) => v.hash(state),
            Self::UInt16(v) => v.hash(state),
            Self::UInt32(v) => v.hash(state),
            Self::UInt64(v) => v.hash(state),
            Self::Float32(v) => (if *v == 0.0 { 0 } else { v.to_bits() }).hash(state),
            Self::Float64(v) => (if *v == 0.0 { 0 } else { v.to_bits() }).hash(state),
            Self::Decimal128(v, p, s) => {
                v.hash(state);
                p.hash(state);
                s.hash(state);
            }
            Self::Decimal256(v, p, s) => {
                v.hash(state);
                p.hash(state);
                s.hash(state);
            }
            Self::Boolean(v) => v.hash(state),
            Self::Null => {}
        }
    }
}

impl AggregateValue {
    /// Convert a supported non-null DataFusion scalar.
    ///
    /// Null and non-numeric/non-Boolean scalar variants return `None`.
    pub fn from_scalar(scalar: &ScalarValue) -> Option<Self> {
        match scalar {
            ScalarValue::Int8(Some(v)) => Some(Self::Int8(*v)),
            ScalarValue::Int16(Some(v)) => Some(Self::Int16(*v)),
            ScalarValue::Int32(Some(v)) => Some(Self::Int32(*v)),
            ScalarValue::Int64(Some(v)) => Some(Self::Int64(*v)),
            ScalarValue::UInt8(Some(v)) => Some(Self::UInt8(*v)),
            ScalarValue::UInt16(Some(v)) => Some(Self::UInt16(*v)),
            ScalarValue::UInt32(Some(v)) => Some(Self::UInt32(*v)),
            ScalarValue::UInt64(Some(v)) => Some(Self::UInt64(*v)),
            ScalarValue::Float32(Some(v)) => Some(Self::Float32(*v)),
            ScalarValue::Float64(Some(v)) => Some(Self::Float64(*v)),
            ScalarValue::Decimal128(Some(v), p, s) => Some(Self::Decimal128(*v, *p, *s)),
            ScalarValue::Decimal256(Some(v), p, s) => Some(Self::Decimal256(*v, *p, *s)),
            ScalarValue::Boolean(Some(v)) => Some(Self::Boolean(*v)),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests;