datafusion_match_recognize/planning/source/spec.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
16use crate::core::{MatchRecognizeOutputContract, MatchRecognizeSpec};
17
18use super::BoundMatchExpr;
19
20/// A `MATCH_RECOGNIZE` request whose expressions are bound to DataFusion
21/// columns, functions, and Arrow types.
22pub type BoundMatchSpec = MatchRecognizeSpec<BoundMatchExpr>;
23
24/// A bound request canonicalized against one specific input and capability set.
25///
26/// Instances are created only as part of `BoundMatchSource`.
27#[derive(Clone, Debug, PartialEq)]
28pub struct CanonicalBoundMatchSpec(BoundMatchSpec);
29
30impl CanonicalBoundMatchSpec {
31 pub(super) fn new(spec: BoundMatchSpec) -> Self {
32 Self(spec)
33 }
34
35 pub(super) fn as_spec_mut(&mut self) -> &mut BoundMatchSpec {
36 &mut self.0
37 }
38
39 /// Return the canonical source request for export or inspection.
40 pub fn as_spec(&self) -> &BoundMatchSpec {
41 &self.0
42 }
43}
44
45// Retained successful requests and early canonicalization errors own the same
46// potentially deep expression trees. Neither may use recursive destruction.
47impl Drop for CanonicalBoundMatchSpec {
48 fn drop(&mut self) {
49 for expr in self.0.partition_by.drain(..) {
50 expr.drop_binary_tree();
51 }
52 for sort in self.0.order_by.drain(..) {
53 sort.expr.drop_binary_tree();
54 }
55 for definition in self.0.definitions.drain(..) {
56 definition.predicate.drop_binary_tree();
57 }
58 for measure in self.0.measures.drain(..) {
59 measure.expression.drop_binary_tree();
60 }
61 if let MatchRecognizeOutputContract::ExplicitNamedExpressions(expressions) =
62 &mut self.0.output
63 {
64 for expr in expressions.drain(..) {
65 expr.expression.drop_binary_tree();
66 }
67 }
68 }
69}