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.

//! Convert SQL output and skip clauses to the shared semantic types.

use datafusion::sql::sqlparser::ast::{
    AfterMatchSkip as SQLAfterMatchSkip, EmptyMatchesMode as SQLEmptyMatchesMode,
    RowsPerMatch as SQLRowsPerMatch,
};

use crate::core::{AfterMatchSkip, EmptyMatchesMode, RowsPerMatch};

/// Convert `ROWS PER MATCH`, defaulting an omitted clause to [`RowsPerMatch::OneRow`].
pub fn convert_rows_per_match(rows_per_match: Option<SQLRowsPerMatch>) -> RowsPerMatch {
    match rows_per_match {
        None | Some(SQLRowsPerMatch::OneRow) => RowsPerMatch::OneRow,
        Some(SQLRowsPerMatch::AllRows(mode)) => {
            let converted_mode = match mode {
                Some(SQLEmptyMatchesMode::Show) | None => EmptyMatchesMode::Show,
                Some(SQLEmptyMatchesMode::Omit) => EmptyMatchesMode::Omit,
                Some(SQLEmptyMatchesMode::WithUnmatched) => {
                    EmptyMatchesMode::WithUnmatched
                }
            };
            RowsPerMatch::AllRows(converted_mode)
        }
    }
}

/// Convert `AFTER MATCH SKIP`, applying the default [`AfterMatchSkip::PastLastRow`].
pub fn convert_after_match_skip(
    after_match_skip: Option<SQLAfterMatchSkip>,
) -> AfterMatchSkip {
    match after_match_skip {
        None | Some(SQLAfterMatchSkip::PastLastRow) => AfterMatchSkip::PastLastRow,
        Some(SQLAfterMatchSkip::ToNextRow) => AfterMatchSkip::ToNextRow,
        Some(SQLAfterMatchSkip::ToFirst(symbol)) => AfterMatchSkip::ToFirst(symbol.value),
        Some(SQLAfterMatchSkip::ToLast(symbol)) => AfterMatchSkip::ToLast(symbol.value),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn omitted_rows_per_match_defaults_to_one_row() {
        assert_eq!(convert_rows_per_match(None), RowsPerMatch::OneRow);
    }
}