1use std::collections::BTreeMap;
18use std::str::FromStr;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
26pub struct PhysicalOperatorId(usize);
27
28impl PhysicalOperatorId {
29 pub const fn from_ordinal(ordinal: usize) -> Self {
31 Self(ordinal)
32 }
33
34 pub const fn ordinal(self) -> usize {
36 self.0
37 }
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
41pub enum PhysicalPlanKind {
42 CreateNodeLabel,
43 CreateRelationshipType,
44 CreateNodeTable,
45 CreateRelationshipTable,
46 CreateProperty,
47 AlterTableState,
48 AlterPropertyState,
49 CreateIndex,
50 CreateCompositeIndex,
51 CreateRangeIndex,
52 CreateFullTextIndex,
53 CreateUniqueConstraint,
54 CreateNodePropertyExistsConstraint,
55 CreateRelationshipUniqueConstraint,
56 CreateRelationshipPropertyExistsConstraint,
57 ProjectGraph,
58 GraphAlgorithm,
59 VectorSeedScan,
60 TextSeedScan,
61 GraphSeedScan,
62 VectorIndexScan,
63 CreateNode,
64 UnwindExec,
65 UnwindRowsExec,
66 UnwindMutationExec,
67 LoadCsvMutationExec,
68 MergeNode,
69 MergeRelationship,
70 MergeMatchedRelationship,
71 MergeRelationshipFromMatchedRelationship,
72 MergeRelationshipToMatchedTarget,
73 MergeRelationshipFromMatchedTarget,
74 CreateMatchedRelationship,
75 SetNodeProperty,
76 SetNodeProperties,
77 SetNodePropertiesReturn,
78 SetRelationshipProperty,
79 SetRelationshipProperties,
80 DeleteNode,
81 DeleteRelationship,
82 DeleteRelationshipTargetNodes,
83 CreateRelationship,
84 GraphMatchExec,
85 EmptyExec,
86 LoadCsvExec,
87 SeqNodeScan,
88 RelationshipSeededNodeScan,
89 NodeProjectionScanExec,
90 SourceSegmentScan,
91 NodeCartesianProductExec,
92 HashJoinExec,
93 NodeColumnLookupExec,
94 IndexNodeSeek,
95 IndexNodeMultiSeek,
96 IndexNodeUnionSeek,
97 IndexNodeCompositeSeek,
98 IndexNodeCompositeRangeSeek,
99 IndexNodeRangeSeek,
100 IndexNodeTextSeek,
101 AdjacencyExpandExec,
102 AdjacencyExistsExec,
103 OptionalDegreeExec,
104 OptionalRelationshipCountSumExec,
105 NodeCountExec,
106 RelationshipCountExec,
107 ThreadRepairStatsExec,
108 ShortestPathExec,
109 FilterExec,
110 ProjectExec,
111 AggregateExec,
112 DistinctExec,
113 SortExec,
114 TopNExec,
115 LimitExec,
116 ScoringRerankExec,
117 ScoringProgramExec,
118 HostScoringExec,
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
122pub enum PhysicalPlanClass {
123 Schema,
124 Mutation,
125 Access,
126 Traversal,
127 Relational,
128 Procedure,
129}
130
131#[derive(Debug, Clone, Copy, PartialEq)]
132pub enum PlanChildren<'a, P> {
133 None,
134 Unary(&'a P),
135 Binary(&'a P, &'a P),
136}
137
138pub trait PhysicalPlanNode {
139 fn kind(&self) -> PhysicalPlanKind;
140
141 fn children(&self) -> PlanChildren<'_, Self>
142 where
143 Self: Sized;
144
145 fn class(&self) -> PhysicalPlanClass {
146 self.kind().class()
147 }
148}
149
150impl PhysicalPlanKind {
151 pub fn all() -> &'static [Self] {
152 const ALL: &[PhysicalPlanKind] = &[
153 PhysicalPlanKind::CreateNodeLabel,
154 PhysicalPlanKind::CreateRelationshipType,
155 PhysicalPlanKind::CreateNodeTable,
156 PhysicalPlanKind::CreateRelationshipTable,
157 PhysicalPlanKind::CreateProperty,
158 PhysicalPlanKind::AlterTableState,
159 PhysicalPlanKind::AlterPropertyState,
160 PhysicalPlanKind::CreateIndex,
161 PhysicalPlanKind::CreateCompositeIndex,
162 PhysicalPlanKind::CreateRangeIndex,
163 PhysicalPlanKind::CreateFullTextIndex,
164 PhysicalPlanKind::CreateUniqueConstraint,
165 PhysicalPlanKind::CreateNodePropertyExistsConstraint,
166 PhysicalPlanKind::CreateRelationshipUniqueConstraint,
167 PhysicalPlanKind::CreateRelationshipPropertyExistsConstraint,
168 PhysicalPlanKind::ProjectGraph,
169 PhysicalPlanKind::GraphAlgorithm,
170 PhysicalPlanKind::VectorSeedScan,
171 PhysicalPlanKind::TextSeedScan,
172 PhysicalPlanKind::GraphSeedScan,
173 PhysicalPlanKind::VectorIndexScan,
174 PhysicalPlanKind::CreateNode,
175 PhysicalPlanKind::UnwindExec,
176 PhysicalPlanKind::UnwindRowsExec,
177 PhysicalPlanKind::UnwindMutationExec,
178 PhysicalPlanKind::LoadCsvMutationExec,
179 PhysicalPlanKind::MergeNode,
180 PhysicalPlanKind::MergeRelationship,
181 PhysicalPlanKind::MergeMatchedRelationship,
182 PhysicalPlanKind::MergeRelationshipFromMatchedRelationship,
183 PhysicalPlanKind::MergeRelationshipToMatchedTarget,
184 PhysicalPlanKind::MergeRelationshipFromMatchedTarget,
185 PhysicalPlanKind::CreateMatchedRelationship,
186 PhysicalPlanKind::SetNodeProperty,
187 PhysicalPlanKind::SetNodeProperties,
188 PhysicalPlanKind::SetNodePropertiesReturn,
189 PhysicalPlanKind::SetRelationshipProperty,
190 PhysicalPlanKind::SetRelationshipProperties,
191 PhysicalPlanKind::DeleteNode,
192 PhysicalPlanKind::DeleteRelationship,
193 PhysicalPlanKind::DeleteRelationshipTargetNodes,
194 PhysicalPlanKind::CreateRelationship,
195 PhysicalPlanKind::GraphMatchExec,
196 PhysicalPlanKind::EmptyExec,
197 PhysicalPlanKind::LoadCsvExec,
198 PhysicalPlanKind::SeqNodeScan,
199 PhysicalPlanKind::RelationshipSeededNodeScan,
200 PhysicalPlanKind::NodeProjectionScanExec,
201 PhysicalPlanKind::SourceSegmentScan,
202 PhysicalPlanKind::NodeCartesianProductExec,
203 PhysicalPlanKind::HashJoinExec,
204 PhysicalPlanKind::NodeColumnLookupExec,
205 PhysicalPlanKind::IndexNodeSeek,
206 PhysicalPlanKind::IndexNodeMultiSeek,
207 PhysicalPlanKind::IndexNodeUnionSeek,
208 PhysicalPlanKind::IndexNodeCompositeSeek,
209 PhysicalPlanKind::IndexNodeCompositeRangeSeek,
210 PhysicalPlanKind::IndexNodeRangeSeek,
211 PhysicalPlanKind::IndexNodeTextSeek,
212 PhysicalPlanKind::AdjacencyExpandExec,
213 PhysicalPlanKind::AdjacencyExistsExec,
214 PhysicalPlanKind::OptionalDegreeExec,
215 PhysicalPlanKind::OptionalRelationshipCountSumExec,
216 PhysicalPlanKind::NodeCountExec,
217 PhysicalPlanKind::RelationshipCountExec,
218 PhysicalPlanKind::ThreadRepairStatsExec,
219 PhysicalPlanKind::ShortestPathExec,
220 PhysicalPlanKind::FilterExec,
221 PhysicalPlanKind::ProjectExec,
222 PhysicalPlanKind::AggregateExec,
223 PhysicalPlanKind::DistinctExec,
224 PhysicalPlanKind::SortExec,
225 PhysicalPlanKind::TopNExec,
226 PhysicalPlanKind::LimitExec,
227 PhysicalPlanKind::ScoringRerankExec,
228 PhysicalPlanKind::ScoringProgramExec,
229 PhysicalPlanKind::HostScoringExec,
230 ];
231 ALL
232 }
233
234 pub fn as_str(self) -> &'static str {
235 match self {
236 PhysicalPlanKind::CreateNodeLabel => "CreateNodeLabel",
237 PhysicalPlanKind::CreateRelationshipType => "CreateRelationshipType",
238 PhysicalPlanKind::CreateNodeTable => "CreateNodeTable",
239 PhysicalPlanKind::CreateRelationshipTable => "CreateRelationshipTable",
240 PhysicalPlanKind::CreateProperty => "CreateProperty",
241 PhysicalPlanKind::AlterTableState => "AlterTableState",
242 PhysicalPlanKind::AlterPropertyState => "AlterPropertyState",
243 PhysicalPlanKind::CreateIndex => "CreateIndex",
244 PhysicalPlanKind::CreateCompositeIndex => "CreateCompositeIndex",
245 PhysicalPlanKind::CreateRangeIndex => "CreateRangeIndex",
246 PhysicalPlanKind::CreateFullTextIndex => "CreateFullTextIndex",
247 PhysicalPlanKind::CreateUniqueConstraint => "CreateUniqueConstraint",
248 PhysicalPlanKind::CreateNodePropertyExistsConstraint => {
249 "CreateNodePropertyExistsConstraint"
250 }
251 PhysicalPlanKind::CreateRelationshipUniqueConstraint => {
252 "CreateRelationshipUniqueConstraint"
253 }
254 PhysicalPlanKind::CreateRelationshipPropertyExistsConstraint => {
255 "CreateRelationshipPropertyExistsConstraint"
256 }
257 PhysicalPlanKind::ProjectGraph => "ProjectGraph",
258 PhysicalPlanKind::GraphAlgorithm => "GraphAlgorithm",
259 PhysicalPlanKind::VectorSeedScan => "VectorSeedScan",
260 PhysicalPlanKind::TextSeedScan => "TextSeedScan",
261 PhysicalPlanKind::GraphSeedScan => "GraphSeedScan",
262 PhysicalPlanKind::VectorIndexScan => "VectorIndexScan",
263 PhysicalPlanKind::CreateNode => "CreateNode",
264 PhysicalPlanKind::UnwindExec => "UnwindExec",
265 PhysicalPlanKind::UnwindRowsExec => "UnwindRowsExec",
266 PhysicalPlanKind::UnwindMutationExec => "UnwindMutationExec",
267 PhysicalPlanKind::LoadCsvMutationExec => "LoadCsvMutationExec",
268 PhysicalPlanKind::MergeNode => "MergeNode",
269 PhysicalPlanKind::MergeRelationship => "MergeRelationship",
270 PhysicalPlanKind::MergeMatchedRelationship => "MergeMatchedRelationship",
271 PhysicalPlanKind::MergeRelationshipFromMatchedRelationship => {
272 "MergeRelationshipFromMatchedRelationship"
273 }
274 PhysicalPlanKind::MergeRelationshipToMatchedTarget => {
275 "MergeRelationshipToMatchedTarget"
276 }
277 PhysicalPlanKind::MergeRelationshipFromMatchedTarget => {
278 "MergeRelationshipFromMatchedTarget"
279 }
280 PhysicalPlanKind::CreateMatchedRelationship => "CreateMatchedRelationship",
281 PhysicalPlanKind::SetNodeProperty => "SetNodeProperty",
282 PhysicalPlanKind::SetNodeProperties => "SetNodeProperties",
283 PhysicalPlanKind::SetNodePropertiesReturn => "SetNodePropertiesReturn",
284 PhysicalPlanKind::SetRelationshipProperty => "SetRelationshipProperty",
285 PhysicalPlanKind::SetRelationshipProperties => "SetRelationshipProperties",
286 PhysicalPlanKind::DeleteNode => "DeleteNode",
287 PhysicalPlanKind::DeleteRelationship => "DeleteRelationship",
288 PhysicalPlanKind::DeleteRelationshipTargetNodes => "DeleteRelationshipTargetNodes",
289 PhysicalPlanKind::CreateRelationship => "CreateRelationship",
290 PhysicalPlanKind::GraphMatchExec => "GraphMatchExec",
291 PhysicalPlanKind::EmptyExec => "EmptyExec",
292 PhysicalPlanKind::LoadCsvExec => "LoadCsvExec",
293 PhysicalPlanKind::SeqNodeScan => "SeqNodeScan",
294 PhysicalPlanKind::RelationshipSeededNodeScan => "RelationshipSeededNodeScan",
295 PhysicalPlanKind::NodeProjectionScanExec => "NodeProjectionScanExec",
296 PhysicalPlanKind::SourceSegmentScan => "SourceSegmentScan",
297 PhysicalPlanKind::HashJoinExec => "HashJoinExec",
298 PhysicalPlanKind::NodeCartesianProductExec => "NodeCartesianProductExec",
299 PhysicalPlanKind::NodeColumnLookupExec => "NodeColumnLookupExec",
300 PhysicalPlanKind::IndexNodeSeek => "IndexNodeSeek",
301 PhysicalPlanKind::IndexNodeMultiSeek => "IndexNodeMultiSeek",
302 PhysicalPlanKind::IndexNodeUnionSeek => "IndexNodeUnionSeek",
303 PhysicalPlanKind::IndexNodeCompositeSeek => "IndexNodeCompositeSeek",
304 PhysicalPlanKind::IndexNodeCompositeRangeSeek => "IndexNodeCompositeRangeSeek",
305 PhysicalPlanKind::IndexNodeRangeSeek => "IndexNodeRangeSeek",
306 PhysicalPlanKind::IndexNodeTextSeek => "IndexNodeTextSeek",
307 PhysicalPlanKind::AdjacencyExpandExec => "AdjacencyExpandExec",
308 PhysicalPlanKind::AdjacencyExistsExec => "AdjacencyExistsExec",
309 PhysicalPlanKind::OptionalDegreeExec => "OptionalDegreeExec",
310 PhysicalPlanKind::OptionalRelationshipCountSumExec => {
311 "OptionalRelationshipCountSumExec"
312 }
313 PhysicalPlanKind::NodeCountExec => "NodeCountExec",
314 PhysicalPlanKind::RelationshipCountExec => "RelationshipCountExec",
315 PhysicalPlanKind::ThreadRepairStatsExec => "ThreadRepairStatsExec",
316 PhysicalPlanKind::ShortestPathExec => "ShortestPathExec",
317 PhysicalPlanKind::FilterExec => "FilterExec",
318 PhysicalPlanKind::ProjectExec => "ProjectExec",
319 PhysicalPlanKind::AggregateExec => "AggregateExec",
320 PhysicalPlanKind::DistinctExec => "DistinctExec",
321 PhysicalPlanKind::SortExec => "SortExec",
322 PhysicalPlanKind::TopNExec => "TopNExec",
323 PhysicalPlanKind::LimitExec => "LimitExec",
324 PhysicalPlanKind::ScoringRerankExec => "ScoringRerankExec",
325 PhysicalPlanKind::ScoringProgramExec => "ScoringProgramExec",
326 PhysicalPlanKind::HostScoringExec => "HostScoringExec",
327 }
328 }
329
330 pub fn class(self) -> PhysicalPlanClass {
331 match self {
332 PhysicalPlanKind::CreateNodeLabel
333 | PhysicalPlanKind::CreateRelationshipType
334 | PhysicalPlanKind::CreateNodeTable
335 | PhysicalPlanKind::CreateRelationshipTable
336 | PhysicalPlanKind::CreateProperty
337 | PhysicalPlanKind::AlterTableState
338 | PhysicalPlanKind::AlterPropertyState
339 | PhysicalPlanKind::CreateIndex
340 | PhysicalPlanKind::CreateCompositeIndex
341 | PhysicalPlanKind::CreateRangeIndex
342 | PhysicalPlanKind::CreateFullTextIndex
343 | PhysicalPlanKind::CreateUniqueConstraint
344 | PhysicalPlanKind::CreateNodePropertyExistsConstraint
345 | PhysicalPlanKind::CreateRelationshipUniqueConstraint
346 | PhysicalPlanKind::CreateRelationshipPropertyExistsConstraint => {
347 PhysicalPlanClass::Schema
348 }
349 PhysicalPlanKind::CreateNode
350 | PhysicalPlanKind::UnwindMutationExec
351 | PhysicalPlanKind::LoadCsvMutationExec
352 | PhysicalPlanKind::MergeNode
353 | PhysicalPlanKind::MergeRelationship
354 | PhysicalPlanKind::MergeMatchedRelationship
355 | PhysicalPlanKind::MergeRelationshipFromMatchedRelationship
356 | PhysicalPlanKind::MergeRelationshipToMatchedTarget
357 | PhysicalPlanKind::MergeRelationshipFromMatchedTarget
358 | PhysicalPlanKind::CreateMatchedRelationship
359 | PhysicalPlanKind::SetNodeProperty
360 | PhysicalPlanKind::SetNodeProperties
361 | PhysicalPlanKind::SetNodePropertiesReturn
362 | PhysicalPlanKind::SetRelationshipProperty
363 | PhysicalPlanKind::SetRelationshipProperties
364 | PhysicalPlanKind::DeleteNode
365 | PhysicalPlanKind::DeleteRelationship
366 | PhysicalPlanKind::DeleteRelationshipTargetNodes
367 | PhysicalPlanKind::CreateRelationship => PhysicalPlanClass::Mutation,
368 PhysicalPlanKind::SeqNodeScan
369 | PhysicalPlanKind::RelationshipSeededNodeScan
370 | PhysicalPlanKind::NodeProjectionScanExec
371 | PhysicalPlanKind::SourceSegmentScan
372 | PhysicalPlanKind::NodeColumnLookupExec
373 | PhysicalPlanKind::IndexNodeSeek
374 | PhysicalPlanKind::IndexNodeMultiSeek
375 | PhysicalPlanKind::IndexNodeUnionSeek
376 | PhysicalPlanKind::IndexNodeCompositeSeek
377 | PhysicalPlanKind::IndexNodeCompositeRangeSeek
378 | PhysicalPlanKind::IndexNodeRangeSeek
379 | PhysicalPlanKind::IndexNodeTextSeek => PhysicalPlanClass::Access,
380 PhysicalPlanKind::GraphMatchExec
381 | PhysicalPlanKind::AdjacencyExpandExec
382 | PhysicalPlanKind::AdjacencyExistsExec
383 | PhysicalPlanKind::OptionalDegreeExec
384 | PhysicalPlanKind::OptionalRelationshipCountSumExec
385 | PhysicalPlanKind::ShortestPathExec => PhysicalPlanClass::Traversal,
386 PhysicalPlanKind::NodeCountExec
387 | PhysicalPlanKind::RelationshipCountExec
388 | PhysicalPlanKind::UnwindExec
389 | PhysicalPlanKind::UnwindRowsExec
390 | PhysicalPlanKind::EmptyExec
391 | PhysicalPlanKind::NodeCartesianProductExec
392 | PhysicalPlanKind::HashJoinExec
393 | PhysicalPlanKind::FilterExec
394 | PhysicalPlanKind::ProjectExec
395 | PhysicalPlanKind::AggregateExec
396 | PhysicalPlanKind::DistinctExec
397 | PhysicalPlanKind::SortExec
398 | PhysicalPlanKind::TopNExec
399 | PhysicalPlanKind::LimitExec
400 | PhysicalPlanKind::ScoringRerankExec
401 | PhysicalPlanKind::ScoringProgramExec
402 | PhysicalPlanKind::HostScoringExec => PhysicalPlanClass::Relational,
403 PhysicalPlanKind::ProjectGraph
404 | PhysicalPlanKind::GraphAlgorithm
405 | PhysicalPlanKind::VectorSeedScan
406 | PhysicalPlanKind::TextSeedScan
407 | PhysicalPlanKind::GraphSeedScan
408 | PhysicalPlanKind::VectorIndexScan
409 | PhysicalPlanKind::ThreadRepairStatsExec
410 | PhysicalPlanKind::LoadCsvExec => PhysicalPlanClass::Procedure,
411 }
412 }
413}
414
415impl PhysicalPlanClass {
416 pub fn all() -> &'static [Self] {
417 const ALL: &[PhysicalPlanClass] = &[
418 PhysicalPlanClass::Schema,
419 PhysicalPlanClass::Mutation,
420 PhysicalPlanClass::Access,
421 PhysicalPlanClass::Traversal,
422 PhysicalPlanClass::Relational,
423 PhysicalPlanClass::Procedure,
424 ];
425 ALL
426 }
427
428 pub fn as_str(self) -> &'static str {
429 match self {
430 PhysicalPlanClass::Schema => "schema",
431 PhysicalPlanClass::Mutation => "mutation",
432 PhysicalPlanClass::Access => "access",
433 PhysicalPlanClass::Traversal => "traversal",
434 PhysicalPlanClass::Relational => "relational",
435 PhysicalPlanClass::Procedure => "procedure",
436 }
437 }
438}
439
440impl FromStr for PhysicalPlanKind {
441 type Err = &'static str;
442
443 fn from_str(value: &str) -> Result<Self, Self::Err> {
444 Self::all()
445 .iter()
446 .copied()
447 .find(|kind| kind.as_str() == value)
448 .ok_or("unknown physical plan kind")
449 }
450}
451
452impl FromStr for PhysicalPlanClass {
453 type Err = &'static str;
454
455 fn from_str(value: &str) -> Result<Self, Self::Err> {
456 Self::all()
457 .iter()
458 .copied()
459 .find(|class| class.as_str() == value)
460 .ok_or("unknown physical plan class")
461 }
462}
463
464impl<'a, P> PlanChildren<'a, P> {
465 pub fn len(self) -> usize {
466 match self {
467 PlanChildren::None => 0,
468 PlanChildren::Unary(_) => 1,
469 PlanChildren::Binary(_, _) => 2,
470 }
471 }
472
473 pub fn is_empty(self) -> bool {
474 self.len() == 0
475 }
476}
477
478pub fn plan_operator_counts<P>(plan: &P) -> BTreeMap<String, usize>
479where
480 P: PhysicalPlanNode,
481{
482 let mut counts = BTreeMap::new();
483 visit_plan(plan, &mut |node| {
484 *counts.entry(node.kind().as_str().to_string()).or_default() += 1;
485 });
486 counts
487}
488
489pub fn plan_class_counts<P>(plan: &P) -> BTreeMap<String, usize>
490where
491 P: PhysicalPlanNode,
492{
493 let mut counts = BTreeMap::new();
494 visit_plan(plan, &mut |node| {
495 *counts.entry(node.class().as_str().to_string()).or_default() += 1;
496 });
497 counts
498}
499
500pub fn visit_plan<P>(plan: &P, visitor: &mut impl FnMut(&P))
501where
502 P: PhysicalPlanNode,
503{
504 visitor(plan);
505 match plan.children() {
506 PlanChildren::None => {}
507 PlanChildren::Unary(input) => visit_plan(input, visitor),
508 PlanChildren::Binary(left, right) => {
509 visit_plan(left, visitor);
510 visit_plan(right, visitor);
511 }
512 }
513}
514
515pub fn visit_plan_with_ids<P>(plan: &P, visitor: &mut impl FnMut(PhysicalOperatorId, &P))
517where
518 P: PhysicalPlanNode,
519{
520 fn visit<P>(
521 plan: &P,
522 next_ordinal: &mut usize,
523 visitor: &mut impl FnMut(PhysicalOperatorId, &P),
524 ) where
525 P: PhysicalPlanNode,
526 {
527 let operator_id = PhysicalOperatorId::from_ordinal(*next_ordinal);
528 *next_ordinal = next_ordinal
529 .checked_add(1)
530 .expect("physical plan operator count exceeds usize");
531 visitor(operator_id, plan);
532 match plan.children() {
533 PlanChildren::None => {}
534 PlanChildren::Unary(input) => visit(input, next_ordinal, visitor),
535 PlanChildren::Binary(left, right) => {
536 visit(left, next_ordinal, visitor);
537 visit(right, next_ordinal, visitor);
538 }
539 }
540 }
541
542 let mut next_ordinal = 0;
543 visit(plan, &mut next_ordinal, visitor);
544}
545
546#[cfg(test)]
547mod tests {
548 use super::{
549 plan_class_counts, plan_operator_counts, visit_plan, visit_plan_with_ids,
550 PhysicalPlanClass, PhysicalPlanKind, PhysicalPlanNode, PlanChildren,
551 };
552
553 #[test]
554 fn physical_plan_kind_exposes_stable_strings_and_classes() {
555 assert_eq!(PhysicalPlanKind::IndexNodeSeek.as_str(), "IndexNodeSeek");
556 assert_eq!(
557 PhysicalPlanKind::IndexNodeSeek.class(),
558 PhysicalPlanClass::Access
559 );
560 assert_eq!(
561 PhysicalPlanKind::AdjacencyExpandExec.class(),
562 PhysicalPlanClass::Traversal
563 );
564 assert_eq!(
565 PhysicalPlanKind::ProjectExec.class(),
566 PhysicalPlanClass::Relational
567 );
568 }
569
570 #[test]
571 fn physical_plan_kind_strings_round_trip_for_diagnostics() {
572 for kind in PhysicalPlanKind::all() {
573 assert_eq!(kind.as_str().parse::<PhysicalPlanKind>(), Ok(*kind));
574 }
575 assert!("UnknownExec".parse::<PhysicalPlanKind>().is_err());
576 }
577
578 #[test]
579 fn physical_plan_class_exposes_stable_strings() {
580 assert_eq!(PhysicalPlanClass::Schema.as_str(), "schema");
581 assert_eq!(PhysicalPlanClass::Mutation.as_str(), "mutation");
582 assert_eq!(PhysicalPlanClass::Access.as_str(), "access");
583 assert_eq!(PhysicalPlanClass::Traversal.as_str(), "traversal");
584 assert_eq!(PhysicalPlanClass::Relational.as_str(), "relational");
585 assert_eq!(PhysicalPlanClass::Procedure.as_str(), "procedure");
586 }
587
588 #[test]
589 fn physical_plan_class_strings_round_trip_for_diagnostics() {
590 for class in PhysicalPlanClass::all() {
591 assert_eq!(class.as_str().parse::<PhysicalPlanClass>(), Ok(*class));
592 }
593 assert!("unknown".parse::<PhysicalPlanClass>().is_err());
594 }
595
596 #[test]
597 fn plan_children_reports_arity_without_knowing_plan_type() {
598 assert_eq!(PlanChildren::<&str>::None.len(), 0);
599 assert!(PlanChildren::<&str>::None.is_empty());
600
601 let child = "scan";
602 assert_eq!(PlanChildren::Unary(&child).len(), 1);
603
604 let left = "left";
605 let right = "right";
606 assert_eq!(PlanChildren::Binary(&left, &right).len(), 2);
607 }
608
609 #[derive(Debug)]
610 enum TestPlan {
611 Seek,
612 Project(Box<TestPlan>),
613 Join(Box<TestPlan>, Box<TestPlan>),
614 }
615
616 impl PhysicalPlanNode for TestPlan {
617 fn kind(&self) -> PhysicalPlanKind {
618 match self {
619 TestPlan::Seek => PhysicalPlanKind::IndexNodeSeek,
620 TestPlan::Project(_) => PhysicalPlanKind::ProjectExec,
621 TestPlan::Join(_, _) => PhysicalPlanKind::NodeCartesianProductExec,
622 }
623 }
624
625 fn children(&self) -> PlanChildren<'_, Self> {
626 match self {
627 TestPlan::Seek => PlanChildren::None,
628 TestPlan::Project(input) => PlanChildren::Unary(input),
629 TestPlan::Join(left, right) => PlanChildren::Binary(left, right),
630 }
631 }
632 }
633
634 #[test]
635 fn plan_node_helpers_traverse_without_knowing_plan_payloads() {
636 let plan = TestPlan::Project(Box::new(TestPlan::Join(
637 Box::new(TestPlan::Seek),
638 Box::new(TestPlan::Seek),
639 )));
640
641 let mut visit_order = Vec::new();
642 visit_plan(&plan, &mut |node| {
643 visit_order.push(node.kind().as_str());
644 });
645
646 assert_eq!(
647 visit_order,
648 vec![
649 "ProjectExec",
650 "NodeCartesianProductExec",
651 "IndexNodeSeek",
652 "IndexNodeSeek"
653 ]
654 );
655 assert_eq!(plan_operator_counts(&plan)["IndexNodeSeek"], 2);
656 assert_eq!(plan_operator_counts(&plan)["ProjectExec"], 1);
657 assert_eq!(plan_class_counts(&plan)["access"], 2);
658 assert_eq!(plan_class_counts(&plan)["relational"], 2);
659
660 let mut identified = Vec::new();
661 visit_plan_with_ids(&plan, &mut |operator_id, node| {
662 identified.push((operator_id.ordinal(), node.kind().as_str()));
663 });
664 assert_eq!(
665 identified,
666 vec![
667 (0, "ProjectExec"),
668 (1, "NodeCartesianProductExec"),
669 (2, "IndexNodeSeek"),
670 (3, "IndexNodeSeek"),
671 ]
672 );
673 }
674}