risingwave_frontend/optimizer/plan_node/
batch_project.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 pretty_xmlish::XmlNode;
16use risingwave_pb::batch_plan::ProjectNode;
17use risingwave_pb::batch_plan::plan_node::NodeBody;
18use risingwave_pb::expr::ExprNode;
19
20use super::batch::prelude::*;
21use super::utils::{Distill, childless_record};
22use super::{
23    BatchPlanRef as PlanRef, ExprRewritable, PlanBase, PlanTreeNodeUnary, ToBatchPb,
24    ToDistributedBatch, generic,
25};
26use crate::error::Result;
27use crate::expr::{Expr, ExprImpl, ExprRewriter, ExprVisitor};
28use crate::optimizer::plan_node::ToLocalBatch;
29use crate::optimizer::plan_node::expr_visitable::ExprVisitable;
30use crate::utils::ColIndexMappingRewriteExt;
31
32/// `BatchProject` implements [`super::LogicalProject`] to evaluate specified expressions on input
33/// rows
34#[derive(Debug, Clone, PartialEq, Eq, Hash)]
35pub struct BatchProject {
36    pub base: PlanBase<Batch>,
37    core: generic::Project<PlanRef>,
38}
39
40impl BatchProject {
41    pub fn new(core: generic::Project<PlanRef>) -> Self {
42        let distribution = core
43            .i2o_col_mapping()
44            .rewrite_provided_distribution(core.input.distribution());
45        let order = core
46            .i2o_col_mapping()
47            .rewrite_provided_order(core.input.order());
48
49        let base = PlanBase::new_batch_with_core(&core, distribution, order);
50        BatchProject { base, core }
51    }
52
53    pub fn exprs(&self) -> &Vec<ExprImpl> {
54        &self.core.exprs
55    }
56}
57
58impl Distill for BatchProject {
59    fn distill<'a>(&self) -> XmlNode<'a> {
60        childless_record("BatchProject", self.core.fields_pretty(self.schema()))
61    }
62}
63
64impl PlanTreeNodeUnary<Batch> for BatchProject {
65    fn input(&self) -> PlanRef {
66        self.core.input.clone()
67    }
68
69    fn clone_with_input(&self, input: PlanRef) -> Self {
70        let mut core = self.core.clone();
71        core.input = input;
72        Self::new(core)
73    }
74}
75
76impl_plan_tree_node_for_unary! { Batch, BatchProject }
77
78impl ToDistributedBatch for BatchProject {
79    fn to_distributed(&self) -> Result<PlanRef> {
80        let new_input = self.input().to_distributed()?;
81        Ok(self.clone_with_input(new_input).into())
82    }
83}
84
85impl ToBatchPb for BatchProject {
86    fn to_batch_prost_body(&self) -> NodeBody {
87        let select_list = self
88            .core
89            .exprs
90            .iter()
91            .map(|expr| expr.to_expr_proto())
92            .collect::<Vec<ExprNode>>();
93        NodeBody::Project(ProjectNode { select_list })
94    }
95}
96
97impl ToLocalBatch for BatchProject {
98    fn to_local(&self) -> Result<PlanRef> {
99        let new_input = self.input().to_local()?;
100        Ok(self.clone_with_input(new_input).into())
101    }
102}
103
104impl ExprRewritable<Batch> for BatchProject {
105    fn has_rewritable_expr(&self) -> bool {
106        true
107    }
108
109    fn rewrite_exprs(&self, r: &mut dyn ExprRewriter) -> PlanRef {
110        let mut core = self.core.clone();
111        core.rewrite_exprs(r);
112        Self::new(core).into()
113    }
114}
115
116impl ExprVisitable for BatchProject {
117    fn visit_exprs(&self, v: &mut dyn ExprVisitor) {
118        self.core.visit_exprs(v);
119    }
120}