risingwave_frontend/optimizer/plan_node/generic/
cte_ref.rs
1use 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 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}