spate-clickhouse 0.1.0

ClickHouse sink for the Spate framework: direct-to-shard writes with replica rotation, batch flushing, and insert deduplication tokens. Applications should depend on the `spate` facade crate with the `clickhouse` feature.
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
//! Opt-in startup schema validation: fetch the target table's columns
//! from every replica, fail fast (with a readable diff) when the
//! configured columns cannot work, and hand the encoder a parsed,
//! config-ordered schema for its first-record struct check.
//!
//! Two moments, two checks:
//!
//! - **Startup** (`validate`): the config's `columns` against
//!   `system.columns` on every replica of every shard — missing columns,
//!   non-insertable (MATERIALIZED/ALIAS) columns, inter-replica drift.
//!   Config order differing from *table* order is deliberately not an
//!   error: the `INSERT` column list maps by name, so only the struct
//!   must agree with the config.
//! - **First record** (`check_first_record`, driven by both encoders —
//!   RowBinary's [`crate::ClickHouseEncoder::with_schema`] and the Native
//!   encoder whenever its schema was fetched): the row struct's probed
//!   field names, order, and — in `full` mode — type classes against the
//!   configured columns, including wire-wrapper scale vs the column's
//!   declared precision (`DateTime64Millis` into `DateTime64(6)` fails
//!   here). This is where the positional wire contract is actually
//!   enforced.

pub(crate) mod probe;
pub(crate) mod typeparse;

use crate::config::SchemaValidation;
use crate::writer::ClickHouseEndpoint;
use probe::{FieldShape, Shape, compatible};
use serde::Deserialize;
use std::fmt::Write as _;
use std::sync::Arc;
use typeparse::ChType;

/// Schema validation failed at sink startup.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SchemaError {
    /// A replica's schema could not be fetched. The user opted into
    /// fail-fast validation, so connectivity problems fail startup too —
    /// the message keeps "could not fetch" and "mismatch" distinguishable
    /// at a glance.
    #[error("sink.clickhouse: could not fetch schema for {table} from {url}: {reason}")]
    Fetch {
        /// The (possibly database-qualified) table.
        table: String,
        /// The replica URL.
        url: String,
        /// Human-readable cause.
        reason: String,
    },
    /// The configuration does not match the live table (pre-formatted
    /// multi-line diff).
    #[error("{0}")]
    Mismatch(String),
}

/// The validated, config-ordered expected schema. Produced by
/// [`crate::ClickHouseSink::validate_schema`]; consumed by
/// [`crate::ClickHouseEncoder::with_schema`] for the first-record check.
/// Opaque: holds no `clickhouse` crate types.
#[derive(Debug)]
pub struct RowSchema {
    pub(crate) mode: SchemaValidation,
    pub(crate) table: String,
    /// `(column name, parsed type, raw type string)` in **config** order.
    pub(crate) columns: Vec<(String, ChType, String)>,
}

/// What `build()` captures for a later `validate_schema()` call.
#[derive(Clone, Debug)]
pub(crate) struct SchemaCheck {
    pub(crate) mode: SchemaValidation,
    pub(crate) database: Option<String>,
    pub(crate) table: String,
    pub(crate) columns: Vec<String>,
}

/// One row of `system.columns`, crate-private.
#[derive(Clone, Debug, PartialEq, Eq, clickhouse::Row, Deserialize)]
pub(crate) struct ColumnRow {
    pub(crate) name: String,
    #[serde(rename = "type")]
    pub(crate) type_: String,
    pub(crate) default_kind: String,
}

impl SchemaCheck {
    /// `(database, bare table)` — a `db.table` qualification wins over the
    /// config's `database` field, mirroring how the INSERT resolves.
    fn target(&self) -> (Option<&str>, &str) {
        match self.table.split_once('.') {
            Some((db, tbl)) => (Some(db), tbl),
            None => (self.database.as_deref(), &self.table),
        }
    }

    fn display_table(&self) -> String {
        match self.target() {
            (Some(db), tbl) => format!("`{db}`.`{tbl}`"),
            (None, tbl) => format!("`{tbl}`"),
        }
    }
}

/// Fetch one replica's view of the table.
async fn fetch_columns(
    endpoint: &ClickHouseEndpoint,
    check: &SchemaCheck,
) -> Result<Vec<ColumnRow>, SchemaError> {
    let (db, table) = check.target();
    let query = match db {
        Some(db) => endpoint
            .client()
            .query(
                "SELECT name, type, default_kind FROM system.columns \
                 WHERE database = ? AND table = ? ORDER BY position",
            )
            .bind(db)
            .bind(table),
        None => endpoint
            .client()
            .query(
                "SELECT name, type, default_kind FROM system.columns \
                 WHERE database = currentDatabase() AND table = ? ORDER BY position",
            )
            .bind(table),
    };
    query
        .fetch_all::<ColumnRow>()
        .await
        .map_err(|e| SchemaError::Fetch {
            table: check.display_table(),
            url: endpoint.url().to_string(),
            reason: e.to_string(),
        })
}

fn table_column_list(cols: &[ColumnRow]) -> String {
    cols.iter()
        .map(|c| {
            if c.default_kind.is_empty() {
                format!("{} {}", c.name, c.type_)
            } else {
                format!("{} {} {}", c.name, c.type_, c.default_kind)
            }
        })
        .collect::<Vec<_>>()
        .join(", ")
}

/// If a configured column's declared type is an `AggregateFunction(...)`,
/// return an actionable error explaining the correct ingestion path.
///
/// The sink cannot write aggregate *states* directly: their wire form is the
/// opaque, unframed, version-dependent internal serialization, and
/// reproducing it client-side would couple us to one ClickHouse serialization
/// version. A sink pointed straight at such a column is therefore a
/// misconfiguration — the supported pattern is to INSERT raw rows into an
/// `ENGINE = Null` landing table and let a `MATERIALIZED VIEW` build the
/// states into the target.
///
/// Matched on the exact constructor name so `SimpleAggregateFunction(...)`,
/// which stores the raw value and *is* directly insertable, is deliberately
/// not flagged.
fn aggregate_function_remedy(col: &str, type_: &str) -> Option<String> {
    let ctor = type_.split_once('(').map(|(name, _)| name.trim())?;
    if ctor != "AggregateFunction" {
        return None;
    }
    Some(format!(
        "configured column `{col}` has type `{type_}`: the sink cannot write \
         aggregate states directly (their wire format is opaque and \
         version-dependent). Insert raw rows into an `ENGINE = Null` landing \
         table and let a `MATERIALIZED VIEW` compute the states \
         (minState/maxState/sumMapState/...) into this table. Client-side \
         aggregate-state serialization is not supported."
    ))
}

/// Startup validation against every replica of every shard. `Ok(None)`
/// when the mode is `Off`; `Ok(Some(schema))` for the encoder otherwise.
pub(crate) async fn validate(
    check: &SchemaCheck,
    endpoints: &[Vec<ClickHouseEndpoint>],
) -> Result<Option<Arc<RowSchema>>, SchemaError> {
    if check.mode == SchemaValidation::Off {
        return Ok(None);
    }

    // Every replica must agree — shard-local tables drift independently,
    // and a broken replica should surface now, not at its first rotated
    // write.
    let mut reference: Option<(String, Vec<ColumnRow>)> = None;
    for shard in endpoints {
        for endpoint in shard {
            let cols = fetch_columns(endpoint, check).await?;
            if cols.is_empty() {
                return Err(SchemaError::Mismatch(format!(
                    "sink.clickhouse: table {} not found (or not visible to this user) \
                     on replica {}",
                    check.display_table(),
                    endpoint.url()
                )));
            }
            match &reference {
                None => reference = Some((endpoint.url().to_string(), cols)),
                Some((ref_url, ref_cols)) => {
                    if *ref_cols != cols {
                        return Err(SchemaError::Mismatch(format!(
                            "sink.clickhouse: replicas disagree on the schema of {}:\n  \
                             {ref_url}: {}\n  {}: {}",
                            check.display_table(),
                            table_column_list(ref_cols),
                            endpoint.url(),
                            table_column_list(&cols),
                        )));
                    }
                }
            }
        }
    }
    let (ref_url, table_cols) =
        reference.expect("config validation guarantees at least one replica");

    let mut findings = Vec::new();
    for col in &check.columns {
        match table_cols.iter().find(|c| c.name == *col) {
            None => findings.push(format!(
                "configured column `{col}` does not exist in the table"
            )),
            Some(c) if c.default_kind == "MATERIALIZED" || c.default_kind == "ALIAS" => {
                findings.push(format!(
                    "configured column `{col}` is {} and cannot be inserted into",
                    c.default_kind
                ));
            }
            Some(c) => {
                if let Some(msg) = aggregate_function_remedy(col, &c.type_) {
                    findings.push(msg);
                }
            }
        }
    }
    for c in &table_cols {
        if !check.columns.contains(&c.name) && c.default_kind.is_empty() {
            tracing::warn!(
                table = %check.display_table(),
                column = %c.name,
                r#type = %c.type_,
                "table column is not in the configured insert columns and has no DEFAULT; \
                 the server will fill type-default values"
            );
        }
    }

    if !findings.is_empty() {
        let mut msg = format!(
            "sink.clickhouse: schema validation failed for {} (replica {ref_url}):\n",
            check.display_table()
        );
        for f in &findings {
            let _ = writeln!(msg, "  - {f}");
        }
        let _ = writeln!(
            msg,
            "  table columns:      {}",
            table_column_list(&table_cols)
        );
        let _ = write!(msg, "  configured columns: {}", check.columns.join(", "));
        return Err(SchemaError::Mismatch(msg));
    }

    let columns = check
        .columns
        .iter()
        .map(|name| {
            let c = table_cols
                .iter()
                .find(|c| c.name == *name)
                .expect("checked above");
            (name.clone(), typeparse::parse(&c.type_), c.type_.clone())
        })
        .collect();
    Ok(Some(Arc::new(RowSchema {
        mode: check.mode,
        table: check.display_table(),
        columns,
    })))
}

/// The first-record struct check: field names and order against the
/// configured columns (both modes), plus class-based type compatibility
/// per position (`full` mode). Returns the pre-formatted diff on failure.
pub(crate) fn check_first_record(schema: &RowSchema, fields: &[FieldShape]) -> Result<(), String> {
    let mut findings = Vec::new();
    if fields.len() != schema.columns.len() {
        findings.push(format!(
            "row struct serialized {} field(s) but {} column(s) are configured \
             (a #[serde(skip)] attribute shortens rows silently)",
            fields.len(),
            schema.columns.len()
        ));
    }
    for (i, (field, (col, ty, ty_str))) in fields.iter().zip(&schema.columns).enumerate() {
        if field.name != col {
            findings.push(format!(
                "position {i}: struct field `{}` vs configured column `{col}`",
                field.name
            ));
        } else if schema.mode == SchemaValidation::Full && !compatible(&field.shape, ty) {
            findings.push(format!(
                "position {i}: struct field `{}` ({}) is not compatible with `{col}` {ty_str}",
                field.name,
                shape_desc(&field.shape),
            ));
        }
    }
    if findings.is_empty() {
        return Ok(());
    }
    let mut msg = format!(
        "row struct does not match configured columns for {}:\n",
        schema.table
    );
    for f in &findings {
        let _ = writeln!(msg, "  - {f}");
    }
    let _ = writeln!(
        msg,
        "  struct fields (declaration order): {}",
        fields.iter().map(|f| f.name).collect::<Vec<_>>().join(", ")
    );
    let _ = write!(
        msg,
        "  configured columns:                {}",
        schema
            .columns
            .iter()
            .map(|(name, ..)| name.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    );
    Err(msg)
}

fn shape_desc(shape: &Shape) -> String {
    match shape {
        Shape::Bool => "bool".into(),
        Shape::I8 => "i8".into(),
        Shape::I16 => "i16".into(),
        Shape::I32 => "i32".into(),
        Shape::I64 => "i64".into(),
        Shape::I128 => "i128".into(),
        Shape::U8 => "u8".into(),
        Shape::U16 => "u16".into(),
        Shape::U32 => "u32".into(),
        Shape::U64 => "u64".into(),
        Shape::U128 => "u128".into(),
        Shape::F32 => "f32".into(),
        Shape::F64 => "f64".into(),
        Shape::Str => "String".into(),
        Shape::Bytes => "bytes".into(),
        Shape::Option(inner) => format!("Option<{}>", shape_desc(inner)),
        Shape::Seq(inner) => format!("Vec<{}>", shape_desc(inner)),
        Shape::Map(k, v) => format!("Map<{}, {}>", shape_desc(k), shape_desc(v)),
        Shape::Tuple(elems) => format!(
            "({})",
            elems.iter().map(shape_desc).collect::<Vec<_>>().join(", ")
        ),
        Shape::Struct(fields) => format!(
            "struct {{ {} }}",
            fields
                .iter()
                .map(|f| f.name.to_string())
                .collect::<Vec<_>>()
                .join(", ")
        ),
        Shape::Newtype(name, _) => (*name).to_string(),
        Shape::Unknown => "_".into(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use probe::probe_row;
    use serde::Serialize;

    fn schema(mode: SchemaValidation, cols: &[(&str, &str)]) -> RowSchema {
        RowSchema {
            mode,
            table: "`orders`".into(),
            columns: cols
                .iter()
                .map(|(n, t)| ((*n).to_string(), typeparse::parse(t), (*t).to_string()))
                .collect(),
        }
    }

    #[derive(Serialize)]
    struct Row {
        id: u64,
        amount: i64,
        name: String,
    }

    fn fields() -> Vec<FieldShape> {
        probe_row(&Row {
            id: 1,
            amount: 2,
            name: "x".into(),
        })
        .unwrap()
    }

    #[test]
    fn matching_struct_passes_both_modes() {
        for mode in [SchemaValidation::Names, SchemaValidation::Full] {
            let s = schema(
                mode,
                &[("id", "UInt64"), ("amount", "Int64"), ("name", "String")],
            );
            assert_eq!(check_first_record(&s, &fields()), Ok(()));
        }
    }

    #[test]
    fn field_order_mismatch_is_reported_per_position() {
        let s = schema(
            SchemaValidation::Names,
            &[("id", "UInt64"), ("name", "String"), ("amount", "Int64")],
        );
        let err = check_first_record(&s, &fields()).unwrap_err();
        assert!(err.contains("position 1: struct field `amount` vs configured column `name`"));
        assert!(err.contains("position 2: struct field `name` vs configured column `amount`"));
        assert!(err.contains("struct fields (declaration order): id, amount, name"));
        assert!(err.contains("configured columns:                id, name, amount"));
    }

    #[test]
    fn type_mismatches_only_fail_full_mode() {
        let cols = [
            ("id", "UInt64"),
            ("amount", "DateTime"), // i64 field is not a u32 DateTime
            ("name", "String"),
        ];
        let names = schema(SchemaValidation::Names, &cols);
        assert_eq!(check_first_record(&names, &fields()), Ok(()));

        let full = schema(SchemaValidation::Full, &cols);
        let err = check_first_record(&full, &fields()).unwrap_err();
        assert!(
            err.contains("struct field `amount` (i64) is not compatible with `amount` DateTime"),
            "{err}"
        );
    }

    #[test]
    fn field_count_mismatch_names_the_skip_footgun() {
        let s = schema(SchemaValidation::Names, &[("id", "UInt64")]);
        let err = check_first_record(&s, &fields()).unwrap_err();
        assert!(err.contains("serialized 3 field(s) but 1 column(s)"));
        assert!(err.contains("#[serde(skip)]"));
    }

    #[test]
    fn aggregate_function_columns_are_flagged_with_the_null_mv_remedy() {
        for ty in [
            "AggregateFunction(min, DateTime)",
            "AggregateFunction(max, DateTime)",
            "AggregateFunction(sumMap, Map(String, UInt64))",
        ] {
            let msg = aggregate_function_remedy("agg", ty)
                .unwrap_or_else(|| panic!("`{ty}` should be flagged"));
            assert!(msg.contains(ty), "{msg}");
            assert!(msg.contains("Null"), "{msg}");
            assert!(msg.contains("MATERIALIZED VIEW"), "{msg}");
        }
    }

    #[test]
    fn directly_insertable_types_are_not_flagged() {
        for ty in [
            // SimpleAggregateFunction stores the raw value — insertable.
            "SimpleAggregateFunction(sum, UInt64)",
            "SimpleAggregateFunction(sumMap, Map(String, UInt64))",
            "UInt64",
            "Map(String, UInt64)",
            "DateTime",
        ] {
            assert_eq!(aggregate_function_remedy("c", ty), None, "{ty}");
        }
    }
}