Skip to main content

risingwave_frontend/binder/relation/
share.rs

1// Copyright 2023 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 itertools::Itertools;
16use risingwave_common::catalog::Field;
17
18use crate::binder::statement::RewriteExprsRecursive;
19use crate::binder::{BoundQuery, Relation, ShareId};
20use crate::error::{ErrorCode, Result};
21use crate::optimizer::plan_node::generic::{_CHANGELOG_ROW_ID, CHANGELOG_OP};
22
23/// Share a relation during binding and planning.
24/// It could be used to share a (recursive) CTE, a source, a view and so on.
25
26#[derive(Debug, Clone)]
27pub enum BoundShareInput {
28    Query(BoundQuery),
29    ChangeLog {
30        relation: Relation,
31        /// Key column indices in the source schema
32        key_indices: Option<Vec<usize>>,
33    },
34}
35impl BoundShareInput {
36    pub fn fields(&self) -> Result<Vec<(bool, Field)>> {
37        match self {
38            BoundShareInput::Query(q) => Ok(q
39                .schema()
40                .fields()
41                .iter()
42                .cloned()
43                .map(|f| (false, f))
44                .collect_vec()),
45            BoundShareInput::ChangeLog { relation: r, .. } => {
46                let (fields, _name) = if let Relation::BaseTable(bound_base_table) = r {
47                    (
48                        bound_base_table.table_catalog.columns().to_vec(),
49                        bound_base_table.table_catalog.name().to_owned(),
50                    )
51                } else if let Relation::Source(bound_source) = r {
52                    (
53                        bound_source.catalog.columns.clone(),
54                        bound_source.catalog.name.clone(),
55                    )
56                } else {
57                    return Err(ErrorCode::BindError(
58                        "Change log CTE must be a table or source".to_owned(),
59                    )
60                    .into());
61                };
62                let fields = fields
63                    .into_iter()
64                    .map(|x| {
65                        (
66                            x.is_hidden,
67                            Field::with_name(x.data_type().clone(), x.name()),
68                        )
69                    })
70                    .chain(vec![
71                        (
72                            false,
73                            Field::with_name(
74                                risingwave_common::types::DataType::Int16,
75                                CHANGELOG_OP.to_owned(),
76                            ),
77                        ),
78                        (
79                            true,
80                            Field::with_name(
81                                risingwave_common::types::DataType::Serial,
82                                _CHANGELOG_ROW_ID.to_owned(),
83                            ),
84                        ),
85                    ])
86                    .collect();
87                Ok(fields)
88            }
89        }
90    }
91}
92#[derive(Debug, Clone)]
93pub struct BoundShare {
94    pub(crate) share_id: ShareId,
95    pub(crate) input: BoundShareInput,
96}
97
98impl RewriteExprsRecursive for BoundShare {
99    fn rewrite_exprs_recursive(&mut self, rewriter: &mut impl crate::expr::ExprRewriter) {
100        match &mut self.input {
101            BoundShareInput::Query(q) => q.rewrite_exprs_recursive(rewriter),
102            BoundShareInput::ChangeLog { relation: r, .. } => r.rewrite_exprs_recursive(rewriter),
103        };
104    }
105}