smooai-clickhouse-kit 0.3.0

Safe-by-construction ClickHouse schema toolkit: an allowlisted type system for user-defined / multi-tenant schemas, DDL generation, forward-only migrations, and drift detection. Rows stay Serde-native.
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
//! Safe-by-construction primitives for user-defined / multi-tenant ClickHouse
//! schemas — the Rust-canonical port of `@smooai/clickhouse-kit`'s safety core.
//!
//! When column names + types come from untrusted input (a customer config, a DB
//! row, JSON), these make SQL injection and unbounded tables impossible on the
//! happy path. In Rust the type allowlist is even stronger than the TS version:
//! disallowed types (`Decimal`, `FixedString`, `Tuple`, …) have **no representation**
//! in [`ColumnTypeSpec`], so untrusted input naming them fails to deserialize.

use serde::Deserialize;

/// Raised when untrusted schema input violates a safety rule.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum SchemaError {
    #[error("empty {0} name")]
    EmptyIdentifier(&'static str),
    #[error("{kind} name too long: {len} > {max}")]
    IdentifierTooLong {
        kind: &'static str,
        len: usize,
        max: usize,
    },
    #[error("invalid {kind} name {name:?}: must match ^[A-Za-z_][A-Za-z0-9_]*$")]
    InvalidIdentifier { kind: &'static str, name: String },
    #[error("a table must declare at least one column")]
    NoColumns,
    #[error("too many columns: {count} > {max}")]
    TooManyColumns { count: usize, max: usize },
    #[error("column name {0:?} is reserved")]
    ReservedColumn(String),
    #[error("duplicate column name {0:?}")]
    DuplicateColumn(String),
    #[error("invalid DateTime64 precision: {precision} (must be 0..=9)")]
    InvalidDateTime64Precision { precision: u8 },
}

/// Size bounds for a schema.
#[derive(Debug, Clone, Copy)]
pub struct SchemaLimits {
    pub max_columns: usize,
    pub max_identifier_length: usize,
}

impl Default for SchemaLimits {
    fn default() -> Self {
        Self {
            max_columns: 1024,
            max_identifier_length: 128,
        }
    }
}

/// Columns reserved for the flexible/hybrid table shape (catch-all + raw payload).
pub const DEFAULT_RESERVED_COLUMNS: &[&str] = &["attrs", "raw"];

fn is_valid_identifier(name: &str) -> bool {
    let mut chars = name.chars();
    match chars.next() {
        Some(c) if c.is_ascii_alphabetic() || c == '_' => {}
        _ => return false,
    }
    chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// Whether `tz` is a plausible IANA timezone name: 1..=64 chars from the
/// `[A-Za-z0-9_+/-]` charset (covers names like `UTC`, `America/New_York`,
/// `Etc/GMT+5`). Anything outside this charset (quotes, semicolons, spaces) is
/// rejected, so an untrusted timezone string cannot inject SQL.
fn is_valid_timezone(tz: &str) -> bool {
    !tz.is_empty()
        && tz.len() <= 64
        && tz
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '+' | '/' | '-'))
}

/// Validate a table/column identifier against the strict ASCII allowlist + length
/// bound. `kind` is `"table"` / `"column"` / `"identifier"` for error messages.
pub fn validate_identifier<'a>(
    name: &'a str,
    kind: &'static str,
    limits: &SchemaLimits,
) -> Result<&'a str, SchemaError> {
    if name.is_empty() {
        return Err(SchemaError::EmptyIdentifier(kind));
    }
    if name.len() > limits.max_identifier_length {
        return Err(SchemaError::IdentifierTooLong {
            kind,
            len: name.len(),
            max: limits.max_identifier_length,
        });
    }
    if !is_valid_identifier(name) {
        return Err(SchemaError::InvalidIdentifier {
            kind,
            name: name.to_string(),
        });
    }
    Ok(name)
}

/// Backtick-quote an identifier, escaping embedded backticks (defense-in-depth).
pub fn quote_identifier(name: &str) -> String {
    format!("`{}`", name.replace('`', "``"))
}

/// Error unless `count` is within the column-count bound.
pub fn assert_column_count(count: usize, limits: &SchemaLimits) -> Result<(), SchemaError> {
    if count < 1 {
        return Err(SchemaError::NoColumns);
    }
    if count > limits.max_columns {
        return Err(SchemaError::TooManyColumns {
            count,
            max: limits.max_columns,
        });
    }
    Ok(())
}

/// Error if `name` is reserved.
pub fn assert_not_reserved(name: &str, reserved: &[&str]) -> Result<(), SchemaError> {
    if reserved.contains(&name) {
        return Err(SchemaError::ReservedColumn(name.to_string()));
    }
    Ok(())
}

// ── Type allowlist ───────────────────────────────────────────────────────────

/// The allowlisted scalar column types. Anything else (Decimal, FixedString,
/// Tuple, Enum, Nested, …) has no variant, so it cannot be constructed and
/// untrusted input naming it fails to deserialize.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
pub enum ScalarType {
    String,
    #[serde(rename = "UUID")]
    Uuid,
    Bool,
    Date,
    DateTime,
    DateTime64,
    Int8,
    Int16,
    Int32,
    Int64,
    UInt8,
    UInt16,
    UInt32,
    UInt64,
    Float32,
    Float64,
    #[serde(rename = "JSON")]
    Json,
}

impl ScalarType {
    fn ch_type(self) -> &'static str {
        match self {
            ScalarType::String => "String",
            ScalarType::Uuid => "UUID",
            ScalarType::Bool => "Bool",
            ScalarType::Date => "Date",
            ScalarType::DateTime => "DateTime",
            ScalarType::DateTime64 => "DateTime64(3)",
            ScalarType::Int8 => "Int8",
            ScalarType::Int16 => "Int16",
            ScalarType::Int32 => "Int32",
            ScalarType::Int64 => "Int64",
            ScalarType::UInt8 => "UInt8",
            ScalarType::UInt16 => "UInt16",
            ScalarType::UInt32 => "UInt32",
            ScalarType::UInt64 => "UInt64",
            ScalarType::Float32 => "Float32",
            ScalarType::Float64 => "Float64",
            ScalarType::Json => "JSON",
        }
    }
}

/// `String` is the only allowed `Array`/`Map` element type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
pub enum StringOnly {
    String,
}

fn default_dt64_precision() -> u8 {
    3
}

/// A parametrised `DateTime64(precision[, 'timezone'])` column type.
///
/// **Safety posture:** `precision` and `timezone` may come from untrusted JSON, so
/// they are **validated before rendering** (via [`DateTime64Spec::validate`], called
/// from the table builder's per-column loop): `precision` must be `0..=9` and
/// `timezone` must match the IANA charset `^[A-Za-z0-9_+/-]{1,64}$`. The default
/// (bare `{"datetime64":{}}`) is `DateTime64(3)`, matching the legacy
/// [`ScalarType::DateTime64`] rendering.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct DateTime64Spec {
    #[serde(default = "default_dt64_precision")]
    pub precision: u8,
    #[serde(default)]
    pub timezone: Option<String>,
}

impl DateTime64Spec {
    /// Validate the (possibly untrusted) precision + timezone before they reach SQL.
    pub fn validate(&self) -> Result<(), SchemaError> {
        if self.precision > 9 {
            return Err(SchemaError::InvalidDateTime64Precision {
                precision: self.precision,
            });
        }
        if let Some(tz) = &self.timezone {
            if !is_valid_timezone(tz) {
                return Err(SchemaError::InvalidIdentifier {
                    kind: "timezone",
                    name: tz.clone(),
                });
            }
        }
        Ok(())
    }
}

/// A column type as supplied by untrusted input — the allowlisted recursive shape.
/// Mirrors the TS `ColumnTypeSpec`: a bare scalar string, or a single-key wrapper
/// object (`nullable` / `lowCardinality` / `array` / `map`).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(untagged)]
pub enum ColumnTypeSpec {
    Scalar(ScalarType),
    /// Parametrised `DateTime64(precision[, 'timezone'])`. JSON:
    /// `{"datetime64": {"precision": 3, "timezone": "UTC"}}`. The single `datetime64`
    /// key keeps the untagged match unambiguous against the other wrappers.
    DateTime64 {
        datetime64: DateTime64Spec,
    },
    Nullable {
        nullable: Box<ColumnTypeSpec>,
    },
    LowCardinality {
        #[serde(rename = "lowCardinality")]
        low_cardinality: Box<ColumnTypeSpec>,
    },
    Array {
        array: StringOnly,
    },
    Map {
        map: (StringOnly, StringOnly),
    },
}

impl ColumnTypeSpec {
    /// The ClickHouse type string for this spec.
    ///
    /// For [`ColumnTypeSpec::DateTime64`] this trusts the spec to be valid; untrusted
    /// precision/timezone must be checked first via [`ColumnTypeSpec::validate`] (the
    /// table builder does this in its per-column loop).
    pub fn to_ch_type(&self) -> String {
        match self {
            ColumnTypeSpec::Scalar(s) => s.ch_type().to_string(),
            ColumnTypeSpec::DateTime64 { datetime64 } => match &datetime64.timezone {
                Some(tz) => format!("DateTime64({}, '{}')", datetime64.precision, tz),
                None => format!("DateTime64({})", datetime64.precision),
            },
            ColumnTypeSpec::Nullable { nullable } => format!("Nullable({})", nullable.to_ch_type()),
            ColumnTypeSpec::LowCardinality { low_cardinality } => {
                format!("LowCardinality({})", low_cardinality.to_ch_type())
            }
            ColumnTypeSpec::Array { .. } => "Array(String)".to_string(),
            ColumnTypeSpec::Map { .. } => "Map(String, String)".to_string(),
        }
    }

    /// Whether a `DateTime64` is at the core (so a TTL move expression must wrap it
    /// in `toDateTime(...)`). Propagates through `Nullable`/`LowCardinality` and covers
    /// both the bare [`ScalarType::DateTime64`] and the parametrised
    /// [`ColumnTypeSpec::DateTime64`] variant.
    pub fn is_datetime64(&self) -> bool {
        match self {
            ColumnTypeSpec::Scalar(ScalarType::DateTime64) => true,
            ColumnTypeSpec::DateTime64 { .. } => true,
            ColumnTypeSpec::Nullable { nullable } => nullable.is_datetime64(),
            ColumnTypeSpec::LowCardinality { low_cardinality } => low_cardinality.is_datetime64(),
            _ => false,
        }
    }

    /// Validate any embedded untrusted parameters (currently the parametrised
    /// `DateTime64` precision + timezone) before this type is rendered to SQL.
    /// Recurses through `Nullable`/`LowCardinality`. Identifier-shaped scalars/arrays/
    /// maps have nothing to validate here.
    pub fn validate(&self) -> Result<(), SchemaError> {
        match self {
            ColumnTypeSpec::DateTime64 { datetime64 } => datetime64.validate(),
            ColumnTypeSpec::Nullable { nullable } => nullable.validate(),
            ColumnTypeSpec::LowCardinality { low_cardinality } => low_cardinality.validate(),
            _ => Ok(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn limits() -> SchemaLimits {
        SchemaLimits::default()
    }

    #[test]
    fn accepts_safe_identifiers() {
        for ok in ["a", "A", "_x", "org_id", "col1", "X_2_y"] {
            assert_eq!(validate_identifier(ok, "column", &limits()).unwrap(), ok);
        }
    }

    #[test]
    fn rejects_injection_and_metacharacters() {
        let attacks = [
            "a; DROP TABLE x",
            "a`,`b",
            "a) ENGINE=Memory AS SELECT * FROM secrets --",
            "a' OR '1'='1",
            "a b",
            "a.b",
            "a-b",
            "1col",
            "",
            "a\"b",
            "a\nb",
            "таблица",
            "a/*x*/",
        ];
        for bad in attacks {
            assert!(
                validate_identifier(bad, "column", &limits()).is_err(),
                "should reject {bad:?}"
            );
        }
    }

    #[test]
    fn enforces_length_bound() {
        let lim = limits();
        let too_long = "a".repeat(lim.max_identifier_length + 1);
        assert!(validate_identifier(&too_long, "column", &lim).is_err());
        let ok = "a".repeat(lim.max_identifier_length);
        assert!(validate_identifier(&ok, "column", &lim).is_ok());
    }

    #[test]
    fn quotes_and_escapes() {
        assert_eq!(quote_identifier("org_id"), "`org_id`");
        assert_eq!(quote_identifier("a`b"), "`a``b`");
    }

    #[test]
    fn bounds_and_reserved() {
        assert!(assert_column_count(0, &limits()).is_err());
        assert!(assert_column_count(limits().max_columns + 1, &limits()).is_err());
        assert!(assert_column_count(10, &limits()).is_ok());
        assert!(assert_not_reserved("attrs", DEFAULT_RESERVED_COLUMNS).is_err());
        assert!(assert_not_reserved("raw", DEFAULT_RESERVED_COLUMNS).is_err());
        assert!(assert_not_reserved("user_col", DEFAULT_RESERVED_COLUMNS).is_ok());
    }

    #[test]
    fn allowlist_builds_allowed_types() {
        let s: ColumnTypeSpec = serde_json::from_str("\"DateTime64\"").unwrap();
        assert_eq!(s.to_ch_type(), "DateTime64(3)");
        assert!(s.is_datetime64());

        let n: ColumnTypeSpec = serde_json::from_str(r#"{"nullable":"String"}"#).unwrap();
        assert_eq!(n.to_ch_type(), "Nullable(String)");

        let lc: ColumnTypeSpec =
            serde_json::from_str(r#"{"lowCardinality":{"nullable":"String"}}"#).unwrap();
        assert_eq!(lc.to_ch_type(), "LowCardinality(Nullable(String))");
        let lcd: ColumnTypeSpec =
            serde_json::from_str(r#"{"lowCardinality":"DateTime64"}"#).unwrap();
        assert!(lcd.is_datetime64());

        let a: ColumnTypeSpec = serde_json::from_str(r#"{"array":"String"}"#).unwrap();
        assert_eq!(a.to_ch_type(), "Array(String)");
        let m: ColumnTypeSpec = serde_json::from_str(r#"{"map":["String","String"]}"#).unwrap();
        assert_eq!(m.to_ch_type(), "Map(String, String)");
    }

    #[test]
    fn allowlist_rejects_disallowed_types() {
        let bad = [
            "\"Decimal(38, 10)\"",
            "\"FixedString(16)\"",
            "\"Enum8\"",
            "\"Tuple\"",
            "\"Nested\"",
            r#"{"map":["String","Int32"]}"#,
            r#"{"array":"Int32"}"#,
            r#"{"array":{"nullable":"String"}}"#,
            r#"{"wat":"String"}"#,
            "42",
        ];
        for b in bad {
            assert!(
                serde_json::from_str::<ColumnTypeSpec>(b).is_err(),
                "should reject {b}"
            );
        }
    }

    #[test]
    fn parametrised_datetime64_renders_and_validates() {
        // Full precision + timezone.
        let utc: ColumnTypeSpec =
            serde_json::from_str(r#"{"datetime64":{"precision":3,"timezone":"UTC"}}"#).unwrap();
        assert_eq!(utc.to_ch_type(), "DateTime64(3, 'UTC')");
        assert!(utc.is_datetime64());
        assert!(utc.validate().is_ok());

        // Precision only, no timezone.
        let p6: ColumnTypeSpec = serde_json::from_str(r#"{"datetime64":{"precision":6}}"#).unwrap();
        assert_eq!(p6.to_ch_type(), "DateTime64(6)");
        assert!(p6.validate().is_ok());

        // Empty object → defaults to DateTime64(3), matching the legacy scalar.
        let def: ColumnTypeSpec = serde_json::from_str(r#"{"datetime64":{}}"#).unwrap();
        assert_eq!(def.to_ch_type(), "DateTime64(3)");
        assert!(def.is_datetime64());
        assert!(def.validate().is_ok());

        // The bare string still deserializes to the legacy scalar variant.
        let bare: ColumnTypeSpec = serde_json::from_str("\"DateTime64\"").unwrap();
        assert!(matches!(
            bare,
            ColumnTypeSpec::Scalar(ScalarType::DateTime64)
        ));

        // A real IANA name with a slash + plus is accepted.
        let tz: ColumnTypeSpec =
            serde_json::from_str(r#"{"datetime64":{"precision":9,"timezone":"America/New_York"}}"#)
                .unwrap();
        assert_eq!(tz.to_ch_type(), "DateTime64(9, 'America/New_York')");
        assert!(tz.validate().is_ok());
    }

    #[test]
    fn parametrised_datetime64_rejects_bad_params() {
        // Injection attempt in the timezone string.
        let bad_tz: ColumnTypeSpec =
            serde_json::from_str(r#"{"datetime64":{"precision":3,"timezone":"UTC'; DROP"}}"#)
                .unwrap();
        assert!(matches!(
            bad_tz.validate(),
            Err(SchemaError::InvalidIdentifier {
                kind: "timezone",
                ..
            })
        ));

        // Out-of-range precision.
        let bad_p: ColumnTypeSpec =
            serde_json::from_str(r#"{"datetime64":{"precision":12}}"#).unwrap();
        assert!(matches!(
            bad_p.validate(),
            Err(SchemaError::InvalidDateTime64Precision { precision: 12 })
        ));
    }

    #[test]
    fn parametrised_datetime64_is_datetime64_through_nullable() {
        let n: ColumnTypeSpec =
            serde_json::from_str(r#"{"nullable":{"datetime64":{"precision":3,"timezone":"UTC"}}}"#)
                .unwrap();
        assert!(n.is_datetime64());
        assert_eq!(n.to_ch_type(), "Nullable(DateTime64(3, 'UTC'))");
        assert!(n.validate().is_ok());

        // Validation propagates through the wrapper too.
        let bad: ColumnTypeSpec =
            serde_json::from_str(r#"{"nullable":{"datetime64":{"precision":12}}}"#).unwrap();
        assert!(bad.validate().is_err());
    }
}