udb 0.3.7

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
#![allow(clippy::result_large_err)]

//! PostgreSQL query building helpers: plan execution, row binding, join fusion,
//! and record serialisation. `rows_to_record_set` and its encryption-aware
//! siblings remain in `core.rs` because they depend on the private
//! `EncryptionMetrics` type.

use serde_json::Value as JsonValue;
use sqlx::Postgres;
use sqlx::postgres::PgArguments;
use sqlx::query::Query;
use uuid::Uuid;

use crate::broker::{RequestContext, table_for_message};
use crate::generation::sql::{resolve_tenant_column_ref, table_requires_tenant_column};
use crate::generation::{CatalogManifest, ManifestColumn, ManifestTable};
use crate::proto::{Mutation, SelectRequest, UpsertRequest};

use super::executor_utils::{
    json_f64, json_i64, json_scalar_to_string, qi_runtime, reject_plan, struct_to_json,
};

// ── Transaction plan execution ────────────────────────────────────────────────

pub(crate) async fn execute_tx_plan(
    tx: &mut sqlx::Transaction<'_, Postgres>,
    manifest: &CatalogManifest,
    message_type: &str,
    sql: &str,
    columns: &[String],
    values: &[JsonValue],
    errors: &[String],
) -> Result<u64, tonic::Status> {
    reject_plan(errors)?;
    let table = table_for_message(manifest, message_type)
        .ok_or_else(|| tonic::Status::invalid_argument("unknown message_type"))?;
    let query = bind_values(sqlx::query(sql), table, columns, values)?;
    let result = query
        .execute(&mut **tx)
        .await
        .map_err(|err| tonic::Status::internal(format!("transaction mutation failed: {err}")))?;
    Ok(result.rows_affected())
}

// ── Join fusion ───────────────────────────────────────────────────────────────

pub(crate) struct JoinFusionPlan {
    pub(crate) sql: String,
    pub(crate) bindings: Vec<(ManifestColumn, JsonValue)>,
}

pub(crate) fn build_join_fusion_sql(
    manifest: &CatalogManifest,
    request: &SelectRequest,
    context: &RequestContext,
    filter: &JsonValue,
) -> Result<JoinFusionPlan, tonic::Status> {
    if context.tenant_id.trim().is_empty() {
        return Err(tonic::Status::invalid_argument(
            "tenant_id is required for join fusion",
        ));
    }
    let message_types = split_join_message_types(&request.message_type);
    if message_types.len() < 2 {
        return Err(tonic::Status::invalid_argument(
            "join fusion requires at least two message types",
        ));
    }
    let tables = message_types
        .iter()
        .map(|message_type| {
            table_for_message(manifest, message_type).ok_or_else(|| {
                tonic::Status::invalid_argument(format!("unknown message_type {message_type}"))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;
    let aliases = (0..tables.len())
        .map(|idx| format!("t{idx}"))
        .collect::<Vec<_>>();
    let select_list = join_select_list(&tables, &aliases, &request.fields)?;
    let mut sql = format!(
        "SELECT {} FROM {}.{} {}",
        select_list.join(", "),
        qi_runtime(&tables[0].schema),
        qi_runtime(&tables[0].table),
        qi_runtime(&aliases[0])
    );
    for idx in 1..tables.len() {
        let join = find_join_edge(
            &tables[0..idx],
            &aliases[0..idx],
            tables[idx],
            &aliases[idx],
        )
        .ok_or_else(|| {
            tonic::Status::invalid_argument(format!(
                "no foreign key path found for join fusion target {}",
                message_types[idx]
            ))
        })?;
        sql.push_str(" JOIN ");
        sql.push_str(&format!(
            "{}.{} {} ON {}",
            qi_runtime(&tables[idx].schema),
            qi_runtime(&tables[idx].table),
            qi_runtime(&aliases[idx]),
            join
        ));
    }

    let mut bindings = Vec::new();
    let mut predicates = Vec::new();
    for (table_idx, table) in tables.iter().enumerate() {
        let Some(column) = tenant_column_ref(table) else {
            if table_requires_tenant_column(table) {
                return Err(tonic::Status::failed_precondition(format!(
                    "join fusion cannot safely select scoped table {}.{} without a tenant column",
                    table.schema, table.table
                )));
            }
            continue;
        };
        bindings.push((column.clone(), JsonValue::String(context.tenant_id.clone())));
        predicates.push(format!(
            "{}.{} = ${}",
            qi_runtime(&aliases[table_idx]),
            qi_runtime(&column.column_name),
            bindings.len()
        ));
    }
    if let JsonValue::Object(map) = filter {
        for (field, value) in map {
            if field.starts_with('$') || value.is_object() || value.is_array() {
                return Err(tonic::Status::invalid_argument(
                    "join fusion supports only simple equality filters",
                ));
            }
            let (table_idx, column_name) = parse_join_field(field, &message_types)?;
            let column = tables[table_idx]
                .columns
                .iter()
                .find(|column| column.column_name == column_name)
                .ok_or_else(|| {
                    tonic::Status::invalid_argument(format!("unknown join filter field {field}"))
                })?;
            bindings.push((column.clone(), value.clone()));
            predicates.push(format!(
                "{}.{} = ${}",
                qi_runtime(&aliases[table_idx]),
                qi_runtime(&column.column_name),
                bindings.len()
            ));
        }
    }
    if !predicates.is_empty() {
        sql.push_str(" WHERE ");
        sql.push_str(&predicates.join(" AND "));
    }
    if request.limit > 0 {
        sql.push_str(&format!(" LIMIT {}", request.limit));
    }
    Ok(JoinFusionPlan { sql, bindings })
}

pub(crate) fn split_join_message_types(message_type: &str) -> Vec<String> {
    message_type
        .split([',', '+'])
        .map(str::trim)
        .filter(|part| !part.is_empty())
        .map(ToString::to_string)
        .collect()
}

pub(crate) fn is_join_fusion_message_type(message_type: &str) -> bool {
    split_join_message_types(message_type).len() > 1
}

fn join_select_list(
    tables: &[&ManifestTable],
    aliases: &[String],
    fields: &[String],
) -> Result<Vec<String>, tonic::Status> {
    if fields.is_empty() {
        return Ok(tables
            .iter()
            .zip(aliases)
            .flat_map(|(table, alias)| {
                table.columns.iter().map(move |column| {
                    format!(
                        "{}.{} AS {}",
                        qi_runtime(alias),
                        qi_runtime(&column.column_name),
                        qi_runtime(&format!("{}__{}", table.message_name, column.column_name))
                    )
                })
            })
            .collect());
    }
    fields
        .iter()
        .map(|field| {
            let message_types = tables
                .iter()
                .map(|table| table.message_name.clone())
                .collect::<Vec<_>>();
            let (table_idx, column_name) = parse_join_field(field, &message_types)?;
            if !tables[table_idx]
                .columns
                .iter()
                .any(|column| column.column_name == column_name)
            {
                return Err(tonic::Status::invalid_argument(format!(
                    "unknown join selected field {field}"
                )));
            }
            Ok(format!(
                "{}.{} AS {}",
                qi_runtime(&aliases[table_idx]),
                qi_runtime(&column_name),
                qi_runtime(&field.replace('.', "__"))
            ))
        })
        .collect()
}

fn parse_join_field(
    field: &str,
    message_types: &[String],
) -> Result<(usize, String), tonic::Status> {
    if let Some((message_type, column)) = field.split_once('.') {
        let idx = message_types
            .iter()
            .position(|candidate| candidate.eq_ignore_ascii_case(message_type))
            .ok_or_else(|| {
                tonic::Status::invalid_argument(format!("unknown join field prefix {message_type}"))
            })?;
        return Ok((idx, column.to_ascii_lowercase()));
    }
    Ok((0, field.to_ascii_lowercase()))
}

fn find_join_edge(
    prior_tables: &[&ManifestTable],
    prior_aliases: &[String],
    next_table: &ManifestTable,
    next_alias: &str,
) -> Option<String> {
    for (prior, prior_alias) in prior_tables.iter().zip(prior_aliases) {
        for fk in &prior.foreign_keys {
            if fk.ref_schema == next_table.schema && fk.ref_table == next_table.table {
                return Some(join_predicate(
                    prior_alias,
                    &fk.columns,
                    next_alias,
                    &fk.ref_columns,
                ));
            }
        }
        for fk in &next_table.foreign_keys {
            if fk.ref_schema == prior.schema && fk.ref_table == prior.table {
                return Some(join_predicate(
                    next_alias,
                    &fk.columns,
                    prior_alias,
                    &fk.ref_columns,
                ));
            }
        }
    }
    None
}

fn join_predicate(
    left_alias: &str,
    left_columns: &[String],
    right_alias: &str,
    right_columns: &[String],
) -> String {
    left_columns
        .iter()
        .zip(right_columns)
        .map(|(left, right)| {
            format!(
                "{}.{} = {}.{}",
                qi_runtime(left_alias),
                qi_runtime(left),
                qi_runtime(right_alias),
                qi_runtime(right)
            )
        })
        .collect::<Vec<_>>()
        .join(" AND ")
}

pub(crate) fn tenant_column_ref(table: &ManifestTable) -> Option<&ManifestColumn> {
    resolve_tenant_column_ref(table)
}

// ── Parameter binding ─────────────────────────────────────────────────────────

pub(crate) fn bind_values<'q>(
    mut query: Query<'q, Postgres, PgArguments>,
    table: &ManifestTable,
    columns: &[String],
    values: &[JsonValue],
) -> Result<Query<'q, Postgres, PgArguments>, tonic::Status> {
    if columns.len() != values.len() {
        return Err(tonic::Status::invalid_argument(format!(
            "parameter mismatch: {} columns, {} values",
            columns.len(),
            values.len()
        )));
    }
    for (column_name, value) in columns.iter().zip(values.iter()) {
        let column = table
            .columns
            .iter()
            .find(|column| column.column_name == *column_name);
        query = bind_one(query, column, value)?;
    }
    Ok(query)
}

pub(crate) fn bind_one<'q>(
    query: Query<'q, Postgres, PgArguments>,
    column: Option<&ManifestColumn>,
    value: &JsonValue,
) -> Result<Query<'q, Postgres, PgArguments>, tonic::Status> {
    let sql_type = column
        .map(|column| column.sql_type.to_ascii_uppercase())
        .unwrap_or_default();
    if value.is_null() {
        return Ok(query.bind(Option::<String>::None));
    }
    if sql_type.contains("JSON") {
        return Ok(query.bind(sqlx::types::Json(strip_nul_json(value))));
    }
    if sql_type == "UUID" {
        let parsed = value
            .as_str()
            .ok_or_else(|| tonic::Status::invalid_argument("UUID value must be a string"))?
            .parse::<Uuid>()
            .map_err(|err| tonic::Status::invalid_argument(format!("invalid UUID: {err}")))?;
        return Ok(query.bind(parsed));
    }
    // Array value — used for `$in` / `col = ANY($N)` filters. (#121)
    // Bind a *typed* array matching the column type: PostgreSQL does NOT
    // implicitly cast a `text[]` element to the column type inside `= ANY`, so a
    // `uuid`/`int`/`numeric` column compared against a text array fails at
    // execution. Typed arrays keep the predicate index-usable (no `::type` cast
    // on the column needed).
    if let JsonValue::Array(items) = value {
        if sql_type == "UUID" {
            let mut arr: Vec<Uuid> = Vec::with_capacity(items.len());
            for item in items {
                let parsed = item
                    .as_str()
                    .ok_or_else(|| {
                        tonic::Status::invalid_argument("UUID $in value must be a string")
                    })?
                    .parse::<Uuid>()
                    .map_err(|err| {
                        tonic::Status::invalid_argument(format!("invalid UUID in $in: {err}"))
                    })?;
                arr.push(parsed);
            }
            return Ok(query.bind(arr));
        }
        if sql_type.contains("INT") || sql_type.contains("SERIAL") {
            let mut arr: Vec<i64> = Vec::with_capacity(items.len());
            for item in items {
                arr.push(json_i64(item)?);
            }
            return Ok(query.bind(arr));
        }
        if sql_type.contains("REAL")
            || sql_type.contains("DOUBLE")
            || sql_type.contains("FLOAT")
            || sql_type.contains("NUMERIC")
            || sql_type.contains("DECIMAL")
        {
            let mut arr: Vec<f64> = Vec::with_capacity(items.len());
            for item in items {
                arr.push(json_f64(item)?);
            }
            return Ok(query.bind(arr));
        }
        if sql_type.contains("BOOL") {
            let arr: Vec<bool> = items.iter().map(|i| i.as_bool().unwrap_or(false)).collect();
            return Ok(query.bind(arr));
        }
        // text / varchar / enum / timestamp and friends: a text[] compares
        // correctly for text-typed columns (`text = ANY(text[])`).
        let arr: Vec<String> = items
            .iter()
            .map(json_scalar_to_string)
            .map(|s| strip_nul(&s))
            .collect();
        return Ok(query.bind(arr));
    }
    if sql_type.contains("BOOL") {
        return Ok(query.bind(value.as_bool().unwrap_or(false)));
    }
    if sql_type.contains("INT") || sql_type.contains("BIGSERIAL") || sql_type.contains("SERIAL") {
        return Ok(query.bind(json_i64(value)?));
    }
    if sql_type.contains("REAL")
        || sql_type.contains("DOUBLE")
        || sql_type.contains("FLOAT")
        || sql_type.contains("NUMERIC")
        || sql_type.contains("DECIMAL")
    {
        return Ok(query.bind(json_f64(value)?));
    }
    Ok(query.bind(strip_nul(&json_scalar_to_string(value))))
}

/// A NUL (`0x00`) byte cannot be stored in a Postgres `text`/`varchar`/`json(b)`
/// value. Strip it at the typed-record bind edge so a hostile/garbage byte cannot
/// fault the whole upsert with Postgres' UTF-8 NUL rejection (B14). This path does
/// NOT go through `bind_generic_pg_param` (which already strips), so it needs its
/// own guard.
fn strip_nul(s: &str) -> String {
    if s.contains('\u{0}') {
        s.replace('\u{0}', "")
    } else {
        s.to_string()
    }
}

fn strip_nul_json(value: &JsonValue) -> JsonValue {
    match value {
        JsonValue::String(s) if s.contains('\u{0}') => JsonValue::String(s.replace('\u{0}', "")),
        JsonValue::Array(items) => JsonValue::Array(items.iter().map(strip_nul_json).collect()),
        JsonValue::Object(map) => JsonValue::Object(
            map.iter()
                .map(|(k, v)| (k.clone(), strip_nul_json(v)))
                .collect(),
        ),
        other => other.clone(),
    }
}

// ── Record serialisation ──────────────────────────────────────────────────────

pub(crate) fn upsert_record_json(request: &UpsertRequest) -> Result<JsonValue, tonic::Status> {
    if let Some(payload) = &request.payload {
        return Ok(struct_to_json(payload));
    }
    if !request.record_json.is_empty() {
        return serde_json::from_slice(&request.record_json).map_err(|err| {
            tonic::Status::invalid_argument(format!("record_json must be valid JSON: {err}"))
        });
    }
    Err(tonic::Status::invalid_argument(
        "payload or record_json is required",
    ))
}

pub(crate) fn mutation_record_json(mutation: &Mutation) -> Result<JsonValue, tonic::Status> {
    if let Some(payload) = &mutation.payload {
        return Ok(struct_to_json(payload));
    }
    if !mutation.record_json.is_empty() {
        return serde_json::from_slice(&mutation.record_json).map_err(|err| {
            tonic::Status::invalid_argument(format!("record_json must be valid JSON: {err}"))
        });
    }
    Err(tonic::Status::invalid_argument(
        "payload or record_json is required",
    ))
}

pub(crate) fn record_values(
    record: &JsonValue,
    columns: &[String],
) -> Result<Vec<JsonValue>, tonic::Status> {
    let object = record
        .as_object()
        .ok_or_else(|| tonic::Status::invalid_argument("record must be a JSON object"))?;
    Ok(columns
        .iter()
        .map(|column| object.get(column).cloned().unwrap_or(JsonValue::Null))
        .collect())
}

pub(crate) fn filter_bind_values(filter: &JsonValue) -> Vec<JsonValue> {
    let mut out = Vec::new();
    collect_filter_values(filter, &mut out);
    out
}

fn collect_filter_values(value: &JsonValue, out: &mut Vec<JsonValue>) {
    match value {
        JsonValue::Object(map) => {
            for (key, nested) in map {
                let normalized = key.to_ascii_lowercase();
                if matches!(normalized.as_str(), "$and" | "and" | "$or" | "or") {
                    collect_filter_values(nested, out);
                } else if normalized.starts_with('$') {
                    // Skip the null predicates — `$is_null`/`$not_null` compile to
                    // `IS NULL` / `IS NOT NULL`, which bind no SQL parameter. Pushing
                    // a value for them would desync the placeholder↔value count and
                    // make `bind_values` fail (or bind onto the wrong $N).
                    if !matches!(normalized.as_str(), "$is_null" | "$not_null") {
                        out.push(nested.clone());
                    }
                } else if let JsonValue::Object(_) = nested {
                    collect_filter_values(nested, out);
                } else {
                    out.push(nested.clone());
                }
            }
        }
        JsonValue::Array(items) => {
            for item in items {
                collect_filter_values(item, out);
            }
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generation::{CatalogManifest, ManifestForeignKey, ManifestTableSecurity};

    fn ctx() -> RequestContext {
        RequestContext {
            tenant_id: "acme".to_string(),
            ..RequestContext::default()
        }
    }

    fn col(name: &str) -> ManifestColumn {
        ManifestColumn {
            field_name: name.to_string(),
            column_name: name.to_string(),
            proto_type: "string".to_string(),
            sql_type: "text".to_string(),
            ..ManifestColumn::default()
        }
    }

    fn tenant_col(field_name: &str, column_name: &str, flagged: bool) -> ManifestColumn {
        ManifestColumn {
            field_name: field_name.to_string(),
            column_name: column_name.to_string(),
            proto_type: "string".to_string(),
            sql_type: "text".to_string(),
            is_tenant_column: flagged,
            ..ManifestColumn::default()
        }
    }

    fn table(message: &str, physical: &str, columns: Vec<ManifestColumn>) -> ManifestTable {
        ManifestTable {
            message_name: format!("acme.test.v1.{message}"),
            schema: "public".to_string(),
            table: physical.to_string(),
            columns,
            primary_key: vec!["id".to_string()],
            ..ManifestTable::default()
        }
    }

    fn join_manifest(mut left: ManifestTable, right: ManifestTable) -> CatalogManifest {
        left.foreign_keys.push(ManifestForeignKey {
            name: "fk_right".to_string(),
            columns: vec!["right_id".to_string()],
            ref_schema: right.schema.clone(),
            ref_table: right.table.clone(),
            ref_columns: vec!["id".to_string()],
            ..ManifestForeignKey::default()
        });
        CatalogManifest {
            tables: vec![left, right],
            ..CatalogManifest::default()
        }
    }

    fn join_request() -> SelectRequest {
        SelectRequest {
            message_type: "Left,Right".to_string(),
            limit: 25,
            ..SelectRequest::default()
        }
    }

    #[test]
    fn tenant_column_ref_prefers_declared_table_security_column() {
        let mut table = table(
            "Left",
            "lefts",
            vec![
                col("id"),
                tenant_col("tenant_id", "tenant_id", true),
                tenant_col("account", "account_id", false),
            ],
        );
        table.table_security = ManifestTableSecurity {
            tenant_column: "account".to_string(),
            ..ManifestTableSecurity::default()
        };

        let resolved = tenant_column_ref(&table).expect("tenant column");

        assert_eq!(resolved.column_name, "account_id");
    }

    #[test]
    fn tenant_column_ref_uses_system_and_legacy_names() {
        let system = table(
            "Left",
            "lefts",
            vec![col("id"), tenant_col("_tenant_id", "_tenant_id", false)],
        );
        assert_eq!(
            tenant_column_ref(&system).map(|column| column.column_name.as_str()),
            Some("_tenant_id")
        );

        let legacy = table(
            "Left",
            "lefts",
            vec![col("id"), tenant_col("org_id", "organization_id", false)],
        );
        assert_eq!(
            tenant_column_ref(&legacy).map(|column| column.column_name.as_str()),
            Some("organization_id")
        );
    }

    #[test]
    fn join_fusion_adds_tenant_predicate_for_every_joined_tenant_table() {
        let mut left = table(
            "Left",
            "lefts",
            vec![
                col("id"),
                col("right_id"),
                tenant_col("tenant_id", "tenant_id", true),
            ],
        );
        left.enable_rls = true;
        let mut right = table(
            "Right",
            "rights",
            vec![col("id"), tenant_col("tenant_id", "tenant_id", true)],
        );
        right.enable_rls = true;
        let manifest = join_manifest(left, right);

        let plan =
            build_join_fusion_sql(&manifest, &join_request(), &ctx(), &JsonValue::Null).unwrap();

        assert!(
            plan.sql.contains(r#""t0"."tenant_id" = $1"#),
            "{}",
            plan.sql
        );
        assert!(
            plan.sql.contains(r#""t1"."tenant_id" = $2"#),
            "{}",
            plan.sql
        );
        assert_eq!(plan.bindings.len(), 2);
        assert_eq!(plan.bindings[0].1, JsonValue::String("acme".to_string()));
        assert_eq!(plan.bindings[1].1, JsonValue::String("acme".to_string()));
    }

    #[test]
    fn join_fusion_fails_closed_for_scoped_table_without_tenant_column() {
        let mut left = table("Left", "lefts", vec![col("id"), col("right_id")]);
        left.enable_rls = true;
        let right = table("Right", "rights", vec![col("id")]);
        let manifest = join_manifest(left, right);

        let err = build_join_fusion_sql(&manifest, &join_request(), &ctx(), &JsonValue::Null)
            .err()
            .expect("scoped table without tenant column must fail closed");

        assert_eq!(err.code(), tonic::Code::FailedPrecondition);
        assert!(err.message().contains("without a tenant column"));
    }
}