risingwave_frontend/optimizer/plan_node/
batch_table_function.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::{Pretty, XmlNode};
16use risingwave_pb::batch_plan::TableFunctionNode;
17use risingwave_pb::batch_plan::plan_node::NodeBody;
18
19use super::batch::prelude::*;
20use super::utils::{Distill, childless_record};
21use super::{
22    BatchPlanRef as PlanRef, ExprRewritable, PlanBase, PlanTreeNodeLeaf, ToBatchPb,
23    ToDistributedBatch,
24};
25use crate::error::Result;
26use crate::expr::{ExprRewriter, ExprVisitor};
27use crate::optimizer::plan_node::ToLocalBatch;
28use crate::optimizer::plan_node::expr_visitable::ExprVisitable;
29use crate::optimizer::plan_node::logical_table_function::LogicalTableFunction;
30use crate::optimizer::property::{Distribution, Order};
31
32#[derive(Debug, Clone, PartialEq, Eq, Hash)]
33pub struct BatchTableFunction {
34    pub base: PlanBase<Batch>,
35    logical: LogicalTableFunction,
36}
37
38impl PlanTreeNodeLeaf for BatchTableFunction {}
39impl_plan_tree_node_for_leaf! { Batch, BatchTableFunction }
40
41impl BatchTableFunction {
42    pub fn new(logical: LogicalTableFunction) -> Self {
43        Self::with_dist(logical, Distribution::Single)
44    }
45
46    pub fn with_dist(logical: LogicalTableFunction, dist: Distribution) -> Self {
47        let ctx = logical.base.ctx().clone();
48        let base = PlanBase::new_batch(ctx, logical.schema().clone(), dist, Order::any());
49        BatchTableFunction { base, logical }
50    }
51
52    #[must_use]
53    pub fn logical(&self) -> &LogicalTableFunction {
54        &self.logical
55    }
56}
57
58impl Distill for BatchTableFunction {
59    fn distill<'a>(&self) -> XmlNode<'a> {
60        let data = Pretty::debug(&self.logical.table_function);
61        childless_record("BatchTableFunction", vec![("table_function", data)])
62    }
63}
64
65impl ToDistributedBatch for BatchTableFunction {
66    fn to_distributed(&self) -> Result<PlanRef> {
67        Ok(Self::with_dist(self.logical().clone(), Distribution::Single).into())
68    }
69}
70
71impl ToBatchPb for BatchTableFunction {
72    fn to_batch_prost_body(&self) -> NodeBody {
73        NodeBody::TableFunction(TableFunctionNode {
74            table_function: Some(self.logical.table_function.to_protobuf()),
75        })
76    }
77}
78
79impl ToLocalBatch for BatchTableFunction {
80    fn to_local(&self) -> Result<PlanRef> {
81        Ok(Self::with_dist(self.logical().clone(), Distribution::Single).into())
82    }
83}
84
85impl ExprRewritable<Batch> for BatchTableFunction {
86    fn has_rewritable_expr(&self) -> bool {
87        true
88    }
89
90    fn rewrite_exprs(&self, r: &mut dyn ExprRewriter) -> PlanRef {
91        Self::new(
92            self.logical
93                .rewrite_exprs(r)
94                .as_logical_table_function()
95                .unwrap()
96                .clone(),
97        )
98        .into()
99    }
100}
101
102impl ExprVisitable for BatchTableFunction {
103    fn visit_exprs(&self, v: &mut dyn ExprVisitor) {
104        self.logical
105            .table_function
106            .args
107            .iter()
108            .for_each(|e| v.visit_expr(e));
109    }
110}