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.

//! Match cardinality, empty-match, and restart policies.

use std::fmt::{self, Display, Formatter};

/// Specification for how many rows to return per match.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
pub enum RowsPerMatch {
    /// Return one row per match
    OneRow,
    /// Return all rows per match with specified empty matches mode
    AllRows(EmptyMatchesMode),
}

/// How to handle empty matches.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
pub enum EmptyMatchesMode {
    /// Show empty matches
    Show,
    /// Omit empty matches
    Omit,
    /// Include unmatched rows
    WithUnmatched,
}

/// Specification for where to continue after a match.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
pub enum AfterMatchSkip {
    /// Skip past the last row of the match
    PastLastRow,
    /// Skip to the next row after the match start
    ToNextRow,
    /// Skip to the first occurrence of the specified symbol
    ToFirst(String),
    /// Skip to the last occurrence of the specified symbol
    ToLast(String),
}

// Display implementations

impl Display for RowsPerMatch {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            RowsPerMatch::OneRow => write!(f, "ONE ROW PER MATCH"),
            RowsPerMatch::AllRows(mode) => write!(f, "ALL ROWS PER MATCH {mode}"),
        }
    }
}

impl Display for EmptyMatchesMode {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            EmptyMatchesMode::Show => write!(f, "SHOW EMPTY MATCHES"),
            EmptyMatchesMode::Omit => write!(f, "OMIT EMPTY MATCHES"),
            EmptyMatchesMode::WithUnmatched => write!(f, "WITH UNMATCHED ROWS"),
        }
    }
}

impl Display for AfterMatchSkip {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            AfterMatchSkip::PastLastRow => write!(f, "PAST LAST ROW"),
            AfterMatchSkip::ToNextRow => write!(f, "TO NEXT ROW"),
            AfterMatchSkip::ToFirst(symbol) => write!(f, "TO FIRST {symbol}"),
            AfterMatchSkip::ToLast(symbol) => write!(f, "TO LAST {symbol}"),
        }
    }
}