datafusion-match-recognize 0.56.0

MATCH_RECOGNIZE extension for 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.

//! Stable identities shared by pattern traversal and partition scheduling.

use crate::execution::matching::PartitionKey;
use datafusion_common::Result;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

macro_rules! typed_id {
    ($name:ident, $raw:ty) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        #[repr(transparent)]
        pub(crate) struct $name($raw);

        impl $name {
            pub(crate) const fn new(value: $raw) -> Self {
                Self(value)
            }
        }

        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                std::fmt::Display::fmt(&self.0, f)
            }
        }
    };
    ($name:ident, $raw:ty, get) => {
        typed_id!($name, $raw);

        impl $name {
            pub(crate) const fn get(self) -> $raw {
                self.0
            }
        }
    };
}

typed_id!(MatchProgramId, u64);
typed_id!(ExecutionPartitionId, usize);
typed_id!(MatchPartitionId, usize, get);
typed_id!(ActiveProgramGeneration, u64, get);
typed_id!(CollectionHeadGeneration, u64, get);

static NEXT_MATCH_PROGRAM_ID: AtomicU64 = AtomicU64::new(1);

impl MatchProgramId {
    /// Allocate an identity retained by every clone of one executable program.
    pub(crate) fn try_allocate() -> Result<Self> {
        NEXT_MATCH_PROGRAM_ID
            .try_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
                current.checked_add(1)
            })
            .map(Self::new)
            .map_err(|_| {
                datafusion_common::DataFusionError::Execution(
                    "MATCH_RECOGNIZE executable program ID space exhausted".to_string(),
                )
            })
    }
}

/// A closed range can resume only in its original physical head. An open
/// partition needs its canonical input key to recognise the same key later.
#[derive(Debug, Clone)]
pub(crate) enum PartitionKeyBinding {
    HeadLocal(CollectionHeadGeneration),
    Canonical(PartitionKey),
}

impl PartitionKeyBinding {
    pub(crate) fn canonical(&self) -> Option<&PartitionKey> {
        match self {
            Self::Canonical(key) => Some(key),
            Self::HeadLocal(_) => None,
        }
    }

    pub(crate) fn same_owner(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::HeadLocal(left), Self::HeadLocal(right)) => left == right,
            (Self::Canonical(left), Self::Canonical(right)) => Arc::ptr_eq(left, right),
            _ => false,
        }
    }
}

impl From<PartitionKey> for PartitionKeyBinding {
    fn from(key: PartitionKey) -> Self {
        Self::Canonical(key)
    }
}