From 2d2f4121b52e75e9cb4662b34450807e6266149a Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Sat, 3 Feb 2024 17:26:07 -0500 Subject: [PATCH] add rewrite rule --- src/frontend/src/binder/expr/mod.rs | 2 +- .../const_case_when_rewriter.rs | 18 ++++++++++++++---- .../rule/const_case_when_eval_rule.rs | 6 ++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/frontend/src/binder/expr/mod.rs b/src/frontend/src/binder/expr/mod.rs index 2de6db034eec4..2a517771db62a 100644 --- a/src/frontend/src/binder/expr/mod.rs +++ b/src/frontend/src/binder/expr/mod.rs @@ -602,7 +602,7 @@ impl Binder { ); if constant_case_when_flag { - return Ok(FunctionCall::new(ExprType::ConstantCaseWhenEval, constant_case_when_eval_inputs)?.into()); + return Ok(FunctionCall::new(ExprType::ConstantLookup, constant_case_when_eval_inputs)?.into()); } // See if the case-when expression can be optimized diff --git a/src/frontend/src/optimizer/plan_expr_rewriter/const_case_when_rewriter.rs b/src/frontend/src/optimizer/plan_expr_rewriter/const_case_when_rewriter.rs index 10c2cc3e1188d..0489a4e41af02 100644 --- a/src/frontend/src/optimizer/plan_expr_rewriter/const_case_when_rewriter.rs +++ b/src/frontend/src/optimizer/plan_expr_rewriter/const_case_when_rewriter.rs @@ -12,13 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::expr::{ExprImpl, ExprRewriter, FunctionCall}; +use risingwave_common::error::RwError; -pub struct ConstCaseWhenRewriter {} +use crate::expr::{ExprImpl, ExprRewriter, FunctionCall, ExprType}; + +pub struct ConstCaseWhenRewriter { + pub error: Option, +} impl ExprRewriter for ConstCaseWhenRewriter { fn rewrite_function_call(&mut self, func_call: FunctionCall) -> ExprImpl { - println!("Current func_call: {:#?}", func_call); - todo!() + if func_call.func_type() != ExprType::ConstantLookup { + return func_call.into(); + } + if func_call.inputs().len() != 1 { + // Normal constant lookup pass + return func_call.into(); + } + func_call.inputs()[0].clone().into() } } \ No newline at end of file diff --git a/src/frontend/src/optimizer/rule/const_case_when_eval_rule.rs b/src/frontend/src/optimizer/rule/const_case_when_eval_rule.rs index 60ec95b746d84..73d712505269e 100644 --- a/src/frontend/src/optimizer/rule/const_case_when_eval_rule.rs +++ b/src/frontend/src/optimizer/rule/const_case_when_eval_rule.rs @@ -19,11 +19,9 @@ use crate::optimizer::plan_expr_rewriter::ConstCaseWhenRewriter; pub struct ConstCaseWhenEvalRule {} impl Rule for ConstCaseWhenEvalRule { fn apply(&self, plan: PlanRef) -> Option { - println!("Current plan: {:#?}", plan); let values: &LogicalValues = plan.as_logical_values()?; - println!("Current values: {:#?}", values); - let _const_case_when_rewriter = ConstCaseWhenRewriter {}; - todo!() + let mut const_case_when_rewriter = ConstCaseWhenRewriter { error: None }; + Some(values.rewrite_exprs(&mut const_case_when_rewriter)) } }