Skip to main content

risingwave_frontend/optimizer/plan_node/
batch_values.rs

1// Copyright 2022 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::ValuesNode;
17use risingwave_pb::batch_plan::plan_node::NodeBody;
18use risingwave_pb::batch_plan::values_node::ExprTuple;
19
20use super::batch::prelude::*;
21use super::utils::{Distill, childless_record};
22use super::{
23    BatchPlanRef as PlanRef, ExprRewritable, PlanBase, PlanTreeNodeLeaf, 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::optimizer::property::{Distribution, Order};
31
32#[derive(Debug, Clone, PartialEq, Eq, Hash)]
33pub struct BatchValues {
34    pub base: PlanBase<Batch>,
35    core: generic::Values,
36}
37
38impl PlanTreeNodeLeaf for BatchValues {}
39impl_plan_tree_node_for_leaf! { Batch, BatchValues }
40
41impl BatchValues {
42    pub fn new(core: generic::Values) -> Self {
43        Self::with_dist(core, Distribution::Single)
44    }
45
46    pub fn with_dist(core: generic::Values, dist: Distribution) -> Self {
47        let base = PlanBase::new_batch_with_core(&core, dist, Order::any());
48        BatchValues { base, core }
49    }
50
51    fn row_to_protobuf(&self, row: &[ExprImpl]) -> ExprTuple {
52        let cells = row.iter().map(|x| x.to_expr_proto()).collect();
53        ExprTuple { cells }
54    }
55}
56
57impl Distill for BatchValues {
58    fn distill<'a>(&self) -> XmlNode<'a> {
59        let data = self.core.rows_pretty();
60        childless_record("BatchValues", vec![("rows", data)])
61    }
62}
63
64impl ToDistributedBatch for BatchValues {
65    fn to_distributed(&self) -> Result<PlanRef> {
66        Ok(Self::with_dist(self.core.clone(), Distribution::Single).into())
67    }
68}
69
70impl ToBatchPb for BatchValues {
71    fn to_batch_prost_body(&self) -> NodeBody {
72        NodeBody::Values(ValuesNode {
73            tuples: self
74                .core
75                .rows()
76                .iter()
77                .map(|row| self.row_to_protobuf(row))
78                .collect(),
79            fields: self
80                .core
81                .schema
82                .fields()
83                .iter()
84                .map(|f| f.to_prost())
85                .collect(),
86        })
87    }
88}
89
90impl ToLocalBatch for BatchValues {
91    fn to_local(&self) -> Result<PlanRef> {
92        Ok(Self::with_dist(self.core.clone(), Distribution::Single).into())
93    }
94}
95
96impl ExprRewritable<Batch> for BatchValues {
97    fn has_rewritable_expr(&self) -> bool {
98        true
99    }
100
101    fn rewrite_exprs(&self, r: &mut dyn ExprRewriter) -> PlanRef {
102        let mut core = self.core.clone();
103        core.rewrite_exprs(r);
104        Self::new(core).into()
105    }
106}
107
108impl ExprVisitable for BatchValues {
109    fn visit_exprs(&self, v: &mut dyn ExprVisitor) {
110        self.core.visit_exprs(v);
111    }
112}