risingwave_frontend/optimizer/plan_visitor/
relation_collector_visitor.rs1use std::collections::HashSet;
16
17use risingwave_common::id::ObjectId;
18
19use super::{
20 BatchPlanVisitor, DefaultBehavior, DefaultValue, LogicalPlanVisitor, StreamPlanVisitor,
21};
22use crate::PlanRef;
23use crate::optimizer::plan_node::{
24 BatchSource, ConventionMarker, LogicalScan, StreamSource, StreamTableScan,
25};
26use crate::optimizer::plan_visitor::PlanVisitor;
27
28#[derive(Debug, Clone, Default)]
30pub struct RelationCollectorVisitor {
31 relations: HashSet<ObjectId>,
32}
33
34impl RelationCollectorVisitor {
35 fn new_with(relations: HashSet<ObjectId>) -> Self {
36 Self { relations }
37 }
38
39 pub fn collect_with<C: ConventionMarker>(
44 relations: HashSet<ObjectId>,
45 plan: PlanRef<C>,
46 ) -> HashSet<ObjectId>
47 where
48 Self: PlanVisitor<C>,
49 {
50 let mut visitor = Self::new_with(relations);
51 visitor.visit(plan);
52 visitor.relations
53 }
54}
55
56impl LogicalPlanVisitor for RelationCollectorVisitor {
57 type Result = ();
58
59 type DefaultBehavior = impl DefaultBehavior<Self::Result>;
60
61 fn default_behavior() -> Self::DefaultBehavior {
62 DefaultValue
63 }
64
65 fn visit_logical_scan(&mut self, plan: &LogicalScan) {
66 self.relations.insert(plan.table().id.as_object_id());
67 }
68}
69
70impl StreamPlanVisitor for RelationCollectorVisitor {
71 type Result = ();
72
73 type DefaultBehavior = impl DefaultBehavior<Self::Result>;
74
75 fn default_behavior() -> Self::DefaultBehavior {
76 DefaultValue
77 }
78
79 fn visit_stream_table_scan(&mut self, plan: &StreamTableScan) {
80 let logical = plan.core();
81 self.relations
82 .insert(logical.table_catalog.id.as_object_id());
83 }
84
85 fn visit_stream_source(&mut self, plan: &StreamSource) {
86 if let Some(catalog) = plan.source_catalog() {
87 self.relations.insert(catalog.id.as_object_id());
88 }
89 }
90}
91
92impl BatchPlanVisitor for RelationCollectorVisitor {
93 type Result = ();
94
95 type DefaultBehavior = impl DefaultBehavior<Self::Result>;
96
97 fn default_behavior() -> Self::DefaultBehavior {
98 DefaultValue
99 }
100
101 fn visit_batch_seq_scan(&mut self, plan: &crate::optimizer::plan_node::BatchSeqScan) {
102 self.relations
103 .insert(plan.core().table_catalog.id.as_object_id());
104 }
105
106 fn visit_batch_source(&mut self, plan: &BatchSource) {
107 if let Some(catalog) = plan.source_catalog() {
108 self.relations.insert(catalog.id.as_object_id());
109 }
110 }
111}