datafusion_match_recognize/core/pattern/output.rs
1// Licensed under the Apache License, Version 2.0 (the
2// "License"); you may not use this file except in compliance
3// with the License. You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing,
8// software distributed under the License is distributed on an
9// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10// KIND, either express or implied. See the License for the
11// specific language governing permissions and limitations
12// under the License.
13//
14// This product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2025-Present Datadog, Inc.
15
16//! Match cardinality, empty-match, and restart policies.
17
18use std::fmt::{self, Display, Formatter};
19
20/// Specification for how many rows to return per match.
21#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
22pub enum RowsPerMatch {
23 /// Return one row per match
24 OneRow,
25 /// Return all rows per match with specified empty matches mode
26 AllRows(EmptyMatchesMode),
27}
28
29/// How to handle empty matches.
30#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
31pub enum EmptyMatchesMode {
32 /// Show empty matches
33 Show,
34 /// Omit empty matches
35 Omit,
36 /// Include unmatched rows
37 WithUnmatched,
38}
39
40/// Specification for where to continue after a match.
41#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
42pub enum AfterMatchSkip {
43 /// Skip past the last row of the match
44 PastLastRow,
45 /// Skip to the next row after the match start
46 ToNextRow,
47 /// Skip to the first occurrence of the specified symbol
48 ToFirst(String),
49 /// Skip to the last occurrence of the specified symbol
50 ToLast(String),
51}
52
53// Display implementations
54
55impl Display for RowsPerMatch {
56 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
57 match self {
58 RowsPerMatch::OneRow => write!(f, "ONE ROW PER MATCH"),
59 RowsPerMatch::AllRows(mode) => write!(f, "ALL ROWS PER MATCH {mode}"),
60 }
61 }
62}
63
64impl Display for EmptyMatchesMode {
65 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
66 match self {
67 EmptyMatchesMode::Show => write!(f, "SHOW EMPTY MATCHES"),
68 EmptyMatchesMode::Omit => write!(f, "OMIT EMPTY MATCHES"),
69 EmptyMatchesMode::WithUnmatched => write!(f, "WITH UNMATCHED ROWS"),
70 }
71 }
72}
73
74impl Display for AfterMatchSkip {
75 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
76 match self {
77 AfterMatchSkip::PastLastRow => write!(f, "PAST LAST ROW"),
78 AfterMatchSkip::ToNextRow => write!(f, "TO NEXT ROW"),
79 AfterMatchSkip::ToFirst(symbol) => write!(f, "TO FIRST {symbol}"),
80 AfterMatchSkip::ToLast(symbol) => write!(f, "TO LAST {symbol}"),
81 }
82 }
83}