Skip to main content

datafusion_match_recognize/core/pattern/
bounds.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//! Structural minimum and maximum row counts derived from the pattern AST.
17
18use super::Pattern;
19
20impl Pattern {
21    /// Return a structural upper bound on matched rows, when representable.
22    ///
23    /// Returns `None` for unbounded repetitions or when the calculated bound
24    /// overflows `usize`.
25    ///
26    /// This is used to determine eligibility for parallel execution without
27    /// PARTITION BY, where bounded patterns allow safe chunk splitting.
28    pub fn max_match_rows(&self) -> Option<usize> {
29        self.structural_info().max_match_rows()
30    }
31
32    /// Returns the minimum number of rows this pattern can match.
33    ///
34    /// This is a conservative structural property derived from the pattern AST:
35    /// - Named symbols consume 1 row
36    /// - Anchors (`^`/`$`) consume 0 rows
37    /// - Concatenation sums minima
38    /// - Alternation takes the minimum across branches
39    /// - Repetition uses the lower bound of the quantifier (0 for `*`, `?`, `{,n}`)
40    ///
41    /// This is useful for determining if a pattern can match the empty string
42    /// (`min_match_rows() == 0`).
43    pub fn min_match_rows(&self) -> usize {
44        self.structural_info().min_match_rows()
45    }
46}