datafusion-match-recognize 0.55.0

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.

//! Parsed-clause rendering.

use super::syntax::push_list;
use super::*;

/// Render a parsed clause in the canonical source-snapshot format.
pub fn render_parsed_match_clause(spec: &ParsedMatchClause) -> String {
    let mut out = String::new();
    push_list(&mut out, "PARTITION BY", &spec.partition_by);
    if !spec.order_by.is_empty() {
        out.push_str("ORDER BY\n");
        for sort in &spec.order_by {
            let direction = match sort.direction {
                MatchRecognizeSortDirection::Asc => "ASC",
                MatchRecognizeSortDirection::Desc => "DESC",
            };
            let nulls = match sort.null_ordering {
                MatchRecognizeNullOrdering::First => "NULLS FIRST",
                MatchRecognizeNullOrdering::Last => "NULLS LAST",
            };
            out.push_str(&format!("  {} {direction} {nulls}\n", sort.expr));
        }
    }
    out.push_str(&format!(
        "ROWS PER MATCH\n  {}\n",
        spec.source_rows_per_match
    ));
    out.push_str(&format!("AFTER MATCH SKIP\n  {}\n", spec.after_match_skip));
    out.push_str(&format!("PATTERN\n  {}\n", spec.pattern));
    push_list(&mut out, "SYMBOLS", &spec.symbols);
    if !spec.measures.is_empty() {
        out.push_str("MEASURES\n");
        for measure in &spec.measures {
            out.push_str(&format!(
                "  {} := {}\n",
                measure.name.as_deref().unwrap_or("<missing>"),
                measure.expression
            ));
        }
    }
    if !spec.definitions.is_empty() {
        out.push_str("DEFINE\n");
        for definition in &spec.definitions {
            out.push_str(&format!(
                "  {} := {}\n",
                definition.symbol, definition.predicate
            ));
        }
    }
    if let MatchRecognizeOutputContract::ExplicitNamedExpressions(expressions) =
        &spec.output
    {
        out.push_str("OUTPUT\n");
        if expressions.is_empty() {
            out.push_str("  <empty>\n");
        } else {
            for expression in expressions {
                out.push_str(&format!(
                    "  {} := {}\n",
                    expression.name, expression.expression
                ));
            }
        }
    }
    out.trim_end().to_string()
}