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.

//! Predicate and output test helpers shared across matching and storage tests.
use crate::core::MatchMetadataColumn;
use datafusion::arrow::{
    datatypes::{DataType, Schema},
    record_batch::RecordBatch,
};
use datafusion::common::{DataFusionError, Result, ScalarValue};
use datafusion::physical_expr::PhysicalExpr;
use datafusion::physical_plan::ColumnarValue;
use std::hash::{Hash, Hasher};
use std::sync::{
    Arc,
    atomic::{AtomicBool, AtomicUsize, Ordering},
};

#[derive(Debug, Clone)]
pub(crate) struct CountingBoolExpr {
    id: usize,
    calls: Arc<AtomicUsize>,
    value: bool,
    fail: bool,
    abort_on_evaluate: Option<Arc<AtomicBool>>,
}

impl CountingBoolExpr {
    pub(crate) fn returning(calls: Arc<AtomicUsize>, value: bool) -> Self {
        static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
        Self {
            id: NEXT_ID.fetch_add(1, Ordering::Relaxed),
            calls,
            value,
            fail: false,
            abort_on_evaluate: None,
        }
    }

    pub(crate) fn failing(calls: Arc<AtomicUsize>) -> Self {
        Self {
            fail: true,
            ..Self::returning(calls, false)
        }
    }

    pub(crate) fn abort_on_evaluate(mut self, signal: Arc<AtomicBool>) -> Self {
        self.abort_on_evaluate = Some(signal);
        self
    }
}

impl PartialEq for CountingBoolExpr {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for CountingBoolExpr {}

impl Hash for CountingBoolExpr {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl std::fmt::Display for CountingBoolExpr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "counting_bool_{}", self.id)
    }
}

impl PhysicalExpr for CountingBoolExpr {
    fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
        Ok(DataType::Boolean)
    }

    fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
        Ok(false)
    }

    fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        if let Some(signal) = &self.abort_on_evaluate {
            signal.store(true, Ordering::SeqCst);
        }
        if self.fail {
            return Err(DataFusionError::Execution(
                "counting physical expression failure".to_string(),
            ));
        }
        Ok(ColumnarValue::Scalar(ScalarValue::Boolean(Some(
            self.value,
        ))))
    }

    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
        Vec::new()
    }

    fn with_new_children(
        self: Arc<Self>,
        children: Vec<Arc<dyn PhysicalExpr>>,
    ) -> Result<Arc<dyn PhysicalExpr>> {
        if !children.is_empty() {
            return Err(DataFusionError::Internal(
                "counting Boolean test expression cannot accept children".to_string(),
            ));
        }
        Ok(self)
    }

    fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self, f)
    }
}
pub(crate) const TEST_OUTPUT_METADATA: [MatchMetadataColumn; 3] = [
    MatchMetadataColumn::Classifier,
    MatchMetadataColumn::MatchNumber,
    MatchMetadataColumn::MatchSequenceNumber,
];