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