udb 0.4.25

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
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
//! ClickHouse compiler (U2 step 6a).
//!
//! ClickHouse SQL is mostly Postgres-shaped but with three operational
//! differences the compiler has to honour:
//!
//! - **No native UPSERT.** `ON CONFLICT` doesn't exist; the common
//!   replacement is a `ReplacingMergeTree` table that dedupes on a sort
//!   key during background merges. The compiler rejects `Replace` /
//!   `Update` and points operators at the right engine choice.
//! - **DELETE is async.** Real-world ClickHouse uses `ALTER TABLE …
//!   DELETE WHERE …` (a mutation). This compiler emits that shape; the
//!   executor handles the optional `SYNC` setting.
//! - **Bind placeholders are `?`** by default in the HTTP interface (the
//!   executor already issues parameterised statements). We keep the IR
//!   contract identical to Postgres: render `?` placeholders and stash
//!   values in the `params` vec for positional binding.
//!
//! Schema / table comes from the manifest like every other compiler.

use crate::backend::BackendKind;
use crate::generation::ManifestTable;
use crate::ir::filter::{ComparisonOp, LogicalFilter};
use crate::ir::operations::{
    AggregateExpr, AggregateFunc, ConflictStrategy, LogicalAggregate, LogicalDelete, LogicalRead,
    LogicalResourceOp, LogicalSearch, LogicalWrite, ResourceKind, ResourceOpKind,
};
use crate::ir::value::LogicalValue;

use super::{CompileContext, CompileError, CompiledRendering, Compiler};

/// ClickHouse SQL compiler.
#[derive(Debug, Default, Clone, Copy)]
pub struct ClickHouseCompiler;

impl ClickHouseCompiler {
    fn resolve_table<'a>(
        &self,
        message_type: &str,
        ctx: &'a CompileContext<'_>,
    ) -> Result<&'a ManifestTable, CompileError> {
        match crate::broker::table_lookup(ctx.manifest, message_type) {
            crate::broker::TableLookup::Found(table) => Ok(table),
            // fix_plan §4.1: an ambiguous short name names its candidates so the
            // caller can FQN-qualify — never a silent first-wins misroute.
            crate::broker::TableLookup::Ambiguous { .. } => Err(CompileError::Malformed {
                reason: crate::broker::describe_table_lookup_miss(ctx.manifest, message_type),
            }),
            crate::broker::TableLookup::Missing => Err(CompileError::UnknownMessageType {
                message_type: message_type.to_string(),
            }),
        }
    }

    fn column_for<'a>(
        &self,
        table: &'a ManifestTable,
        field: &str,
        message_type: &str,
    ) -> Result<&'a str, CompileError> {
        table
            .columns
            .iter()
            .find(|c| c.field_name.eq_ignore_ascii_case(field) || c.column_name == field)
            .map(|c| c.column_name.as_str())
            .ok_or_else(|| CompileError::UnknownField {
                message_type: message_type.to_string(),
                field: field.to_string(),
            })
    }

    /// A2: detect whether the manifest's table declares a tenant_id
    /// column. ClickHouse has no native session-context (it can do
    /// per-user row policies, but those are operator-installed and
    /// not portable), so RLS is performed at the compiler layer:
    /// AND `_tenant_id = ?` (or `tenant_id` — whatever the
    /// manifest names it) into every read/delete/aggregate, and
    /// stamp on writes. Resolution policy is shared with the other
    /// compiler-layer-RLS backends — see `util::resolve_tenant_column`.
    fn tenant_column(table: &ManifestTable) -> Option<&str> {
        super::util::resolve_tenant_column(table)
    }

    fn project_column(table: &ManifestTable) -> Option<&str> {
        super::util::resolve_project_column(table)
    }

    /// A2: append tenant/project predicates to a WHERE body. Returns
    /// None when nothing to inject AND no existing body. Each
    /// injection adds one `?` placeholder to `params`. ClickHouse
    /// quotes identifiers with backticks.
    fn append_context_predicates(
        body: Option<String>,
        table: &ManifestTable,
        ctx: &CompileContext<'_>,
        params: &mut Vec<LogicalValue>,
    ) -> Option<String> {
        super::util::append_context_predicates(body, table, ctx, params, '`')
    }

    fn render_filter(
        &self,
        filter: &LogicalFilter,
        table: &ManifestTable,
        message_type: &str,
        params: &mut Vec<LogicalValue>,
    ) -> Result<String, CompileError> {
        match filter {
            LogicalFilter::And(c) if c.is_empty() => Ok("1=1".into()),
            LogicalFilter::Or(c) if c.is_empty() => Ok("1=0".into()),
            LogicalFilter::And(clauses) => {
                let parts = clauses
                    .iter()
                    .map(|c| self.render_filter(c, table, message_type, params))
                    .collect::<Result<Vec<_>, _>>()?;
                Ok(format!("({})", parts.join(" AND ")))
            }
            LogicalFilter::Or(clauses) => {
                let parts = clauses
                    .iter()
                    .map(|c| self.render_filter(c, table, message_type, params))
                    .collect::<Result<Vec<_>, _>>()?;
                Ok(format!("({})", parts.join(" OR ")))
            }
            LogicalFilter::Not(inner) => {
                let r = self.render_filter(inner, table, message_type, params)?;
                Ok(format!("(NOT {r})"))
            }
            LogicalFilter::IsNull(field) => {
                let c = self.column_for(table, field, message_type)?;
                // ClickHouse stores null as a distinct value type; isNull(c)
                // is the canonical check.
                Ok(format!("isNull(`{c}`)"))
            }
            LogicalFilter::InList { field, values } => {
                if values.is_empty() {
                    return Ok("1=0".into());
                }
                let c = self.column_for(table, field, message_type)?;
                let placeholders = values
                    .iter()
                    .map(|v| {
                        params.push(v.clone());
                        "?".to_string()
                    })
                    .collect::<Vec<_>>()
                    .join(", ");
                Ok(format!("`{c}` IN ({placeholders})"))
            }
            LogicalFilter::Comparison { field, op, value } => {
                let column = self.column_for(table, field, message_type)?;
                if value.is_null() {
                    return Err(CompileError::Malformed {
                        reason: format!(
                            "comparison with NULL on field '{field}' must use IsNull, not {}",
                            op.token()
                        ),
                    });
                }
                params.push(value.clone());
                let lhs = format!("`{column}`");
                let rendered = match op {
                    ComparisonOp::Eq => format!("{lhs} = ?"),
                    ComparisonOp::Ne => format!("{lhs} != ?"),
                    ComparisonOp::Lt => format!("{lhs} < ?"),
                    ComparisonOp::Le => format!("{lhs} <= ?"),
                    ComparisonOp::Gt => format!("{lhs} > ?"),
                    ComparisonOp::Ge => format!("{lhs} >= ?"),
                    // ClickHouse uses `LIKE` / `ILIKE` like SQL, plus
                    // `position()`/`startsWith`/`endsWith` for substring ops.
                    ComparisonOp::Like => format!("{lhs} LIKE ?"),
                    ComparisonOp::ILike => format!("lowerUTF8({lhs}) LIKE lowerUTF8(?)"),
                    ComparisonOp::Contains => format!("position({lhs}, ?) > 0"),
                    ComparisonOp::StartsWith => format!("startsWith({lhs}, ?)"),
                    ComparisonOp::EndsWith => format!("endsWith({lhs}, ?)"),
                };
                Ok(rendered)
            }
        }
    }
}

impl Compiler for ClickHouseCompiler {
    fn kind(&self) -> BackendKind {
        BackendKind::Clickhouse
    }

    fn compile_read(
        &self,
        op: &LogicalRead,
        ctx: &CompileContext<'_>,
    ) -> Result<CompiledRendering, CompileError> {
        let table = self.resolve_table(&op.message_type, ctx)?;
        let mut params: Vec<LogicalValue> = Vec::new();

        let select = match &op.projection {
            Some(p) if !p.is_select_all() => p
                .fields
                .iter()
                .map(|f| {
                    let c = self.column_for(table, f, &op.message_type)?;
                    Ok(format!("`{c}`"))
                })
                .collect::<Result<Vec<_>, CompileError>>()?
                .join(", "),
            _ => "*".to_string(),
        };

        // ClickHouse: `database`.`table`. Reuse manifest's `schema` as DB.
        let mut sql = format!(
            "SELECT {select} FROM `{db}`.`{table}`",
            db = table.schema,
            table = table.table,
        );

        let user_body: Option<String> = if let Some(filter) = &op.filter {
            let body = self.render_filter(filter, table, &op.message_type, &mut params)?;
            if body == "1=1" { None } else { Some(body) }
        } else {
            None
        };
        // A2: inject tenant/project predicates so cross-tenant reads
        // are impossible even if the user's filter omits them.
        if let Some(body) = Self::append_context_predicates(user_body, table, ctx, &mut params) {
            sql.push_str(&format!(" WHERE {body}"));
        }

        if !op.sort.is_empty() {
            let parts = op
                .sort
                .iter()
                .map(|s| {
                    let c = self.column_for(table, &s.field, &op.message_type)?;
                    let dir = s.direction.token().to_uppercase();
                    Ok(format!("`{c}` {dir}"))
                })
                .collect::<Result<Vec<_>, CompileError>>()?;
            sql.push_str(&format!(" ORDER BY {}", parts.join(", ")));
        }

        if let Some(pag) = &op.pagination {
            if pag.uses_cursor() {
                return Err(CompileError::OperatorUnsupported {
                    backend: BackendKind::Clickhouse,
                    op: "keyset_cursor",
                });
            }
            // ClickHouse: LIMIT n OFFSET m (or LIMIT m, n).
            if let Some(limit) = pag.limit {
                sql.push_str(&format!(" LIMIT {limit}"));
            }
            if let Some(offset) = pag.offset
                && offset > 0
            {
                sql.push_str(&format!(" OFFSET {offset}"));
            }
        }

        Ok(CompiledRendering::Sql {
            backend: BackendKind::Clickhouse,
            statement: sql,
            params,
        })
    }

    fn compile_write(
        &self,
        op: &LogicalWrite,
        ctx: &CompileContext<'_>,
    ) -> Result<CompiledRendering, CompileError> {
        if op.records.is_empty() {
            return Err(CompileError::Malformed {
                reason: "LogicalWrite::records must be non-empty".into(),
            });
        }
        // ClickHouse: no UPSERT in the SQL surface.
        match &op.conflict {
            ConflictStrategy::Error => {}
            ConflictStrategy::Ignore => {
                // The `IGNORE` keyword exists for INSERT but its semantics
                // (skip rows with type mismatches) aren't what callers want
                // here. Reject explicitly so they reach for the right tool.
                return Err(CompileError::OperatorUnsupported {
                    backend: BackendKind::Clickhouse,
                    op: "insert_ignore",
                });
            }
            ConflictStrategy::Replace | ConflictStrategy::Update { .. } => {
                return Err(CompileError::OperatorUnsupported {
                    backend: BackendKind::Clickhouse,
                    op: "upsert",
                });
            }
        }
        if !op.return_fields.is_empty() {
            return Err(CompileError::OperatorUnsupported {
                backend: BackendKind::Clickhouse,
                op: "returning",
            });
        }
        let table = self.resolve_table(&op.message_type, ctx)?;
        let mut params: Vec<LogicalValue> = Vec::new();

        // A2: clone records so we can stamp tenant_id / project_id
        // before computing the column set. ClickHouse INSERT is the
        // only place values land in the row, so stamping here
        // closes the write side of the RLS loop.
        let stamp_tenant = ctx
            .tenant_id
            .filter(|s| !s.is_empty())
            .and_then(|tid| Self::tenant_column(table).map(|col| (col, tid)));
        let stamp_project = ctx
            .project_id
            .filter(|s| !s.is_empty())
            .and_then(|pid| Self::project_column(table).map(|col| (col, pid)));
        let mut records: Vec<crate::ir::operations::LogicalRecord> = op.records.clone();
        for rec in records.iter_mut() {
            // Records are keyed by field names; a caller may supply the
            // tenant/project value under any field alias that maps to the
            // physical column. Resolve every existing key to its column so
            // we never stamp a column that is already present — stamping
            // by literal key would emit a duplicate column in the INSERT.
            let existing_cols: std::collections::HashSet<String> = rec
                .keys()
                .filter_map(|k| self.column_for(table, k, &op.message_type).ok())
                .map(|c| c.to_string())
                .collect();
            if let Some((col, tid)) = stamp_tenant
                && !existing_cols.contains(col)
            {
                rec.insert(col.to_string(), LogicalValue::String(tid.to_string()));
            }
            if let Some((col, pid)) = stamp_project
                && !existing_cols.contains(col)
            {
                rec.insert(col.to_string(), LogicalValue::String(pid.to_string()));
            }
        }

        let first = &records[0];
        let columns: Vec<&str> = first
            .keys()
            .map(|k| self.column_for(table, k, &op.message_type))
            .collect::<Result<Vec<_>, _>>()?;
        let column_list = columns
            .iter()
            .map(|c| format!("`{c}`"))
            .collect::<Vec<_>>()
            .join(", ");

        let mut value_rows = Vec::with_capacity(records.len());
        for (idx, record) in records.iter().enumerate() {
            if record.len() != first.len() || !first.keys().all(|k| record.contains_key(k)) {
                return Err(CompileError::Malformed {
                    reason: format!(
                        "record {idx} has different field set than record 0; \
                         all records in one LogicalWrite must share the same fields"
                    ),
                });
            }
            let row = first
                .keys()
                .map(|k| {
                    params.push(record[k].clone());
                    "?".to_string()
                })
                .collect::<Vec<_>>()
                .join(", ");
            value_rows.push(format!("({row})"));
        }

        let sql = format!(
            "INSERT INTO `{db}`.`{table}` ({column_list}) VALUES {values}",
            db = table.schema,
            table = table.table,
            values = value_rows.join(", "),
        );
        Ok(CompiledRendering::Sql {
            backend: BackendKind::Clickhouse,
            statement: sql,
            params,
        })
    }

    fn compile_delete(
        &self,
        op: &LogicalDelete,
        ctx: &CompileContext<'_>,
    ) -> Result<CompiledRendering, CompileError> {
        let table = self.resolve_table(&op.message_type, ctx)?;
        let mut params: Vec<LogicalValue> = Vec::new();
        let body = self.render_filter(&op.filter, table, &op.message_type, &mut params)?;
        if body == "1=1" || body == "1=0" {
            return Err(CompileError::Malformed {
                reason: "LogicalDelete::filter cannot be empty or false-constant".into(),
            });
        }
        if !op.return_fields.is_empty() {
            return Err(CompileError::OperatorUnsupported {
                backend: BackendKind::Clickhouse,
                op: "returning",
            });
        }
        // A2: AND tenant/project so a DELETE by id can't reach into
        // another tenant's rows.
        let final_body = Self::append_context_predicates(Some(body), table, ctx, &mut params)
            .expect("body was Some so result is Some");
        // `ALTER TABLE … DELETE` is the canonical mutation form; the
        // executor controls whether to set `mutations_sync=2`.
        let sql = format!(
            "ALTER TABLE `{db}`.`{table}` DELETE WHERE {body}",
            db = table.schema,
            table = table.table,
            body = final_body,
        );
        Ok(CompiledRendering::Sql {
            backend: BackendKind::Clickhouse,
            statement: sql,
            params,
        })
    }

    fn compile_aggregate(
        &self,
        op: &LogicalAggregate,
        ctx: &CompileContext<'_>,
    ) -> Result<CompiledRendering, CompileError> {
        if op.aggregates.is_empty() {
            return Err(CompileError::Malformed {
                reason: "LogicalAggregate::aggregates must be non-empty".into(),
            });
        }
        super::util::validate_aggregate_aliases(&op.aggregates)?;
        let table = self.resolve_table(&op.message_type, ctx)?;
        // #151: a GROUP BY column resolving to an aggregate alias name would emit
        // two identically-keyed result columns. Reject (SQL backends already do).
        let group_names: Vec<&str> = op
            .group_by
            .iter()
            .map(|f| self.column_for(table, f, &op.message_type))
            .collect::<Result<Vec<_>, _>>()?;
        super::util::validate_no_groupby_alias_collision(&group_names, &op.aggregates)?;
        let mut params: Vec<LogicalValue> = Vec::new();

        let mut select_parts: Vec<String> = Vec::new();
        let group_columns: Vec<String> = op
            .group_by
            .iter()
            .map(|f| {
                let col = self.column_for(table, f, &op.message_type)?;
                Ok::<_, CompileError>(format!("`{col}`"))
            })
            .collect::<Result<Vec<_>, _>>()?;
        for col in &group_columns {
            select_parts.push(col.clone());
        }
        for agg in &op.aggregates {
            select_parts.push(render_ch_aggregate(agg, table, &op.message_type)?);
        }
        let mut sql = format!(
            "SELECT {sel} FROM `{db}`.`{table}`",
            sel = select_parts.join(", "),
            db = table.schema,
            table = table.table,
        );

        let user_body: Option<String> = if let Some(filter) = &op.filter {
            let body = self.render_filter(filter, table, &op.message_type, &mut params)?;
            if body == "1=1" { None } else { Some(body) }
        } else {
            None
        };
        // A2: tenant/project predicates AND'd into aggregate WHERE.
        if let Some(body) = Self::append_context_predicates(user_body, table, ctx, &mut params) {
            sql.push_str(&format!(" WHERE {body}"));
        }

        if !group_columns.is_empty() {
            sql.push_str(&format!(" GROUP BY {}", group_columns.join(", ")));
        }

        if let Some(having) = &op.having {
            let body = render_ch_having(having, op, &mut params)?;
            sql.push_str(&format!(" HAVING {body}"));
        }

        if !op.sort.is_empty() {
            let parts = op
                .sort
                .iter()
                .map(|s| {
                    let token = resolve_ch_sort_field(&s.field, op, table)?;
                    let direction = s.direction.token().to_uppercase();
                    Ok::<_, CompileError>(format!("{token} {direction}"))
                })
                .collect::<Result<Vec<_>, _>>()?;
            sql.push_str(&format!(" ORDER BY {}", parts.join(", ")));
        }

        if let Some(pag) = &op.pagination {
            if pag.uses_cursor() {
                return Err(CompileError::OperatorUnsupported {
                    backend: BackendKind::Clickhouse,
                    op: "keyset_cursor",
                });
            }
            if let Some(limit) = pag.limit {
                sql.push_str(&format!(" LIMIT {limit}"));
            }
            if let Some(offset) = pag.offset
                && offset > 0
            {
                sql.push_str(&format!(" OFFSET {offset}"));
            }
        }
        Ok(CompiledRendering::Sql {
            backend: BackendKind::Clickhouse,
            statement: sql,
            params,
        })
    }

    fn compile_search(
        &self,
        op: &LogicalSearch,
        ctx: &CompileContext<'_>,
    ) -> Result<CompiledRendering, CompileError> {
        // ClickHouse has no native vector type in OSS; rejecting vector
        // requests is honest. Text-search lowers to `multiSearchAny` /
        // `positionCaseInsensitive` across all string columns and ranks
        // by a simple match-count score.
        let text = op
            .text_query
            .as_deref()
            .ok_or_else(|| CompileError::OperatorUnsupported {
                backend: BackendKind::Clickhouse,
                op: "vector_search",
            })?;
        if text.trim().is_empty() {
            return Err(CompileError::Malformed {
                reason: "text_query must be non-empty for ClickHouse search".into(),
            });
        }
        let table = self.resolve_table(&op.message_type, ctx)?;
        let text_columns: Vec<String> = table
            .columns
            .iter()
            .filter(|c| {
                c.proto_type.eq_ignore_ascii_case("string")
                    || c.sql_type.to_lowercase().contains("string")
            })
            .map(|c| format!("`{}`", c.column_name))
            .collect();
        if text_columns.is_empty() {
            return Err(CompileError::Malformed {
                reason: format!(
                    "text search requires at least one string column in '{}'",
                    op.message_type
                ),
            });
        }
        let mut params: Vec<LogicalValue> = Vec::new();
        // Build OR-of-positionCaseInsensitive(<col>, ?) > 0 across cols
        // and a score = sum of position hits. One bind per column for
        // both the predicate and the score expression, so we push the
        // text twice per column.
        let mut where_parts = Vec::with_capacity(text_columns.len());
        let mut score_parts = Vec::with_capacity(text_columns.len());
        for col in &text_columns {
            params.push(LogicalValue::String(text.to_string()));
            where_parts.push(format!("positionCaseInsensitive({col}, ?) > 0"));
        }
        for col in &text_columns {
            params.push(LogicalValue::String(text.to_string()));
            score_parts.push(format!("(positionCaseInsensitive({col}, ?) > 0)"));
        }
        let score_expr = score_parts.join(" + ");
        let mut sql = format!(
            "SELECT *, ({score_expr}) AS _score FROM `{db}`.`{table}` WHERE ({})",
            where_parts.join(" OR "),
            db = table.schema,
            table = table.table,
        );

        if let Some(filter) = &op.filter {
            let body = self.render_filter(filter, table, &op.message_type, &mut params)?;
            if body != "1=1" {
                sql.push_str(&format!(" AND {body}"));
            }
        }

        // A2: also AND tenant/project into the search WHERE so
        // text search respects RLS too.
        if let Some(extra) = Self::append_context_predicates(None, table, ctx, &mut params) {
            sql.push_str(&format!(" AND {extra}"));
        }

        if let Some(threshold) = op.score_threshold {
            params.push(LogicalValue::Float(threshold as f64));
            sql.push_str(" HAVING _score >= ?");
        }
        sql.push_str(&format!(" ORDER BY _score DESC LIMIT {}", op.top_k));
        Ok(CompiledRendering::Sql {
            backend: BackendKind::Clickhouse,
            statement: sql,
            params,
        })
    }

    fn compile_resource_op(
        &self,
        op: &LogicalResourceOp,
        _ctx: &CompileContext<'_>,
    ) -> Result<CompiledRendering, CompileError> {
        if !matches!(op.resource_kind, ResourceKind::Table) {
            return Err(CompileError::OperatorUnsupported {
                backend: BackendKind::Clickhouse,
                op: "non_table_resource",
            });
        }
        let sql = match op.op {
            ResourceOpKind::Ensure => {
                // Spec: { "database": "...", "columns": [...], "engine":
                // "MergeTree() ORDER BY (id)" }. Engine string is
                // operator-supplied — ClickHouse engine choice is
                // semantic (Replacing/Summing/etc.) and we don't pick
                // for them.
                let spec = op.spec.as_ref().ok_or_else(|| CompileError::Malformed {
                    reason: "Ensure Table requires a spec".into(),
                })?;
                let db = spec
                    .get("database")
                    .and_then(|v| v.as_str())
                    .unwrap_or("default");
                let engine = spec
                    .get("engine")
                    .and_then(|v| v.as_str())
                    .unwrap_or("MergeTree() ORDER BY tuple()");
                let cols = spec
                    .get("columns")
                    .and_then(|v| v.as_array())
                    .ok_or_else(|| CompileError::Malformed {
                        reason: "Ensure Table spec.columns must be an array".into(),
                    })?;
                let column_defs = cols
                    .iter()
                    .map(|c| {
                        let name = c.get("name").and_then(|v| v.as_str()).ok_or_else(|| {
                            CompileError::Malformed {
                                reason: "column missing 'name'".into(),
                            }
                        })?;
                        let ty = c.get("type").and_then(|v| v.as_str()).ok_or_else(|| {
                            CompileError::Malformed {
                                reason: "column missing 'type'".into(),
                            }
                        })?;
                        Ok(format!("`{name}` {ty}"))
                    })
                    .collect::<Result<Vec<_>, CompileError>>()?
                    .join(", ");
                format!(
                    "CREATE TABLE IF NOT EXISTS `{db}`.`{name}` ({defs}) ENGINE = {engine}",
                    name = op.resource_name,
                    defs = column_defs,
                )
            }
            ResourceOpKind::Drop => {
                let db = op
                    .spec
                    .as_ref()
                    .and_then(|s| s.get("database"))
                    .and_then(|v| v.as_str())
                    .unwrap_or("default");
                format!("DROP TABLE IF EXISTS `{db}`.`{}`", op.resource_name)
            }
            ResourceOpKind::List => {
                let db = op
                    .spec
                    .as_ref()
                    .and_then(|s| s.get("database"))
                    .and_then(|v| v.as_str())
                    .unwrap_or("default");
                format!("SHOW TABLES FROM `{db}`")
            }
        };
        Ok(CompiledRendering::Sql {
            backend: BackendKind::Clickhouse,
            statement: sql,
            params: Vec::new(),
        })
    }
}

fn render_ch_aggregate(
    agg: &AggregateExpr,
    table: &ManifestTable,
    message_type: &str,
) -> Result<String, CompileError> {
    let body = match agg.func {
        AggregateFunc::Count if agg.field == "*" => "COUNT(*)".to_string(),
        AggregateFunc::Count => {
            let col = resolve_ch_column(table, &agg.field, message_type)?;
            format!("COUNT(`{col}`)")
        }
        AggregateFunc::CountDistinct => {
            if agg.field == "*" {
                return Err(CompileError::Malformed {
                    reason: "COUNT(DISTINCT *) is not allowed; specify a field".into(),
                });
            }
            let col = resolve_ch_column(table, &agg.field, message_type)?;
            // ClickHouse prefers `uniqExact` over `COUNT(DISTINCT)`
            // for accuracy; both compile but uniqExact is the canonical
            // CH form.
            format!("uniqExact(`{col}`)")
        }
        AggregateFunc::Sum | AggregateFunc::Avg | AggregateFunc::Min | AggregateFunc::Max => {
            if agg.field == "*" {
                return Err(CompileError::Malformed {
                    reason: format!("{} requires a field name, not '*'", agg.func.sql_token()),
                });
            }
            let col = resolve_ch_column(table, &agg.field, message_type)?;
            format!("{}(`{col}`)", agg.func.sql_token())
        }
    };
    Ok(format!("{body} AS `{}`", agg.alias))
}

fn resolve_ch_sort_field(
    field: &str,
    op: &LogicalAggregate,
    table: &ManifestTable,
) -> Result<String, CompileError> {
    if op.aggregates.iter().any(|a| a.alias == field) {
        return Ok(format!("`{field}`"));
    }
    if op.group_by.iter().any(|f| f == field) {
        let col = resolve_ch_column(table, field, &op.message_type)?;
        return Ok(format!("`{col}`"));
    }
    Err(CompileError::Malformed {
        reason: format!(
            "ORDER BY field '{field}' is neither an aggregate alias nor a GROUP BY column"
        ),
    })
}

fn render_ch_having(
    filter: &LogicalFilter,
    op: &LogicalAggregate,
    params: &mut Vec<LogicalValue>,
) -> Result<String, CompileError> {
    match filter {
        LogicalFilter::And(c) if c.is_empty() => Ok("1=1".to_string()),
        LogicalFilter::Or(c) if c.is_empty() => Ok("1=0".to_string()),
        LogicalFilter::And(clauses) => {
            let parts = clauses
                .iter()
                .map(|c| render_ch_having(c, op, params))
                .collect::<Result<Vec<_>, _>>()?;
            Ok(format!("({})", parts.join(" AND ")))
        }
        LogicalFilter::Or(clauses) => {
            let parts = clauses
                .iter()
                .map(|c| render_ch_having(c, op, params))
                .collect::<Result<Vec<_>, _>>()?;
            Ok(format!("({})", parts.join(" OR ")))
        }
        LogicalFilter::Not(inner) => {
            let r = render_ch_having(inner, op, params)?;
            Ok(format!("(NOT {r})"))
        }
        LogicalFilter::Comparison {
            field,
            op: cmp,
            value,
        } => {
            if value.is_null() {
                return Err(CompileError::Malformed {
                    reason: format!(
                        "HAVING comparison with NULL on '{field}' must use IsNull, not {}",
                        cmp.token()
                    ),
                });
            }
            let token = resolve_ch_having_field(field, op)?;
            params.push(value.clone());
            let sql_op = match cmp {
                ComparisonOp::Eq => "=",
                ComparisonOp::Ne => "!=",
                ComparisonOp::Lt => "<",
                ComparisonOp::Le => "<=",
                ComparisonOp::Gt => ">",
                ComparisonOp::Ge => ">=",
                ComparisonOp::Like => "LIKE",
                ComparisonOp::ILike => "ILIKE",
                _ => {
                    return Err(CompileError::OperatorUnsupported {
                        backend: BackendKind::Clickhouse,
                        op: "having_text_match",
                    });
                }
            };
            Ok(format!("{token} {sql_op} ?"))
        }
        LogicalFilter::IsNull(field) => {
            let token = resolve_ch_having_field(field, op)?;
            Ok(format!("isNull({token})"))
        }
        LogicalFilter::InList { field, values } => {
            if values.is_empty() {
                return Ok("1=0".to_string());
            }
            let token = resolve_ch_having_field(field, op)?;
            let placeholders = values
                .iter()
                .map(|v| {
                    params.push(v.clone());
                    "?".to_string()
                })
                .collect::<Vec<_>>()
                .join(", ");
            Ok(format!("{token} IN ({placeholders})"))
        }
    }
}

fn resolve_ch_having_field(field: &str, op: &LogicalAggregate) -> Result<String, CompileError> {
    if op.aggregates.iter().any(|a| a.alias == field) {
        return Ok(format!("`{field}`"));
    }
    if op.group_by.iter().any(|f| f == field) {
        return Ok(format!("`{field}`"));
    }
    Err(CompileError::Malformed {
        reason: format!(
            "HAVING field '{field}' is neither an aggregate alias nor a GROUP BY column"
        ),
    })
}

fn resolve_ch_column<'a>(
    table: &'a ManifestTable,
    field: &str,
    message_type: &str,
) -> Result<&'a str, CompileError> {
    table
        .columns
        .iter()
        .find(|c| c.field_name.eq_ignore_ascii_case(field) || c.column_name == field)
        .map(|c| c.column_name.as_str())
        .ok_or_else(|| CompileError::UnknownField {
            message_type: message_type.to_string(),
            field: field.to_string(),
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generation::{CatalogManifest, ManifestColumn, ManifestTable};
    use crate::ir::filter::{ComparisonOp, LogicalFilter};
    use crate::ir::operations::{
        ConflictStrategy, LogicalDelete, LogicalRead, LogicalRecord, LogicalWrite,
    };
    use crate::ir::projection::LogicalPagination;
    use crate::ir::value::LogicalValue;

    fn fixture() -> CatalogManifest {
        let table = ManifestTable {
            message_name: "acme.billing.v1.Event".to_string(),
            schema: "analytics".into(),
            table: "events".into(),
            primary_key: vec!["id".into()],
            columns: vec![
                ManifestColumn {
                    field_name: "id".into(),
                    column_name: "id".into(),
                    proto_type: "string".into(),
                    sql_type: "String".into(),
                    is_primary: true,
                    ..Default::default()
                },
                ManifestColumn {
                    field_name: "amount".into(),
                    column_name: "amount".into(),
                    proto_type: "int64".into(),
                    sql_type: "Int64".into(),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        CatalogManifest {
            tables: vec![table],
            ..Default::default()
        }
    }

    fn sql(rendering: CompiledRendering) -> (String, Vec<LogicalValue>) {
        match rendering {
            CompiledRendering::Sql {
                statement, params, ..
            } => (statement, params),
            other => panic!("expected Sql rendering, got {other:?}"),
        }
    }

    #[test]
    fn select_uses_question_mark_placeholders_and_backticks() {
        let m = fixture();
        let ctx = CompileContext::new(&m);
        let read = LogicalRead::message("acme.billing.v1.Event")
            .with_filter(LogicalFilter::Comparison {
                field: "amount".into(),
                op: ComparisonOp::Gt,
                value: LogicalValue::Int(100),
            })
            .with_pagination(LogicalPagination::limit(50));
        let (statement, params) = sql(ClickHouseCompiler.compile_read(&read, &ctx).unwrap());
        assert!(statement.starts_with("SELECT * FROM `analytics`.`events`"));
        assert!(statement.contains("WHERE `amount` > ?"));
        assert!(statement.ends_with("LIMIT 50"));
        assert_eq!(params, vec![LogicalValue::Int(100)]);
    }

    #[test]
    fn delete_emits_alter_table_delete() {
        let m = fixture();
        let ctx = CompileContext::new(&m);
        let del = LogicalDelete {
            message_type: "acme.billing.v1.Event".into(),
            filter: LogicalFilter::Comparison {
                field: "id".into(),
                op: ComparisonOp::Eq,
                value: LogicalValue::String("abc".into()),
            },
            return_fields: vec![],
        };
        let (statement, _) = sql(ClickHouseCompiler.compile_delete(&del, &ctx).unwrap());
        assert!(statement.starts_with("ALTER TABLE `analytics`.`events` DELETE"));
        assert!(statement.contains("WHERE `id` = ?"));
    }

    #[test]
    fn upsert_is_typed_capability_error() {
        let m = fixture();
        let ctx = CompileContext::new(&m);
        let mut rec = LogicalRecord::new();
        rec.insert("id".into(), LogicalValue::String("abc".into()));
        let write = LogicalWrite {
            message_type: "acme.billing.v1.Event".into(),
            records: vec![rec],
            conflict: ConflictStrategy::Replace,
            return_fields: vec![],
        };
        let err = ClickHouseCompiler.compile_write(&write, &ctx).unwrap_err();
        assert!(matches!(
            err,
            CompileError::OperatorUnsupported {
                backend: BackendKind::Clickhouse,
                op: "upsert"
            }
        ));
    }

    /// A2 fixtures: table with tenant_id + project_id columns.
    fn tenant_fixture() -> CatalogManifest {
        let table = ManifestTable {
            message_name: "acme.billing.v1.Event".to_string(),
            schema: "analytics".into(),
            table: "events".into(),
            primary_key: vec!["id".into()],
            columns: vec![
                ManifestColumn {
                    field_name: "id".into(),
                    column_name: "id".into(),
                    proto_type: "string".into(),
                    sql_type: "String".into(),
                    is_primary: true,
                    ..Default::default()
                },
                ManifestColumn {
                    field_name: "tenant_id".into(),
                    column_name: "tenant_id".into(),
                    proto_type: "string".into(),
                    sql_type: "String".into(),
                    ..Default::default()
                },
                ManifestColumn {
                    field_name: "project_id".into(),
                    column_name: "project_id".into(),
                    proto_type: "string".into(),
                    sql_type: "String".into(),
                    ..Default::default()
                },
                ManifestColumn {
                    field_name: "amount".into(),
                    column_name: "amount".into(),
                    proto_type: "int64".into(),
                    sql_type: "Int64".into(),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        CatalogManifest {
            tables: vec![table],
            ..Default::default()
        }
    }

    #[test]
    fn a2_read_with_tenant_ctx_appends_predicate() {
        let m = tenant_fixture();
        let mut ctx = CompileContext::new(&m);
        ctx.tenant_id = Some("acme");
        ctx.project_id = Some("p1");
        let read =
            LogicalRead::message("acme.billing.v1.Event").with_filter(LogicalFilter::Comparison {
                field: "amount".into(),
                op: ComparisonOp::Gt,
                value: LogicalValue::Int(100),
            });
        let (statement, params) = sql(ClickHouseCompiler.compile_read(&read, &ctx).unwrap());
        assert!(statement.contains("WHERE `amount` > ?"));
        assert!(statement.contains("AND `tenant_id` = ?"));
        assert!(statement.contains("AND `project_id` = ?"));
        assert_eq!(params.len(), 3);
        assert_eq!(params[0], LogicalValue::Int(100));
        assert_eq!(params[1], LogicalValue::String("acme".into()));
        assert_eq!(params[2], LogicalValue::String("p1".into()));
    }

    #[test]
    fn a2_read_no_user_filter_still_emits_tenant_where() {
        let m = tenant_fixture();
        let mut ctx = CompileContext::new(&m);
        ctx.tenant_id = Some("acme");
        let read = LogicalRead::message("acme.billing.v1.Event");
        let (statement, params) = sql(ClickHouseCompiler.compile_read(&read, &ctx).unwrap());
        assert_eq!(
            statement,
            "SELECT * FROM `analytics`.`events` WHERE `tenant_id` = ?"
        );
        assert_eq!(params, vec![LogicalValue::String("acme".into())]);
    }

    #[test]
    fn a2_read_without_tenant_column_no_injection() {
        let m = fixture(); // no tenant_id column
        let mut ctx = CompileContext::new(&m);
        ctx.tenant_id = Some("acme");
        let read = LogicalRead::message("acme.billing.v1.Event");
        let (statement, params) = sql(ClickHouseCompiler.compile_read(&read, &ctx).unwrap());
        assert_eq!(statement, "SELECT * FROM `analytics`.`events`");
        assert!(params.is_empty());
    }

    #[test]
    fn a2_write_stamps_tenant_id_when_absent() {
        let m = tenant_fixture();
        let mut ctx = CompileContext::new(&m);
        ctx.tenant_id = Some("acme");
        let mut rec = LogicalRecord::new();
        rec.insert("id".into(), LogicalValue::String("abc".into()));
        rec.insert("amount".into(), LogicalValue::Int(42));
        let write = LogicalWrite {
            message_type: "acme.billing.v1.Event".into(),
            records: vec![rec],
            conflict: ConflictStrategy::Error,
            return_fields: vec![],
        };
        let (statement, params) = sql(ClickHouseCompiler.compile_write(&write, &ctx).unwrap());
        assert!(statement.contains("`tenant_id`"));
        assert!(params.contains(&LogicalValue::String("acme".into())));
    }

    #[test]
    fn a2_delete_appends_tenant_predicate() {
        let m = tenant_fixture();
        let mut ctx = CompileContext::new(&m);
        ctx.tenant_id = Some("acme");
        let del = LogicalDelete {
            message_type: "acme.billing.v1.Event".into(),
            filter: LogicalFilter::Comparison {
                field: "id".into(),
                op: ComparisonOp::Eq,
                value: LogicalValue::String("abc".into()),
            },
            return_fields: vec![],
        };
        let (statement, params) = sql(ClickHouseCompiler.compile_delete(&del, &ctx).unwrap());
        assert!(statement.contains("WHERE `id` = ?"));
        assert!(statement.contains("AND `tenant_id` = ?"));
        assert_eq!(params.len(), 2);
    }

    #[test]
    fn plain_insert_compiles_clean() {
        let m = fixture();
        let ctx = CompileContext::new(&m);
        let mut rec = LogicalRecord::new();
        rec.insert("id".into(), LogicalValue::String("abc".into()));
        rec.insert("amount".into(), LogicalValue::Int(42));
        let write = LogicalWrite {
            message_type: "acme.billing.v1.Event".into(),
            records: vec![rec],
            conflict: ConflictStrategy::Error,
            return_fields: vec![],
        };
        let (statement, params) = sql(ClickHouseCompiler.compile_write(&write, &ctx).unwrap());
        assert!(statement.starts_with("INSERT INTO `analytics`.`events` (`amount`, `id`)"));
        assert!(statement.ends_with("VALUES (?, ?)"));
        assert_eq!(params.len(), 2);
    }
}