risingwave_frontend/expr/
expr_mutator.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    AggCall, CorrelatedInputRef, ExprImpl, FunctionCall, FunctionCallWithLambda, InputRef, Literal,
17    Now, Parameter, Subquery, TableFunction, UserDefinedFunction, WindowFunction,
18};
19
20/// with the same visit logic of `ExprVisitor`, but mutable.
21pub trait ExprMutator {
22    fn visit_expr(&mut self, expr: &mut ExprImpl) {
23        match expr {
24            ExprImpl::InputRef(inner) => self.visit_input_ref(inner),
25            ExprImpl::Literal(inner) => self.visit_literal(inner),
26            ExprImpl::FunctionCall(inner) => self.visit_function_call(inner),
27            ExprImpl::FunctionCallWithLambda(inner) => self.visit_function_call_with_lambda(inner),
28            ExprImpl::AggCall(inner) => self.visit_agg_call(inner),
29            ExprImpl::Subquery(inner) => self.visit_subquery(inner),
30            ExprImpl::CorrelatedInputRef(inner) => self.visit_correlated_input_ref(inner),
31            ExprImpl::TableFunction(inner) => self.visit_table_function(inner),
32            ExprImpl::WindowFunction(inner) => self.visit_window_function(inner),
33            ExprImpl::UserDefinedFunction(inner) => self.visit_user_defined_function(inner),
34            ExprImpl::Parameter(inner) => self.visit_parameter(inner),
35            ExprImpl::Now(inner) => self.visit_now(inner),
36        }
37    }
38    fn visit_function_call(&mut self, func_call: &mut FunctionCall) {
39        func_call
40            .inputs_mut()
41            .iter_mut()
42            .for_each(|expr| self.visit_expr(expr))
43    }
44
45    fn visit_function_call_with_lambda(&mut self, func_call: &mut FunctionCallWithLambda) {
46        self.visit_function_call(func_call.base_mut())
47    }
48
49    fn visit_agg_call(&mut self, agg_call: &mut AggCall) {
50        agg_call
51            .args_mut()
52            .iter_mut()
53            .for_each(|expr| self.visit_expr(expr));
54        agg_call.order_by_mut().visit_expr_mut(self);
55        agg_call.filter_mut().visit_expr_mut(self);
56    }
57    fn visit_literal(&mut self, _: &mut Literal) {}
58    fn visit_parameter(&mut self, _: &mut Parameter) {}
59    fn visit_input_ref(&mut self, _: &mut InputRef) {}
60    fn visit_subquery(&mut self, _: &mut Subquery) {}
61    fn visit_correlated_input_ref(&mut self, _: &mut CorrelatedInputRef) {}
62    fn visit_table_function(&mut self, func_call: &mut TableFunction) {
63        func_call
64            .args
65            .iter_mut()
66            .for_each(|expr| self.visit_expr(expr))
67    }
68    fn visit_window_function(&mut self, func_call: &mut WindowFunction) {
69        func_call
70            .args
71            .iter_mut()
72            .for_each(|expr| self.visit_expr(expr));
73    }
74    fn visit_user_defined_function(&mut self, func_call: &mut UserDefinedFunction) {
75        func_call
76            .args
77            .iter_mut()
78            .for_each(|expr| self.visit_expr(expr));
79    }
80    fn visit_now(&mut self, _: &mut Now) {}
81}