vibesql-executor 0.2.0

Query execution engine for vibesql SQL 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
1068
1069
1070
//! Tests for subquery-to-join transformations

use vibesql_ast::{BinaryOperator, GroupByClause, JoinType, SelectItem};
use vibesql_types::SqlValue;

use super::*;

fn simple_table_from(name: &str) -> FromClause {
    FromClause::Table {
        index_hint: None,
        name: name.to_string(),
        alias: None,
        column_aliases: None,
        quoted: false,
    }
}

fn column_ref(column: &str) -> Expression {
    Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(&column, false))
}

fn simple_select(table: &str, column: &str) -> SelectStmt {
    SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Expression {
            expr: column_ref(column),
            alias: None,
            source_text: None,
        }],
        into_table: None,
        into_variables: None,
        from: Some(simple_table_from(table)),
        where_clause: None,
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    }
}

#[test]
fn test_in_subquery_to_semi_join() {
    let mut stmt = simple_select("orders", "o_orderkey");
    let subquery = simple_select("lineitem", "l_orderkey");

    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery),
        negated: false,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created a SEMI JOIN
    assert!(transformed.where_clause.is_none(), "WHERE clause should be removed");
    match transformed.from {
        Some(FromClause::Join { join_type, .. }) => {
            assert!(matches!(join_type, JoinType::Semi), "Should be SEMI join");
        }
        _ => panic!("Expected JOIN in FROM clause"),
    }
}

#[test]
fn test_not_in_subquery_to_anti_join() {
    let mut stmt = simple_select("orders", "o_orderkey");
    let subquery = simple_select("lineitem", "l_orderkey");

    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery),
        negated: true,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created an ANTI JOIN
    assert!(transformed.where_clause.is_none(), "WHERE clause should be removed");
    match transformed.from {
        Some(FromClause::Join { join_type, .. }) => {
            assert!(matches!(join_type, JoinType::Anti), "Should be ANTI join");
        }
        _ => panic!("Expected JOIN in FROM clause"),
    }
}

#[test]
fn test_complex_subquery_unchanged() {
    let mut stmt = simple_select("orders", "o_orderkey");
    let mut subquery = simple_select("lineitem", "l_orderkey");
    // Add LIMIT to make it complex (LIMIT subqueries can't be safely transformed)
    subquery.limit = Some(Expression::Literal(SqlValue::Integer(10)));

    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery),
        negated: false,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should be unchanged because subquery has LIMIT
    assert!(
        transformed.where_clause.is_some(),
        "Complex subquery with LIMIT should remain in WHERE"
    );
    match transformed.from {
        Some(FromClause::Table { .. }) => {} // Good, no join created
        _ => panic!("Complex subquery with LIMIT should not create JOIN"),
    }
}

#[test]
fn test_aggregate_subquery_transforms_to_derived_table() {
    // Test TPC-H Q18-like pattern: IN subquery with GROUP BY/HAVING
    let mut stmt = simple_select("orders", "o_orderkey");
    let mut subquery = simple_select("lineitem", "l_orderkey");
    // Add GROUP BY and HAVING - this should now be transformed using derived table
    subquery.group_by = Some(GroupByClause::Simple(vec![column_ref("l_orderkey")]));
    subquery.having = Some(Expression::BinaryOp {
        op: BinaryOperator::GreaterThan,
        left: Box::new(Expression::AggregateFunction {
            name: vibesql_ast::FunctionIdentifier::new("SUM"),
            distinct: false,
            args: vec![column_ref("l_quantity")],
            order_by: None,
            filter: None,
        }),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Integer(300))),
    });

    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery),
        negated: false,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should be transformed: WHERE clause removed (IN -> semi-join)
    assert!(transformed.where_clause.is_none(), "Aggregate IN subquery should be transformed");

    // Check that we got a SEMI JOIN with a derived table (Subquery)
    match transformed.from {
        Some(FromClause::Join { join_type, right, .. }) => {
            assert!(matches!(join_type, JoinType::Semi), "Should be SEMI join");
            // Right side should be a Subquery (derived table)
            match right.as_ref() {
                FromClause::Subquery { alias, .. } => {
                    assert!(alias.starts_with("__in_agg_"), "Should have __in_agg_ alias");
                }
                _ => panic!("Expected Subquery (derived table) on right side of JOIN"),
            }
        }
        _ => panic!("Expected SEMI JOIN in FROM clause"),
    }
}

#[test]
fn test_multiple_subqueries_to_joins() {
    // Test Q21-like pattern with multiple IN subqueries in a deep AND chain
    // WHERE a = b AND c = d AND x IN (...) AND y NOT IN (...)
    let mut stmt = simple_select("orders", "o_orderkey");
    let subquery1 = simple_select("lineitem", "l_orderkey");
    let subquery2 = simple_select("supplier", "s_suppkey");

    // Build WHERE: o_custkey = 1 AND o_orderkey IN (subquery1) AND o_custkey NOT IN (subquery2)
    let predicate1 = Expression::BinaryOp {
        op: BinaryOperator::Equal,
        left: Box::new(column_ref("o_custkey")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Integer(1))),
    };

    let in_subquery = Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery1),
        negated: false,
    };

    let not_in_subquery = Expression::In {
        expr: Box::new(column_ref("o_custkey")),
        subquery: Box::new(subquery2),
        negated: true,
    };

    // Build: predicate1 AND in_subquery AND not_in_subquery
    let combined_where = Expression::BinaryOp {
        op: BinaryOperator::And,
        left: Box::new(Expression::BinaryOp {
            op: BinaryOperator::And,
            left: Box::new(predicate1),
            right: Box::new(in_subquery),
        }),
        right: Box::new(not_in_subquery),
    };

    stmt.where_clause = Some(combined_where);

    // Transform should extract BOTH subqueries iteratively
    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have two joins (SEMI and ANTI)
    match transformed.from {
        Some(FromClause::Join { left: inner_join_box, join_type: outer_join_type, .. }) => {
            // Outer join should be either SEMI or ANTI
            assert!(
                matches!(outer_join_type, JoinType::Semi | JoinType::Anti),
                "Outer join should be SEMI or ANTI, got: {:?}",
                outer_join_type
            );

            // Inner join should also be a join (not just a table)
            match *inner_join_box {
                FromClause::Join { join_type: inner_join_type, .. } => {
                    assert!(
                        matches!(inner_join_type, JoinType::Semi | JoinType::Anti),
                        "Inner join should be SEMI or ANTI, got: {:?}",
                        inner_join_type
                    );
                }
                _ => panic!("Expected nested JOIN, got table"),
            }
        }
        _ => panic!("Expected JOIN in FROM clause"),
    }

    // WHERE should only have the simple predicate left
    assert!(transformed.where_clause.is_some(), "Simple predicate should remain in WHERE");
}

#[test]
fn test_nested_in_subquery_self_join_column_qualification() {
    // Test that nested IN subqueries in self-joins properly qualify the outer expression
    // This is the bug from issue #2630:
    // SELECT pk FROM tab0 WHERE col3 IN (SELECT col0 FROM tab0 WHERE col0 IN (...) AND col4 >=
    // 7680.91)
    //
    // When the outer IN is transformed to a SEMI JOIN, the nested IN's outer column (col0)
    // should be qualified with the subquery alias (__subquery_TAB0), not left unqualified.

    // Create a nested IN subquery pattern
    let innermost_subquery = simple_select("tab0", "col3"); // SELECT col3 FROM tab0

    // Middle subquery: SELECT col0 FROM tab0 WHERE col0 IN (innermost) AND col4 >= 7680
    let mut middle_subquery = simple_select("tab0", "col0");
    middle_subquery.where_clause = Some(Expression::BinaryOp {
        op: BinaryOperator::And,
        left: Box::new(Expression::In {
            expr: Box::new(column_ref("col0")), // This should get qualified!
            subquery: Box::new(innermost_subquery),
            negated: false,
        }),
        right: Box::new(Expression::BinaryOp {
            op: BinaryOperator::GreaterThanOrEqual,
            left: Box::new(column_ref("col4")),
            right: Box::new(Expression::Literal(vibesql_types::SqlValue::Integer(7680))),
        }),
    });

    // Outer query: SELECT pk FROM tab0 WHERE col3 IN (middle_subquery)
    let mut stmt = simple_select("tab0", "pk");
    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("col3")),
        subquery: Box::new(middle_subquery),
        negated: false,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have transformed to a SEMI JOIN
    match &transformed.from {
        Some(FromClause::Join { join_type, condition, right, .. }) => {
            assert!(matches!(join_type, JoinType::Semi), "Should be SEMI join");

            // Check that the right side has the alias
            match right.as_ref() {
                FromClause::Table { alias: Some(alias), .. } => {
                    assert!(alias.starts_with("__subquery_"), "Should have subquery alias");
                }
                _ => panic!("Expected aliased table on right side"),
            }

            // Check the join condition - nested IN's outer column should be qualified
            if let Some(cond) = condition {
                // The condition should contain a nested IN expression
                // with a qualified column reference for col0
                fn check_nested_in_qualification(expr: &Expression) -> bool {
                    match expr {
                        Expression::In { expr: inner_expr, .. } => {
                            // The outer expression of the nested IN should be qualified
                            match inner_expr.as_ref() {
                                Expression::ColumnRef(col_id)
                                    if col_id.schema_canonical().is_none()
                                        && col_id.table_canonical().is_some() =>
                                {
                                    col_id.table_canonical().unwrap().starts_with("__subquery_")
                                        && col_id.column_canonical().eq_ignore_ascii_case("col0")
                                }
                                _ => false,
                            }
                        }
                        Expression::BinaryOp { left, right, .. } => {
                            check_nested_in_qualification(left)
                                || check_nested_in_qualification(right)
                        }
                        _ => false,
                    }
                }

                assert!(
                    check_nested_in_qualification(cond),
                    "Nested IN subquery's outer column should be qualified with subquery alias. Condition: {:?}",
                    cond
                );
            }
        }
        _ => panic!("Expected SEMI JOIN in FROM clause"),
    }
}

fn table_from_with_alias(name: &str, alias: &str) -> FromClause {
    FromClause::Table {
        index_hint: None,
        name: name.to_string(),
        alias: Some(alias.to_string()),
        column_aliases: None,
        quoted: false,
    }
}

fn qualified_column_ref(table: &str, column: &str) -> Expression {
    Expression::ColumnRef(vibesql_ast::ColumnIdentifier::qualified(table, false, column, false))
}

#[test]
fn test_exists_self_join_column_qualification() {
    // Test EXISTS with self-join aliasing, similar to TPC-H Q21 pattern:
    // SELECT * FROM lineitem l1 WHERE EXISTS (
    //   SELECT * FROM lineitem l2 WHERE l2.l_orderkey = l1.l_orderkey AND l2.l_suppkey <>
    // l1.l_suppkey )
    //
    // The EXISTS subquery references the same table with a different alias.
    // After transformation to a SEMI join, the join condition should properly
    // reference both the outer alias (l1) and the subquery's alias.

    // Create outer query: SELECT * FROM lineitem l1
    let outer_from = table_from_with_alias("lineitem", "l1");
    let mut stmt = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Wildcard { alias: None }],
        into_table: None,
        into_variables: None,
        from: Some(outer_from.clone()),
        where_clause: None,
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    // Create correlated EXISTS subquery:
    // EXISTS (SELECT * FROM lineitem l2 WHERE l2.l_orderkey = l1.l_orderkey AND l2.l_suppkey <>
    // l1.l_suppkey)
    let exists_subquery = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Wildcard { alias: None }],
        into_table: None,
        into_variables: None,
        from: Some(table_from_with_alias("lineitem", "l2")),
        where_clause: Some(Expression::BinaryOp {
            op: BinaryOperator::And,
            left: Box::new(Expression::BinaryOp {
                op: BinaryOperator::Equal,
                left: Box::new(qualified_column_ref("l2", "l_orderkey")),
                right: Box::new(qualified_column_ref("l1", "l_orderkey")),
            }),
            right: Box::new(Expression::BinaryOp {
                op: BinaryOperator::NotEqual,
                left: Box::new(qualified_column_ref("l2", "l_suppkey")),
                right: Box::new(qualified_column_ref("l1", "l_suppkey")),
            }),
        }),
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    stmt.where_clause =
        Some(Expression::Exists { subquery: Box::new(exists_subquery), negated: false });

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created a SEMI JOIN
    assert!(
        transformed.where_clause.is_none(),
        "EXISTS should be fully transformed, no WHERE clause should remain"
    );

    match &transformed.from {
        Some(FromClause::Join { join_type, condition, right, .. }) => {
            assert!(
                matches!(join_type, JoinType::Semi),
                "EXISTS should transform to SEMI join, got: {:?}",
                join_type
            );

            // Check that the right side has the rewritten alias for self-join
            // Self-joins get a unique alias like "__subquery_l2" to avoid conflicts
            match right.as_ref() {
                FromClause::Table { name, alias, .. } => {
                    assert_eq!(name, "lineitem", "Table name should be lineitem");
                    assert_eq!(
                        alias.as_deref(),
                        Some("__subquery_l2"),
                        "Self-join alias should be rewritten to __subquery_l2"
                    );
                }
                _ => panic!("Expected Table on right side of join"),
            }

            // Verify the join condition includes the correlation predicate
            // The condition should have column refs rewritten to use __subquery_l2
            assert!(condition.is_some(), "Join should have a condition");

            if let Some(cond) = condition {
                fn contains_rewritten_alias(expr: &Expression) -> bool {
                    match expr {
                        Expression::ColumnRef(col_id)
                            if col_id.schema_canonical().is_none()
                                && col_id.table_canonical().is_some() =>
                        {
                            col_id.table_canonical().unwrap() == "__subquery_l2"
                        }
                        Expression::BinaryOp { left, right, .. } => {
                            contains_rewritten_alias(left) || contains_rewritten_alias(right)
                        }
                        _ => false,
                    }
                }

                assert!(
                    contains_rewritten_alias(cond),
                    "Join condition should have column refs rewritten to __subquery_l2. Condition: {:?}",
                    cond
                );
            }
        }
        _ => panic!("Expected SEMI JOIN in FROM clause"),
    }
}

#[test]
fn test_not_exists_self_join_column_qualification() {
    // Test NOT EXISTS with self-join aliasing, similar to TPC-H Q21 pattern:
    // SELECT * FROM lineitem l1 WHERE NOT EXISTS (
    //   SELECT * FROM lineitem l3 WHERE l3.l_orderkey = l1.l_orderkey AND l3.l_receiptdate >
    // l3.l_commitdate )
    //
    // NOT EXISTS should transform to an ANTI join with proper alias handling.

    // Create outer query: SELECT * FROM lineitem l1
    let outer_from = table_from_with_alias("lineitem", "l1");
    let mut stmt = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Wildcard { alias: None }],
        into_table: None,
        into_variables: None,
        from: Some(outer_from.clone()),
        where_clause: None,
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    // Create correlated NOT EXISTS subquery:
    // NOT EXISTS (SELECT * FROM lineitem l3 WHERE l3.l_orderkey = l1.l_orderkey AND
    // l3.l_receiptdate > l3.l_commitdate)
    let not_exists_subquery = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Wildcard { alias: None }],
        into_table: None,
        into_variables: None,
        from: Some(table_from_with_alias("lineitem", "l3")),
        where_clause: Some(Expression::BinaryOp {
            op: BinaryOperator::And,
            left: Box::new(Expression::BinaryOp {
                op: BinaryOperator::Equal,
                left: Box::new(qualified_column_ref("l3", "l_orderkey")),
                right: Box::new(qualified_column_ref("l1", "l_orderkey")),
            }),
            right: Box::new(Expression::BinaryOp {
                op: BinaryOperator::GreaterThan,
                left: Box::new(qualified_column_ref("l3", "l_receiptdate")),
                right: Box::new(qualified_column_ref("l3", "l_commitdate")),
            }),
        }),
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    stmt.where_clause = Some(Expression::Exists {
        subquery: Box::new(not_exists_subquery),
        negated: true, // NOT EXISTS
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created an ANTI JOIN
    assert!(
        transformed.where_clause.is_none(),
        "NOT EXISTS should be fully transformed, no WHERE clause should remain"
    );

    match &transformed.from {
        Some(FromClause::Join { join_type, condition, right, .. }) => {
            assert!(
                matches!(join_type, JoinType::Anti),
                "NOT EXISTS should transform to ANTI join, got: {:?}",
                join_type
            );

            // Check that the right side has the rewritten alias for self-join
            // Self-joins get a unique alias like "__subquery_l3" to avoid conflicts
            match right.as_ref() {
                FromClause::Table { name, alias, .. } => {
                    assert_eq!(name, "lineitem", "Table name should be lineitem");
                    assert_eq!(
                        alias.as_deref(),
                        Some("__subquery_l3"),
                        "Self-join alias should be rewritten to __subquery_l3"
                    );
                }
                _ => panic!("Expected Table on right side of join"),
            }

            // Verify the join condition includes the correlation predicate
            // The condition should have column refs rewritten to use __subquery_l3
            assert!(condition.is_some(), "Join should have a condition");

            // Verify the condition contains rewritten column references
            if let Some(cond) = condition {
                fn contains_rewritten_l3_ref(expr: &Expression) -> bool {
                    match expr {
                        Expression::ColumnRef(col_id)
                            if col_id.schema_canonical().is_none()
                                && col_id.table_canonical().is_some() =>
                        {
                            col_id.table_canonical().unwrap() == "__subquery_l3"
                        }
                        Expression::BinaryOp { left, right, .. } => {
                            contains_rewritten_l3_ref(left) || contains_rewritten_l3_ref(right)
                        }
                        _ => false,
                    }
                }

                assert!(
                    contains_rewritten_l3_ref(cond),
                    "Join condition should have column refs rewritten to __subquery_l3. Condition: {:?}",
                    cond
                );
            }
        }
        _ => panic!("Expected ANTI JOIN in FROM clause"),
    }
}

// =============================================================================
// Tests for Expression::Conjunction handling (arena parser output)
// =============================================================================
// The arena parser produces Expression::Conjunction for AND chains instead of
// nested BinaryOp::And. These tests verify the optimizer handles both forms.

#[test]
fn test_conjunction_exists_to_semi_join() {
    // Test EXISTS inside a Conjunction (arena parser output for TPC-H Q4 pattern)
    // WHERE o_orderdate >= '1993-07-01' AND o_orderdate < '1993-10-01' AND EXISTS (...)
    let outer_from = simple_table_from("orders");
    let mut stmt = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Wildcard { alias: None }],
        into_table: None,
        into_variables: None,
        from: Some(outer_from),
        where_clause: None,
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    // Create correlated EXISTS subquery
    let exists_subquery = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Expression {
            expr: Expression::Literal(vibesql_types::SqlValue::Integer(1)),
            alias: None,
            source_text: None,
        }],
        into_table: None,
        into_variables: None,
        from: Some(simple_table_from("lineitem")),
        where_clause: Some(Expression::BinaryOp {
            op: BinaryOperator::Equal,
            left: Box::new(column_ref("l_orderkey")),
            right: Box::new(column_ref("o_orderkey")),
        }),
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    // Build Conjunction: [predicate1, predicate2, EXISTS(...)]
    // This is what the arena parser produces for: pred1 AND pred2 AND EXISTS(...)
    let predicate1 = Expression::BinaryOp {
        op: BinaryOperator::GreaterThanOrEqual,
        left: Box::new(column_ref("o_orderdate")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Varchar("1993-07-01".into()))),
    };
    let predicate2 = Expression::BinaryOp {
        op: BinaryOperator::LessThan,
        left: Box::new(column_ref("o_orderdate")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Varchar("1993-10-01".into()))),
    };
    let exists_expr = Expression::Exists { subquery: Box::new(exists_subquery), negated: false };

    stmt.where_clause = Some(Expression::Conjunction(vec![predicate1, predicate2, exists_expr]));

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created a SEMI JOIN
    match &transformed.from {
        Some(FromClause::Join { join_type, .. }) => {
            assert!(
                matches!(join_type, JoinType::Semi),
                "EXISTS in Conjunction should transform to SEMI join, got: {:?}",
                join_type
            );
        }
        _ => panic!("Expected SEMI JOIN in FROM clause"),
    }

    // Remaining WHERE should be a Conjunction with the two predicates
    assert!(transformed.where_clause.is_some(), "Other predicates should remain in WHERE clause");
    match &transformed.where_clause {
        Some(Expression::Conjunction(children)) => {
            assert_eq!(children.len(), 2, "Should have 2 remaining predicates");
        }
        Some(Expression::BinaryOp { .. }) => {
            // Also acceptable if there's only one predicate left after further transformations
        }
        other => panic!("Expected Conjunction or BinaryOp in WHERE, got: {:?}", other),
    }
}

#[test]
fn test_conjunction_not_exists_to_anti_join() {
    // Test NOT EXISTS inside a Conjunction
    let outer_from = simple_table_from("orders");
    let mut stmt = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Wildcard { alias: None }],
        into_table: None,
        into_variables: None,
        from: Some(outer_from),
        where_clause: None,
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    // Create correlated NOT EXISTS subquery
    let not_exists_subquery = SelectStmt {
        with_clause: None,
        distinct: false,
        select_list: vec![SelectItem::Expression {
            expr: Expression::Literal(vibesql_types::SqlValue::Integer(1)),
            alias: None,
            source_text: None,
        }],
        into_table: None,
        into_variables: None,
        from: Some(simple_table_from("lineitem")),
        where_clause: Some(Expression::BinaryOp {
            op: BinaryOperator::Equal,
            left: Box::new(column_ref("l_orderkey")),
            right: Box::new(column_ref("o_orderkey")),
        }),
        group_by: None,
        having: None,
        window_definitions: None,
        order_by: None,
        limit: None,
        offset: None,
        set_operation: None,
        values: None,
    };

    // Build Conjunction with NOT EXISTS
    let predicate = Expression::BinaryOp {
        op: BinaryOperator::Equal,
        left: Box::new(column_ref("o_orderstatus")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Varchar("F".into()))),
    };
    let not_exists_expr =
        Expression::Exists { subquery: Box::new(not_exists_subquery), negated: true };

    stmt.where_clause = Some(Expression::Conjunction(vec![predicate, not_exists_expr]));

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created an ANTI JOIN
    match &transformed.from {
        Some(FromClause::Join { join_type, .. }) => {
            assert!(
                matches!(join_type, JoinType::Anti),
                "NOT EXISTS in Conjunction should transform to ANTI join, got: {:?}",
                join_type
            );
        }
        _ => panic!("Expected ANTI JOIN in FROM clause"),
    }

    // Remaining WHERE should have the single predicate (not a Conjunction anymore)
    assert!(transformed.where_clause.is_some(), "Other predicate should remain in WHERE clause");
}

#[test]
fn test_conjunction_in_to_semi_join() {
    // Test IN subquery inside a Conjunction
    let mut stmt = simple_select("orders", "o_orderkey");
    let subquery = simple_select("lineitem", "l_orderkey");

    // Build Conjunction: [predicate, IN(...)]
    let predicate = Expression::BinaryOp {
        op: BinaryOperator::GreaterThan,
        left: Box::new(column_ref("o_totalprice")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Integer(1000))),
    };
    let in_expr = Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery),
        negated: false,
    };

    stmt.where_clause = Some(Expression::Conjunction(vec![predicate, in_expr]));

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created a SEMI JOIN
    match &transformed.from {
        Some(FromClause::Join { join_type, .. }) => {
            assert!(
                matches!(join_type, JoinType::Semi),
                "IN in Conjunction should transform to SEMI join, got: {:?}",
                join_type
            );
        }
        _ => panic!("Expected SEMI JOIN in FROM clause"),
    }

    // Remaining WHERE should have just the predicate
    assert!(transformed.where_clause.is_some(), "Other predicate should remain in WHERE clause");
}

#[test]
fn test_conjunction_not_in_to_anti_join() {
    // Test NOT IN subquery inside a Conjunction
    let mut stmt = simple_select("orders", "o_orderkey");
    let subquery = simple_select("lineitem", "l_orderkey");

    // Build Conjunction: [predicate, NOT IN(...)]
    let predicate = Expression::BinaryOp {
        op: BinaryOperator::Equal,
        left: Box::new(column_ref("o_orderstatus")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Varchar("O".into()))),
    };
    let not_in_expr = Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery),
        negated: true,
    };

    stmt.where_clause = Some(Expression::Conjunction(vec![predicate, not_in_expr]));

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created an ANTI JOIN
    match &transformed.from {
        Some(FromClause::Join { join_type, .. }) => {
            assert!(
                matches!(join_type, JoinType::Anti),
                "NOT IN in Conjunction should transform to ANTI join, got: {:?}",
                join_type
            );
        }
        _ => panic!("Expected ANTI JOIN in FROM clause"),
    }

    // Remaining WHERE should have just the predicate
    assert!(transformed.where_clause.is_some(), "Other predicate should remain in WHERE clause");
}

#[test]
fn test_conjunction_preserves_all_other_predicates() {
    // Test that all non-subquery predicates are preserved in the residual WHERE
    let mut stmt = simple_select("orders", "o_orderkey");
    let subquery = simple_select("lineitem", "l_orderkey");

    // Build Conjunction with multiple predicates and one subquery
    let pred1 = Expression::BinaryOp {
        op: BinaryOperator::GreaterThan,
        left: Box::new(column_ref("o_totalprice")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Integer(1000))),
    };
    let pred2 = Expression::BinaryOp {
        op: BinaryOperator::Equal,
        left: Box::new(column_ref("o_orderstatus")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Varchar("F".into()))),
    };
    let pred3 = Expression::BinaryOp {
        op: BinaryOperator::LessThan,
        left: Box::new(column_ref("o_orderdate")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Varchar("1995-01-01".into()))),
    };
    let in_expr = Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery),
        negated: false,
    };

    stmt.where_clause =
        Some(Expression::Conjunction(vec![pred1.clone(), pred2.clone(), pred3.clone(), in_expr]));

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have created a SEMI JOIN
    match &transformed.from {
        Some(FromClause::Join { join_type, .. }) => {
            assert!(matches!(join_type, JoinType::Semi), "IN should transform to SEMI join");
        }
        _ => panic!("Expected SEMI JOIN in FROM clause"),
    }

    // Remaining WHERE should be a Conjunction with exactly 3 predicates
    match &transformed.where_clause {
        Some(Expression::Conjunction(children)) => {
            assert_eq!(children.len(), 3, "Should have all 3 non-subquery predicates remaining");
        }
        _ => panic!("Expected Conjunction with 3 predicates in WHERE clause"),
    }
}

#[test]
fn test_conjunction_multiple_subqueries_iterative() {
    // Test that multiple subqueries in a Conjunction are handled iteratively
    let mut stmt = simple_select("orders", "o_orderkey");
    let subquery1 = simple_select("lineitem", "l_orderkey");
    let subquery2 = simple_select("supplier", "s_suppkey");

    // Build Conjunction: [predicate, IN(...), NOT IN(...)]
    let predicate = Expression::BinaryOp {
        op: BinaryOperator::Equal,
        left: Box::new(column_ref("o_orderstatus")),
        right: Box::new(Expression::Literal(vibesql_types::SqlValue::Varchar("F".into()))),
    };
    let in_expr = Expression::In {
        expr: Box::new(column_ref("o_orderkey")),
        subquery: Box::new(subquery1),
        negated: false,
    };
    let not_in_expr = Expression::In {
        expr: Box::new(column_ref("o_custkey")),
        subquery: Box::new(subquery2),
        negated: true,
    };

    stmt.where_clause = Some(Expression::Conjunction(vec![predicate, in_expr, not_in_expr]));

    let transformed = transform_subqueries_to_joins(&stmt);

    // Should have two joins (nested)
    match &transformed.from {
        Some(FromClause::Join { left: inner_join_box, join_type: outer_type, .. }) => {
            assert!(
                matches!(outer_type, JoinType::Semi | JoinType::Anti),
                "Outer join should be SEMI or ANTI"
            );

            match inner_join_box.as_ref() {
                FromClause::Join { join_type: inner_type, .. } => {
                    assert!(
                        matches!(inner_type, JoinType::Semi | JoinType::Anti),
                        "Inner join should be SEMI or ANTI"
                    );
                }
                _ => panic!("Expected nested JOIN"),
            }
        }
        _ => panic!("Expected JOIN in FROM clause"),
    }

    // Remaining WHERE should have just the one predicate
    assert!(transformed.where_clause.is_some(), "Simple predicate should remain in WHERE clause");
}

// =============================================================================
// Window function guard tests (issue #5231)
//
// Window functions in an IN-subquery's SELECT list are computed over the
// subquery's entire result set, so they cannot be hoisted into a per-row
// join ON condition. The transform must be skipped so evaluation falls back
// to row-by-row IN evaluation (eval_in_subquery), which handles both
// correlated and uncorrelated forms correctly.
// =============================================================================

fn row_number_over(order_by_column: &str) -> Expression {
    Expression::WindowFunction {
        function: vibesql_ast::WindowFunctionSpec::Ranking {
            name: vibesql_ast::FunctionIdentifier::new("row_number"),
            args: vec![],
        },
        over: vibesql_ast::WindowSpec {
            base_window_name: None,
            partition_by: None,
            order_by: Some(vec![vibesql_ast::OrderByItem {
                expr: column_ref(order_by_column),
                direction: vibesql_ast::OrderDirection::Asc,
                nulls_order: None,
            }]),
            frame: None,
        },
    }
}

/// Build a single-item SELECT over `table` whose select expression is `expr`
fn select_with_expr(table: &str, expr: Expression) -> SelectStmt {
    let mut stmt = simple_select(table, "placeholder");
    stmt.select_list =
        vec![SelectItem::Expression { expr, alias: None, source_text: None }];
    stmt
}

#[test]
fn test_in_subquery_with_window_function_not_transformed() {
    // SELECT t1_id FROM t1 WHERE t1_id IN (SELECT row_number() OVER (ORDER BY t3_id) FROM t3)
    let mut stmt = simple_select("t1", "t1_id");
    let subquery = select_with_expr("t3", row_number_over("t3_id"));

    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("t1_id")),
        subquery: Box::new(subquery),
        negated: false,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    // The transform must be skipped: WHERE clause keeps the IN subquery and
    // the FROM clause stays a plain table scan.
    assert!(
        matches!(transformed.where_clause, Some(Expression::In { .. })),
        "IN subquery with window function must not be transformed"
    );
    assert!(
        matches!(transformed.from, Some(FromClause::Table { .. })),
        "FROM clause must remain a plain table (no semi-join)"
    );
}

#[test]
fn test_in_subquery_with_nested_window_function_not_transformed() {
    // SELECT t1_id FROM t1 WHERE t1_id IN
    //   (SELECT t1_id + row_number() OVER (ORDER BY t1_id) FROM t3)
    // The window function is nested inside a binary expression and the
    // ORDER BY references a correlated outer column.
    let mut stmt = simple_select("t1", "t1_id");
    let nested = Expression::BinaryOp {
        op: BinaryOperator::Plus,
        left: Box::new(column_ref("t1_id")),
        right: Box::new(row_number_over("t1_id")),
    };
    let subquery = select_with_expr("t3", nested);

    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("t1_id")),
        subquery: Box::new(subquery),
        negated: false,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    assert!(
        matches!(transformed.where_clause, Some(Expression::In { .. })),
        "IN subquery with nested window function must not be transformed"
    );
    assert!(
        matches!(transformed.from, Some(FromClause::Table { .. })),
        "FROM clause must remain a plain table (no semi-join)"
    );
}

#[test]
fn test_not_in_subquery_with_window_function_not_transformed() {
    // NOT IN goes through the same code path (ANTI join) and must share the guard.
    let mut stmt = simple_select("t1", "t1_id");
    let subquery = select_with_expr("t3", row_number_over("t3_id"));

    stmt.where_clause = Some(Expression::In {
        expr: Box::new(column_ref("t1_id")),
        subquery: Box::new(subquery),
        negated: true,
    });

    let transformed = transform_subqueries_to_joins(&stmt);

    assert!(
        matches!(transformed.where_clause, Some(Expression::In { negated: true, .. })),
        "NOT IN subquery with window function must not be transformed"
    );
    assert!(
        matches!(transformed.from, Some(FromClause::Table { .. })),
        "FROM clause must remain a plain table (no anti-join)"
    );
}