risingwave_frontend/optimizer/plan_node/generic/
cte_ref.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 std::hash::Hash;
16
17use itertools::Itertools;
18use pretty_xmlish::{Pretty, StrAssocArr};
19use risingwave_common::catalog::Schema;
20
21use super::{GenericPlanNode, GenericPlanRef, impl_distill_unit_from_fields};
22use crate::binder::ShareId;
23use crate::optimizer::property::FunctionalDependencySet;
24use crate::{OptimizerContextRef, optimizer};
25
26#[derive(Clone, Debug)]
27pub struct CteRef<PlanRef> {
28    share_id: ShareId,
29    base: PlanRef,
30}
31
32impl<PlanRef> PartialEq for CteRef<PlanRef> {
33    fn eq(&self, other: &Self) -> bool {
34        self.share_id == other.share_id
35    }
36}
37
38impl<PlanRef> Eq for CteRef<PlanRef> {}
39
40impl<PlanRef> Hash for CteRef<PlanRef> {
41    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
42        self.share_id.hash(state);
43    }
44}
45
46impl<PlanRef> CteRef<PlanRef> {
47    pub fn new(share_id: ShareId, base: PlanRef) -> Self {
48        Self { share_id, base }
49    }
50}
51
52impl<PlanRef: GenericPlanRef> CteRef<PlanRef> {
53    pub fn get_cte_ref(&self) -> Option<optimizer::plan_node::PlanRef> {
54        self.ctx().get_rcte_cache_plan(&self.share_id)
55    }
56}
57
58impl<PlanRef: GenericPlanRef> GenericPlanNode for CteRef<PlanRef> {
59    fn schema(&self) -> Schema {
60        if let Some(plan_ref) = self.get_cte_ref() {
61            plan_ref.schema().clone()
62        } else {
63            self.base.schema().clone()
64        }
65    }
66
67    fn stream_key(&self) -> Option<Vec<usize>> {
68        if let Some(plan_ref) = self.get_cte_ref() {
69            plan_ref
70                .stream_key()
71                .map(|s| s.iter().map(|i| i.to_owned()).collect_vec())
72        } else {
73            self.base
74                .stream_key()
75                .map(|s| s.iter().map(|i| i.to_owned()).collect_vec())
76        }
77    }
78
79    fn ctx(&self) -> OptimizerContextRef {
80        // it does not matter where the context is coming from,
81        // since we are only getting a reference.
82        self.base.ctx()
83    }
84
85    fn functional_dependency(&self) -> FunctionalDependencySet {
86        if let Some(plan_ref) = self.get_cte_ref() {
87            plan_ref.functional_dependency().clone()
88        } else {
89            self.base.functional_dependency().clone()
90        }
91    }
92}
93
94impl<PlanRef: GenericPlanRef> CteRef<PlanRef> {
95    pub fn fields_pretty<'a>(&self) -> StrAssocArr<'a> {
96        vec![("share_id", Pretty::debug(&self.share_id))]
97    }
98}
99
100impl_distill_unit_from_fields! {CteRef, GenericPlanRef}