faucet-sink-spanner 1.0.0

Google Cloud Spanner sink connector for the faucet-stream ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
//! Cloud Spanner sink implementation.
//!
//! Records are written as batched **mutations** (`Insert` for append,
//! `InsertOrUpdate` for upsert, `Delete` for delete mode), one atomic
//! `Commit` per chunk. Values are encoded against the destination column
//! types read once from `INFORMATION_SCHEMA` (see
//! [`faucet_common_spanner::encode`]). Exactly-once delivery commits the
//! page's mutations and a `faucet_commit_token` watermark row in a single
//! read-write transaction.

use crate::config::SpannerSinkConfig;
use async_trait::async_trait;
use faucet_common_spanner::encode::{EncodedKind, encode_to_kind};
use faucet_common_spanner::quote_ident_spanner;
use faucet_common_spanner::types::{SpannerType, parse_spanner_type, spanner_type_to_json_schema};
use faucet_core::FaucetError;
use gcloud_googleapis::spanner::admin::database::v1::UpdateDatabaseDdlRequest;
use gcloud_googleapis::spanner::v1::Mutation;
use gcloud_spanner::client::Client;
use gcloud_spanner::key::Key;
use gcloud_spanner::mutation::{delete, insert, insert_or_update};
use gcloud_spanner::statement::{Statement, ToKind};
use gcloud_spanner::value::CommitTimestamp;
use serde_json::Value;
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;

/// Conservative bound on mutated cells per commit. Spanner rejects commits
/// above ~80,000 mutated cells (rows × columns, **plus** secondary-index
/// amplification the client cannot see), so the sink chunks well below it.
const CELL_BUDGET: usize = 60_000;

/// Live table metadata read from `INFORMATION_SCHEMA` (cached per sink).
#[derive(Debug, Clone)]
struct TableMeta {
    /// `(name, type, nullable)` in declared column order.
    columns: Vec<(String, SpannerType, bool)>,
    /// PRIMARY KEY column names, in key order.
    pk: Vec<String>,
}

impl TableMeta {
    fn type_of(&self, col: &str) -> Option<&SpannerType> {
        self.columns
            .iter()
            .find(|(name, _, _)| name == col)
            .map(|(_, ty, _)| ty)
    }

    fn has_column(&self, col: &str) -> bool {
        self.columns.iter().any(|(name, _, _)| name == col)
    }
}

/// One planned mutation plus the number of cells it mutates (drives the
/// per-commit cell budget).
#[derive(Debug)]
struct Planned {
    mutation: Mutation,
    cells: usize,
}

/// Which write mutation to build for a data row.
#[derive(Clone, Copy, PartialEq)]
enum WriteOp {
    /// Append mode — duplicate PKs fail the commit.
    Insert,
    /// Upsert mode — Spanner's native keyed upsert.
    InsertOrUpdate,
}

/// Build the mutation for one record: columns are the intersection of the
/// record's top-level fields with the table's columns, encoded per column
/// type. Fields with no matching column are dropped with a one-shot warning
/// (`warned` dedups across the sink's lifetime). A record with *no* matching
/// column, a non-object record, or an encode failure is an `Err` naming the
/// column.
fn build_row_mutation(
    table: &str,
    record: &Value,
    meta: &TableMeta,
    op: WriteOp,
    warned: &mut HashSet<String>,
) -> Result<Planned, String> {
    let obj = record
        .as_object()
        .ok_or_else(|| "record is not a JSON object".to_string())?;

    let mut cols: Vec<&str> = Vec::new();
    let mut vals: Vec<EncodedKind> = Vec::new();
    for (name, ty, _) in &meta.columns {
        if let Some(v) = obj.get(name) {
            let kind = encode_to_kind(v, ty).map_err(|e| format!("column `{name}`: {e}"))?;
            cols.push(name.as_str());
            vals.push(EncodedKind(kind));
        }
    }
    for key in obj.keys() {
        if !meta.has_column(key) && warned.insert(key.clone()) {
            tracing::warn!(
                field = %key,
                table = %table,
                "record field has no matching Spanner column; dropping it (warned once per field)"
            );
        }
    }
    if cols.is_empty() {
        return Err("record has no fields matching table columns".into());
    }

    let refs: Vec<&dyn ToKind> = vals.iter().map(|v| v as &dyn ToKind).collect();
    let mutation = match op {
        WriteOp::Insert => insert(table, &cols, &refs),
        WriteOp::InsertOrUpdate => insert_or_update(table, &cols, &refs),
    };
    Ok(Planned {
        mutation,
        cells: cols.len(),
    })
}

/// Build the delete mutation for one planned key tuple. Spanner delete keys
/// must be supplied in **PRIMARY KEY column order**, which may differ from
/// the configured `key` order — values are re-ordered here.
fn build_delete_mutation(
    table: &str,
    key_tuple: &faucet_core::KeyTuple,
    meta: &TableMeta,
) -> Result<Planned, String> {
    let mut vals: Vec<EncodedKind> = Vec::with_capacity(meta.pk.len());
    for pk_col in &meta.pk {
        let (_, v) = key_tuple
            .0
            .iter()
            .find(|(col, _)| col == pk_col)
            .ok_or_else(|| format!("delete key is missing PK column `{pk_col}`"))?;
        let ty = meta
            .type_of(pk_col)
            .ok_or_else(|| format!("PK column `{pk_col}` not found in table metadata"))?;
        let kind = encode_to_kind(v, ty).map_err(|e| format!("key column `{pk_col}`: {e}"))?;
        vals.push(EncodedKind(kind));
    }
    let refs: Vec<&dyn ToKind> = vals.iter().map(|v| v as &dyn ToKind).collect();
    Ok(Planned {
        mutation: delete(table, Key::composite(&refs)),
        cells: meta.pk.len().max(1),
    })
}

/// Split planned mutations into commit-sized chunks: at most `batch_size`
/// rows (0 = unbounded) and at most [`CELL_BUDGET`] cells per chunk. A single
/// row above the budget still ships alone — Spanner, not the sink, is the
/// authority on rejecting it.
fn chunk_by_cells(planned: Vec<Planned>, batch_size: usize, budget: usize) -> Vec<Vec<Mutation>> {
    let row_cap = if batch_size == 0 {
        usize::MAX
    } else {
        batch_size
    };
    let mut chunks: Vec<Vec<Mutation>> = Vec::new();
    let mut current: Vec<Mutation> = Vec::new();
    let mut cells = 0usize;
    for p in planned {
        if !current.is_empty() && (cells + p.cells > budget || current.len() >= row_cap) {
            chunks.push(std::mem::take(&mut current));
            cells = 0;
        }
        cells += p.cells;
        current.push(p.mutation);
    }
    if !current.is_empty() {
        chunks.push(current);
    }
    chunks
}

/// Upsert/delete requires the configured `key` to be exactly the table's
/// PRIMARY KEY columns (Spanner mutations always key on the PK). Order is
/// irrelevant — set equality.
fn validate_key_matches_pk(key: &[String], pk: &[String], table: &str) -> Result<(), FaucetError> {
    let key_set: HashSet<&str> = key.iter().map(|s| s.as_str()).collect();
    let pk_set: HashSet<&str> = pk.iter().map(|s| s.as_str()).collect();
    if key_set != pk_set {
        return Err(FaucetError::Config(format!(
            "spanner sink: write key {key:?} must equal table `{table}`'s PRIMARY KEY columns \
             {pk:?} (Spanner mutations always key on the primary key)"
        )));
    }
    Ok(())
}

/// Map a [`faucet_core::SqlBaseType`] to the Spanner DDL keyword used when
/// adding a column during schema evolution. Integers land as `INT64` and
/// floats as `FLOAT64` — Spanner's widest numeric types.
fn spanner_keyword(t: faucet_core::SqlBaseType) -> &'static str {
    use faucet_core::SqlBaseType::*;
    match t {
        Integer => "INT64",
        Double => "FLOAT64",
        Boolean => "BOOL",
        Text => "STRING(MAX)",
        Json => "JSON",
    }
}

/// Render a [`SpannerType`] back to its DDL form, used when re-emitting a
/// column to relax `NOT NULL`. `Other` (STRUCT/PROTO) cannot be re-emitted.
fn spanner_type_ddl(ty: &SpannerType) -> Result<String, String> {
    Ok(match ty {
        SpannerType::Bool => "BOOL".into(),
        SpannerType::Int64 => "INT64".into(),
        SpannerType::Float32 => "FLOAT32".into(),
        SpannerType::Float64 => "FLOAT64".into(),
        SpannerType::Timestamp => "TIMESTAMP".into(),
        SpannerType::Date => "DATE".into(),
        SpannerType::String => "STRING(MAX)".into(),
        SpannerType::Bytes => "BYTES(MAX)".into(),
        SpannerType::Numeric => "NUMERIC".into(),
        SpannerType::Json => "JSON".into(),
        SpannerType::Array(inner) => format!("ARRAY<{}>", spanner_type_ddl(inner)?),
        SpannerType::Other => {
            return Err("cannot re-emit a STRUCT/PROTO column in DDL".into());
        }
    })
}

/// `ALTER TABLE <t> ADD COLUMN IF NOT EXISTS <c> <kw>` — idempotent addition.
/// New columns are always nullable (Spanner requires a default for NOT NULL
/// additions, and drift additions are nullable by construction).
fn build_add_column_sql(table: &str, col: &str, t: faucet_core::SqlBaseType) -> String {
    format!(
        "ALTER TABLE {} ADD COLUMN IF NOT EXISTS {} {}",
        quote_ident_spanner(table),
        quote_ident_spanner(col),
        spanner_keyword(t)
    )
}

/// `ALTER TABLE <t> ALTER COLUMN <c> <TYPE>` — re-emitting the column at its
/// current type *without* `NOT NULL` relaxes the null constraint (Spanner has
/// no `DROP NOT NULL`). Idempotent.
fn build_alter_column_sql(table: &str, col: &str, type_ddl: &str) -> String {
    format!(
        "ALTER TABLE {} ALTER COLUMN {} {}",
        quote_ident_spanner(table),
        quote_ident_spanner(col),
        type_ddl
    )
}

/// Commit-token watermark table name. Spanner (GoogleSQL) identifiers must
/// start with a letter, so the canonical `_faucet_commit_token` used by the
/// other SQL sinks (`faucet_core::idempotency::COMMIT_TOKEN_TABLE`) is not a
/// valid Spanner table name — this sink drops the leading underscore.
pub(crate) const SPANNER_COMMIT_TOKEN_TABLE: &str = "faucet_commit_token";

/// DDL for the commit-token watermark table (exactly-once). `updated_at` is a
/// commit-timestamp column so replays are debuggable without a clock source.
fn commit_token_ddl() -> String {
    format!(
        "CREATE TABLE IF NOT EXISTS {t} ({s} STRING(MAX) NOT NULL, {k} STRING(MAX) NOT NULL, \
         updated_at TIMESTAMP OPTIONS (allow_commit_timestamp=true)) PRIMARY KEY ({s})",
        t = quote_ident_spanner(SPANNER_COMMIT_TOKEN_TABLE),
        s = quote_ident_spanner(faucet_core::idempotency::COMMIT_TOKEN_SCOPE_COL),
        k = quote_ident_spanner(faucet_core::idempotency::COMMIT_TOKEN_TOKEN_COL),
    )
}

/// Render cached metadata as the `infer_schema`-shaped object
/// (`{"type":"object","properties":{…}}`) the drift pass diffs against.
fn current_schema_json(meta: &TableMeta) -> Value {
    let mut props = serde_json::Map::new();
    for (name, ty, nullable) in &meta.columns {
        props.insert(name.clone(), spanner_type_to_json_schema(ty, *nullable));
    }
    serde_json::json!({ "type": "object", "properties": props })
}

fn sink_err(context: &str, e: impl std::fmt::Display) -> FaucetError {
    FaucetError::Sink(format!("spanner {context}: {e}"))
}

/// A sink that writes JSON records to a Cloud Spanner table.
pub struct SpannerSink {
    config: SpannerSinkConfig,
    client: Client,
    /// Cached table metadata; `None` until first use, cleared by
    /// [`evolve_schema`](faucet_core::Sink::evolve_schema).
    meta: tokio::sync::RwLock<Option<Arc<TableMeta>>>,
    /// One-shot commit-token-table creation guard.
    token_table_ready: tokio::sync::OnceCell<()>,
    /// Record fields already warned-about as having no matching column.
    warned_fields: std::sync::Mutex<HashSet<String>>,
}

impl SpannerSink {
    /// Create a new Spanner sink. Validates the config and builds the client
    /// (session pool). Table metadata is read lazily on first write.
    pub async fn new(config: SpannerSinkConfig) -> Result<Self, FaucetError> {
        config.validate()?;
        let client = config.connection.connect().await?;
        Ok(Self {
            config,
            client,
            meta: tokio::sync::RwLock::new(None),
            token_table_ready: tokio::sync::OnceCell::new(),
            warned_fields: std::sync::Mutex::new(HashSet::new()),
        })
    }

    /// Read `(columns, pk)` for the target table from `INFORMATION_SCHEMA`.
    /// Returns `Ok(None)` when the table does not exist.
    async fn fetch_meta(&self) -> Result<Option<TableMeta>, FaucetError> {
        let mut columns: Vec<(String, SpannerType, bool)> = Vec::new();
        {
            let mut tx = self
                .client
                .single()
                .await
                .map_err(|e| sink_err("metadata read", e))?;
            let mut stmt = Statement::new(
                "SELECT c.COLUMN_NAME AS name, c.SPANNER_TYPE AS spanner_type, \
                 c.IS_NULLABLE AS is_nullable FROM INFORMATION_SCHEMA.COLUMNS c \
                 WHERE c.TABLE_SCHEMA = '' AND c.TABLE_NAME = @table \
                 ORDER BY c.ORDINAL_POSITION",
            );
            stmt.add_param("table", &self.config.table_name);
            let mut iter = tx
                .query(stmt)
                .await
                .map_err(|e| sink_err("metadata query", e))?;
            while let Some(row) = iter
                .next()
                .await
                .map_err(|e| sink_err("metadata read", e))?
            {
                let name = row
                    .column_by_name::<String>("name")
                    .map_err(|e| sink_err("metadata decode", e))?;
                let ty = row
                    .column_by_name::<String>("spanner_type")
                    .map_err(|e| sink_err("metadata decode", e))?;
                let nullable = row
                    .column_by_name::<String>("is_nullable")
                    .map_err(|e| sink_err("metadata decode", e))?;
                columns.push((name, parse_spanner_type(&ty), nullable == "YES"));
            }
        }
        if columns.is_empty() {
            return Ok(None);
        }

        let mut pk: Vec<String> = Vec::new();
        {
            let mut tx = self
                .client
                .single()
                .await
                .map_err(|e| sink_err("metadata read", e))?;
            let mut stmt = Statement::new(
                "SELECT ic.COLUMN_NAME AS name FROM INFORMATION_SCHEMA.INDEX_COLUMNS ic \
                 WHERE ic.TABLE_SCHEMA = '' AND ic.TABLE_NAME = @table \
                 AND ic.INDEX_NAME = 'PRIMARY_KEY' ORDER BY ic.ORDINAL_POSITION",
            );
            stmt.add_param("table", &self.config.table_name);
            let mut iter = tx
                .query(stmt)
                .await
                .map_err(|e| sink_err("primary-key query", e))?;
            while let Some(row) = iter
                .next()
                .await
                .map_err(|e| sink_err("primary-key read", e))?
            {
                pk.push(
                    row.column_by_name::<String>("name")
                        .map_err(|e| sink_err("primary-key decode", e))?,
                );
            }
        }
        Ok(Some(TableMeta { columns, pk }))
    }

    /// Cached metadata, fetched on first use. Errors when the table is absent
    /// — every caller here is a write path that requires it.
    async fn require_meta(&self) -> Result<Arc<TableMeta>, FaucetError> {
        if let Some(meta) = self.meta.read().await.as_ref() {
            return Ok(Arc::clone(meta));
        }
        let mut slot = self.meta.write().await;
        // Another writer may have raced us here.
        if let Some(meta) = slot.as_ref() {
            return Ok(Arc::clone(meta));
        }
        let fetched = self.fetch_meta().await?.ok_or_else(|| {
            FaucetError::Sink(format!(
                "spanner table `{}` does not exist in {}",
                self.config.table_name,
                self.config.connection.database_path()
            ))
        })?;
        let arc = Arc::new(fetched);
        *slot = Some(Arc::clone(&arc));
        Ok(arc)
    }

    /// Plan the whole page into mutations (mode-aware). Returns the planned
    /// mutations and the written-row count, or the first failure.
    fn plan_page(
        &self,
        records: &[Value],
        meta: &TableMeta,
    ) -> Result<(Vec<Planned>, usize), FaucetError> {
        let table = &self.config.table_name;
        let mut warned = self
            .warned_fields
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());

        if matches!(self.config.write.write_mode, faucet_core::WriteMode::Append) {
            let mut planned = Vec::with_capacity(records.len());
            for (idx, record) in records.iter().enumerate() {
                let p = build_row_mutation(table, record, meta, WriteOp::Insert, &mut warned)
                    .map_err(|msg| {
                        FaucetError::Sink(format!("spanner append: row {idx}: {msg}"))
                    })?;
                planned.push(p);
            }
            let count = planned.len();
            return Ok((planned, count));
        }

        validate_key_matches_pk(&self.config.write.key, &meta.pk, table)?;
        let plan = faucet_core::plan_writes(records, &self.config.write);
        if let Some((idx, msg)) = plan.failed.first() {
            return Err(FaucetError::Sink(format!(
                "spanner {}: row {idx}: {msg}",
                self.config.write.write_mode.as_str()
            )));
        }
        let mut planned = Vec::with_capacity(plan.upserts.len() + plan.deletes.len());
        for record in &plan.upserts {
            let p = build_row_mutation(table, record, meta, WriteOp::InsertOrUpdate, &mut warned)
                .map_err(|msg| FaucetError::Sink(format!("spanner upsert: {msg}")))?;
            planned.push(p);
        }
        for key_tuple in &plan.deletes {
            let p = build_delete_mutation(table, key_tuple, meta)
                .map_err(|msg| FaucetError::Sink(format!("spanner delete: {msg}")))?;
            planned.push(p);
        }
        let count = planned.len();
        Ok((planned, count))
    }

    /// Run DDL statements through the admin API, bounded by
    /// `ddl_timeout_secs`.
    async fn run_ddl(&self, statements: Vec<String>) -> Result<(), FaucetError> {
        let admin = self.config.connection.connect_admin().await?;
        let mut op = admin
            .database()
            .update_database_ddl(
                UpdateDatabaseDdlRequest {
                    database: self.config.connection.database_path(),
                    statements,
                    ..Default::default()
                },
                None,
            )
            .await
            .map_err(|e| sink_err("DDL submit", e))?;
        let timeout = Duration::from_secs(self.config.ddl_timeout_secs);
        match tokio::time::timeout(timeout, op.wait(None)).await {
            Ok(Ok(_)) => Ok(()),
            Ok(Err(e)) => Err(sink_err("DDL operation", e)),
            Err(_) => Err(FaucetError::Sink(format!(
                "spanner DDL operation did not complete within {}s (ddl_timeout_secs)",
                self.config.ddl_timeout_secs
            ))),
        }
    }

    /// Ensure the commit-token watermark table exists (once per sink).
    async fn ensure_token_table(&self) -> Result<(), FaucetError> {
        self.token_table_ready
            .get_or_try_init(|| async { self.run_ddl(vec![commit_token_ddl()]).await })
            .await?;
        Ok(())
    }
}

#[async_trait]
impl faucet_core::Sink for SpannerSink {
    fn config_schema(&self) -> Value {
        serde_json::to_value(faucet_core::schema_for!(SpannerSinkConfig))
            .expect("schema serialization")
    }

    fn connector_name(&self) -> &'static str {
        "spanner"
    }

    fn supported_write_modes(&self) -> &'static [faucet_core::WriteMode] {
        &[
            faucet_core::WriteMode::Append,
            faucet_core::WriteMode::Upsert,
            faucet_core::WriteMode::Delete,
        ]
    }

    fn dedups_by_key(&self) -> bool {
        self.config.write.dedups_by_key()
    }

    fn supports_schema_evolution(&self) -> bool {
        true
    }

    /// Read the live destination schema from `INFORMATION_SCHEMA` as an
    /// `infer_schema`-shaped object, or `None` when the table does not exist
    /// yet (issue #194). Always fetches fresh — the drift pass caches on its
    /// side and refreshes after `evolve`.
    async fn current_schema(&self) -> Result<Option<Value>, FaucetError> {
        Ok(self.fetch_meta().await?.map(|m| current_schema_json(&m)))
    }

    /// Apply additive schema evolution: new columns via `ADD COLUMN IF NOT
    /// EXISTS`; nullability relaxations by re-emitting the column at its
    /// current type without `NOT NULL`. Spanner cannot change a column's base
    /// type (e.g. INT64→FLOAT64), so a base-type widening is a typed error —
    /// set `allow_type_widening: false` so the drift policy classifies it
    /// `incompatible` and routes it via `on_incompatible` instead.
    async fn evolve_schema(
        &self,
        evolution: &faucet_core::SchemaEvolution,
    ) -> Result<(), FaucetError> {
        let table = &self.config.table_name;
        let mut statements: Vec<String> = Vec::new();

        for c in &evolution.additions {
            let t =
                faucet_core::json_schema_base_type(&c.to).unwrap_or(faucet_core::SqlBaseType::Text);
            statements.push(build_add_column_sql(table, &c.name, t));
        }

        // Widenings: Spanner can only "widen" nullability. A widening whose
        // base type actually changes is unsupported.
        let meta = if evolution.widenings.is_empty() && evolution.relax_nullability.is_empty() {
            None
        } else {
            Some(self.require_meta().await?)
        };
        for c in &evolution.widenings {
            let from_base = c.from.as_ref().and_then(faucet_core::json_schema_base_type);
            let to_base = faucet_core::json_schema_base_type(&c.to);
            if from_base != to_base {
                return Err(FaucetError::Sink(format!(
                    "spanner cannot widen column `{}`'s base type ({:?} -> {:?}): Spanner does \
                     not support changing a column's type; set `allow_type_widening: false` so \
                     the drift policy treats this as incompatible",
                    c.name, from_base, to_base
                )));
            }
            let meta = meta.as_ref().expect("meta fetched when widenings present");
            let ty = meta.type_of(&c.name).ok_or_else(|| {
                FaucetError::Sink(format!(
                    "spanner widen: column `{}` not found in table `{table}`",
                    c.name
                ))
            })?;
            let ddl = spanner_type_ddl(ty).map_err(|e| {
                FaucetError::Sink(format!("spanner widen column `{}`: {e}", c.name))
            })?;
            statements.push(build_alter_column_sql(table, &c.name, &ddl));
        }
        for col in &evolution.relax_nullability {
            let meta = meta
                .as_ref()
                .expect("meta fetched when relaxations present");
            let ty = meta.type_of(col).ok_or_else(|| {
                FaucetError::Sink(format!(
                    "spanner relax: column `{col}` not found in table `{table}`"
                ))
            })?;
            let ddl = spanner_type_ddl(ty)
                .map_err(|e| FaucetError::Sink(format!("spanner relax column `{col}`: {e}")))?;
            statements.push(build_alter_column_sql(table, col, &ddl));
        }

        if !statements.is_empty() {
            self.run_ddl(statements).await?;
            // The table changed shape — drop the cached metadata.
            *self.meta.write().await = None;
        }
        Ok(())
    }

    fn dataset_uri(&self) -> String {
        format!(
            "spanner://{}/{}/{}/{}",
            self.config.connection.project_id,
            self.config.connection.instance,
            self.config.connection.database,
            self.config.table_name
        )
    }

    /// Preflight probes (`faucet doctor`): `auth` runs `SELECT 1`; `schema`
    /// verifies the target table exists and — for upsert/delete — that the
    /// configured `key` equals the table's PRIMARY KEY. Non-mutating.
    async fn check(
        &self,
        ctx: &faucet_core::check::CheckContext,
    ) -> Result<faucet_core::check::CheckReport, FaucetError> {
        use faucet_core::check::{CheckReport, Probe};

        let started = std::time::Instant::now();
        let select_one = async {
            let mut tx = self.client.single().await.map_err(|e| e.to_string())?;
            let mut iter = tx
                .query(Statement::new("SELECT 1"))
                .await
                .map_err(|e| e.to_string())?;
            iter.next().await.map_err(|e| e.to_string())?;
            Ok::<(), String>(())
        };
        let auth = match tokio::time::timeout(ctx.timeout, select_one).await {
            Ok(Ok(())) => Probe::pass("auth", started.elapsed()),
            Ok(Err(e)) => Probe::fail_hint(
                "auth",
                started.elapsed(),
                e,
                "check project/instance/database and credentials",
            ),
            Err(_) => Probe::fail_hint(
                "auth",
                started.elapsed(),
                "timed out",
                "check project/instance/database and credentials",
            ),
        };

        let started = std::time::Instant::now();
        let schema = match tokio::time::timeout(ctx.timeout, self.fetch_meta()).await {
            Ok(Ok(Some(meta))) => {
                if matches!(self.config.write.write_mode, faucet_core::WriteMode::Append) {
                    Probe::pass("schema", started.elapsed())
                } else {
                    match validate_key_matches_pk(
                        &self.config.write.key,
                        &meta.pk,
                        &self.config.table_name,
                    ) {
                        Ok(()) => Probe::pass("schema", started.elapsed()),
                        Err(e) => Probe::fail_hint(
                            "schema",
                            started.elapsed(),
                            e.to_string(),
                            "set `key` to exactly the table's PRIMARY KEY columns",
                        ),
                    }
                }
            }
            Ok(Ok(None)) => Probe::fail_hint(
                "schema",
                started.elapsed(),
                format!("table `{}` does not exist", self.config.table_name),
                "create the table (Spanner mutations require an existing table)",
            ),
            Ok(Err(e)) => Probe::fail("schema", started.elapsed(), e.to_string()),
            Err(_) => Probe::fail("schema", started.elapsed(), "timed out"),
        };

        Ok(CheckReport {
            probes: vec![auth, schema],
        })
    }

    /// Write records as batched mutations, one atomic commit per chunk
    /// (`batch_size` rows and ≤60,000 cells per commit). Append mode uses
    /// `Insert` (a duplicate PK fails the commit); upsert/delete are planned
    /// via [`faucet_core::plan_writes`].
    async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
        if records.is_empty() {
            return Ok(0);
        }
        let meta = self.require_meta().await?;
        let (planned, count) = self.plan_page(records, &meta)?;
        for chunk in chunk_by_cells(planned, self.config.batch_size, CELL_BUDGET) {
            self.client
                .apply(chunk)
                .await
                .map_err(|e| sink_err("commit", e))?;
        }
        tracing::info!(
            table = %self.config.table_name,
            rows = count,
            "Spanner write complete"
        );
        Ok(count)
    }

    /// Write a batch and report per-row outcomes.
    ///
    /// Append mode delegates to `write_batch` (all-or-nothing per chunk). In
    /// upsert/delete mode the good rows are applied and rows whose key could
    /// not be extracted are reported as per-row `Err` so the pipeline routes
    /// them to the DLQ.
    async fn write_batch_partial(
        &self,
        records: &[Value],
    ) -> Result<Vec<faucet_core::RowOutcome>, FaucetError> {
        if matches!(self.config.write.write_mode, faucet_core::WriteMode::Append) {
            self.write_batch(records).await?;
            return Ok(records.iter().map(|_| Ok(())).collect());
        }

        let meta = self.require_meta().await?;
        validate_key_matches_pk(&self.config.write.key, &meta.pk, &self.config.table_name)?;
        let plan = faucet_core::plan_writes(records, &self.config.write);

        let table = &self.config.table_name;
        let mut planned: Vec<Planned> = Vec::with_capacity(plan.upserts.len() + plan.deletes.len());
        {
            let mut warned = self
                .warned_fields
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            for record in &plan.upserts {
                let p =
                    build_row_mutation(table, record, &meta, WriteOp::InsertOrUpdate, &mut warned)
                        .map_err(|msg| FaucetError::Sink(format!("spanner upsert: {msg}")))?;
                planned.push(p);
            }
        }
        for key_tuple in &plan.deletes {
            let p = build_delete_mutation(table, key_tuple, &meta)
                .map_err(|msg| FaucetError::Sink(format!("spanner delete: {msg}")))?;
            planned.push(p);
        }
        for chunk in chunk_by_cells(planned, self.config.batch_size, CELL_BUDGET) {
            self.client
                .apply(chunk)
                .await
                .map_err(|e| sink_err("commit", e))?;
        }

        let mut outcomes: Vec<faucet_core::RowOutcome> = records.iter().map(|_| Ok(())).collect();
        for (idx, msg) in &plan.failed {
            outcomes[*idx] = Err(FaucetError::Sink(format!(
                "spanner {}: {msg}",
                self.config.write.write_mode.as_str()
            )));
        }
        Ok(outcomes)
    }

    async fn flush(&self) -> Result<(), FaucetError> {
        // Every commit is already durable; nothing is buffered client-side.
        Ok(())
    }

    fn supports_idempotent_writes(&self) -> bool {
        true
    }

    /// Read the last committed watermark token for `scope` from the
    /// `faucet_commit_token` table. `Ok(None)` when the table (or row) does
    /// not exist yet.
    async fn last_committed_token(&self, scope: &str) -> Result<Option<String>, FaucetError> {
        let mut tx = self
            .client
            .single()
            .await
            .map_err(|e| sink_err("token read", e))?;
        let scope_key = scope.to_string();
        match tx
            .read_row(
                SPANNER_COMMIT_TOKEN_TABLE,
                &[faucet_core::idempotency::COMMIT_TOKEN_TOKEN_COL],
                Key::new(&scope_key),
            )
            .await
        {
            Ok(Some(row)) => Ok(Some(
                row.column_by_name::<String>(faucet_core::idempotency::COMMIT_TOKEN_TOKEN_COL)
                    .map_err(|e| sink_err("token decode", e))?,
            )),
            Ok(None) => Ok(None),
            // First exactly-once run: the watermark table hasn't been created
            // yet — that's "no token", not an error.
            Err(status) if status.code() == gcloud_gax::grpc::Code::NotFound => Ok(None),
            Err(status)
                if status.message().to_ascii_lowercase().contains("not found")
                    || status.message().contains(SPANNER_COMMIT_TOKEN_TABLE) =>
            {
                Ok(None)
            }
            Err(status) => Err(sink_err("token read", status)),
        }
    }

    /// Commit the page's mutations **and** the `(scope, token)` watermark in
    /// one read-write transaction — on crash either both land or neither
    /// does. The whole page is a single commit here (no re-chunking), so it
    /// is bounded by Spanner's ~80,000-cell commit limit: size the source's
    /// `batch_size` down for very wide tables.
    async fn write_batch_idempotent(
        &self,
        records: &[Value],
        scope: &str,
        token: &str,
    ) -> Result<usize, FaucetError> {
        self.ensure_token_table().await?;
        let meta = self.require_meta().await?;
        // Plan before the transaction so planning failures abort cleanly.
        let (planned, count) = self.plan_page(records, &meta)?;

        let mut mutations: Vec<Mutation> = planned.into_iter().map(|p| p.mutation).collect();
        let scope_owned = scope.to_string();
        // The token may carry a `#<bookmark>` suffix — stored verbatim,
        // never parsed.
        let token_owned = token.to_string();
        mutations.push(insert_or_update(
            SPANNER_COMMIT_TOKEN_TABLE,
            &[
                faucet_core::idempotency::COMMIT_TOKEN_SCOPE_COL,
                faucet_core::idempotency::COMMIT_TOKEN_TOKEN_COL,
                "updated_at",
            ],
            &[&scope_owned, &token_owned, &CommitTimestamp::new()],
        ));

        // The closure retries on ABORTED and must not keep state between
        // attempts — it clones the pre-built mutation set per attempt.
        let mutations = Arc::new(mutations);
        self.client
            .read_write_transaction(move |tx| {
                let mutations = Arc::clone(&mutations);
                Box::pin(async move {
                    tx.buffer_write(mutations.as_ref().clone());
                    Ok::<(), gcloud_spanner::client::Error>(())
                })
            })
            .await
            .map_err(|e| sink_err("idempotent commit", e))?;
        Ok(count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use faucet_core::{WriteMode, WriteSpec};
    use gcloud_googleapis::spanner::v1::mutation::Operation;
    use serde_json::json;

    fn meta() -> TableMeta {
        TableMeta {
            columns: vec![
                ("id".into(), SpannerType::Int64, false),
                ("name".into(), SpannerType::String, true),
                ("score".into(), SpannerType::Float64, true),
                ("meta".into(), SpannerType::Json, true),
            ],
            pk: vec!["id".into()],
        }
    }

    fn composite_meta() -> TableMeta {
        TableMeta {
            columns: vec![
                ("tenant".into(), SpannerType::String, false),
                ("id".into(), SpannerType::Int64, false),
                ("v".into(), SpannerType::String, true),
            ],
            pk: vec!["tenant".into(), "id".into()],
        }
    }

    fn write_columns(m: &Mutation) -> Vec<String> {
        match m.operation.as_ref().expect("operation") {
            Operation::Insert(w) | Operation::InsertOrUpdate(w) => w.columns.clone(),
            other => panic!("expected write mutation, got {other:?}"),
        }
    }

    #[test]
    fn row_mutation_intersects_columns_and_encodes() {
        let mut warned = HashSet::new();
        let p = build_row_mutation(
            "t",
            &json!({"id": 1, "name": "a", "unknown": true}),
            &meta(),
            WriteOp::Insert,
            &mut warned,
        )
        .unwrap();
        assert_eq!(p.cells, 2);
        assert_eq!(write_columns(&p.mutation), vec!["id", "name"]);
        // Unknown field warned exactly once.
        assert!(warned.contains("unknown"));
        // A second record with the same unknown field doesn't re-insert.
        let before = warned.len();
        build_row_mutation(
            "t",
            &json!({"id": 2, "unknown": false}),
            &meta(),
            WriteOp::Insert,
            &mut warned,
        )
        .unwrap();
        assert_eq!(warned.len(), before);
    }

    #[test]
    fn row_mutation_rejects_non_objects_and_no_matches() {
        let mut warned = HashSet::new();
        assert!(
            build_row_mutation("t", &json!([1]), &meta(), WriteOp::Insert, &mut warned)
                .unwrap_err()
                .contains("not a JSON object")
        );
        assert!(
            build_row_mutation(
                "t",
                &json!({"nope": 1}),
                &meta(),
                WriteOp::Insert,
                &mut warned
            )
            .unwrap_err()
            .contains("no fields matching")
        );
    }

    #[test]
    fn row_mutation_surfaces_encode_errors_with_column_name() {
        let mut warned = HashSet::new();
        let err = build_row_mutation(
            "t",
            &json!({"id": "not-an-int-at-all"}),
            &meta(),
            WriteOp::Insert,
            &mut warned,
        )
        .unwrap_err();
        assert!(err.contains("column `id`"), "err: {err}");
    }

    #[test]
    fn upsert_op_builds_insert_or_update() {
        let mut warned = HashSet::new();
        let p = build_row_mutation(
            "t",
            &json!({"id": 1}),
            &meta(),
            WriteOp::InsertOrUpdate,
            &mut warned,
        )
        .unwrap();
        assert!(matches!(
            p.mutation.operation,
            Some(Operation::InsertOrUpdate(_))
        ));
    }

    #[test]
    fn delete_mutation_reorders_keys_into_pk_order() {
        // WriteSpec key order differs from PK order.
        let kt = faucet_core::KeyTuple(vec![
            ("id".into(), json!(7)),
            ("tenant".into(), json!("acme")),
        ]);
        let p = build_delete_mutation("t", &kt, &composite_meta()).unwrap();
        assert_eq!(p.cells, 2);
        let Some(Operation::Delete(d)) = &p.mutation.operation else {
            panic!("expected delete");
        };
        let keys = &d.key_set.as_ref().expect("key set").keys;
        assert_eq!(keys.len(), 1);
        // PK order is (tenant, id) — first value must be the tenant string.
        let vals = &keys[0].values;
        assert_eq!(
            vals[0].kind,
            Some(prost_types::value::Kind::StringValue("acme".into()))
        );
        assert_eq!(
            vals[1].kind,
            Some(prost_types::value::Kind::StringValue("7".into()))
        );
    }

    #[test]
    fn delete_mutation_errors_on_missing_pk_column() {
        let kt = faucet_core::KeyTuple(vec![("id".into(), json!(7))]);
        let err = build_delete_mutation("t", &kt, &composite_meta()).unwrap_err();
        assert!(err.contains("missing PK column `tenant`"));
    }

    fn planned(cells: usize) -> Planned {
        Planned {
            mutation: insert("t", &["a"], &[&"x".to_string()]),
            cells,
        }
    }

    #[test]
    fn chunking_respects_cell_budget() {
        let chunks = chunk_by_cells(vec![planned(40), planned(40), planned(40)], 0, 100);
        assert_eq!(chunks.iter().map(Vec::len).collect::<Vec<_>>(), vec![2, 1]);
    }

    #[test]
    fn chunking_respects_row_cap() {
        let chunks = chunk_by_cells((0..5).map(|_| planned(1)).collect(), 2, 1000);
        assert_eq!(
            chunks.iter().map(Vec::len).collect::<Vec<_>>(),
            vec![2, 2, 1]
        );
    }

    #[test]
    fn chunking_lets_an_oversized_row_ship_alone() {
        let chunks = chunk_by_cells(vec![planned(500), planned(1)], 0, 100);
        assert_eq!(chunks.iter().map(Vec::len).collect::<Vec<_>>(), vec![1, 1]);
    }

    #[test]
    fn chunking_zero_batch_size_is_cell_bounded_only() {
        let chunks = chunk_by_cells((0..100).map(|_| planned(1)).collect(), 0, 1000);
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].len(), 100);
    }

    #[test]
    fn chunking_empty_input_is_empty() {
        assert!(chunk_by_cells(vec![], 10, 100).is_empty());
    }

    #[test]
    fn key_pk_validation_is_order_insensitive_set_equality() {
        let key = vec!["id".to_string(), "tenant".to_string()];
        let pk = vec!["tenant".to_string(), "id".to_string()];
        assert!(validate_key_matches_pk(&key, &pk, "t").is_ok());
        let err = validate_key_matches_pk(&["id".to_string()], &pk, "t").unwrap_err();
        assert!(err.to_string().contains("PRIMARY KEY"));
        assert!(matches!(err, FaucetError::Config(_)));
    }

    #[test]
    fn add_column_ddl() {
        assert_eq!(
            build_add_column_sql("t", "email", faucet_core::SqlBaseType::Text),
            "ALTER TABLE `t` ADD COLUMN IF NOT EXISTS `email` STRING(MAX)"
        );
        assert_eq!(
            build_add_column_sql("t", "n", faucet_core::SqlBaseType::Integer),
            "ALTER TABLE `t` ADD COLUMN IF NOT EXISTS `n` INT64"
        );
        assert_eq!(
            build_add_column_sql("t", "j", faucet_core::SqlBaseType::Json),
            "ALTER TABLE `t` ADD COLUMN IF NOT EXISTS `j` JSON"
        );
    }

    #[test]
    fn alter_column_ddl_reemits_type_without_not_null() {
        assert_eq!(
            build_alter_column_sql("t", "name", "STRING(MAX)"),
            "ALTER TABLE `t` ALTER COLUMN `name` STRING(MAX)"
        );
    }

    #[test]
    fn spanner_type_ddl_round_trips() {
        assert_eq!(spanner_type_ddl(&SpannerType::Int64).unwrap(), "INT64");
        assert_eq!(
            spanner_type_ddl(&SpannerType::Array(Box::new(SpannerType::Float64))).unwrap(),
            "ARRAY<FLOAT64>"
        );
        assert_eq!(spanner_type_ddl(&SpannerType::Bytes).unwrap(), "BYTES(MAX)");
        assert!(spanner_type_ddl(&SpannerType::Other).is_err());
    }

    #[test]
    fn commit_token_ddl_shape() {
        let ddl = commit_token_ddl();
        assert!(ddl.contains("CREATE TABLE IF NOT EXISTS `faucet_commit_token`"));
        assert!(ddl.contains("`scope` STRING(MAX) NOT NULL"));
        assert!(ddl.contains("`token` STRING(MAX) NOT NULL"));
        assert!(ddl.contains("allow_commit_timestamp=true"));
        assert!(ddl.ends_with("PRIMARY KEY (`scope`)"));
    }

    #[test]
    fn current_schema_json_shape() {
        let schema = current_schema_json(&meta());
        assert_eq!(schema["type"], "object");
        assert_eq!(schema["properties"]["id"]["type"], "integer");
        assert_eq!(
            schema["properties"]["name"]["type"],
            json!(["string", "null"])
        );
        assert_eq!(
            schema["properties"]["score"]["type"],
            json!(["number", "null"])
        );
    }

    #[test]
    fn table_meta_lookups() {
        let m = meta();
        assert!(m.has_column("id"));
        assert!(!m.has_column("missing"));
        assert_eq!(m.type_of("score"), Some(&SpannerType::Float64));
        assert_eq!(m.type_of("missing"), None);
    }

    /// Offline plan_page coverage through a sink built without a server —
    /// exercised via the pure helpers instead (plan_page requires a client).
    #[test]
    fn write_spec_key_reorder_composite_delete_encodes_types() {
        let kt = faucet_core::KeyTuple(vec![
            ("tenant".into(), json!("t1")),
            ("id".into(), json!(9_007_199_254_740_993_i64)),
        ]);
        let p = build_delete_mutation("t", &kt, &composite_meta()).unwrap();
        let Some(Operation::Delete(d)) = &p.mutation.operation else {
            panic!("expected delete");
        };
        let vals = &d.key_set.as_ref().unwrap().keys[0].values;
        // INT64 keys travel string-encoded, losslessly.
        assert_eq!(
            vals[1].kind,
            Some(prost_types::value::Kind::StringValue(
                "9007199254740993".into()
            ))
        );
    }

    #[test]
    fn plan_writes_integration_with_write_spec() {
        // Sanity: plan_writes + our builders compose (dedup by key, delete marker).
        let spec = WriteSpec {
            write_mode: WriteMode::Upsert,
            key: vec!["id".into()],
            delete_marker: Some(faucet_core::DeleteMarker {
                field: "__op".into(),
                values: vec!["d".into()],
            }),
        };
        let page = vec![
            json!({"id": 1, "name": "a"}),
            json!({"id": 1, "name": "b"}),
            json!({"id": 2, "__op": "d"}),
            json!({"name": "no-key"}),
        ];
        let plan = faucet_core::plan_writes(&page, &spec);
        assert_eq!(plan.upserts.len(), 1); // last-write-wins dedup
        assert_eq!(plan.deletes.len(), 1);
        assert_eq!(plan.failed.len(), 1);
        let mut warned = HashSet::new();
        for u in &plan.upserts {
            build_row_mutation("t", u, &meta(), WriteOp::InsertOrUpdate, &mut warned).unwrap();
        }
        for d in &plan.deletes {
            build_delete_mutation("t", d, &meta()).unwrap();
        }
    }
}