clickhouse-c-rs 0.2.2

Rust bindings for clickhouse-c, the header-only C client for the ClickHouse Native wire format
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
//! ClickHouse type parsing and inspection.

use core::ffi::c_char;
use core::ptr::NonNull;
use core::slice;

use crate::alloc::Allocator;
use crate::error::{Result, check};
use crate::sys;

/// ClickHouse type kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum Kind {
    Void = sys::CHC_VOID,
    Int8 = sys::CHC_INT8,
    Int16 = sys::CHC_INT16,
    Int32 = sys::CHC_INT32,
    Int64 = sys::CHC_INT64,
    Int128 = sys::CHC_INT128,
    Int256 = sys::CHC_INT256,
    UInt8 = sys::CHC_UINT8,
    UInt16 = sys::CHC_UINT16,
    UInt32 = sys::CHC_UINT32,
    UInt64 = sys::CHC_UINT64,
    UInt128 = sys::CHC_UINT128,
    UInt256 = sys::CHC_UINT256,
    Float32 = sys::CHC_FLOAT32,
    Float64 = sys::CHC_FLOAT64,
    BFloat16 = sys::CHC_BFLOAT16,
    Bool = sys::CHC_BOOL,
    Date = sys::CHC_DATE,
    Date32 = sys::CHC_DATE32,
    DateTime = sys::CHC_DATETIME,
    DateTime64 = sys::CHC_DATETIME64,
    Time = sys::CHC_TIME,
    Time64 = sys::CHC_TIME64,
    String = sys::CHC_STRING,
    FixedString = sys::CHC_FIXED_STRING,
    Decimal32 = sys::CHC_DECIMAL32,
    Decimal64 = sys::CHC_DECIMAL64,
    Decimal128 = sys::CHC_DECIMAL128,
    Decimal256 = sys::CHC_DECIMAL256,
    Uuid = sys::CHC_UUID,
    Ipv4 = sys::CHC_IPV4,
    Ipv6 = sys::CHC_IPV6,
    Enum8 = sys::CHC_ENUM8,
    Enum16 = sys::CHC_ENUM16,
    Nullable = sys::CHC_NULLABLE,
    Array = sys::CHC_ARRAY,
    Tuple = sys::CHC_TUPLE,
    Map = sys::CHC_MAP,
    Nested = sys::CHC_NESTED,
    LowCardinality = sys::CHC_LOW_CARDINALITY,
    Interval = sys::CHC_INTERVAL,
    Point = sys::CHC_POINT,
    Ring = sys::CHC_RING,
    LineString = sys::CHC_LINE_STRING,
    Polygon = sys::CHC_POLYGON,
    MultiPolygon = sys::CHC_MULTI_POLYGON,
    MultiLineString = sys::CHC_MULTI_LINE_STRING,
    Variant = sys::CHC_VARIANT,
    Dynamic = sys::CHC_DYNAMIC,
    Json = sys::CHC_JSON,
    Object = sys::CHC_OBJECT,
    AggregateFunction = sys::CHC_AGGREGATE_FUNCTION,
    SimpleAggregateFunction = sys::CHC_SIMPLE_AGGREGATE_FUNCTION,
    Nothing = sys::CHC_NOTHING,
    QBit = sys::CHC_QBIT,
}

impl Kind {
    pub(crate) fn from_raw(k: sys::chc_kind) -> Option<Self> {
        // Return None for values added to C API but not represented here
        Some(match k {
            sys::CHC_VOID => Self::Void,
            sys::CHC_INT8 => Self::Int8,
            sys::CHC_INT16 => Self::Int16,
            sys::CHC_INT32 => Self::Int32,
            sys::CHC_INT64 => Self::Int64,
            sys::CHC_INT128 => Self::Int128,
            sys::CHC_INT256 => Self::Int256,
            sys::CHC_UINT8 => Self::UInt8,
            sys::CHC_UINT16 => Self::UInt16,
            sys::CHC_UINT32 => Self::UInt32,
            sys::CHC_UINT64 => Self::UInt64,
            sys::CHC_UINT128 => Self::UInt128,
            sys::CHC_UINT256 => Self::UInt256,
            sys::CHC_FLOAT32 => Self::Float32,
            sys::CHC_FLOAT64 => Self::Float64,
            sys::CHC_BFLOAT16 => Self::BFloat16,
            sys::CHC_BOOL => Self::Bool,
            sys::CHC_DATE => Self::Date,
            sys::CHC_DATE32 => Self::Date32,
            sys::CHC_DATETIME => Self::DateTime,
            sys::CHC_DATETIME64 => Self::DateTime64,
            sys::CHC_TIME => Self::Time,
            sys::CHC_TIME64 => Self::Time64,
            sys::CHC_STRING => Self::String,
            sys::CHC_FIXED_STRING => Self::FixedString,
            sys::CHC_DECIMAL32 => Self::Decimal32,
            sys::CHC_DECIMAL64 => Self::Decimal64,
            sys::CHC_DECIMAL128 => Self::Decimal128,
            sys::CHC_DECIMAL256 => Self::Decimal256,
            sys::CHC_UUID => Self::Uuid,
            sys::CHC_IPV4 => Self::Ipv4,
            sys::CHC_IPV6 => Self::Ipv6,
            sys::CHC_ENUM8 => Self::Enum8,
            sys::CHC_ENUM16 => Self::Enum16,
            sys::CHC_NULLABLE => Self::Nullable,
            sys::CHC_ARRAY => Self::Array,
            sys::CHC_TUPLE => Self::Tuple,
            sys::CHC_MAP => Self::Map,
            sys::CHC_NESTED => Self::Nested,
            sys::CHC_LOW_CARDINALITY => Self::LowCardinality,
            sys::CHC_INTERVAL => Self::Interval,
            sys::CHC_POINT => Self::Point,
            sys::CHC_RING => Self::Ring,
            sys::CHC_LINE_STRING => Self::LineString,
            sys::CHC_POLYGON => Self::Polygon,
            sys::CHC_MULTI_POLYGON => Self::MultiPolygon,
            sys::CHC_MULTI_LINE_STRING => Self::MultiLineString,
            sys::CHC_VARIANT => Self::Variant,
            sys::CHC_DYNAMIC => Self::Dynamic,
            sys::CHC_JSON => Self::Json,
            sys::CHC_OBJECT => Self::Object,
            sys::CHC_AGGREGATE_FUNCTION => Self::AggregateFunction,
            sys::CHC_SIMPLE_AGGREGATE_FUNCTION => Self::SimpleAggregateFunction,
            sys::CHC_NOTHING => Self::Nothing,
            sys::CHC_QBIT => Self::QBit,
            _ => return None,
        })
    }
}

/// Unit of an `Interval` type.
///
/// Every `Interval*` type parses to [`Kind::Interval`]; unit is metadata on
/// type, reported by [`TypeRef::interval_unit`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum IntervalUnit {
    Nanosecond = sys::CHC_INTERVAL_NANOSECOND,
    Microsecond = sys::CHC_INTERVAL_MICROSECOND,
    Millisecond = sys::CHC_INTERVAL_MILLISECOND,
    Second = sys::CHC_INTERVAL_SECOND,
    Minute = sys::CHC_INTERVAL_MINUTE,
    Hour = sys::CHC_INTERVAL_HOUR,
    Day = sys::CHC_INTERVAL_DAY,
    Week = sys::CHC_INTERVAL_WEEK,
    Month = sys::CHC_INTERVAL_MONTH,
    Quarter = sys::CHC_INTERVAL_QUARTER,
    Year = sys::CHC_INTERVAL_YEAR,
}

impl IntervalUnit {
    /// Returns None for `CHC_INTERVAL_NONE` and for units added to C API but
    /// not represented here.
    pub(crate) fn from_raw(u: sys::chc_interval_unit) -> Option<Self> {
        Some(match u {
            sys::CHC_INTERVAL_NANOSECOND => Self::Nanosecond,
            sys::CHC_INTERVAL_MICROSECOND => Self::Microsecond,
            sys::CHC_INTERVAL_MILLISECOND => Self::Millisecond,
            sys::CHC_INTERVAL_SECOND => Self::Second,
            sys::CHC_INTERVAL_MINUTE => Self::Minute,
            sys::CHC_INTERVAL_HOUR => Self::Hour,
            sys::CHC_INTERVAL_DAY => Self::Day,
            sys::CHC_INTERVAL_WEEK => Self::Week,
            sys::CHC_INTERVAL_MONTH => Self::Month,
            sys::CHC_INTERVAL_QUARTER => Self::Quarter,
            sys::CHC_INTERVAL_YEAR => Self::Year,
            _ => return None,
        })
    }
}

/// Parsed ClickHouse type.
///
/// Value releases its memory with allocator passed to [`parse`](Self::parse).
pub struct TypeAst {
    raw: NonNull<sys::chc_type>,
    alloc: Allocator,
}

impl TypeAst {
    /// Parses a ClickHouse type name such as `"Array(Nullable(UInt32))"`.
    pub fn parse(name: &str, alloc: Allocator) -> Result<Self> {
        let mut out: *mut sys::chc_type = core::ptr::null_mut();
        let mut err = sys::chc_err::zeroed();
        let rc = unsafe {
            sys::chc_type_parse(
                name.as_ptr().cast::<c_char>(),
                name.len(),
                alloc.as_ptr(),
                &mut out,
                &mut err,
            )
        };
        check(rc, &err)?;
        Ok(Self {
            raw: NonNull::new(out).expect("chc_type_parse returned OK with NULL"),
            alloc,
        })
    }

    /// Returns a borrowed view of parsed type.
    pub fn view(&self) -> TypeRef<'_> {
        TypeRef {
            raw: self.raw.as_ptr().cast_const(),
            _marker: core::marker::PhantomData,
        }
    }
}

impl Drop for TypeAst {
    fn drop(&mut self) {
        unsafe { sys::chc_type_destroy(self.raw.as_ptr(), self.alloc.as_ptr()) };
    }
}

unsafe impl Send for TypeAst {}

/// Borrowed view of a ClickHouse type.
#[derive(Clone, Copy)]
pub struct TypeRef<'a> {
    pub(crate) raw: *const sys::chc_type,
    pub(crate) _marker: core::marker::PhantomData<&'a sys::chc_type>,
}

impl<'a> TypeRef<'a> {
    pub fn kind(&self) -> Option<Kind> {
        Kind::from_raw(unsafe { sys::chc_type_kind(self.raw) })
    }

    /// Returns number of child types.
    ///
    /// Child types include array elements, tuple and nested fields, map keys
    /// and values, aggregate function argument types, and inner types of
    /// `Nullable` and `LowCardinality`.
    pub fn n_children(&self) -> usize {
        unsafe { sys::chc_type_n_children(self.raw) }
    }

    pub fn child(&self, i: usize) -> Option<TypeRef<'a>> {
        let c = unsafe { sys::chc_type_child(self.raw, i) };
        if c.is_null() {
            None
        } else {
            Some(TypeRef {
                raw: c,
                _marker: core::marker::PhantomData,
            })
        }
    }

    /// Returns `N` for `FixedString(N)`, or zero for other types.
    pub fn fixed_size(&self) -> i32 {
        unsafe { sys::chc_type_fixed_size(self.raw) }
    }

    /// Returns byte width in a [`Fixed`](crate::ColumnLayout::Fixed) column.
    /// Returns zero for types without fixed-width storage.
    pub fn elem_size(&self) -> usize {
        unsafe { sys::chc_type_elem_size(self.raw) }
    }

    /// Returns `P` for `Decimal(P, S)`, or zero for other types.
    pub fn decimal_precision(&self) -> i32 {
        unsafe { sys::chc_type_decimal_precision(self.raw) }
    }

    /// Returns `S` for `Decimal(P, S)`, or zero for other types.
    pub fn decimal_scale(&self) -> i32 {
        unsafe { sys::chc_type_decimal_scale(self.raw) }
    }

    /// Returns subsecond precision for `DateTime64(S)` or `Time64(S)`.
    /// Returns zero for other types.
    pub fn datetime64_scale(&self) -> i32 {
        unsafe { sys::chc_type_datetime64_scale(self.raw) }
    }

    /// Returns unit for an `Interval*` type, or None for other types.
    pub fn interval_unit(&self) -> Option<IntervalUnit> {
        IntervalUnit::from_raw(unsafe { sys::chc_type_interval_unit(self.raw) })
    }

    /// Returns `N` for `QBit(T, N)`, or zero for other types.
    ///
    /// A QBit column uses [`ColumnLayout::Tuple`] with one fixed column per
    /// bit in `T`. Columns use most significant bit first, and each row holds
    /// `ceil(N / 8)` bytes.
    ///
    /// [`ColumnLayout::Tuple`]: crate::ColumnLayout::Tuple
    /// [`qbit_element_size`]: TypeRef::qbit_element_size
    pub fn qbit_dimension(&self) -> usize {
        unsafe { sys::chc_type_qbit_dimension(self.raw) }
    }

    /// Returns bit width of `T` in `QBit(T, N)`, or zero for other types.
    /// Supported widths are 16, 32, and 64.
    pub fn qbit_element_size(&self) -> usize {
        unsafe { sys::chc_type_qbit_element_size(self.raw) }
    }

    /// Returns timezone bytes without UTF-8 validation.
    pub fn timezone(&self) -> Option<&'a [u8]> {
        let mut len = 0;
        let p = unsafe { sys::chc_type_timezone(self.raw, &mut len) };
        if p.is_null() {
            None
        } else {
            // SAFETY: C allocation remains valid for lifetime of borrowed type
            Some(unsafe { slice::from_raw_parts(p.cast::<u8>(), len) })
        }
    }

    /// Returns type name bytes without UTF-8 validation.
    pub fn name(&self) -> Option<&'a [u8]> {
        let mut len = 0;
        let p = unsafe { sys::chc_type_name(self.raw, &mut len) };
        (!p.is_null()).then(|| unsafe { slice::from_raw_parts(p.cast::<u8>(), len) })
    }

    /// Returns number of entries for `Enum8` or `Enum16`.
    /// Returns zero for other types.
    pub fn enum_count(&self) -> usize {
        unsafe { sys::chc_type_enum_count(self.raw) }
    }

    /// Returns name and value for enum entry at `i`.
    /// Name bytes are not validated as UTF-8.
    pub fn enum_at(&self, i: usize) -> Option<(&'a [u8], i64)> {
        if i >= self.enum_count() {
            return None;
        }
        let mut name_ptr: *const c_char = core::ptr::null();
        let mut name_len: usize = 0;
        let mut value: i64 = 0;
        unsafe {
            sys::chc_type_enum_at(self.raw, i, &mut name_ptr, &mut name_len, &mut value);
        }
        (!name_ptr.is_null()).then(|| {
            (
                unsafe { slice::from_raw_parts(name_ptr.cast::<u8>(), name_len) },
                value,
            )
        })
    }

    /// Returns `Tuple` or `Nested` field name bytes without UTF-8 validation.
    pub fn tuple_field_name(&self, i: usize) -> Option<&'a [u8]> {
        let mut len = 0;
        let p = unsafe { sys::chc_type_tuple_field_name(self.raw, i, &mut len) };
        if p.is_null() {
            None
        } else {
            Some(unsafe { slice::from_raw_parts(p.cast::<u8>(), len) })
        }
    }

    /// Returns function name of `AggregateFunction` or
    /// `SimpleAggregateFunction` without UTF-8 validation.
    ///
    /// Function parameters are not part of name. Children hold argument
    /// types, first of which carries stored values.
    pub fn agg_function(&self) -> Option<&'a [u8]> {
        let mut len = 0;
        let p = unsafe { sys::chc_type_agg_function(self.raw, &mut len) };
        if p.is_null() {
            None
        } else {
            Some(unsafe { slice::from_raw_parts(p.cast::<u8>(), len) })
        }
    }

    /// Formats type as a ClickHouse type name.
    pub fn format(&self) -> String {
        let needed = unsafe { sys::chc_type_format(self.raw, core::ptr::null_mut(), 0) };
        // C writes a trailing NUL past `needed` bytes
        let mut buf = vec![0u8; needed.saturating_add(1)];
        let _ =
            unsafe { sys::chc_type_format(self.raw, buf.as_mut_ptr().cast::<c_char>(), buf.len()) };
        buf.truncate(needed);
        String::from_utf8_lossy(&buf).into_owned()
    }
}

#[cfg(test)]
mod tests {
    use super::{IntervalUnit, Kind, TypeAst};
    use crate::Allocator;
    use crate::sys;

    fn parse(name: &str) -> TypeAst {
        TypeAst::parse(name, Allocator::stdlib()).expect(name)
    }

    #[test]
    fn qbit_metadata() {
        let ty = parse("QBit(Float32, 16)");
        let view = ty.view();
        assert_eq!(view.kind(), Some(Kind::QBit));
        assert_eq!(view.qbit_dimension(), 16);
        assert_eq!(view.qbit_element_size(), 32);
        assert_eq!(view.format(), "QBit(Float32, 16)");
        assert_eq!(view.child(0).and_then(|c| c.kind()), Some(Kind::Float32));
    }

    #[test]
    fn qbit_accessors_are_zero_off_qbit() {
        let ty = parse("Array(UInt32)");
        assert_eq!(ty.view().qbit_dimension(), 0);
        assert_eq!(ty.view().qbit_element_size(), 0);
    }

    #[test]
    fn interval_names_carry_their_unit() {
        let ty = parse("IntervalQuarter");
        assert_eq!(ty.view().kind(), Some(Kind::Interval));
        assert_eq!(ty.view().interval_unit(), Some(IntervalUnit::Quarter));

        let nested = parse("Nullable(IntervalDay)");
        let inner = nested.view().child(0).expect("Nullable child");
        assert_eq!(inner.interval_unit(), Some(IntervalUnit::Day));
        assert_eq!(nested.view().interval_unit(), None);
    }

    #[test]
    fn line_string_kinds_parse() {
        assert_eq!(parse("LineString").view().kind(), Some(Kind::LineString));
        assert_eq!(
            parse("MultiLineString").view().kind(),
            Some(Kind::MultiLineString)
        );
    }

    #[test]
    fn nested_fields_carry_names() {
        let ty = parse("Nested(a UInt32, b String)");
        let view = ty.view();
        assert_eq!(view.kind(), Some(Kind::Nested));
        assert_eq!(view.n_children(), 2);
        assert_eq!(view.tuple_field_name(0), Some(&b"a"[..]));
        assert_eq!(view.tuple_field_name(1), Some(&b"b"[..]));
    }

    #[test]
    fn aggregate_children_drop_function_parameters() {
        let ty = parse("AggregateFunction(quantiles(0.5, 0.9), UInt64)");
        let view = ty.view();
        assert_eq!(view.kind(), Some(Kind::AggregateFunction));
        assert_eq!(view.agg_function(), Some(&b"quantiles"[..]));
        assert_eq!(view.n_children(), 1);
        assert_eq!(view.child(0).and_then(|c| c.kind()), Some(Kind::UInt64));
    }

    // Server may prefix a serialization version before function name
    #[test]
    fn aggregate_version_prefix_parses() {
        let ty = parse("AggregateFunction(1, sumMap, Array(UInt32), Array(UInt64))");
        let view = ty.view();
        assert_eq!(view.agg_function(), Some(&b"sumMap"[..]));
        assert_eq!(view.n_children(), 2);
    }

    #[test]
    fn simple_aggregate_stores_its_first_argument() {
        let ty = parse("SimpleAggregateFunction(anyLast, Nullable(String))");
        let view = ty.view();
        assert_eq!(view.kind(), Some(Kind::SimpleAggregateFunction));
        assert_eq!(view.agg_function(), Some(&b"anyLast"[..]));
        assert_eq!(view.child(0).and_then(|c| c.kind()), Some(Kind::Nullable));
    }

    #[test]
    fn agg_function_is_none_off_aggregates() {
        assert_eq!(parse("Tuple(UInt32)").view().agg_function(), None);
    }

    // JSON hints steer server storage, not string serialization
    #[test]
    fn json_parameters_leave_no_children() {
        let ty = parse("JSON(max_dynamic_paths=16, `a.b` UInt32, SKIP `a.e`)");
        assert_eq!(ty.view().kind(), Some(Kind::Json));
        assert_eq!(ty.view().n_children(), 0);
    }

    // Unknown C discriminants must not convert to adjacent Rust variants
    #[test]
    fn unknown_discriminant_is_none() {
        assert_eq!(Kind::from_raw(i32::MAX), None);
        assert_eq!(Kind::from_raw(-1), None);
    }

    #[test]
    fn unknown_interval_unit_is_none() {
        assert_eq!(IntervalUnit::from_raw(sys::CHC_INTERVAL_NONE), None);
        assert_eq!(IntervalUnit::from_raw(i32::MAX), None);
    }

    #[test]
    fn every_c_interval_unit_maps_to_its_variant() {
        for (raw, unit) in [
            (sys::CHC_INTERVAL_NANOSECOND, IntervalUnit::Nanosecond),
            (sys::CHC_INTERVAL_MICROSECOND, IntervalUnit::Microsecond),
            (sys::CHC_INTERVAL_MILLISECOND, IntervalUnit::Millisecond),
            (sys::CHC_INTERVAL_SECOND, IntervalUnit::Second),
            (sys::CHC_INTERVAL_MINUTE, IntervalUnit::Minute),
            (sys::CHC_INTERVAL_HOUR, IntervalUnit::Hour),
            (sys::CHC_INTERVAL_DAY, IntervalUnit::Day),
            (sys::CHC_INTERVAL_WEEK, IntervalUnit::Week),
            (sys::CHC_INTERVAL_MONTH, IntervalUnit::Month),
            (sys::CHC_INTERVAL_QUARTER, IntervalUnit::Quarter),
            (sys::CHC_INTERVAL_YEAR, IntervalUnit::Year),
        ] {
            assert_eq!(IntervalUnit::from_raw(raw), Some(unit));
        }
    }

    #[test]
    fn fixed_string_reports_its_width() {
        let ty = parse("FixedString(12)");
        assert_eq!(ty.view().kind(), Some(Kind::FixedString));
        assert_eq!(ty.view().fixed_size(), 12);
        assert_eq!(ty.view().elem_size(), 12);
        assert_eq!(parse("UInt32").view().fixed_size(), 0);
    }

    #[test]
    fn decimal_carries_precision_and_scale() {
        let ty = parse("Decimal(18, 4)");
        assert_eq!(ty.view().kind(), Some(Kind::Decimal64));
        assert_eq!(ty.view().decimal_precision(), 18);
        assert_eq!(ty.view().decimal_scale(), 4);

        let plain = parse("String");
        assert_eq!(plain.view().decimal_precision(), 0);
        assert_eq!(plain.view().decimal_scale(), 0);
    }

    #[test]
    fn subsecond_scale_comes_from_the_type() {
        assert_eq!(parse("DateTime64(9)").view().datetime64_scale(), 9);
        assert_eq!(parse("Time64(3)").view().datetime64_scale(), 3);
        assert_eq!(parse("DateTime").view().datetime64_scale(), 0);
    }

    #[test]
    fn timezone_is_present_only_on_temporal_types() {
        let ty = parse("DateTime64(3, 'Europe/Paris')");
        assert_eq!(ty.view().timezone(), Some(&b"Europe/Paris"[..]));
        assert_eq!(parse("UInt32").view().timezone(), None);
    }

    #[test]
    fn every_parsed_type_reports_its_source_name() {
        assert_eq!(
            parse("Array(UInt32)").view().name(),
            Some(&b"Array(UInt32)"[..])
        );
        let child = parse("Array(UInt32)");
        let child = child.view().child(0).expect("element type");
        assert_eq!(child.name(), Some(&b"UInt32"[..]));
        assert_eq!(child.format(), "UInt32");
    }

    #[test]
    fn enum_entries_are_addressable_by_index() {
        let ty = parse("Enum8('red' = -1, 'green' = 2)");
        let view = ty.view();
        assert_eq!(view.kind(), Some(Kind::Enum8));
        assert_eq!(view.enum_count(), 2);
        assert_eq!(view.enum_at(0), Some((&b"red"[..], -1)));
        assert_eq!(view.enum_at(1), Some((&b"green"[..], 2)));
        assert_eq!(view.enum_at(2), None);
    }

    #[test]
    fn enum_accessors_are_empty_off_enums() {
        let ty = parse("UInt32");
        assert_eq!(ty.view().enum_count(), 0);
        assert_eq!(ty.view().enum_at(0), None);
    }

    #[test]
    fn unnamed_and_out_of_range_fields_have_no_name() {
        let ty = parse("Tuple(UInt8, String)");
        assert_eq!(ty.view().tuple_field_name(0), None);
        assert_eq!(ty.view().tuple_field_name(9), None);
        assert_eq!(parse("UInt32").view().tuple_field_name(0), None);
    }

    #[test]
    fn child_index_past_the_end_is_none() {
        let ty = parse("Map(String, UInt64)");
        assert_eq!(ty.view().n_children(), 2);
        assert!(ty.view().child(2).is_none());
        assert!(parse("UInt32").view().child(0).is_none());
    }

    #[test]
    fn a_bad_type_name_is_an_error() {
        let err = TypeAst::parse("NotAType(", Allocator::stdlib())
            .err()
            .expect("unterminated type name accepted");
        assert_eq!(err.kind, crate::ErrorKind::Type);
    }
}