akar-planner 0.2.0

Query planner for the Akar embedded graph database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
//! Logical operator types for query planning.

use akar_binder::bound_statement::BoundExpression;
use akar_catalog::CatalogColumn;
use akar_parser::ast::Expression;

/// A logical operator in the query plan.
/// A logical expressions scan operator that reads correlated variables
/// from an outer accumulate (for correlated subquery execution).
///
/// Ported from C++ `LogicalExpressionsScan`.
#[derive(Debug, Clone)]
pub struct LogicalExpressionsScan {
    /// Names of the expressions/variables to scan from the outer context.
    pub expressions: Vec<String>,
    /// Index of the outer accumulate operator in the plan (set by optimizer).
    pub outer_accumulate_idx: Option<usize>,
    /// Estimated cardinality.
    pub cardinality: u64,
}

/// A logical accumulate operator that materializes all input into memory.
///
/// Used as the build-side input for hash joins (via SIP/acc-hash-join)
/// and for correlated subquery execution. Collects all input rows into
/// an in-memory table for later probe.
///
/// Ported from C++ `LogicalAccumulate`.
#[derive(Debug, Clone)]
pub struct LogicalAccumulate {
    /// Accumulate type (Regular or Optional).
    pub accumulate_type: akar_common::enums::AccumulateType,
    /// Expressions to flatten before accumulating.
    pub flat_exprs: Vec<akar_parser::ast::Expression>,
    /// Optional mark expression (for OPTIONAL MATCH).
    pub mark: Option<akar_parser::ast::Expression>,
    /// Child operator.
    pub children: Vec<LogicalOperator>,
    /// Estimated cardinality.
    pub cardinality: u64,
}

/// Logical COUNT on a rel table — optimized via CSR metadata (Ladybug).
#[derive(Debug, Clone)]
pub struct LogicalCountRelTable {
    pub table_name: String,
    pub table_id: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalPartitioner {
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalPathPropertyProbe {
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
    pub node_ids_col_idx: usize,
    pub edge_ids_col_idx: Option<usize>,
    pub properties: Vec<(String, bool, Vec<String>)>,
}

#[derive(Debug, Clone)]
pub enum LogicalOperator {
    ScanNode(LogicalScanNode),
    ScanRel(LogicalScanRel),
    VectorSimilarityScan(LogicalVectorSimilarityScan),
    ArtIndexRangeScan(LogicalArtIndexRangeScan),
    Filter(LogicalFilter),
    Projection(LogicalProjection),
    HashJoin(LogicalHashJoin),
    CrossProduct(LogicalCrossProduct),
    OrderBy(LogicalOrderBy),
    Limit(LogicalLimit),
    TopK(LogicalTopK),
    Aggregate(LogicalAggregate),
    Union(LogicalUnion),
    Flatten(LogicalFlatten),
    TableFunctionCall(LogicalTableFunctionCall),
    StandaloneCall(LogicalStandaloneCall),
    CopyFrom(LogicalCopyFrom),
    BatchInsert(LogicalBatchInsert),
    IndexLookup(LogicalIndexLookup),
    Delete(LogicalDelete),
    Set(LogicalSet),
    OptionalMatch(LogicalOptionalMatch),
    OptionalExtend(LogicalOptionalExtend),
    Unwind(LogicalUnwind),
    Foreach(LogicalForeach),
    Merge(LogicalMerge),
    MergeRel(LogicalMergeRel),
    SemiJoin(LogicalSemiJoin),
    AntiJoin(LogicalAntiJoin),
    Intersect(LogicalIntersect),
    Explain(LogicalExplain),
    RecursiveExtend(LogicalRecursiveExtend),
    Accumulate(LogicalAccumulate),
    ExpressionsScan(LogicalExpressionsScan),
    CountRelTable(LogicalCountRelTable),
    Partitioner(LogicalPartitioner),
    PathPropertyProbe(LogicalPathPropertyProbe),
    // DDL operators
    CreateNodeTable(LogicalCreateNodeTable),
    CreateRelTable(LogicalCreateRelTable),
    DropTable(LogicalDropTable),
    AlterTable(LogicalAlterTable),
    CreateIndex(LogicalCreateIndex),
    DropIndex(LogicalDropIndex),
    CreateVectorIndex(LogicalCreateVectorIndex),
    CreateSequence(LogicalCreateSequence),
    DropSequence(LogicalDropSequence),
    CreateDml(LogicalCreateDml),
    CreateNode(LogicalCreateNode),
    CreateRel(LogicalCreateRel),
    Extend(LogicalExtend),
    ExportDatabase(LogicalExportDatabase),
    ImportDatabase(LogicalImportDatabase),
    CreateFtsIndex(LogicalCreateFtsIndex),
    FtsScan(LogicalFtsScan),
    EmptyResult(LogicalEmptyResult),
    MultiplicityReducer(LogicalMultiplicityReducer),
    Skip(LogicalSkip),
    Insert(LogicalInsert),
    ExtensionClause(LogicalExtensionClause),
}

impl LogicalOperator {
    /// Get the estimated cardinality for this operator.
    pub fn cardinality(&self) -> u64 {
        match self {
            LogicalOperator::ScanNode(s) => s.cardinality,
            LogicalOperator::ScanRel(s) => s.cardinality,
            LogicalOperator::VectorSimilarityScan(s) => s.cardinality,
            LogicalOperator::ArtIndexRangeScan(s) => s.cardinality,
            LogicalOperator::Filter(s) => s.cardinality,
            LogicalOperator::Projection(s) => s.cardinality,
            LogicalOperator::HashJoin(s) => s.cardinality,
            LogicalOperator::CrossProduct(s) => s.cardinality,
            LogicalOperator::OrderBy(s) => s.cardinality,
            LogicalOperator::TopK(s) => s.cardinality,
            LogicalOperator::Limit(s) => s.cardinality,
            LogicalOperator::Aggregate(s) => s.cardinality,
            LogicalOperator::Union(s) => s.cardinality,
            LogicalOperator::Flatten(s) => s.cardinality,
            LogicalOperator::TableFunctionCall(s) => s.cardinality,
            LogicalOperator::StandaloneCall(s) => s.cardinality,
            LogicalOperator::CopyFrom(s) => s.cardinality,
            LogicalOperator::BatchInsert(s) => s.cardinality,
            LogicalOperator::IndexLookup(s) => s.cardinality,
            LogicalOperator::Delete(s) => s.cardinality,
            LogicalOperator::Set(s) => s.cardinality,
            LogicalOperator::OptionalMatch(s) => s.cardinality,
            LogicalOperator::OptionalExtend(s) => s.cardinality,
            LogicalOperator::Unwind(s) => s.cardinality,
            LogicalOperator::Foreach(s) => s.cardinality,
            LogicalOperator::Merge(s) => s.cardinality,
            LogicalOperator::MergeRel(s) => s.cardinality,
            LogicalOperator::SemiJoin(s) => s.cardinality,
            LogicalOperator::AntiJoin(s) => s.cardinality,
            LogicalOperator::Intersect(s) => s.cardinality,
            LogicalOperator::Explain(s) => s.cardinality,
            LogicalOperator::RecursiveExtend(s) => s.cardinality,
            LogicalOperator::Accumulate(s) => s.cardinality,
            LogicalOperator::ExpressionsScan(s) => s.cardinality,
            LogicalOperator::CountRelTable(_) => 1,
            LogicalOperator::Partitioner(s) => s.cardinality,
            LogicalOperator::PathPropertyProbe(s) => s.cardinality,
            // DDL operators
            LogicalOperator::CreateNodeTable(s) => s.cardinality,
            LogicalOperator::CreateRelTable(s) => s.cardinality,
            LogicalOperator::DropTable(s) => s.cardinality,
            LogicalOperator::AlterTable(s) => s.cardinality,
            LogicalOperator::CreateIndex(s) => s.cardinality,
            LogicalOperator::DropIndex(s) => s.cardinality,
            LogicalOperator::CreateVectorIndex(s) => s.cardinality,
            LogicalOperator::CreateSequence(s) => s.cardinality,
            LogicalOperator::DropSequence(s) => s.cardinality,
            LogicalOperator::CreateDml(s) => s.cardinality,
            LogicalOperator::CreateNode(s) => s.cardinality,
            LogicalOperator::CreateRel(s) => s.cardinality,
            LogicalOperator::Extend(s) => s.cardinality,
            LogicalOperator::ExportDatabase(s) => s.cardinality,
            LogicalOperator::ImportDatabase(s) => s.cardinality,
            LogicalOperator::CreateFtsIndex(s) => s.cardinality,
            LogicalOperator::FtsScan(s) => s.cardinality,
            LogicalOperator::EmptyResult(s) => s.cardinality,
            LogicalOperator::MultiplicityReducer(s) => s.cardinality,
            LogicalOperator::Skip(s) => s.cardinality,
            LogicalOperator::Insert(s) => s.cardinality,
            LogicalOperator::ExtensionClause(s) => s.cardinality,
        }
    }

    /// Set the estimated cardinality for this operator.
    pub fn set_cardinality(&mut self, card: u64) {
        match self {
            LogicalOperator::ScanNode(s) => s.cardinality = card,
            LogicalOperator::ScanRel(s) => s.cardinality = card,
            LogicalOperator::VectorSimilarityScan(s) => s.cardinality = card,
            LogicalOperator::ArtIndexRangeScan(s) => s.cardinality = card,
            LogicalOperator::Filter(s) => s.cardinality = card,
            LogicalOperator::Projection(s) => s.cardinality = card,
            LogicalOperator::HashJoin(s) => s.cardinality = card,
            LogicalOperator::CrossProduct(s) => s.cardinality = card,
            LogicalOperator::OrderBy(s) => s.cardinality = card,
            LogicalOperator::TopK(s) => s.cardinality = card,
            LogicalOperator::Limit(s) => s.cardinality = card,
            LogicalOperator::Aggregate(s) => s.cardinality = card,
            LogicalOperator::Union(s) => s.cardinality = card,
            LogicalOperator::Flatten(s) => s.cardinality = card,
            LogicalOperator::TableFunctionCall(s) => s.cardinality = card,
            LogicalOperator::StandaloneCall(s) => s.cardinality = card,
            LogicalOperator::CopyFrom(s) => s.cardinality = card,
            LogicalOperator::BatchInsert(s) => s.cardinality = card,
            LogicalOperator::IndexLookup(s) => s.cardinality = card,
            LogicalOperator::Delete(s) => s.cardinality = card,
            LogicalOperator::Set(s) => s.cardinality = card,
            LogicalOperator::OptionalMatch(s) => s.cardinality = card,
            LogicalOperator::OptionalExtend(s) => s.cardinality = card,
            LogicalOperator::Unwind(s) => s.cardinality = card,
            LogicalOperator::Foreach(s) => s.cardinality = card,
            LogicalOperator::Merge(s) => s.cardinality = card,
            LogicalOperator::MergeRel(s) => s.cardinality = card,
            LogicalOperator::SemiJoin(s) => s.cardinality = card,
            LogicalOperator::AntiJoin(s) => s.cardinality = card,
            LogicalOperator::Intersect(s) => s.cardinality = card,
            LogicalOperator::Explain(s) => s.cardinality = card,
            LogicalOperator::RecursiveExtend(s) => s.cardinality = card,
            LogicalOperator::Accumulate(s) => s.cardinality = card,
            LogicalOperator::ExpressionsScan(s) => s.cardinality = card,
            LogicalOperator::CountRelTable(_) => {}
            LogicalOperator::Partitioner(s) => s.cardinality = card,
            LogicalOperator::PathPropertyProbe(s) => s.cardinality = card,
            // DDL operators
            LogicalOperator::CreateNodeTable(s) => s.cardinality = card,
            LogicalOperator::CreateRelTable(s) => s.cardinality = card,
            LogicalOperator::DropTable(s) => s.cardinality = card,
            LogicalOperator::AlterTable(s) => s.cardinality = card,
            LogicalOperator::CreateIndex(s) => s.cardinality = card,
            LogicalOperator::DropIndex(s) => s.cardinality = card,
            LogicalOperator::CreateVectorIndex(s) => s.cardinality = card,
            LogicalOperator::CreateSequence(s) => s.cardinality = card,
            LogicalOperator::DropSequence(s) => s.cardinality = card,
            LogicalOperator::CreateDml(s) => s.cardinality = card,
            LogicalOperator::CreateNode(s) => s.cardinality = card,
            LogicalOperator::CreateRel(s) => s.cardinality = card,
            LogicalOperator::Extend(s) => s.cardinality = card,
            LogicalOperator::ExportDatabase(s) => s.cardinality = card,
            LogicalOperator::ImportDatabase(s) => s.cardinality = card,
            LogicalOperator::CreateFtsIndex(s) => s.cardinality = card,
            LogicalOperator::FtsScan(s) => s.cardinality = card,
            LogicalOperator::EmptyResult(s) => s.cardinality = card,
            LogicalOperator::MultiplicityReducer(s) => s.cardinality = card,
            LogicalOperator::Skip(s) => s.cardinality = card,
            LogicalOperator::Insert(s) => s.cardinality = card,
            LogicalOperator::ExtensionClause(s) => s.cardinality = card,
        }
    }

    /// Recursively visit all operators in the tree bottom-up.
    pub fn visit_bottom_up<F: FnMut(&mut LogicalOperator)>(op: &mut LogicalOperator, f: &mut F) {
        let children = op.children_mut();
        for child in children {
            Self::visit_bottom_up(child, f);
        }
        f(op);
    }

    /// Get mutable references to all direct children of this operator.
    pub fn children_mut(&mut self) -> Vec<&mut LogicalOperator> {
        match self {
            LogicalOperator::Filter(s) => s.children.iter_mut().collect(),
            LogicalOperator::Projection(s) => s.children.iter_mut().collect(),
            LogicalOperator::HashJoin(s) => vec![&mut *s.probe_side, &mut *s.build_side],
            LogicalOperator::CrossProduct(s) => vec![&mut *s.left, &mut *s.right],
            LogicalOperator::OrderBy(s) => s.children.iter_mut().collect(),
            LogicalOperator::TopK(s) => s.children.iter_mut().collect(),
            LogicalOperator::Limit(s) => s.children.iter_mut().collect(),
            LogicalOperator::Aggregate(s) => s.children.iter_mut().collect(),
            LogicalOperator::Union(s) => vec![&mut *s.left, &mut *s.right],
            LogicalOperator::Flatten(s) => s.children.iter_mut().collect(),
            LogicalOperator::OptionalMatch(s) => vec![&mut *s.left, &mut *s.right],
            LogicalOperator::OptionalExtend(s) => s.children.iter_mut().collect(),
            LogicalOperator::SemiJoin(s) => vec![&mut *s.left, &mut *s.right],
            LogicalOperator::AntiJoin(s) => vec![&mut *s.left, &mut *s.right],
            LogicalOperator::Intersect(s) => vec![&mut *s.left, &mut *s.right],
            LogicalOperator::Explain(s) => vec![&mut *s.inner],
            LogicalOperator::RecursiveExtend(_) => vec![],
            LogicalOperator::Accumulate(s) => s.children.iter_mut().collect(),
            LogicalOperator::Partitioner(s) => s.children.iter_mut().collect(),
            LogicalOperator::PathPropertyProbe(s) => s.children.iter_mut().collect(),
            LogicalOperator::CountRelTable(_) => vec![],
            LogicalOperator::ExpressionsScan(_) => vec![],
            LogicalOperator::TableFunctionCall(_) => vec![],
            LogicalOperator::StandaloneCall(_) => vec![],
            LogicalOperator::CopyFrom(_)
            | LogicalOperator::BatchInsert(_)
            | LogicalOperator::IndexLookup(_)
            | LogicalOperator::Delete(_)
            | LogicalOperator::Set(_)
            | LogicalOperator::Unwind(_)
            | LogicalOperator::Foreach(_)
            | LogicalOperator::Merge(_)
            | LogicalOperator::MergeRel(_) => vec![],
            // Leaf operators have no children
            LogicalOperator::ArtIndexRangeScan(_)
            | LogicalOperator::VectorSimilarityScan(_)
            | LogicalOperator::ScanNode(_)
            | LogicalOperator::ScanRel(_)
            | LogicalOperator::CreateNodeTable(_)
            | LogicalOperator::CreateRelTable(_)
            | LogicalOperator::DropTable(_)
            | LogicalOperator::AlterTable(_)
            | LogicalOperator::CreateIndex(_)
            | LogicalOperator::DropIndex(_)
            | LogicalOperator::CreateVectorIndex(_)
            | LogicalOperator::CreateSequence(_)
            | LogicalOperator::DropSequence(_)
            | LogicalOperator::CreateDml(_)
            | LogicalOperator::CreateNode(_)
            | LogicalOperator::CreateRel(_)
            | LogicalOperator::Extend(_)
            | LogicalOperator::ExportDatabase(_)
            | LogicalOperator::ImportDatabase(_)
            | LogicalOperator::CreateFtsIndex(_)
            | LogicalOperator::FtsScan(_)
            | LogicalOperator::EmptyResult(_)
            | LogicalOperator::Insert(_)
            | LogicalOperator::ExtensionClause(_) => vec![],
            LogicalOperator::MultiplicityReducer(s) => s.children.iter_mut().collect(),
            LogicalOperator::Skip(s) => s.children.iter_mut().collect(),
        }
    }

    /// Get the child operators (immutable).
    pub fn children(&self) -> Vec<&LogicalOperator> {
        match self {
            LogicalOperator::Filter(s) => s.children.iter().collect(),
            LogicalOperator::Projection(s) => s.children.iter().collect(),
            LogicalOperator::HashJoin(s) => vec![&*s.probe_side, &*s.build_side],
            LogicalOperator::CrossProduct(s) => vec![&*s.left, &*s.right],
            LogicalOperator::OrderBy(s) => s.children.iter().collect(),
            LogicalOperator::TopK(s) => s.children.iter().collect(),
            LogicalOperator::Limit(s) => s.children.iter().collect(),
            LogicalOperator::Aggregate(s) => s.children.iter().collect(),
            LogicalOperator::Union(s) => vec![&*s.left, &*s.right],
            LogicalOperator::Flatten(s) => s.children.iter().collect(),
            LogicalOperator::OptionalMatch(s) => vec![&*s.left, &*s.right],
            LogicalOperator::OptionalExtend(s) => s.children.iter().collect(),
            LogicalOperator::SemiJoin(s) => vec![&*s.left, &*s.right],
            LogicalOperator::AntiJoin(s) => vec![&*s.left, &*s.right],
            LogicalOperator::Intersect(s) => vec![&*s.left, &*s.right],
            LogicalOperator::Explain(s) => vec![&*s.inner],
            LogicalOperator::RecursiveExtend(_) => vec![],
            LogicalOperator::Accumulate(s) => s.children.iter().collect(),
            LogicalOperator::Partitioner(s) => s.children.iter().collect(),
            LogicalOperator::PathPropertyProbe(s) => s.children.iter().collect(),
            LogicalOperator::CountRelTable(_) => vec![],
            LogicalOperator::ExpressionsScan(_) => vec![],
            LogicalOperator::TableFunctionCall(_) => vec![],
            LogicalOperator::StandaloneCall(_) => vec![],
            LogicalOperator::CopyFrom(_)
            | LogicalOperator::BatchInsert(_)
            | LogicalOperator::IndexLookup(_)
            | LogicalOperator::Delete(_)
            | LogicalOperator::Set(_)
            | LogicalOperator::Unwind(_)
            | LogicalOperator::Foreach(_)
            | LogicalOperator::Merge(_)
            | LogicalOperator::MergeRel(_) => vec![],
            LogicalOperator::ArtIndexRangeScan(_)
            | LogicalOperator::VectorSimilarityScan(_)
            | LogicalOperator::ScanNode(_)
            | LogicalOperator::ScanRel(_)
            | LogicalOperator::CreateNodeTable(_)
            | LogicalOperator::CreateRelTable(_)
            | LogicalOperator::DropTable(_)
            | LogicalOperator::AlterTable(_)
            | LogicalOperator::CreateIndex(_)
            | LogicalOperator::DropIndex(_)
            | LogicalOperator::CreateVectorIndex(_)
            | LogicalOperator::CreateSequence(_)
            | LogicalOperator::DropSequence(_)
            | LogicalOperator::CreateDml(_)
            | LogicalOperator::CreateNode(_)
            | LogicalOperator::CreateRel(_)
            | LogicalOperator::Extend(_)
            | LogicalOperator::ExportDatabase(_)
            | LogicalOperator::ImportDatabase(_)
            | LogicalOperator::CreateFtsIndex(_)
            | LogicalOperator::FtsScan(_)
            | LogicalOperator::EmptyResult(_)
            | LogicalOperator::Insert(_)
            | LogicalOperator::ExtensionClause(_) => vec![],
            LogicalOperator::MultiplicityReducer(s) => s.children.iter().collect(),
            LogicalOperator::Skip(s) => s.children.iter().collect(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct LogicalArtIndexRangeScan {
    pub table_name: String,
    pub table_id: u64,
    pub alias: Option<String>,
    pub lower_bound: Option<akar_common::types::Value>,
    pub upper_bound: Option<akar_common::types::Value>,
    pub lower_inclusive: bool,
    pub upper_inclusive: bool,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalVectorSimilarityScan {
    pub table_name: String,
    pub column_name: String,
    pub query_vector: Vec<f64>,
    pub top_k: u64,
    /// Node alias for field-name prefixing (e.g. `n` in
    /// `MATCH (n:Memory) ...`). `None` for the bare `CALL vector_similarity_scan`
    /// path, which emits unprefixed output columns like `distance`/`_id`.
    pub alias: Option<String>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalScanNode {
    pub table_name: String,
    pub table_id: u64,
    pub alias: Option<String>,
    pub columns: Vec<String>,
    pub cardinality: u64,
    pub fts_query: Option<LogicalFtsScan>,
    pub predicate: Option<Expression>,
}

#[derive(Debug, Clone)]
pub struct LogicalScanRel {
    pub table_name: String,
    pub table_id: u64,
    pub direction: akar_parser::ast::EdgeDirection,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalFilter {
    pub expression: Expression,
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalProjection {
    pub expressions: Vec<BoundExpression>,
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalHashJoin {
    pub join_keys: Vec<Expression>,
    pub build_side: Box<LogicalOperator>,
    pub probe_side: Box<LogicalOperator>,
    pub cardinality: u64,
    /// Whether this join is eligible for foreign join push-down optimization.
    /// Set by the ForeignJoinPushDown optimizer pass when all tables in the
    /// pattern belong to the same foreign database.
    pub push_down_eligible: bool,
}

/// Semi-join: returns left rows that have a matching key in the right side.
/// Like HashJoin but only emits left columns for matching rows.
#[derive(Debug, Clone)]
pub struct LogicalSemiJoin {
    pub join_keys: Vec<Expression>,
    pub left: Box<LogicalOperator>,
    pub right: Box<LogicalOperator>,
    pub cardinality: u64,
}

/// Anti-join: returns left rows that have NO matching key in the right side.
/// Like SemiJoin but inverts the match condition.
#[derive(Debug, Clone)]
pub struct LogicalAntiJoin {
    pub join_keys: Vec<Expression>,
    pub left: Box<LogicalOperator>,
    pub right: Box<LogicalOperator>,
    pub cardinality: u64,
}

/// EXPLAIN operator — wraps a child plan and produces a textual plan description.
///
/// Unlike other operators, Explain does not execute its child; instead it
/// serializes the operator tree to a human-readable string.
#[derive(Debug, Clone)]
pub struct LogicalExplain {
    /// The inner operator tree to explain.
    pub inner: Box<LogicalOperator>,
    /// The type of explain output.
    pub explain_type: akar_parser::ast::ExplainType,
    /// Cardinality (always 1 — one row with the plan string).
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalCrossProduct {
    pub left: Box<LogicalOperator>,
    pub right: Box<LogicalOperator>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalOrderBy {
    pub sort_keys: Vec<(Expression, bool)>, // (expression, ascending)
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

/// A fused ORDER BY + LIMIT operator for Top-K optimization.
///
/// When the optimizer detects a consecutive ORDER BY followed by LIMIT,
/// it fuses them into a single LogicalTopK. This signals the processor
/// to use a BinaryHeap-based TopK execution (O(n log k)) instead of
/// full sort + limit (O(n log n)).
#[derive(Debug, Clone)]
pub struct LogicalTopK {
    pub sort_keys: Vec<(Expression, bool)>,
    pub limit: u64,
    pub offset: u64,
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalLimit {
    pub limit: u64,
    pub offset: u64,
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalAggregate {
    pub group_by: Vec<Expression>,
    pub aggregates: Vec<(String, Vec<Expression>)>, // (function_name, args)
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalUnion {
    pub left: Box<LogicalOperator>,
    pub right: Box<LogicalOperator>,
    pub all: bool,
    pub cardinality: u64,
}

/// A flatten operator that converts a specific factorization group from
/// unflat (list-like) to flat (scalar) representation.
///
/// Inserted by `FactorizationRewriting` to ensure operators like HashJoin
/// receive the correct factorization layout.
#[derive(Debug, Clone)]
pub struct LogicalFlatten {
    pub group_pos: usize,
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

/// UNWIND operator — expands a list expression into rows.
#[derive(Debug, Clone)]
pub struct LogicalUnwind {
    pub expression: akar_parser::ast::Expression,
    pub variable: String,
    pub cardinality: u64,
}

/// INTERSECT operator — finds common keys across multiple build sides.
///
/// Used for multi-pattern matching like `MATCH (a)-[:r1]->(b), (a)-[:r2]->(c)`
/// where `a` is the shared key. Intersect probes multiple build hash tables
/// and outputs combined payloads only for keys present in all build sides.
#[derive(Debug, Clone)]
pub struct LogicalIntersect {
    /// Number of build sides (hash tables to probe).
    pub num_build_sides: u32,
    /// Key expressions for each build side.
    pub build_key_exprs: Vec<Expression>,
    /// Probe side — produces the shared key value to look up.
    pub left: Box<LogicalOperator>,
    /// Build side — produces hash tables for each pattern.
    pub right: Box<LogicalOperator>,
    /// Estimated cardinality.
    pub cardinality: u64,
}

/// Variable-length path (recursive extend) operator.
///
/// Corresponds to `MATCH (a)-[e*1..3]->(b)` — traverses the graph
/// up to `upper_bound` hops from source nodes and produces results
/// for each path whose length is between `lower_bound` and `upper_bound`.
///
/// Supports both unweighted BFS and weighted shortest path (Dijkstra)
/// when `weight_property` is specified.
///
/// This is a leaf operator that executes BFS/Dijkstra traversal during query execution.
#[derive(Debug, Clone)]
pub struct LogicalRecursiveExtend {
    /// Source node variable name.
    pub source_var: String,
    /// Source node table ID.
    pub source_table_id: u64,
    /// Edge variable name (optional).
    pub edge_var: Option<String>,
    /// Destination node variable name.
    pub target_var: String,
    /// Relationship table ID(s) to traverse.
    pub rel_table_ids: Vec<u64>,
    /// Relationship label(s).
    pub rel_labels: Vec<String>,
    /// Minimum path length.
    pub lower_bound: u64,
    /// Maximum path length.
    pub upper_bound: u64,
    /// Traversal direction.
    pub direction: akar_common::enums::ExtendDirection,
    /// Path semantic (WALK / TRAIL / ACYCLIC).
    pub semantic: akar_common::enums::PathSemantic,
    /// Optional edge weight property name for weighted shortest path.
    /// When `Some(prop_name)`, traversal uses Dijkstra's algorithm instead of BFS,
    /// and results are sorted by cumulative path cost.
    pub weight_property: Option<String>,
    /// Optional name for the cost output column (e.g., "cost" or "totalWeight").
    /// Only used when `weight_property` is set.
    pub cost_output_name: Option<String>,
    /// Estimated cardinality.
    pub cardinality: u64,
}

/// OPTIONAL MATCH operator — a tree node with required (left) and optional (right) children.
///
/// The required side is executed first. For each resulting row, the optional
/// side is attempted. If the optional side produces a match, the combined
/// row (left + right columns) is emitted. If no match, left columns are
/// emitted with NULLs for right-side columns.
#[derive(Debug, Clone)]
pub struct LogicalOptionalMatch {
    pub left: Box<LogicalOperator>,
    pub right: Box<LogicalOperator>,
    pub cardinality: u64,
}

/// OPTIONAL MATCH over an already-bound pair of node variables.
///
/// Used when both endpoint node variables of the optional pattern are bound by
/// the required side (e.g. `OPTIONAL MATCH (a)-[existing:Connected]-(b)` where
/// `a` and `b` are already in scope). Unlike `LogicalOptionalMatch`, the right
/// side is not a scan: the pattern is probed per input row against the
/// relationship table adjacency (forward and/or reverse), emitting the edge
/// property columns, or NULL-padding them when no edge exists (P53.25).
#[derive(Debug, Clone)]
pub struct LogicalOptionalExtend {
    pub children: Vec<LogicalOperator>,
    /// Name of the relationship table to probe.
    pub rel_table_name: String,
    /// ID of the relationship table.
    pub rel_table_id: u64,
    /// Variable name of the relationship (e.g., "existing"); prefix for the
    /// emitted edge property columns.
    pub rel_var: String,
    /// Variable name of the bound source node (e.g., "a").
    pub src_node_var: String,
    /// Variable name of the bound destination node (e.g., "b"). An empty
    /// string selects fan-out mode (P53.40): the destination is anonymous
    /// (`OPTIONAL MATCH (m)-[r:R]-(:T)`), so every edge incident on the source
    /// becomes one output row.
    pub dst_node_var: String,
    /// Direction of the probe (forward, backward, or both).
    pub direction: akar_parser::ast::EdgeDirection,
    /// Estimated cardinality.
    pub cardinality: u64,
}

/// SET operator — updates properties on matched rows.
#[derive(Debug, Clone)]
pub struct LogicalSet {
    pub table_name: String,
    pub table_id: u64,
    pub is_node: bool,
    /// All assignments of the SET clause, evaluated against the pre-update
    /// snapshot as a group (P53.17). Multiple items were previously emitted as
    /// a chain of single-item operators, so items after the first received the
    /// previous item's count chunk and lost the scan rows.
    pub items: Vec<SetItem>,
    /// True when the SET is the terminal clause (no trailing RETURN): the
    /// operator reports "N rows updated" as column 0. False when a RETURN
    /// follows — an empty match must flow 0 rows, not a phantom count row
    /// (P53.39, kairos Finding P59.1).
    pub emit_count: bool,
    pub cardinality: u64,
}

/// A single `SET n.prop = <expr>` assignment inside a [`LogicalSet`].
#[derive(Debug, Clone)]
pub struct SetItem {
    pub column_name: String,
    pub column_idx: usize,
    pub value: akar_parser::ast::Expression,
}

/// DELETE operator — removes rows from a table.
#[derive(Debug, Clone)]
pub struct LogicalDelete {
    pub table_name: String,
    pub table_id: u64,
    pub primary_key_column: String,
    pub is_node: bool,
    pub detach: bool,
    pub cardinality: u64,
}

/// COPY FROM operator — loads data from a file into a table.
/// This is a leaf-level DML operator (no children) that the processor
/// resolves into a `PhysicalCopyFrom` for execution.
#[derive(Debug, Clone)]
pub struct LogicalCopyFrom {
    pub table_name: String,
    pub table_id: u64,
    pub file_path: String,
    pub options: std::collections::HashMap<String, String>,
    pub cardinality: u64,
}

/// Batch insert operator — inserts multiple rows/rels in a single operation.
///
/// Unlike `CopyFrom` which reads from a file, `BatchInsert` takes pre-collected
/// data from the plan pipeline (e.g., multiple fused CREATE statements).
/// Uses `NodeTable::insert_rows_batch()` / `RelTable::insert_rels_batch()`.
#[derive(Debug, Clone)]
pub struct LogicalBatchInsert {
    pub table_name: String,
    pub table_id: u64,
    /// Rows to insert: each row is a Vec<Value> matching column order.
    pub rows: Vec<Vec<akar_common::types::Value>>,
    pub cardinality: u64,
}

/// Index lookup operator — point lookup via ART index on a PK column.
#[derive(Debug, Clone)]
pub struct LogicalIndexLookup {
    pub table_name: String,
    pub table_id: u64,
    pub key_value: akar_common::types::Value,
    pub cardinality: u64,
}

/// FOREACH operator — iterates over list elements and executes sub-plans.
#[derive(Debug, Clone)]
pub struct LogicalForeach {
    pub variable: String,
    pub expression: akar_parser::ast::Expression,
    /// Sub-plans to execute for each list element.
    pub sub_plans: Vec<Vec<LogicalOperator>>,
    pub cardinality: u64,
}

/// A table function call operator.
///
/// Invokes a registered table function (e.g., `duckdb_scan`, `delta_scan`)
/// and produces a DataChunk as output. The function is looked up by name
/// in the FunctionRegistry during execution.
#[derive(Debug, Clone)]
pub struct LogicalTableFunctionCall {
    pub function_name: String,
    pub args: Vec<akar_parser::ast::Expression>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalStandaloneCall {
    pub function_name: String,
    pub args: Vec<akar_parser::ast::Expression>,
    pub cardinality: u64,
}

/// MERGE operator — match or create a node/pattern.
///
/// The processor first attempts to match a node with the given properties.
/// If found, applies `ON MATCH SET` operations. If not found, creates a
/// new node with the pattern properties and applies `ON CREATE SET`.
#[derive(Debug, Clone)]
pub struct LogicalMerge {
    pub table_name: String,
    pub table_id: u64,
    /// Properties from the MERGE pattern (name, expression pairs).
    pub properties: Vec<(String, akar_parser::ast::Expression)>,
    /// SET operations to apply when the node already exists (matched).
    pub on_match: Vec<LogicalSet>,
    /// SET operations to apply when a new node is created.
    pub on_create: Vec<LogicalSet>,
    pub cardinality: u64,
}

/// Logical operator for edge MERGE (P53.20): `MERGE (a)-[r:R]->(b)`.
///
/// Matches an existing edge from `src` to `dst` on the rel table whose props
/// equal the pattern properties; if absent, inserts a new edge. Emits the
/// matched/inserted edge's `_id` as `<edge_var>._id` so a following SET clause
/// can target it.
#[derive(Debug, Clone)]
pub struct LogicalMergeRel {
    pub rel_table_name: String,
    pub rel_table_id: u64,
    /// Variable bound to the edge (e.g. `r`).
    pub edge_var: String,
    /// Node variables bound by a prior MATCH that anchor the endpoints.
    pub src_node_var: String,
    pub dst_node_var: String,
    /// Inline properties from the edge pattern (`{type: $type}`).
    pub properties: Vec<(String, akar_parser::ast::Expression)>,
    /// SET operations applied when the edge already exists (empty for the
    /// standalone-SET form used by Kairos `add_connection`).
    pub on_match: Vec<LogicalSet>,
    /// SET operations applied when a new edge is created.
    pub on_create: Vec<LogicalSet>,
    pub cardinality: u64,
}

// ==================== DDL Operators ====================

/// Logical operator for CREATE NODE TABLE.
#[derive(Debug, Clone)]
pub struct LogicalCreateNodeTable {
    pub name: String,
    pub columns: Vec<CatalogColumn>,
    pub primary_key: String,
    pub if_not_exists: bool,
    pub cardinality: u64,
}

/// Logical operator for CREATE REL TABLE.
#[derive(Debug, Clone)]
pub struct LogicalCreateRelTable {
    pub name: String,
    pub from: String,
    pub to: String,
    pub columns: Vec<CatalogColumn>,
    pub if_not_exists: bool,
    pub cardinality: u64,
}

/// Logical operator for DROP TABLE.
#[derive(Debug, Clone)]
pub struct LogicalDropTable {
    pub name: String,
    pub cardinality: u64,
}

/// Logical operator for ALTER TABLE.
#[derive(Debug, Clone)]
pub struct LogicalAlterTable {
    pub table_name: String,
    pub action: akar_parser::ast::AlterAction,
    pub cardinality: u64,
}

/// Logical operator for CREATE [ART|HASH] INDEX.
#[derive(Debug, Clone)]
pub struct LogicalCreateIndex {
    pub index_type: akar_catalog::IndexType,
    pub index_name: String,
    pub table_name: String,
    pub column_name: String,
    pub cardinality: u64,
}

/// Logical operator for DROP INDEX.
#[derive(Debug, Clone)]
pub struct LogicalDropIndex {
    pub index_name: String,
    pub table_name: String,
    pub cardinality: u64,
}

/// Logical operator for CREATE VECTOR INDEX.
#[derive(Debug, Clone)]
pub struct LogicalCreateVectorIndex {
    pub index_name: String,
    pub table_name: String,
    pub column_name: String,
    pub metric: String,
    pub dimensions: u64,
    pub cardinality: u64,
}

/// Logical operator for CREATE SEQUENCE.
#[derive(Debug, Clone)]
pub struct LogicalCreateSequence {
    pub name: String,
    pub if_not_exists: bool,
    pub or_replace: bool,
    pub start_with: i64,
    pub increment: i64,
    pub min_value: i64,
    pub max_value: i64,
    pub cycle: bool,
    pub cardinality: u64,
}

/// Logical operator for DROP SEQUENCE.
#[derive(Debug, Clone)]
pub struct LogicalDropSequence {
    pub name: String,
    pub if_exists: bool,
    pub cardinality: u64,
}

/// Logical operator for CREATE DML (node creation with properties).
#[derive(Debug, Clone)]
pub struct LogicalCreateDml {
    pub table_name: String,
    pub table_id: u64,
    pub properties: Vec<(String, akar_parser::ast::Expression)>,
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalCreateNode {
    pub table_name: String,
    pub table_id: u64,
    pub out_var_name: String,
    pub properties: Vec<(String, akar_parser::ast::Expression)>,
    pub cardinality: u64,
}

/// Logical operator for extending from a source node through a relationship.
///
/// Replaces the combination of ScanRel + ScanNode(dest) in the pipeline.
/// For each source node, looks up adjacency list entries in the rel table,
/// producing output rows that include source fields, rel properties, and
/// destination node properties.
///
/// Ported from C++ `LogicalExtend`.
#[derive(Debug, Clone)]
pub struct LogicalExtend {
    /// Name of the relationship table to extend through.
    pub rel_table_name: String,
    /// ID of the relationship table.
    pub rel_table_id: u64,
    /// Variable name of the relationship (e.g., "r" in `-[r:RELATES_TO]->`).
    /// Used as the field-name prefix for relationship properties.
    pub rel_var: String,
    /// Variable name of the bound (source) node.
    pub bound_node_var: String,
    /// Direction of the extend (forward, backward, or both).
    pub direction: akar_parser::ast::EdgeDirection,
    /// Variable name of the destination node (e.g., "p").
    pub dst_node_var: String,
    /// Table name of the destination node (e.g., "Post").
    pub dst_table_name: String,
    /// Table ID of the destination node.
    pub dst_table_id: u64,
    /// Optional FTS query applied to the *destination* rows of this extend,
    /// when the indexed table is only reachable as the extend destination —
    /// `MATCH (a:Author)-[:AUTHORED_BY]->(d:Document) USING FTS INDEX ...`
    /// has no scan of `Document`, so the document-id filter must run on the
    /// rows this hop produces (P108.4).
    pub fts_query: Option<LogicalFtsScan>,
    /// Estimated cardinality.
    pub cardinality: u64,
}

#[derive(Debug, Clone)]
pub struct LogicalCreateRel {
    pub table_name: String,
    pub table_id: u64,
    pub src_node_name: String,
    pub dst_node_name: String,
    pub out_var_name: String,
    pub properties: Vec<(String, akar_parser::ast::Expression)>,
    pub cardinality: u64,
}

/// Logical operator for EXPORT DATABASE.
#[derive(Debug, Clone)]
pub struct LogicalExportDatabase {
    pub file_path: String,
    pub file_type: String,
    pub schema_only: bool,
    pub options: std::collections::HashMap<String, String>,
    pub cardinality: u64,
}

/// Logical operator for IMPORT DATABASE.
#[derive(Debug, Clone)]
pub struct LogicalImportDatabase {
    pub file_path: String,
    pub query: String,
    pub index_query: String,
    pub cardinality: u64,
}

/// Logical operator for creating an FTS index (P8 architecture)
#[derive(Debug, Clone)]
pub struct LogicalCreateFtsIndex {
    pub index_name: String,
    pub table_name: String,
    pub column_name: String,
    /// Tokenizer name from `WITH TOKENIZER('...')` (P109.1); `None` = `en_stem`.
    pub tokenizer: Option<String>,
    pub if_not_exists: bool,
    pub cardinality: u64,
}

/// Logical operator for querying an FTS index via `USING FTS INDEX` clause.
#[derive(Debug, Clone)]
pub struct LogicalFtsScan {
    pub index_name: String,
    pub query_string: String,
    /// Source node table/column the index was created on (P52.39).
    pub table_name: String,
    pub column_name: String,
    pub cardinality: u64,
}

/// EmptyResult operator — returns an empty result set (0 rows).
/// Inserted when planner knows the query will yield no rows (e.g. WHERE false).
#[derive(Debug, Clone)]
pub struct LogicalEmptyResult {
    pub cardinality: u64,
}

/// MultiplicityReducer operator — deduplicates rows from pattern matching fan-out.
#[derive(Debug, Clone)]
pub struct LogicalMultiplicityReducer {
    pub key_columns: Vec<usize>,
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

/// Skip operator — skips the first N rows (like LIMIT offset without limit).
#[derive(Debug, Clone)]
pub struct LogicalSkip {
    pub offset: u64,
    pub children: Vec<LogicalOperator>,
    pub cardinality: u64,
}

/// Insert operator — row-level insertion (unlike BatchInsert).
#[derive(Debug, Clone)]
pub struct LogicalInsert {
    pub table_name: String,
    pub table_id: u64,
    pub columns: Vec<String>,
    pub values: Vec<Vec<akar_common::types::Value>>,
    pub cardinality: u64,
}

/// ExtensionClause operator — handles EXTENSION commands (INSTALL, LOAD).
#[derive(Debug, Clone)]
pub struct LogicalExtensionClause {
    pub action: akar_parser::ast::ExtensionAction,
    pub extension_name: String,
    pub cardinality: u64,
}