activecube-rs 0.1.16

A generic GraphQL-to-SQL OLAP query engine library
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
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
use std::collections::HashMap;

use crate::compiler::ir::*;
use crate::compiler::ir::{CompileResult, JoinType};
use crate::sql::dialect::SqlDialect;

pub struct ClickHouseDialect;

impl ClickHouseDialect {
    pub fn new() -> Self {
        Self
    }
}

impl Default for ClickHouseDialect {
    fn default() -> Self {
        Self::new()
    }
}

impl SqlDialect for ClickHouseDialect {
    fn compile(&self, ir: &QueryIR) -> CompileResult {
        if let Some(ref builder) = ir.custom_query_builder {
            return (builder.0)(ir);
        }

        let mut bindings = Vec::new();
        let mut alias_remap: Vec<(String, String)> = Vec::new();

        if ir.joins.is_empty() {
            let inner_sql = self.compile_inner(ir, &mut bindings, &mut alias_remap);
            return CompileResult { sql: inner_sql, bindings, alias_remap };
        }

        // When JOINs are present, force aliases on function expression columns
        // (e.g. `argMaxMerge(latest_balance)`) so the outer SELECT can reference
        // them by simple identifier names. Without aliases, `_main.argMaxMerge(x)`
        // is parsed as a function call — a ClickHouse syntax error.
        let mut ir_mod = ir.clone();
        let mut mc_counter = 0u32;
        for sel in &mut ir_mod.selects {
            if let SelectExpr::Column { column, alias } = sel {
                if column.contains('(') && alias.is_none() {
                    let a = format!("__mc_{mc_counter}");
                    mc_counter += 1;
                    alias_remap.push((a.clone(), column.clone()));
                    *alias = Some(a);
                }
            }
        }

        let mut jc_counter = 0u32;
        for join in &mut ir_mod.joins {
            for sel in &mut join.selects {
                if let SelectExpr::Column { column, alias } = sel {
                    if column.contains('(') && alias.is_none() {
                        let a = format!("__jc_{jc_counter}");
                        jc_counter += 1;
                        *alias = Some(a);
                    }
                }
            }
        }

        let inner_sql = self.compile_inner(&ir_mod, &mut bindings, &mut alias_remap);

        // Build outer SELECT with explicit column listing.
        // Always backtick-quote to prevent ClickHouse auto-prefixing ambiguous
        // names when multiple JOINed tables share column names.
        let main_cols: Vec<String> = ir_mod.selects.iter().map(|s| {
            let col = match s {
                SelectExpr::Column { column, alias } => alias.as_ref().unwrap_or(column).clone(),
                SelectExpr::Aggregate { alias, .. } | SelectExpr::DimAggregate { alias, .. } => alias.clone(),
            };
            format!("_main.`{}` AS `{}`", col, col)
        }).collect();

        let mut sql = String::from("SELECT ");
        sql.push_str(&main_cols.join(", "));

        // Collect joined column aliases for the outer SELECT
        for join in &ir_mod.joins {
            for sel in &join.selects {
                let col_name = match sel {
                    SelectExpr::Column { column, alias } => alias.as_ref().unwrap_or(column).clone(),
                    SelectExpr::Aggregate { alias, .. } | SelectExpr::DimAggregate { alias, .. } => alias.clone(),
                };
                let outer_alias = format!("{}.{}", join.alias, col_name);
                if let SelectExpr::Column { column, alias: Some(_) } = sel {
                    if column.contains('(') {
                        let outer_original = format!("{}.{}", join.alias, column);
                        alias_remap.push((outer_alias.clone(), outer_original));
                    }
                }
                sql.push_str(&format!(", {}.`{}` AS `{}`",
                    join.alias, col_name, outer_alias));
            }
        }

        sql.push_str(&format!(" FROM ({}) AS _main", inner_sql));

        // Build a lookup for main query aliases to resolve ON condition columns
        // that might be function expressions (aliased above).
        let main_alias_map: HashMap<String, String> = ir_mod.selects.iter()
            .filter_map(|s| {
                if let SelectExpr::Column { column, alias: Some(a) } = s {
                    if column.contains('(') { Some((column.clone(), a.clone())) } else { None }
                } else { None }
            })
            .collect();

        for join in &ir_mod.joins {
            let join_kw = join.join_type.sql_keyword();

            if join.is_aggregate {
                // Mode B: subquery JOIN for AggregatingMergeTree targets
                sql.push_str(&format!(" {} (SELECT ", join_kw));
                let mut sub_parts: Vec<String> = Vec::new();
                for gb_col in &join.group_by {
                    sub_parts.push(quote_col(gb_col));
                }
                for sel in &join.selects {
                    match sel {
                        SelectExpr::Column { column, alias } => {
                            let col = if column.contains('(') { column.clone() } else { format!("`{column}`") };
                            if let Some(a) = alias {
                                sub_parts.push(format!("{col} AS `{a}`"));
                            } else if column.contains('(') || !join.group_by.contains(column) {
                                sub_parts.push(col);
                            }
                        }
                        SelectExpr::Aggregate { function, column, alias, condition } => {
                            let func = function.to_lowercase();
                            let qcol = quote_col(column);
                            let expr = match (func.as_str(), column.as_str(), condition) {
                                ("count", "*", None) => format!("count() AS `{alias}`"),
                                ("count", "*", Some(cond)) => format!("countIf({cond}) AS `{alias}`"),
                                ("count", _, None) => format!("uniqExact({qcol}) AS `{alias}`"),
                                ("count", _, Some(cond)) => format!("uniqExactIf({qcol}, {cond}) AS `{alias}`"),
                                ("uniq", _, None) => format!("uniq({qcol}) AS `{alias}`"),
                                ("uniq", _, Some(cond)) => format!("uniqIf({qcol}, {cond}) AS `{alias}`"),
                                (f, _, None) => format!("{f}({qcol}) AS `{alias}`"),
                                (f, _, Some(cond)) => format!("{f}If({qcol}, {cond}) AS `{alias}`"),
                            };
                            sub_parts.push(expr);
                        }
                        SelectExpr::DimAggregate { agg_type, value_column, compare_column, alias, condition } => {
                            let func = match agg_type {
                                DimAggType::ArgMax => "argMax",
                                DimAggType::ArgMin => "argMin",
                            };
                            let qv = quote_col(value_column);
                            let qc = quote_col(compare_column);
                            let expr = match condition {
                                None => format!("{func}({qv}, {qc}) AS `{alias}`"),
                                Some(cond) => format!("{func}If({qv}, {qc}, {cond}) AS `{alias}`"),
                            };
                            sub_parts.push(expr);
                        }
                    }
                }
                sql.push_str(&sub_parts.join(", "));
                sql.push_str(&format!(" FROM `{}`.`{}`", join.schema, join.table));
                if !join.group_by.is_empty() {
                    sql.push_str(" GROUP BY ");
                    let gb: Vec<String> = join.group_by.iter().map(|c| quote_col(c)).collect();
                    sql.push_str(&gb.join(", "));
                }
                sql.push_str(&format!(") AS {}", join.alias));
            } else {
                // Mode A: direct JOIN for MergeTree / ReplacingMergeTree targets
                sql.push_str(&format!(" {} `{}`.`{}` AS {}",
                    join_kw, join.schema, join.table, join.alias));
                if join.use_final {
                    sql.push_str(" FINAL");
                }
            }

            if join.join_type == JoinType::Cross {
                // CROSS JOIN has no ON clause
                continue;
            }

            // ON conditions — use alias if the local column was a func expression
            let on_parts: Vec<String> = join.conditions.iter().map(|(local, remote)| {
                let local_ref = main_alias_map.get(local).unwrap_or(local);
                format!("_main.`{}` = {}.`{}`", local_ref, join.alias, remote)
            }).collect();
            sql.push_str(" ON ");
            sql.push_str(&on_parts.join(" AND "));
        }

        CompileResult { sql, bindings, alias_remap }
    }

    fn quote_identifier(&self, name: &str) -> String {
        format!("`{name}`")
    }

    fn name(&self) -> &str {
        "ClickHouse"
    }
}

impl ClickHouseDialect {
    fn compile_inner(
        &self,
        ir: &QueryIR,
        bindings: &mut Vec<SqlValue>,
        alias_remap: &mut Vec<(String, String)>,
    ) -> String {
        let mut sql = String::new();

        let mut augmented_selects = ir.selects.clone();
        let mut agg_alias_map: HashMap<String, String> = HashMap::new();
        let mut alias_counter = 0u32;

        let having_cols: std::collections::HashSet<String> =
            collect_filter_columns(&ir.having).into_iter().collect();
        let has_having_agg = having_cols.iter().any(|c| c.contains('('));

        if has_having_agg {
            for sel in &mut augmented_selects {
                if let SelectExpr::Column { column, alias } = sel {
                    if column.contains('(') && having_cols.contains(column.as_str()) {
                        if alias.is_none() {
                            let a = format!("__f_{alias_counter}");
                            alias_counter += 1;
                            alias_remap.push((a.clone(), column.clone()));
                            agg_alias_map.insert(column.clone(), a.clone());
                            *alias = Some(a);
                        } else if let Some(existing) = alias {
                            agg_alias_map.insert(column.clone(), existing.clone());
                        }
                    }
                }
            }
            for col in &having_cols {
                if col.contains('(') && !agg_alias_map.contains_key(col.as_str()) {
                    let a = format!("__f_{alias_counter}");
                    alias_counter += 1;
                    agg_alias_map.insert(col.clone(), a.clone());
                    augmented_selects.push(SelectExpr::Column {
                        column: col.clone(),
                        alias: Some(a),
                    });
                }
            }
        }

        for sel in &augmented_selects {
            if let SelectExpr::DimAggregate { agg_type, value_column, compare_column, alias, .. } = sel {
                let func = match agg_type {
                    DimAggType::ArgMax => "argMax",
                    DimAggType::ArgMin => "argMin",
                };
                let qv = quote_col(value_column);
                let qc = quote_col(compare_column);
                let expr = format!("{func}({qv}, {qc})");
                agg_alias_map.insert(expr, alias.clone());
            }
        }

        sql.push_str("SELECT ");
        let select_parts: Vec<String> = augmented_selects.iter().map(|s| match s {
            SelectExpr::Column { column, alias } => {
                let col = if column.contains('(') { column.clone() } else { format!("`{column}`") };
                match alias {
                    Some(a) => format!("{col} AS `{a}`"),
                    None => col,
                }
            },
            SelectExpr::Aggregate { function, column, alias, condition } => {
                let func = function.to_uppercase();
                let qcol = quote_col(column);
                match (func.as_str(), column.as_str(), condition) {
                    ("COUNT", "*", None) => format!("count() AS `{alias}`"),
                    ("COUNT", "*", Some(cond)) => format!("countIf({cond}) AS `{alias}`"),
                    ("COUNT", _, None) => format!("uniqExact({qcol}) AS `{alias}`"),
                    ("COUNT", _, Some(cond)) => format!("uniqExactIf({qcol}, {cond}) AS `{alias}`"),
                    ("UNIQ", _, None) => format!("uniq({qcol}) AS `{alias}`"),
                    ("UNIQ", _, Some(cond)) => format!("uniqIf({qcol}, {cond}) AS `{alias}`"),
                    (_, _, None) => format!("{f}({qcol}) AS `{alias}`", f = func.to_lowercase()),
                    (_, _, Some(cond)) => format!("{f}If({qcol}, {cond}) AS `{alias}`", f = func.to_lowercase()),
                }
            }
            SelectExpr::DimAggregate { agg_type, value_column, compare_column, alias, condition } => {
                let func = match agg_type {
                    DimAggType::ArgMax => "argMax",
                    DimAggType::ArgMin => "argMin",
                };
                let qv = quote_col(value_column);
                let qc = quote_col(compare_column);
                match condition {
                    None => format!("{func}({qv}, {qc}) AS `{alias}`"),
                    Some(cond) => format!("{func}If({qv}, {qc}, {cond}) AS `{alias}`"),
                }
            }
        }).collect();
        sql.push_str(&select_parts.join(", "));

        if let Some(ref subquery) = ir.from_subquery {
            sql.push_str(&format!(" FROM ({}) AS _t", subquery));
        } else {
            sql.push_str(&format!(" FROM `{}`.`{}`", ir.schema, ir.table));
            if ir.use_final {
                sql.push_str(" FINAL");
            }
        }

        let where_clause = compile_filter(&ir.filters, bindings);
        if !where_clause.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&where_clause);
        }

        let effective_group_by = if !ir.group_by.is_empty() {
            ir.group_by.clone()
        } else {
            let has_agg_cols = augmented_selects.iter().any(|s| match s {
                SelectExpr::Column { column, .. } => column.contains("Merge("),
                SelectExpr::Aggregate { .. } | SelectExpr::DimAggregate { .. } => true,
            });
            if has_agg_cols {
                augmented_selects.iter().filter_map(|s| match s {
                    SelectExpr::Column { column, alias } if !contains_aggregate_expr(column) && alias.is_none() => {
                        Some(column.clone())
                    }
                    _ => None,
                }).collect()
            } else {
                vec![]
            }
        };

        if !effective_group_by.is_empty() {
            sql.push_str(" GROUP BY ");
            let cols: Vec<String> = effective_group_by.iter().map(|c| quote_col(c)).collect();
            sql.push_str(&cols.join(", "));
        }

        if has_having_agg {
            let having_clause = compile_filter_with_aliases(&ir.having, bindings, &agg_alias_map);
            if !having_clause.is_empty() {
                sql.push_str(" HAVING ");
                sql.push_str(&having_clause);
            }
        } else {
            let having_clause = compile_filter(&ir.having, bindings);
            if !having_clause.is_empty() {
                sql.push_str(" HAVING ");
                sql.push_str(&having_clause);
            }
        }

        if !ir.order_by.is_empty() {
            sql.push_str(" ORDER BY ");
            let parts: Vec<String> = ir.order_by.iter().map(|o| {
                let col = if o.column.contains('(') {
                    agg_alias_map.get(&o.column)
                        .map(|a| format!("`{a}`"))
                        .unwrap_or_else(|| o.column.clone())
                } else {
                    format!("`{}`", o.column)
                };
                let dir = if o.descending { "DESC" } else { "ASC" };
                format!("{col} {dir}")
            }).collect();
            sql.push_str(&parts.join(", "));
        }

        if let Some(ref lb) = ir.limit_by {
            let by_cols: Vec<String> = lb.columns.iter().map(|c| format!("`{c}`")).collect();
            sql.push_str(&format!(" LIMIT {} BY {}", lb.count, by_cols.join(", ")));
            if lb.offset > 0 {
                sql.push_str(&format!(" OFFSET {}", lb.offset));
            }
        }

        sql.push_str(&format!(" LIMIT {}", ir.limit));
        if ir.offset > 0 {
            sql.push_str(&format!(" OFFSET {}", ir.offset));
        }

        sql
    }
}

/// Collect all column names referenced in a filter tree.
fn collect_filter_columns(node: &FilterNode) -> Vec<String> {
    match node {
        FilterNode::Empty => vec![],
        FilterNode::Condition { column, .. } => vec![column.clone()],
        FilterNode::And(children) | FilterNode::Or(children) => {
            children.iter().flat_map(collect_filter_columns).collect()
        }
        FilterNode::ArrayIncludes { array_columns, .. } => array_columns.clone(),
    }
}

/// Like `compile_filter` but replaces aggregate expression columns with their
/// SELECT aliases so ClickHouse can resolve them in HAVING scope.
fn compile_filter_with_aliases(
    node: &FilterNode,
    bindings: &mut Vec<SqlValue>,
    aliases: &HashMap<String, String>,
) -> String {
    match node {
        FilterNode::Empty => String::new(),
        FilterNode::Condition { column, op, value } => {
            let effective_col = aliases.get(column)
                .map(|a| a.as_str())
                .unwrap_or(column.as_str());
            compile_condition(effective_col, op, value, bindings)
        }
        FilterNode::And(children) => {
            let parts: Vec<String> = children.iter()
                .map(|c| compile_filter_with_aliases(c, bindings, aliases))
                .filter(|s| !s.is_empty())
                .collect();
            match parts.len() {
                0 => String::new(),
                1 => parts.into_iter().next().unwrap(),
                _ => format!("({})", parts.join(" AND ")),
            }
        }
        FilterNode::Or(children) => {
            let parts: Vec<String> = children.iter()
                .map(|c| compile_filter_with_aliases(c, bindings, aliases))
                .filter(|s| !s.is_empty())
                .collect();
            match parts.len() {
                0 => String::new(),
                1 => parts.into_iter().next().unwrap(),
                _ => format!("({})", parts.join(" OR ")),
            }
        }
        FilterNode::ArrayIncludes { array_columns, element_conditions } => {
            compile_array_includes(array_columns, element_conditions, bindings)
        }
    }
}

fn compile_filter(node: &FilterNode, bindings: &mut Vec<SqlValue>) -> String {
    match node {
        FilterNode::Empty => String::new(),
        FilterNode::Condition { column, op, value } => {
            compile_condition(column, op, value, bindings)
        }
        FilterNode::And(children) => {
            let parts: Vec<String> = children.iter()
                .map(|c| compile_filter(c, bindings))
                .filter(|s| !s.is_empty())
                .collect();
            match parts.len() {
                0 => String::new(),
                1 => parts.into_iter().next().unwrap(),
                _ => format!("({})", parts.join(" AND ")),
            }
        }
        FilterNode::Or(children) => {
            let parts: Vec<String> = children.iter()
                .map(|c| compile_filter(c, bindings))
                .filter(|s| !s.is_empty())
                .collect();
            match parts.len() {
                0 => String::new(),
                1 => parts.into_iter().next().unwrap(),
                _ => format!("({})", parts.join(" OR ")),
            }
        }
        FilterNode::ArrayIncludes { array_columns, element_conditions } => {
            compile_array_includes(array_columns, element_conditions, bindings)
        }
    }
}

/// Compile ArrayIncludes into one or more `arrayExists(lambda, arrays)` expressions.
fn compile_array_includes(
    array_columns: &[String],
    element_conditions: &[Vec<FilterNode>],
    bindings: &mut Vec<SqlValue>,
) -> String {
    let params: Vec<String> = (0..array_columns.len())
        .map(|i| format!("_p{i}"))
        .collect();
    let arrays_sql: Vec<String> = array_columns.iter()
        .map(|c| quote_col(c))
        .collect();
    let arrays_ref = arrays_sql.join(", ");
    let params_ref = params.join(", ");

    let col_to_param: std::collections::HashMap<&str, &str> = array_columns.iter()
        .zip(params.iter())
        .map(|(c, p)| (c.as_str(), p.as_str()))
        .collect();

    let exists_parts: Vec<String> = element_conditions.iter().map(|conds| {
        let cond_parts: Vec<String> = conds.iter()
            .map(|c| compile_filter_with_param_remap(c, bindings, &col_to_param))
            .filter(|s| !s.is_empty())
            .collect();
        let cond_sql = match cond_parts.len() {
            0 => "1".to_string(),
            1 => cond_parts.into_iter().next().unwrap(),
            _ => format!("({})", cond_parts.join(" AND ")),
        };
        format!("arrayExists(({params_ref}) -> {cond_sql}, {arrays_ref})")
    }).collect();

    match exists_parts.len() {
        0 => String::new(),
        1 => exists_parts.into_iter().next().unwrap(),
        _ => format!("({})", exists_parts.join(" AND ")),
    }
}

/// Compile a filter node but remap column names to lambda parameter names.
/// Lambda parameters must NOT be backtick-quoted in ClickHouse.
fn compile_filter_with_param_remap(
    node: &FilterNode,
    bindings: &mut Vec<SqlValue>,
    col_to_param: &std::collections::HashMap<&str, &str>,
) -> String {
    match node {
        FilterNode::Empty => String::new(),
        FilterNode::Condition { column, op, value } => {
            if let Some(&param) = col_to_param.get(column.as_str()) {
                compile_condition_raw(param, op, value, bindings)
            } else {
                compile_condition(column, op, value, bindings)
            }
        }
        FilterNode::And(children) => {
            let parts: Vec<String> = children.iter()
                .map(|c| compile_filter_with_param_remap(c, bindings, col_to_param))
                .filter(|s| !s.is_empty())
                .collect();
            match parts.len() {
                0 => String::new(),
                1 => parts.into_iter().next().unwrap(),
                _ => format!("({})", parts.join(" AND ")),
            }
        }
        FilterNode::Or(children) => {
            let parts: Vec<String> = children.iter()
                .map(|c| compile_filter_with_param_remap(c, bindings, col_to_param))
                .filter(|s| !s.is_empty())
                .collect();
            match parts.len() {
                0 => String::new(),
                1 => parts.into_iter().next().unwrap(),
                _ => format!("({})", parts.join(" OR ")),
            }
        }
        FilterNode::ArrayIncludes { array_columns, element_conditions } => {
            compile_array_includes(array_columns, element_conditions, bindings)
        }
    }
}

fn quote_col(column: &str) -> String {
    if column.contains('(') {
        column.to_string()
    } else {
        format!("`{column}`")
    }
}

/// Like compile_condition but uses the column name as-is (no backtick quoting).
/// Used for lambda parameters in arrayExists.
fn compile_condition_raw(
    col: &str, op: &CompareOp, value: &SqlValue, bindings: &mut Vec<SqlValue>,
) -> String {
    compile_condition_inner(col, op, value, bindings)
}

fn compile_condition(
    column: &str, op: &CompareOp, value: &SqlValue, bindings: &mut Vec<SqlValue>,
) -> String {
    let col = quote_col(column);
    compile_condition_inner(&col, op, value, bindings)
}

fn compile_condition_inner(
    col: &str, op: &CompareOp, value: &SqlValue, bindings: &mut Vec<SqlValue>,
) -> String {
    match op {
        CompareOp::In | CompareOp::NotIn => {
            if let SqlValue::String(csv) = value {
                let items: Vec<&str> = csv.split(',').collect();
                let placeholders: Vec<&str> = items.iter().map(|_| "?").collect();
                for item in &items {
                    bindings.push(SqlValue::String(item.trim().to_string()));
                }
                format!("{col} {} ({})", op.sql_op(), placeholders.join(", "))
            } else {
                bindings.push(value.clone());
                format!("{col} {} (?)", op.sql_op())
            }
        }
        CompareOp::Includes | CompareOp::NotIncludes => {
            if let SqlValue::String(s) = value {
                bindings.push(SqlValue::String(format!("%{s}%")));
            } else {
                bindings.push(value.clone());
            }
            format!("{col} {} ?", op.sql_op())
        }
        CompareOp::IlikeIncludes => {
            if let SqlValue::String(s) = value {
                bindings.push(SqlValue::String(format!("%{s}%")));
            } else {
                bindings.push(value.clone());
            }
            format!("{col} ilike ?")
        }
        CompareOp::NotIlikeIncludes => {
            if let SqlValue::String(s) = value {
                bindings.push(SqlValue::String(format!("%{s}%")));
            } else {
                bindings.push(value.clone());
            }
            format!("{col} NOT ilike ?")
        }
        CompareOp::StartsWith | CompareOp::IlikeStartsWith => {
            if let SqlValue::String(s) = value {
                bindings.push(SqlValue::String(format!("{s}%")));
            } else {
                bindings.push(value.clone());
            }
            format!("{col} {} ?", op.sql_op())
        }
        CompareOp::EndsWith => {
            if let SqlValue::String(s) = value {
                bindings.push(SqlValue::String(format!("%{s}")));
            } else {
                bindings.push(value.clone());
            }
            format!("{col} LIKE ?")
        }
        CompareOp::IsNull | CompareOp::IsNotNull => {
            format!("{col} {}", op.sql_op())
        }
        _ => {
            if let SqlValue::Expression(expr) = value {
                format!("{col} {} {expr}", op.sql_op())
            } else {
                bindings.push(value.clone());
                format!("{col} {} ?", op.sql_op())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ch() -> ClickHouseDialect { ClickHouseDialect::new() }

    #[test]
    fn test_simple_select() {
        let ir = QueryIR {
            cube: "DEXTrades".into(), schema: "default".into(),
            table: "dwd_dex_trades".into(),
            selects: vec![
                SelectExpr::Column { column: "tx_hash".into(), alias: None },
                SelectExpr::Column { column: "token_a_amount".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None,
            use_final: false,
            joins: vec![],
            custom_query_builder: None,
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert_eq!(r.sql, "SELECT `tx_hash`, `token_a_amount` FROM `default`.`dwd_dex_trades` LIMIT 10");
        assert!(r.bindings.is_empty());
    }

    #[test]
    fn test_final_keyword() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "tokens".into(),
            selects: vec![SelectExpr::Column { column: "id".into(), alias: None }],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None,
            use_final: true,
            joins: vec![],
            custom_query_builder: None,
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("FROM `db`.`tokens` FINAL"), "FINAL should be appended, got: {}", r.sql);
    }

    #[test]
    fn test_uniq_uses_native_function() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Aggregate { function: "UNIQ".into(), column: "wallet".into(), alias: "__uniq".into(), condition: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("uniq(`wallet`) AS `__uniq`"), "ClickHouse should use native uniq(), got: {}", r.sql);
    }

    #[test]
    fn test_count_star() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Aggregate { function: "COUNT".into(), column: "*".into(), alias: "__count".into(), condition: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("count() AS `__count`"), "ClickHouse should use count() not COUNT(*), got: {}", r.sql);
    }

    #[test]
    fn test_aggregate_lowercase() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Aggregate { function: "SUM".into(), column: "amount".into(), alias: "__sum".into(), condition: None },
                SelectExpr::Aggregate { function: "AVG".into(), column: "price".into(), alias: "__avg".into(), condition: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("sum(`amount`) AS `__sum`"), "ClickHouse functions should be lowercase, got: {}", r.sql);
        assert!(r.sql.contains("avg(`price`) AS `__avg`"), "got: {}", r.sql);
    }

    #[test]
    fn test_where_and_order() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![SelectExpr::Column { column: "id".into(), alias: None }],
            filters: FilterNode::And(vec![
                FilterNode::Condition { column: "chain_id".into(), op: CompareOp::Eq, value: SqlValue::Int(1) },
                FilterNode::Condition { column: "amount_usd".into(), op: CompareOp::Gt, value: SqlValue::Float(1000.0) },
            ]),
            having: FilterNode::Empty, group_by: vec![],
            order_by: vec![OrderExpr { column: "block_timestamp".into(), descending: true }],
            limit: 25, offset: 0,
            limit_by: None, use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("WHERE (`chain_id` = ? AND `amount_usd` > ?)"));
        assert!(r.sql.contains("ORDER BY `block_timestamp` DESC"));
        assert_eq!(r.bindings.len(), 2);
    }

    #[test]
    fn test_having_with_aggregate_expr() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Column { column: "token_address".into(), alias: None },
                SelectExpr::Aggregate { function: "SUM".into(), column: "amount_usd".into(), alias: "__sum".into(), condition: None },
            ],
            filters: FilterNode::Empty,
            having: FilterNode::Condition {
                column: "sum(`amount_usd`)".into(), op: CompareOp::Gt, value: SqlValue::Float(1000000.0),
            },
            group_by: vec!["token_address".into()], order_by: vec![], limit: 25, offset: 0,
            limit_by: None, use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("GROUP BY `token_address`"));
        assert!(r.sql.contains("HAVING `__f_0` > ?"), "expected alias in HAVING, got: {}", r.sql);
        assert!(r.sql.contains("sum(`amount_usd`) AS `__f_0`"), "expected alias in SELECT, got: {}", r.sql);
        assert_eq!(r.bindings.len(), 1);
    }

    #[test]
    fn test_having_appends_missing_agg_column() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Column { column: "pool_address".into(), alias: None },
                SelectExpr::Column { column: "argMaxMerge(latest_liquidity_usd_state)".into(), alias: None },
            ],
            filters: FilterNode::Empty,
            having: FilterNode::And(vec![
                FilterNode::Condition {
                    column: "argMaxMerge(latest_liquidity_usd_state)".into(),
                    op: CompareOp::Gt, value: SqlValue::Float(2.0),
                },
                FilterNode::Condition {
                    column: "argMaxMerge(latest_token_a_amount_state)".into(),
                    op: CompareOp::Gt, value: SqlValue::Float(3.0),
                },
            ]),
            group_by: vec!["pool_address".into()], order_by: vec![], limit: 25, offset: 0,
            limit_by: None, use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("argMaxMerge(latest_liquidity_usd_state) AS `__f_0`"),
            "existing HAVING col should be aliased, got: {}", r.sql);
        assert!(r.sql.contains("argMaxMerge(latest_token_a_amount_state) AS `__f_1`"),
            "missing agg col should be appended, got: {}", r.sql);
        assert!(r.sql.contains("HAVING (`__f_0` > ? AND `__f_1` > ?)"),
            "HAVING should use aliases, got: {}", r.sql);
        assert_eq!(r.bindings.len(), 2);
        assert_eq!(r.alias_remap.len(), 1);
        assert_eq!(r.alias_remap[0], ("__f_0".to_string(), "argMaxMerge(latest_liquidity_usd_state)".to_string()));
    }

    #[test]
    fn test_limit_by() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Column { column: "owner".into(), alias: None },
                SelectExpr::Column { column: "amount".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], 
            order_by: vec![OrderExpr { column: "amount".into(), descending: true }],
            limit: 100, offset: 0,
            limit_by: Some(LimitByExpr { count: 3, offset: 0, columns: vec!["owner".into()] }),
            use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        let sql = &r.sql;
        assert!(sql.contains("LIMIT 3 BY `owner`"), "LIMIT BY should be present, got: {sql}");
        assert!(sql.contains("ORDER BY `amount` DESC"), "ORDER BY should be present, got: {sql}");
        assert!(sql.contains("LIMIT 100"), "outer LIMIT should be present, got: {sql}");
        let order_by_pos = sql.find("ORDER BY").unwrap();
        let limit_by_pos = sql.find("LIMIT 3 BY").unwrap();
        let limit_pos = sql.rfind("LIMIT 100").unwrap();
        assert!(order_by_pos < limit_by_pos, "ORDER BY should come before LIMIT BY in ClickHouse");
        assert!(limit_by_pos < limit_pos, "LIMIT BY should come before outer LIMIT");
    }

    #[test]
    fn test_limit_by_with_offset() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![SelectExpr::Column { column: "id".into(), alias: None }],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: Some(LimitByExpr { count: 5, offset: 2, columns: vec!["token".into(), "wallet".into()] }),
            use_final: false, joins: vec![], custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("LIMIT 5 BY `token`, `wallet` OFFSET 2"), "multi-column LIMIT BY with offset, got: {}", r.sql);
    }

    #[test]
    fn test_join_direct() {
        let ir = QueryIR {
            cube: "DEXTrades".into(), schema: "dexes_dwd".into(),
            table: "sol_dex_trades".into(),
            selects: vec![
                SelectExpr::Column { column: "tx_hash".into(), alias: None },
                SelectExpr::Column { column: "buy_token_address".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 25, offset: 0,
            limit_by: None, use_final: false,
            joins: vec![JoinExpr {
                schema: "dexes_dim".into(), table: "sol_tokens".into(),
                alias: "_j0".into(),
                conditions: vec![("buy_token_address".into(), "token_address".into())],
                selects: vec![
                    SelectExpr::Column { column: "name".into(), alias: None },
                    SelectExpr::Column { column: "symbol".into(), alias: None },
                ],
                group_by: vec![], use_final: true, is_aggregate: false,
                target_cube: "TokenSearch".into(), join_field: "joinBuyToken".into(),
                join_type: JoinType::Left,
            }],
            custom_query_builder: None,
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("FROM (SELECT"), "main query should be wrapped, got: {}", r.sql);
        assert!(r.sql.contains("LEFT JOIN `dexes_dim`.`sol_tokens` AS _j0 FINAL"),
            "direct JOIN with FINAL after alias, got: {}", r.sql);
        assert!(r.sql.contains("_main.`buy_token_address` = _j0.`token_address`"),
            "ON condition, got: {}", r.sql);
        assert!(r.sql.contains("_j0.`name` AS `_j0.name`"), "joined col alias, got: {}", r.sql);
    }

    #[test]
    fn test_join_aggregate_subquery() {
        let ir = QueryIR {
            cube: "DEXTrades".into(), schema: "dexes_dwd".into(),
            table: "sol_dex_trades".into(),
            selects: vec![
                SelectExpr::Column { column: "tx_hash".into(), alias: None },
                SelectExpr::Column { column: "buy_token_address".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false,
            joins: vec![JoinExpr {
                schema: "dexes_dws".into(), table: "sol_token_market_cap".into(),
                alias: "_j0".into(),
                conditions: vec![("buy_token_address".into(), "token_address".into())],
                selects: vec![
                    SelectExpr::Column { column: "argMaxMerge(latest_market_cap_usd_state)".into(), alias: None },
                ],
                group_by: vec!["token_address".into()],
                use_final: false, is_aggregate: true,
                target_cube: "TokenMarketCap".into(), join_field: "joinBuyTokenMarketCap".into(),
                join_type: JoinType::Left,
            }],
            custom_query_builder: None,
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("LEFT JOIN (SELECT"), "aggregate should use subquery, got: {}", r.sql);
        assert!(r.sql.contains("GROUP BY `token_address`"), "subquery GROUP BY, got: {}", r.sql);
        assert!(r.sql.contains("FROM `dexes_dws`.`sol_token_market_cap`"), "subquery FROM, got: {}", r.sql);
        assert!(r.sql.contains("argMaxMerge(latest_market_cap_usd_state) AS `__jc_0`"),
            "join func expr should be aliased in subquery, got: {}", r.sql);
        assert!(r.sql.contains("_j0.`__jc_0` AS `_j0.__jc_0`"),
            "outer SELECT should use alias for join func col, got: {}", r.sql);
    }

    #[test]
    fn test_join_main_query_func_expression_columns() {
        let ir = QueryIR {
            cube: "TokenHolders".into(), schema: "dws".into(),
            table: "sol_token_holders".into(),
            selects: vec![
                SelectExpr::Column { column: "token".into(), alias: None },
                SelectExpr::Column { column: "holder".into(), alias: None },
                SelectExpr::Column { column: "argMaxMerge(latest_balance)".into(), alias: None },
                SelectExpr::Column { column: "argMaxMerge(latest_balance_usd)".into(), alias: None },
                SelectExpr::Column { column: "minMerge(first_seen)".into(), alias: None },
                SelectExpr::Column { column: "maxMerge(last_seen)".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![
                OrderExpr { column: "argMaxMerge(latest_balance_usd)".into(), descending: true },
            ],
            limit: 100, offset: 0,
            limit_by: None, use_final: false,
            joins: vec![JoinExpr {
                schema: "dim".into(), table: "sol_tokens".into(),
                alias: "_j0".into(),
                conditions: vec![("token".into(), "token_address".into())],
                selects: vec![
                    SelectExpr::Column { column: "name".into(), alias: None },
                    SelectExpr::Column { column: "symbol".into(), alias: None },
                ],
                group_by: vec![], use_final: true, is_aggregate: false,
                target_cube: "TokenSearch".into(), join_field: "joinToken".into(),
                join_type: JoinType::Left,
            }],
            custom_query_builder: None,
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        let sql = &r.sql;

        assert!(sql.contains("_main.`__mc_0` AS `__mc_0`"),
            "func expr should use alias __mc_0 in outer SELECT, got: {sql}");
        assert!(sql.contains("_main.`__mc_1` AS `__mc_1`"),
            "func expr should use alias __mc_1, got: {sql}");
        assert!(sql.contains("_main.`token` AS `token`"),
            "simple col should be backtick-quoted, got: {sql}");

        assert!(!sql.contains("_main.argMaxMerge("),
            "outer SELECT must NOT have bare _main.argMaxMerge(...), got: {sql}");

        assert!(sql.contains("argMaxMerge(latest_balance) AS `__mc_0`"),
            "inner query should alias func expr, got: {sql}");

        assert!(r.alias_remap.iter().any(|(a, o)| a == "__mc_0" && o == "argMaxMerge(latest_balance)"),
            "alias_remap should map __mc_0 → original, got: {:?}", r.alias_remap);
        assert!(r.alias_remap.iter().any(|(a, o)| a == "__mc_1" && o == "argMaxMerge(latest_balance_usd)"),
            "alias_remap should map __mc_1, got: {:?}", r.alias_remap);
    }

    #[test]
    fn test_join_inner_type() {
        let ir = QueryIR {
            cube: "DEXTrades".into(), schema: "dexes_dwd".into(),
            table: "sol_dex_trades".into(),
            selects: vec![
                SelectExpr::Column { column: "tx_hash".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false,
            joins: vec![JoinExpr {
                schema: "dexes_dim".into(), table: "sol_tokens".into(),
                alias: "_j0".into(),
                conditions: vec![("buy_token_address".into(), "token_address".into())],
                selects: vec![
                    SelectExpr::Column { column: "name".into(), alias: None },
                ],
                group_by: vec![], use_final: false, is_aggregate: false,
                target_cube: "TokenSearch".into(), join_field: "joinBuyToken".into(),
                join_type: JoinType::Inner,
            }],
            custom_query_builder: None,
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("INNER JOIN `dexes_dim`.`sol_tokens` AS _j0"),
            "should use INNER JOIN, got: {}", r.sql);
    }

    #[test]
    fn test_join_full_outer_type() {
        let ir = QueryIR {
            cube: "T".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Column { column: "id".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false,
            joins: vec![JoinExpr {
                schema: "db2".into(), table: "t2".into(),
                alias: "_j0".into(),
                conditions: vec![("id".into(), "ref_id".into())],
                selects: vec![
                    SelectExpr::Column { column: "val".into(), alias: None },
                ],
                group_by: vec![], use_final: false, is_aggregate: false,
                target_cube: "Other".into(), join_field: "joinOther".into(),
                join_type: JoinType::Full,
            }],
            custom_query_builder: None,
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("FULL OUTER JOIN `db2`.`t2` AS _j0"),
            "should use FULL OUTER JOIN, got: {}", r.sql);
    }

    #[test]
    fn test_custom_query_builder() {
        let ir = QueryIR {
            cube: "Custom".into(), schema: "db".into(), table: "t".into(),
            selects: vec![
                SelectExpr::Column { column: "id".into(), alias: None },
            ],
            filters: FilterNode::Empty, having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![],
            custom_query_builder: Some(QueryBuilderFn(std::sync::Arc::new(|_ir| {
                CompileResult {
                    sql: "SELECT 1 FROM custom_view".into(),
                    bindings: vec![],
                    alias_remap: vec![],
                }
            }))),
            from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert_eq!(r.sql, "SELECT 1 FROM custom_view",
            "custom builder should bypass standard compilation, got: {}", r.sql);
    }

    #[test]
    fn test_from_subquery() {
        let ir = QueryIR {
            cube: "DEXTradeByTokens".into(), schema: "dwd".into(),
            table: "sol_trades".into(),
            selects: vec![
                SelectExpr::Column { column: "amount".into(), alias: None },
                SelectExpr::Column { column: "side_type".into(), alias: None },
            ],
            filters: FilterNode::Condition {
                column: "token".into(), op: CompareOp::Eq,
                value: SqlValue::String("SOL".into()),
            },
            having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![],
            custom_query_builder: None,
            from_subquery: Some(
                "SELECT amount, 'buy' AS side_type, token FROM dwd.sol_a UNION ALL SELECT amount, 'sell' AS side_type, token FROM dwd.sol_b".into()
            ),
        };
        let r = ch().compile(&ir);
        assert!(r.sql.starts_with("SELECT `amount`, `side_type` FROM (SELECT"),
            "should use subquery in FROM, got: {}", r.sql);
        assert!(r.sql.contains("UNION ALL"),
            "subquery should contain UNION ALL, got: {}", r.sql);
        assert!(r.sql.contains(") AS _t"),
            "subquery should be aliased as _t, got: {}", r.sql);
        assert!(r.sql.contains("WHERE `token` = ?"),
            "WHERE clause should be applied to subquery result, got: {}", r.sql);
        assert!(!r.sql.contains("FROM `dwd`.`sol_trades`"),
            "should NOT use schema.table when from_subquery is set, got: {}", r.sql);
    }

    #[test]
    fn test_array_includes_single_condition() {
        let ir = QueryIR {
            cube: "Instructions".into(), schema: "dexes_dwd2".into(),
            table: "sol_instructions".into(),
            selects: vec![SelectExpr::Column { column: "tx_hash".into(), alias: None }],
            filters: FilterNode::ArrayIncludes {
                array_columns: vec![
                    "instruction_arg_names".into(),
                    "instruction_arg_types".into(),
                    "instruction_arg_values".into(),
                ],
                element_conditions: vec![vec![
                    FilterNode::Condition {
                        column: "instruction_arg_names".into(),
                        op: CompareOp::Eq,
                        value: SqlValue::String("amount_in".into()),
                    },
                ]],
            },
            having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![],
            custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("arrayExists((_p0, _p1, _p2) -> _p0 = ?"),
            "should generate arrayExists with lambda params, got: {}", r.sql);
        assert!(r.sql.contains("`instruction_arg_names`, `instruction_arg_types`, `instruction_arg_values`"),
            "should reference all parallel array columns, got: {}", r.sql);
        assert_eq!(r.bindings.len(), 1);
    }

    #[test]
    fn test_array_includes_multiple_conditions() {
        let ir = QueryIR {
            cube: "Instructions".into(), schema: "dexes_dwd2".into(),
            table: "sol_instructions".into(),
            selects: vec![SelectExpr::Column { column: "tx_hash".into(), alias: None }],
            filters: FilterNode::ArrayIncludes {
                array_columns: vec![
                    "instruction_arg_names".into(),
                    "instruction_arg_values".into(),
                ],
                element_conditions: vec![
                    vec![
                        FilterNode::Condition {
                            column: "instruction_arg_names".into(),
                            op: CompareOp::Eq,
                            value: SqlValue::String("amount_in".into()),
                        },
                        FilterNode::Condition {
                            column: "instruction_arg_values".into(),
                            op: CompareOp::Gt,
                            value: SqlValue::String("10000".into()),
                        },
                    ],
                    vec![
                        FilterNode::Condition {
                            column: "instruction_arg_names".into(),
                            op: CompareOp::Eq,
                            value: SqlValue::String("owner".into()),
                        },
                    ],
                ],
            },
            having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![],
            custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        let sql = &r.sql;
        let count = sql.matches("arrayExists").count();
        assert_eq!(count, 2, "should have two arrayExists calls (AND-ed), got: {sql}");
        assert!(sql.contains(" AND arrayExists("),
            "two arrayExists should be AND-ed, got: {sql}");
        assert_eq!(r.bindings.len(), 3);
    }

    #[test]
    fn test_array_includes_with_in_operator() {
        let ir = QueryIR {
            cube: "Instructions".into(), schema: "dexes_dwd2".into(),
            table: "sol_instructions".into(),
            selects: vec![SelectExpr::Column { column: "tx_hash".into(), alias: None }],
            filters: FilterNode::ArrayIncludes {
                array_columns: vec![
                    "instruction_arg_names".into(),
                    "instruction_arg_values".into(),
                ],
                element_conditions: vec![vec![
                    FilterNode::Condition {
                        column: "instruction_arg_names".into(),
                        op: CompareOp::Eq,
                        value: SqlValue::String("authorityType".into()),
                    },
                    FilterNode::Condition {
                        column: "instruction_arg_values".into(),
                        op: CompareOp::In,
                        value: SqlValue::String("0,1".into()),
                    },
                ]],
            },
            having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![],
            custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        assert!(r.sql.contains("arrayExists((_p0, _p1) -> (_p0 = ? AND _p1 IN (?, ?))"),
            "should generate arrayExists with AND-ed conditions, got: {}", r.sql);
        assert_eq!(r.bindings.len(), 3);
    }

    #[test]
    fn test_array_includes_combined_with_regular_filter() {
        let ir = QueryIR {
            cube: "Instructions".into(), schema: "dexes_dwd2".into(),
            table: "sol_instructions".into(),
            selects: vec![SelectExpr::Column { column: "tx_hash".into(), alias: None }],
            filters: FilterNode::And(vec![
                FilterNode::Condition {
                    column: "instruction_program_address".into(),
                    op: CompareOp::Eq,
                    value: SqlValue::String("pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA".into()),
                },
                FilterNode::ArrayIncludes {
                    array_columns: vec!["instruction_arg_names".into(), "instruction_arg_values".into()],
                    element_conditions: vec![vec![
                        FilterNode::Condition {
                            column: "instruction_arg_names".into(),
                            op: CompareOp::Eq,
                            value: SqlValue::String("amount".into()),
                        },
                    ]],
                },
            ]),
            having: FilterNode::Empty,
            group_by: vec![], order_by: vec![], limit: 10, offset: 0,
            limit_by: None, use_final: false, joins: vec![],
            custom_query_builder: None, from_subquery: None,
        };
        let r = ch().compile(&ir);
        let sql = &r.sql;
        assert!(sql.contains("`instruction_program_address` = ?"),
            "should have regular condition, got: {sql}");
        assert!(sql.contains("arrayExists("),
            "should have arrayExists, got: {sql}");
        assert!(sql.contains(" AND "),
            "regular + array conditions should be AND-ed, got: {sql}");
    }
}