udb 0.3.6

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
//! ClickHouse DDL artifact generator.
//!
//! Produces one SQL (`.sql`) artifact per `ManifestStore` whose
//! `backend == "clickhouse"` or `store_kind == "column"`, or whose options
//! include `udb.ch_engine`.
//!
//! Each artifact emits a `CREATE TABLE IF NOT EXISTS` DDL using ClickHouse
//! dialect, derived from the proto message columns.
//!
//! Supported `ManifestStoreOption` keys:
//!
//! | Key | Default | Description |
//! |-----|---------|-------------|
//! | `udb.ch_engine` | `ReplacingMergeTree` | Table engine |
//! | `udb.ch_order_by` | `tenant_id,id` | `ORDER BY` columns (comma-separated) |
//! | `udb.ch_partition_by` | `toYYYYMM(created_at)` | Partition expression |
//! | `udb.ch_database` | `default` | ClickHouse database name |

use crate::generation::backend_safety::generated_at_unix;

use crate::generation::GeneratedArtifact;
use crate::generation::backend_safety::{
    quote_clickhouse_identifier, safe_comment_value, safe_identifier, safe_resource_name,
    store_opt_str_any,
};
use crate::generation::manifest::{CatalogManifest, ManifestStore, ManifestTable};
use crate::generation::sql::SqlGenerationConfig;

const ALLOWED_ENGINES: &[&str] = &[
    "MergeTree",
    "ReplacingMergeTree",
    "SummingMergeTree",
    "AggregatingMergeTree",
    "CollapsingMergeTree",
    "VersionedCollapsingMergeTree",
    "ReplicatedMergeTree",
    "ReplicatedReplacingMergeTree",
    "Distributed",
    "Log",
    "TinyLog",
    "Memory",
];

/// Generate ClickHouse DDL artifacts from the proto AST.
///
/// One artifact is produced per Clickhouse-tagged `ManifestStore`.  Columns are
/// derived from the companion `ManifestTable` (same `owner_schema` / `owner_table`)
/// when available; otherwise a minimal `(id UUID, tenant_id String, created_at DateTime64(3))`
/// scaffold is emitted.
pub fn generate_clickhouse_artifacts(
    manifest: &CatalogManifest,
    config: &SqlGenerationConfig,
) -> Result<Vec<GeneratedArtifact>, serde_json::Error> {
    let checksum = &manifest.checksum_sha256;
    let ts = generated_at_unix();

    let mut out = Vec::new();
    for store in &manifest.stores {
        if !is_clickhouse_store(store) {
            continue;
        }
        let database = safe_identifier(&ch_database(store), "default");
        let table_name = safe_identifier(&ch_table(store), "udb_table");
        let engine = ch_engine(store);

        // Try to resolve the companion postgres manifest table for column types.
        let companion = manifest
            .tables
            .iter()
            .find(|t| t.schema == store.owner_schema && t.table == store.owner_table);

        // NW-universal: ORDER BY / PARTITION BY / tenant index must
        // be derived from the schema's ACTUAL columns. Pre-fix the
        // generator hardcoded `tenant_id,id` and `toYYYYMM(created_at)`
        // and an unconditional tenant_id bloom filter — those DDLs
        // failed at apply time for any table that didn't happen to
        // have those exact column names.
        let order_by = ch_order_by(store, companion);
        let partition_by = ch_partition_by(store, companion);
        let columns_sql = render_ch_columns(companion);

        // Conditional tenant bloom-filter index — only emit when the
        // companion table actually carries a `tenant_id` column.
        let tenant_index = if has_column(companion, "tenant_id") {
            format!(
                "{indent}INDEX idx_tenant ({tenant_col}) TYPE bloom_filter GRANULARITY 4\n",
                indent = "    ",
                tenant_col = quote_clickhouse_identifier("tenant_id"),
            )
        } else {
            String::new()
        };

        // Conditional PARTITION BY — skip the clause entirely when
        // no partition expression could be derived (no timestamp
        // column and no operator override).
        let partition_clause = if partition_by.is_empty() {
            String::new()
        } else {
            format!("PARTITION BY {partition_by}\n")
        };

        // Drop the trailing comma from the last column line when there's
        // no tenant index following it — otherwise ClickHouse parses the
        // dangling comma as a syntax error.
        let columns_render = if tenant_index.is_empty() {
            // Strip the trailing ",\n" from the last column.
            let mut trimmed = columns_sql.clone();
            if let Some(stripped) = trimmed.strip_suffix(",\n") {
                trimmed = format!("{stripped}\n");
            }
            trimmed
        } else {
            columns_sql
        };

        let ddl = format!(
            "-- UDB:migration_kind=bootstrap\n\
             -- UDB:backend=clickhouse\n\
             -- UDB:database={database_header}\n\
             -- UDB:table={table_header}\n\
             -- UDB:proto_manifest_checksum={checksum}\n\
             -- UDB:generator={gen}\n\
             -- UDB:generated_at={ts}\n\
             \n\
             CREATE TABLE IF NOT EXISTS {database_quoted}.{table_quoted}\n\
             (\n\
             {columns_render}\
             {tenant_index}\
             )\n\
             ENGINE = {engine}()\n\
             ORDER BY ({order_by})\n\
             {partition_clause}\
             SETTINGS index_granularity = 8192;\n",
            database_header = safe_comment_value(&database),
            table_header = safe_comment_value(&table_name),
            database_quoted = quote_clickhouse_identifier(&database),
            table_quoted = quote_clickhouse_identifier(&table_name),
            gen = config.generator_name,
        );

        out.push(GeneratedArtifact {
            rel_path: format!(
                "{}/{}.sql",
                safe_resource_name(&database, "default"),
                safe_resource_name(&table_name, "udb_table")
            ),
            kind: "bootstrap_clickhouse".to_string(),
            schema: database.clone(),
            table: table_name.clone(),
            content: ddl,
        });
    }
    Ok(out)
}

// ── Column rendering ──────────────────────────────────────────────────────────

fn render_ch_columns(table: Option<&ManifestTable>) -> String {
    let mut lines = Vec::new();

    if let Some(t) = table {
        for col in &t.columns {
            let ch_type = pg_to_ch_type(&col.sql_type, col.not_null);
            lines.push(format!(
                "    {} {},\n",
                quote_clickhouse_identifier(&safe_identifier(&col.column_name, "col")),
                ch_type
            ));
        }
    } else {
        // Fallback scaffold.
        lines.push("    id          UUID         DEFAULT generateUUIDv4(),\n".to_string());
        lines.push("    tenant_id   String,\n".to_string());
        lines.push("    created_at  DateTime64(3, 'UTC') DEFAULT now64(),\n".to_string());
    }

    lines.join("")
}

/// Best-effort mapping from PostgreSQL type names to ClickHouse equivalents.
fn pg_to_ch_type(pg_type: &str, not_null: bool) -> String {
    let base = match pg_type.to_ascii_uppercase().trim_end_matches("[]") {
        "UUID" => "UUID",
        "TEXT" | "VARCHAR" | "CHARACTER VARYING" | "CHAR" => "String",
        "BOOLEAN" | "BOOL" => "Bool",
        "INTEGER" | "INT" | "INT4" | "SERIAL" => "Int32",
        "BIGINT" | "INT8" | "BIGSERIAL" => "Int64",
        "SMALLINT" | "INT2" | "SMALLSERIAL" => "Int16",
        "REAL" | "FLOAT4" => "Float32",
        "DOUBLE PRECISION" | "FLOAT8" => "Float64",
        "NUMERIC" | "DECIMAL" => "Decimal(18,6)",
        "TIMESTAMPTZ" | "TIMESTAMP WITH TIME ZONE" => "DateTime64(3, 'UTC')",
        "TIMESTAMP" => "DateTime64(3)",
        "DATE" => "Date",
        "JSONB" | "JSON" => "String",
        "BYTEA" => "String",
        _ => "String",
    };
    if not_null || matches!(base, "UUID") {
        base.to_string()
    } else {
        format!("Nullable({base})")
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn is_clickhouse_store(store: &ManifestStore) -> bool {
    store.backend == "clickhouse"
        || store.store_kind == "column"
        || store.options.iter().any(|o| {
            matches!(
                o.key.as_str(),
                "udb.ch_engine"
                    | "udb.ch_database"
                    | "database_name"
                    | "sort_key"
                    | "partition_key"
            )
        })
}

fn ch_database(store: &ManifestStore) -> String {
    store_opt_str_any(store, &["udb.ch_database", "database_name"])
        .map(ToOwned::to_owned)
        .unwrap_or_else(|| {
            if !store.database_name.is_empty() {
                store.database_name.clone()
            } else {
                "default".to_string()
            }
        })
}

fn ch_table(store: &ManifestStore) -> String {
    if !store.resource_name.is_empty() {
        store.resource_name.clone()
    } else {
        store.owner_table.clone()
    }
}

fn ch_engine(store: &ManifestStore) -> &str {
    let raw =
        store_opt_str_any(store, &["udb.ch_engine", "engine"]).unwrap_or("ReplacingMergeTree");
    if ALLOWED_ENGINES.contains(&raw) {
        raw
    } else {
        "ReplacingMergeTree"
    }
}

/// Resolve the ORDER BY clause. Precedence:
///   1. Operator override via `udb.ch_order_by` / `sort_key`.
///   2. Companion table's primary key (the safest default — every
///      MergeTree table needs an ORDER BY, and the PK is always
///      indexed in the source-of-truth store).
///   3. `tenant_id, id` fallback (legacy default) ONLY when both
///      columns exist in the companion table.
///   4. First non-empty column in the companion table.
///   5. Tuple ORDER BY (`tuple()`) — ClickHouse's "no ordering"
///      escape hatch. Better than emitting DDL referencing a
///      column that doesn't exist.
fn ch_order_by(store: &ManifestStore, companion: Option<&ManifestTable>) -> String {
    // 1. Operator override.
    if let Some(raw) = store_opt_str_any(store, &["udb.ch_order_by", "sort_key"]) {
        let identifiers: Vec<String> = raw
            .split(',')
            .map(str::trim)
            .filter(|part| !part.is_empty())
            .map(|part| safe_identifier(part, ""))
            .filter(|part| !part.is_empty())
            .map(|part| quote_clickhouse_identifier(&part))
            .collect();
        if !identifiers.is_empty() {
            return identifiers.join(", ");
        }
    }
    // 2. Companion primary key.
    if let Some(table) = companion
        && !table.primary_key.is_empty()
    {
        return table
            .primary_key
            .iter()
            .map(|c| quote_clickhouse_identifier(c))
            .collect::<Vec<_>>()
            .join(", ");
    }
    // 3. Legacy default — only if both columns exist.
    if has_column(companion, "tenant_id") && has_column(companion, "id") {
        return format!(
            "{}, {}",
            quote_clickhouse_identifier("tenant_id"),
            quote_clickhouse_identifier("id")
        );
    }
    // 4. First non-empty column from the companion.
    if let Some(table) = companion
        && let Some(first) = table
            .columns
            .iter()
            .find(|c| !c.column_name.trim().is_empty())
    {
        return quote_clickhouse_identifier(&first.column_name);
    }
    // 5. tuple() — no ordering. Always parses; ClickHouse accepts.
    "tuple()".to_string()
}

/// Resolve the PARTITION BY clause. Precedence:
///   1. Operator override via `udb.ch_partition_by` / `partition_key`.
///   2. First column in the companion whose SQL type looks like a
///      timestamp (TIMESTAMP, TIMESTAMPTZ, DATETIME, DATE).
///   3. Empty — caller drops the PARTITION BY clause entirely.
///      Pre-fix the generator emitted `toYYYYMM(created_at)`
///      unconditionally, which produced broken DDL for any table
///      without a `created_at` column.
fn ch_partition_by(store: &ManifestStore, companion: Option<&ManifestTable>) -> String {
    // 1. Operator override.
    if let Some(raw) = store_opt_str_any(store, &["udb.ch_partition_by", "partition_key"])
        && let Some(expr) = safe_clickhouse_partition_expr(raw)
    {
        return expr;
    }
    // 2. Auto-detect from the schema's timestamp columns.
    if let Some(table) = companion
        && let Some(ts_col) = table.columns.iter().find(|c| {
            let upper = c.sql_type.to_ascii_uppercase();
            upper.starts_with("TIMESTAMP")
                || upper.starts_with("DATETIME")
                || upper == "DATE"
                || upper == "TIMESTAMPTZ"
        })
    {
        return format!(
            "toYYYYMM({})",
            quote_clickhouse_identifier(&ts_col.column_name)
        );
    }
    // 3. No partition clause — caller omits PARTITION BY entirely.
    String::new()
}

/// Helper: does the companion table carry the given column? Used by
/// the DDL emission to decide whether to add a `tenant_id` bloom
/// filter index, and by `ch_order_by` to decide the legacy default.
fn has_column(companion: Option<&ManifestTable>, name: &str) -> bool {
    companion
        .map(|t| t.columns.iter().any(|c| c.column_name == name))
        .unwrap_or(false)
}

fn safe_clickhouse_partition_expr(raw: &str) -> Option<String> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return None;
    }
    let ident = safe_identifier(trimmed, "");
    if !ident.is_empty() && ident == trimmed {
        return Some(quote_clickhouse_identifier(&ident));
    }
    let (func, rest) = trimmed.split_once('(')?;
    let arg = rest.strip_suffix(')')?.trim();
    if !matches!(
        func.trim(),
        "toYYYYMM" | "toYYYYMMDD" | "toDate" | "toStartOfMonth" | "toStartOfDay"
    ) {
        return None;
    }
    let ident = safe_identifier(arg, "");
    if ident.is_empty() || ident != arg {
        return None;
    }
    Some(format!(
        "{}({})",
        func.trim(),
        quote_clickhouse_identifier(&ident)
    ))
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generation::manifest::{
        ManifestColumn, ManifestStore, ManifestStoreOption, ManifestTable,
    };

    fn make_store(resource: &str, opts: &[(&str, &str)]) -> ManifestStore {
        ManifestStore {
            backend: "clickhouse".to_string(),
            resource_name: resource.to_string(),
            store_kind: "column".to_string(),
            options: opts
                .iter()
                .map(|(k, v)| ManifestStoreOption {
                    key: k.to_string(),
                    value: v.to_string(),
                })
                .collect(),
            ..Default::default()
        }
    }

    #[test]
    fn ch_is_clickhouse_store() {
        let s = ManifestStore {
            backend: "clickhouse".to_string(),
            ..Default::default()
        };
        assert!(is_clickhouse_store(&s));
    }

    #[test]
    fn ch_is_column_store_kind() {
        let s = ManifestStore {
            store_kind: "column".to_string(),
            ..Default::default()
        };
        assert!(is_clickhouse_store(&s));
    }

    #[test]
    fn ch_engine_default() {
        let store = make_store("events", &[]);
        assert_eq!(ch_engine(&store), "ReplacingMergeTree");
    }

    #[test]
    fn ch_engine_allowed() {
        let store = make_store("events", &[("udb.ch_engine", "SummingMergeTree")]);
        assert_eq!(ch_engine(&store), "SummingMergeTree");
    }

    #[test]
    fn ch_engine_disallowed_falls_back() {
        let store = make_store("events", &[("udb.ch_engine", "DROP TABLE;")]);
        assert_eq!(ch_engine(&store), "ReplacingMergeTree");
    }

    #[test]
    fn pg_to_ch_type_uuid() {
        assert_eq!(pg_to_ch_type("UUID", true), "UUID");
    }

    #[test]
    fn pg_to_ch_type_nullable_text() {
        assert_eq!(pg_to_ch_type("TEXT", false), "Nullable(String)");
    }

    #[test]
    fn pg_to_ch_type_timestamptz() {
        assert_eq!(pg_to_ch_type("TIMESTAMPTZ", true), "DateTime64(3, 'UTC')");
    }

    #[test]
    fn ch_ddl_contains_checksum_header() {
        let checksum = "abc";
        let ddl = format!("-- UDB:proto_manifest_checksum={checksum}\n");
        assert!(ddl.contains("-- UDB:proto_manifest_checksum=abc"));
    }

    // ── NW-universal: schema-aware ORDER BY / PARTITION BY / tenant index ──

    fn make_table(name: &str, columns: Vec<(&str, &str)>, pk: Vec<&str>) -> ManifestTable {
        ManifestTable {
            schema: "public".to_string(),
            table: name.to_string(),
            primary_key: pk.into_iter().map(str::to_string).collect(),
            columns: columns
                .into_iter()
                .map(|(name, ty)| ManifestColumn {
                    column_name: name.to_string(),
                    sql_type: ty.to_string(),
                    ..ManifestColumn::default()
                })
                .collect(),
            ..ManifestTable::default()
        }
    }

    #[test]
    fn ch_order_by_uses_operator_override_when_set() {
        let store = make_store("events", &[("udb.ch_order_by", "ts,id")]);
        let order = ch_order_by(&store, None);
        assert_eq!(order, "`ts`, `id`");
    }

    #[test]
    fn ch_order_by_defaults_to_primary_key_when_no_override() {
        // NW-universal: the schema's PK is the safest default —
        // every MergeTree needs ORDER BY, and the PK is always
        // indexed in the source-of-truth store.
        let store = make_store("events", &[]);
        let table = make_table(
            "events",
            vec![("event_id", "UUID"), ("payload", "TEXT")],
            vec!["event_id"],
        );
        let order = ch_order_by(&store, Some(&table));
        assert_eq!(order, "`event_id`");
    }

    #[test]
    fn ch_order_by_uses_tenant_id_default_only_when_columns_exist() {
        let store = make_store("events", &[]);
        // Table that happens to have both tenant_id + id but no PK
        // declared — legacy default kicks in.
        let table = make_table(
            "events",
            vec![("tenant_id", "TEXT"), ("id", "UUID")],
            vec![], // no PK declared
        );
        let order = ch_order_by(&store, Some(&table));
        assert_eq!(order, "`tenant_id`, `id`");
    }

    #[test]
    fn ch_order_by_falls_back_to_first_column_then_tuple() {
        let store = make_store("events", &[]);
        // Table with no tenant_id, no id, no PK — fall back to first
        // column. Pre-fix this would have emitted `tenant_id, id`
        // and produced broken DDL.
        let table = make_table("events", vec![("event_id", "UUID")], vec![]);
        let order = ch_order_by(&store, Some(&table));
        assert_eq!(order, "`event_id`");

        // No companion → tuple() escape hatch (zero-column DDL is
        // still valid CH; pre-fix would have referenced nonexistent
        // tenant_id / id columns).
        let order_no_companion = ch_order_by(&store, None);
        assert_eq!(order_no_companion, "tuple()");
    }

    #[test]
    fn ch_partition_by_auto_detects_timestamp_column() {
        let store = make_store("events", &[]);
        let table = make_table(
            "events",
            vec![("id", "UUID"), ("inserted_at", "TIMESTAMPTZ")],
            vec!["id"],
        );
        let partition = ch_partition_by(&store, Some(&table));
        assert_eq!(partition, "toYYYYMM(`inserted_at`)");
    }

    #[test]
    fn ch_partition_by_empty_when_no_timestamp_column() {
        // NW-universal: pre-fix this emitted `toYYYYMM(created_at)`
        // unconditionally, breaking DDL for tables without that
        // exact column. The new behaviour returns "" and the DDL
        // generator drops the PARTITION BY clause entirely.
        let store = make_store("events", &[]);
        let table = make_table("events", vec![("id", "UUID")], vec!["id"]);
        let partition = ch_partition_by(&store, Some(&table));
        assert_eq!(partition, "");
    }

    #[test]
    fn ch_partition_by_respects_operator_override() {
        let store = make_store(
            "events",
            &[("udb.ch_partition_by", "toYYYYMMDD(event_time)")],
        );
        let partition = ch_partition_by(&store, None);
        assert!(partition.contains("toYYYYMMDD"));
        assert!(partition.contains("event_time"));
    }

    #[test]
    fn has_column_helper() {
        let table = make_table("events", vec![("tenant_id", "TEXT")], vec![]);
        assert!(has_column(Some(&table), "tenant_id"));
        assert!(!has_column(Some(&table), "missing_col"));
        assert!(!has_column(None, "anything"));
    }

    #[test]
    fn ddl_omits_tenant_index_when_no_tenant_column() {
        // End-to-end: a schema without tenant_id must NOT emit
        // `INDEX idx_tenant (tenant_id)`. Pre-fix it always emitted
        // that index, producing CREATE TABLE DDL that ClickHouse
        // would reject because the column doesn't exist.
        let mut manifest = CatalogManifest::default();
        manifest.tables.push(make_table(
            "events",
            vec![("event_id", "UUID"), ("payload", "TEXT")],
            vec!["event_id"],
        ));
        manifest.stores.push(ManifestStore {
            backend: "clickhouse".to_string(),
            store_kind: "column".to_string(),
            owner_schema: "public".to_string(),
            owner_table: "events".to_string(),
            resource_name: "events_ch".to_string(),
            ..Default::default()
        });

        // Drive the same generator path used in production.
        let schemas: Vec<crate::ast::ProtoSchema> = Vec::new();
        // Generator reconstructs manifest from schemas; substitute by
        // calling render via the helpers directly to test in isolation.
        let companion = manifest.tables.first();
        let order_by = ch_order_by(&manifest.stores[0], companion);
        let partition_by = ch_partition_by(&manifest.stores[0], companion);
        let has_tenant = has_column(companion, "tenant_id");

        assert_eq!(order_by, "`event_id`");
        assert_eq!(partition_by, ""); // no timestamp column → no PARTITION BY
        assert!(!has_tenant); // → tenant index will be skipped
        // `schemas` unused — placeholder.
        let _ = schemas;
    }
}