risingwave_frontend/optimizer/plan_node/
expr_rewritable.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::*;
16
17/// Rewrites expressions in a `PlanRef`. Due to `Share` operator,
18/// the `ExprRewriter` needs to be idempotent i.e., applying it more than once
19/// to the same `ExprImpl` will be a noop on subsequent applications.
20/// `rewrite_exprs` should only return a plan with the given node modified.
21/// To rewrite recursively, call `rewrite_exprs_recursive` on [`RewriteExprsRecursive`].
22pub trait ExprRewritable {
23    fn has_rewritable_expr(&self) -> bool {
24        false
25    }
26
27    fn rewrite_exprs(&self, _r: &mut dyn ExprRewriter) -> PlanRef {
28        unimplemented!()
29    }
30}
31
32impl ExprRewritable for PlanRef {
33    fn has_rewritable_expr(&self) -> bool {
34        true
35    }
36
37    fn rewrite_exprs(&self, r: &mut dyn ExprRewriter) -> PlanRef {
38        if self.deref().has_rewritable_expr() {
39            self.deref().rewrite_exprs(r)
40        } else {
41            self.clone()
42        }
43    }
44}