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
//! Types for building and populating a [`Block`] with data.
use crate::error::BoxedError;
use crate::native::encode::{Encode, ValueWriter};
use crate::native::string::MaybeUtf8;
use crate::native::utils::{DebugFixedData, DebugNullMap, DebugVariableData, type_fixed_width};
use crate::native::{Block, Column, Layout, LayoutKind};
use bytes::{BufMut, BytesMut};
use clickhouse_types::DataTypeNode;
use hashbrown::{HashMap, hash_map};
use std::collections::VecDeque;
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::mem;
/// Builder type for [`Block`]. Use this to create data for insertion.
#[derive(Default)]
pub struct BlockBuilder {
column_names: HashMap<MaybeUtf8, usize>,
columns: Vec<ColumnBuilderRaw>,
}
/// Errors that may be returned by [`BlockBuilder::upsert_column()`].
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum UpsertColumnError {
/// [`BlockBuilder::upsert_column()`] was called with one type, but the column already existed
/// using a different type.
#[error(
"attempting to overwrite existing column `{name} {existing_type}` with a different type: {new_type}"
)]
ColumnExists {
/// The name of the column.
name: Box<str>,
/// The existing data type of the column.
// Clippy complains about the error type being too large if these aren't boxed
existing_type: Box<DataTypeNode>,
/// The data type of the attempted upsert, according to [`Encode::produces()`].
new_type: Box<DataTypeNode>,
},
/// [`BlockBuilder::upsert_column()`] was called with a type that is not currently supported.
#[error("unsupported type or subtype of column `{column_name}`: `{data_type}`")]
UnsupportedType {
/// The name of the column for the attempted upsert.
column_name: Box<str>,
/// The data type of the attempted upsert, according to [`Encode::produces()`].
data_type: Box<DataTypeNode>,
},
}
/// Errors that may be returned by [`BlockBuilder::build()`].
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum BlockBuilderError {
/// [`BlockBuilder::build()`] was called while the block contained columns with different lengths.
///
/// Ensure every column in the block has the same number of elements.
#[error(
"block contains columns of mismatched lengths; \
longest column: `{longest_column}` (len: {longest_len}), \
shortest column: `{shortest_column}` (len: {shortest_len})"
)]
MismatchedLengths {
/// The name of the column with the most elements.
longest_column: Box<str>,
/// The number of elements in the longest column.
longest_len: usize,
/// The name of the column with the fewest elements.
shortest_column: Box<str>,
/// The number of elements in the shortest column.
shortest_len: usize,
},
/// Returned from [`BlockBuilder::build()`] when a column contains invalid data for its type.
///
/// This likely indicates a bug in an `Encode` implementation,
/// be it manual or provided by this crate.
#[error("column `{column_name} {column_type}` contains invalid data: {message}")]
ColumnDataInvalid {
/// The column name that failed validation.
column_name: Box<str>,
/// The column's data type.
column_type: Box<DataTypeNode>,
/// The validation error.
message: Box<str>,
},
/// Returned from [`BlockBuilder::build()`] if the block contains zero rows of data.
///
/// An empty block is used as a sentinel value in the `Native` format,
/// so it is a logic error to try to explicitly send an empty block when inserting.
///
/// Instead, simply call [`InsertNative::end()`][crate::insert_native::InsertNative::end]
/// to finish the insert.
#[error("cannot build an empty block")]
BlockEmpty,
}
impl BlockBuilder {
/// Begin with an empty block (zero columns, zero rows).
pub fn new() -> Self {
Self::default()
}
/// Add an empty column to the block, or get a reference to an existing one.
///
/// The data type of the column is taken from [`Encode::produces()`].
///
/// The given data type will have any `LowCardinality(_)` or `SimpleAggregateFunction(...)`
/// wrappers erased for ease of implementation.
///
/// # Errors
/// * [`UpsertColumnError::ColumnExists`] if a column with the same name already exists,
/// but with a different type.
/// * [`UpsertColumnError::UnsupportedType`] if the given type is not currently supported
/// by the implementation.
pub fn upsert_column<T: Encode>(
&mut self,
name: impl Into<String>,
) -> Result<ColumnBuilder<'_, T>, UpsertColumnError> {
self.upsert_column_with(name, T::produces())
.map(|inner| ColumnBuilder {
inner,
_marker: PhantomData,
})
}
fn upsert_column_with(
&mut self,
name: impl Into<String>,
data_type: DataTypeNode,
) -> Result<&mut ColumnBuilderRaw, UpsertColumnError> {
let data_type = erase_wrappers(data_type);
match self.column_names.entry(MaybeUtf8::from_string(name)) {
hash_map::Entry::Occupied(existing) => {
let col = &mut self.columns[*existing.get()];
if col.data_type != data_type {
return Err(UpsertColumnError::ColumnExists {
name: col.name.to_string().into(),
existing_type: col.data_type.clone().into(),
new_type: data_type.into(),
});
}
Ok(col)
}
hash_map::Entry::Vacant(vacant) => {
let col = ColumnBuilderRaw {
layout: LayoutBuilder::new(vacant.key(), &data_type)?,
data_type,
name: vacant.key().clone(),
};
vacant.insert(self.columns.len());
// FIXME: replace with `Vec::push_mut()` after Rust 1.95
self.columns.push(col);
Ok(self.columns.last_mut().unwrap())
}
}
}
/// Validate and finish building the block.
///
/// # Errors
/// * If the block is empty (contains zero rows).
/// * If the columns of the block have mismatched lengths.
pub fn build(&mut self) -> Result<Block, BlockBuilderError> {
let mut num_rows = 0;
// Check that all the columns have the same length
if let Some((mut longest_col, columns)) = self.columns.split_first() {
let mut len_mismatch = false;
let mut shortest_col = longest_col;
num_rows = longest_col.num_values();
for col in columns {
if col.num_values() > longest_col.num_values() {
longest_col = col;
len_mismatch = true;
}
if col.num_values() < shortest_col.num_values() {
shortest_col = col;
len_mismatch = true;
}
}
if len_mismatch {
return Err(BlockBuilderError::MismatchedLengths {
longest_column: longest_col.name.to_string().into(),
longest_len: longest_col.num_values(),
shortest_column: shortest_col.name.to_string().into(),
shortest_len: shortest_col.num_values(),
});
}
}
if num_rows == 0 {
return Err(BlockBuilderError::BlockEmpty);
}
// Note: try to perform as much validation as possible before consuming `self`
for col in &self.columns {
col.layout.validate(&col.data_type).map_err(|message| {
BlockBuilderError::ColumnDataInvalid {
column_name: col.name.to_string().into(),
column_type: col.data_type.clone().into(),
message: message.into(),
}
})?;
}
let columns = self
.columns
.drain(..)
.map(|col| Column {
name: col.name,
data_type: col.data_type,
layout: col.layout.into_layout(),
})
.collect();
Ok(Block {
columns,
column_names: mem::take(&mut self.column_names),
num_rows,
})
}
}
impl Debug for BlockBuilder {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BlockBuilder")
// Ignore `column_names`, implementation detail
.field("columns", &self.columns)
.finish()
}
}
/// Builder for an individual [`Column`]. May only be created by [`BlockBuilder`].
pub struct ColumnBuilder<'a, T> {
pub(super) inner: &'a mut ColumnBuilderRaw,
_marker: PhantomData<fn(T)>,
}
impl<T> ColumnBuilder<'_, T>
where
T: Encode,
{
/// The number of values added to this column so far (including nulls, if applicable).
pub fn num_values(&self) -> usize {
self.inner.layout.num_values()
}
/// Add a value to this column.
///
/// Any error returned from [`Encode::encode()`] is returned without additional wrapping.
pub fn add(&mut self, value: T) -> Result<&mut Self, BoxedError> {
// Compatibility checked when this was created
self.inner.add_unchecked(value)?;
Ok(self)
}
/// Add multiple values to this column at once.
///
/// This is more efficient than adding values one at a time, because space can be pre-allocated
/// using [`Iterator::size_hint()`].
///
/// Any error returned from [`Encode::encode()`] is returned without additional wrapping.
///
/// # Note: Errors Do Not Trigger Rollback
/// Any encoding error does not cause previously encoded values to be rolled back.
///
/// If resuming after an error, check [`Self::num_values()`] to see how many values
/// have been successfully encoded in total.
pub fn add_all<I>(&mut self, values: I) -> Result<&mut Self, BoxedError>
where
I: IntoIterator<Item = T>,
{
self.inner.add_all_unchecked(values)?;
Ok(self)
}
#[cfg(test)]
pub(super) fn validate(&self) -> Result<(), String> {
self.inner.layout.validate(&self.inner.data_type)
}
}
impl<T> Debug for ColumnBuilder<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
#[derive(Debug)] // Derived impl works for us here
pub(super) struct ColumnBuilderRaw {
name: MaybeUtf8,
data_type: DataTypeNode,
layout: LayoutBuilder,
}
impl ColumnBuilderRaw {
fn num_values(&self) -> usize {
self.layout.num_values()
}
fn add_unchecked<T>(&mut self, value: T) -> Result<&mut Self, BoxedError>
where
T: Encode,
{
value.encode(&mut ValueWriter {
data_type: &self.data_type,
layout: &mut self.layout,
})?;
Ok(self)
}
fn add_all_unchecked<I>(&mut self, values: I) -> Result<&mut Self, BoxedError>
where
I: IntoIterator,
I::Item: Encode,
{
let mut values = values.into_iter();
while let Some(value) = values.next() {
// Catches an infinite-length iterator that returns `usize::MAX` for its size hint
// This is comparable to the default behavior of `impl Extend<T> for Vec<T>`
let (lower_bound, _) = values.size_hint();
self.layout.reserve(lower_bound.saturating_add(1));
self.add_unchecked(value)?;
}
Ok(self)
}
}
// These types may be identical to `Layout` but they need to use growable containers.
// `LowCardinality` is also deliberately omitted in the initial implementation
// since the server can do the transformation automatically.
pub(super) struct LayoutBuilder {
pub(super) kind: LayoutBuilderKind,
pub(super) nulls: Option<BytesMut>,
}
pub(super) enum LayoutBuilderKind {
/// Fixed layout. Width of each cell depends only on [`DataTypeNode`].
Fixed {
type_width: usize,
data: BytesMut,
},
/// Variable-length data (namely strings)
Variable {
/// Ending offset of each string in `data`.
///
/// The offset of the first string is always `0` unless this is empty.
end_offsets: Vec<usize>,
// Each `Bytes` instance is 4 `usizes` (32 bytes on 64-bit),
// so we save 24 bytes per string by linearizing the string data and storing offsets,
// assuming many small strings instead of fewer big ones, which also amortizes allocations
data: BytesMut,
},
/// Array data. Element data governed by `elem_layout`.
Array {
/// Ending index of each array in `elem_layout`.
end_indices: Vec<usize>,
elem_layout: Box<LayoutBuilder>,
},
Tuple {
layouts: Box<[LayoutBuilder]>,
},
Map {
key_val_layouts: Box<[LayoutBuilder; 2]>,
end_indices: Vec<usize>,
},
}
impl LayoutBuilder {
fn new(column_name: &MaybeUtf8, data_type: &DataTypeNode) -> Result<Self, UpsertColumnError> {
let (non_nullable, nulls) = if let DataTypeNode::Nullable(inner) = data_type {
if is_forbidden_nullable(inner) {
return Err(UpsertColumnError::UnsupportedType {
column_name: column_name.to_string().into(),
data_type: data_type.clone().into(),
});
}
(&**inner, Some(BytesMut::new()))
} else {
(data_type, None)
};
if let Some(type_width) = type_fixed_width(non_nullable) {
return Ok(Self {
nulls,
kind: LayoutBuilderKind::Fixed {
type_width,
data: Default::default(),
},
});
};
match non_nullable {
DataTypeNode::String => Ok(Self {
nulls,
kind: LayoutBuilderKind::Variable {
end_offsets: vec![],
data: Default::default(),
},
}),
DataTypeNode::Tuple(types) => Ok(Self {
nulls,
kind: LayoutBuilderKind::Tuple {
layouts: types
.iter()
.map(|ty| LayoutBuilder::new(column_name, ty))
.collect::<Result<_, _>>()?,
},
}),
DataTypeNode::Array(elem_type) => Ok(Self {
nulls,
kind: LayoutBuilderKind::Array {
end_indices: vec![],
elem_layout: Box::new(LayoutBuilder::new(column_name, elem_type)?),
},
}),
DataTypeNode::Map(key_val_types) => Ok(Self {
nulls,
kind: LayoutBuilderKind::Map {
key_val_layouts: Box::new([
LayoutBuilder::new(column_name, &key_val_types[0])?,
LayoutBuilder::new(column_name, &key_val_types[1])?,
]),
end_indices: vec![],
},
}),
_ => Err(UpsertColumnError::UnsupportedType {
column_name: column_name.to_string().into(),
data_type: data_type.clone().into(),
}),
}
}
pub(super) fn num_values(&self) -> usize {
match &self.kind {
LayoutBuilderKind::Fixed { type_width, data } => data.len() / type_width,
LayoutBuilderKind::Variable { end_offsets, .. } => end_offsets.len(),
LayoutBuilderKind::Array { end_indices, .. } => end_indices.len(),
LayoutBuilderKind::Tuple { layouts, .. } => {
layouts.first().map_or(0, |layout| layout.num_values())
}
LayoutBuilderKind::Map { end_indices, .. } => end_indices.len(),
}
}
pub(super) fn reserve(&mut self, additional: usize) {
match &mut self.kind {
LayoutBuilderKind::Fixed { type_width, data } => {
data.reserve(type_width.saturating_mul(additional));
}
LayoutBuilderKind::Variable { end_offsets, .. } => {
end_offsets.reserve(additional);
// Don't reserve in `data` because we don't know the total additional size
}
LayoutBuilderKind::Array { .. } => {}
LayoutBuilderKind::Tuple { .. } => {}
LayoutBuilderKind::Map { .. } => {}
}
}
/// Push a valid placeholder value
pub(super) fn push_placeholder(&mut self) {
match &mut self.kind {
LayoutBuilderKind::Fixed { type_width, data } => {
data.put_bytes(0, *type_width);
}
LayoutBuilderKind::Variable { end_offsets, data } => {
end_offsets.push(data.len());
}
LayoutBuilderKind::Array { end_indices, .. } => {
let end_index = end_indices.last().copied().unwrap_or(0);
end_indices.push(end_index);
}
// This is only needed for `Nullable(Tuple(...))` which is currently experimental
LayoutBuilderKind::Tuple { layouts } => {
for layout in layouts {
layout.push_placeholder();
}
}
LayoutBuilderKind::Map { end_indices, .. } => {
let end_index = end_indices.last().copied().unwrap_or(0);
end_indices.push(end_index);
}
}
}
/// Truncate to the given number of values.
///
/// For arrays and maps, this truncates to the length of the array at `num_values`.
pub(super) fn truncate(&mut self, num_values: usize) {
match &mut self.kind {
LayoutBuilderKind::Fixed { type_width, data } => {
data.truncate(type_width.saturating_mul(num_values));
}
LayoutBuilderKind::Variable { end_offsets, data } => {
end_offsets.truncate(num_values);
let last_offset = end_offsets.last().copied().unwrap_or(0);
data.truncate(last_offset);
}
LayoutBuilderKind::Array {
end_indices,
elem_layout,
} => {
end_indices.truncate(num_values);
let last_index = end_indices.last().copied().unwrap_or(0);
elem_layout.truncate(last_index);
}
LayoutBuilderKind::Tuple { layouts } => {
for layout in layouts {
layout.truncate(num_values);
}
}
LayoutBuilderKind::Map {
key_val_layouts,
end_indices,
} => {
end_indices.truncate(num_values);
let last_index = end_indices.last().copied().unwrap_or(0);
key_val_layouts[0].truncate(last_index);
key_val_layouts[1].truncate(last_index);
}
}
}
pub(super) fn validate(&self, data_type: &DataTypeNode) -> Result<(), String> {
self.validate_nulls(data_type)?;
let non_nullable = if let DataTypeNode::Nullable(inner) = data_type {
inner
} else {
data_type
};
match &self.kind {
LayoutBuilderKind::Fixed { type_width, data } => {
let expected_width = type_fixed_width(non_nullable)
.ok_or_else(|| format!("data type {non_nullable} is not fixed-width but we encoded {} bytes of {type_width}-byte values", data.len()))?;
if expected_width != *type_width {
return Err(format!(
"data type {non_nullable} has a fixed width of {expected_width} but we encoded {} bytes of {type_width}-byte values",
data.len()
));
}
if !data.len().is_multiple_of(*type_width) {
return Err(format!(
"data length ({}) is not a multiple of type_width ({type_width})",
data.len()
));
}
Ok(())
}
LayoutBuilderKind::Variable { end_offsets, data } => {
for (i, &end_offset) in end_offsets.iter().enumerate() {
if end_offset > data.len() {
return Err(format!(
"string {i} end offset {end_offset} is out of bounds: {}",
data.len()
));
}
}
Ok(())
}
LayoutBuilderKind::Array {
end_indices,
elem_layout,
} => {
let DataTypeNode::Array(elem_type) = non_nullable else {
return Err(format!("expected type Array(_), got {non_nullable}"));
};
let num_elements = elem_layout.num_values();
for (i, &end_index) in end_indices.iter().enumerate() {
if end_index > num_elements {
return Err(format!(
"array {i} end index ({end_index}) out of bounds: {num_elements}"
));
}
}
let last_index = end_indices.last().copied().unwrap_or(0);
if last_index != num_elements {
// Most likely cause of this error is a leaked `ArrayWriter`
return Err(format!(
"last array index ({last_index}) out of sync with total elements: {num_elements}"
));
}
elem_layout.validate(elem_type)
}
LayoutBuilderKind::Tuple { layouts } => {
let DataTypeNode::Tuple(types) = non_nullable else {
return Err(format!("expected type Tuple(...), got {non_nullable}"));
};
let expected_len = layouts.first().map_or(0, LayoutBuilder::num_values);
for (i, (ty, layout)) in types.iter().zip(layouts).enumerate() {
layout.validate(ty)?;
let actual_len = layout.num_values();
if layout.num_values() != expected_len {
// Most likely cause of this error is a leaked `TupleWriter`
return Err(format!(
"tuple index {i} (type {ty}) total elements out of sync: {actual_len} vs {expected_len}"
));
}
}
Ok(())
}
LayoutBuilderKind::Map {
key_val_layouts,
end_indices,
} => {
let DataTypeNode::Map([key_ty, val_ty]) = non_nullable else {
return Err(format!("expected type Map(...), got {non_nullable}"));
};
let keys_len = key_val_layouts[0].num_values();
let values_len = key_val_layouts[1].num_values();
if keys_len != values_len {
return Err(format!(
"number of keys and values is out of sync: {keys_len} vs {values_len}"
));
}
for (i, &end_index) in end_indices.iter().enumerate() {
if end_index > keys_len {
return Err(format!(
"map {i} end index ({end_index}) out of bounds: {keys_len}"
));
}
}
let last_index = end_indices.last().copied().unwrap_or(0);
if last_index != keys_len {
// Most likely cause of this error is a leaked `MapWriter`
return Err(format!(
"last map index ({last_index}) out of sync with total elements: {keys_len}"
));
}
key_val_layouts[0].validate(key_ty)?;
key_val_layouts[1].validate(val_ty)?;
Ok(())
}
}
}
fn validate_nulls(&self, data_type: &DataTypeNode) -> Result<(), String> {
match (&self.nulls, data_type) {
(Some(nulls), DataTypeNode::Nullable(_)) => {
if nulls.len() != self.num_values() {
return Err(format!(
"null bitmap length invalid: {}; expected: {}",
nulls.len(),
self.num_values()
));
}
}
(Some(nulls), _) => {
return Err(format!(
"null bitmap of length {} created for non-nullable type {data_type}",
nulls.len()
));
}
(None, DataTypeNode::Nullable(_)) => {
return Err(format!("nullable type {data_type} missing null bitmap"));
}
_ => (),
}
Ok(())
}
fn into_layout(self) -> Layout {
Layout {
num_values: self.num_values(),
nulls: self.nulls.map(BytesMut::freeze),
kind: match self.kind {
LayoutBuilderKind::Fixed { type_width, data } => LayoutKind::Fixed {
type_width,
data: data.freeze(),
},
LayoutBuilderKind::Variable { end_offsets, data } => LayoutKind::Variable {
end_offsets: end_offsets.into(),
data: data.freeze(),
},
LayoutBuilderKind::Array {
end_indices,
elem_layout,
} => LayoutKind::Array {
end_indices: end_indices.into(),
elem_layout: Box::new(elem_layout.into_layout()),
},
LayoutBuilderKind::Tuple { layouts } => LayoutKind::Tuple {
layouts: layouts
.into_iter()
.map(LayoutBuilder::into_layout)
.collect(),
},
LayoutBuilderKind::Map {
key_val_layouts,
end_indices,
} => LayoutKind::Map {
key_val_layouts: Box::new(key_val_layouts.map(LayoutBuilder::into_layout)),
end_indices: end_indices.into(),
},
},
}
}
}
impl Debug for LayoutBuilder {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LayoutBuilder")
.field("kind", &self.kind)
.field("nulls", &self.nulls.as_deref().map(DebugNullMap))
.finish()
}
}
impl Debug for LayoutBuilderKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use LayoutBuilderKind::*;
match self {
Fixed { type_width, data } => f
.debug_struct("Fixed")
.field(
"data",
&DebugFixedData {
type_width: *type_width,
data,
},
)
.finish(),
Variable { end_offsets, data } => f
.debug_struct("Variable")
.field("data", &DebugVariableData { end_offsets, data })
.finish(),
Array {
elem_layout,
end_indices,
} => f
.debug_struct("Array")
.field("elem_layout", elem_layout)
.field("end_indices", end_indices)
.finish(),
Tuple { layouts } => {
let mut tuple = f.debug_tuple("Tuple");
for layout in layouts {
tuple.field(layout);
}
tuple.finish()
}
Map {
key_val_layouts,
end_indices,
} => f
.debug_struct("Map")
.field("keys", &key_val_layouts[0])
.field("values", &key_val_layouts[1])
.field("end_indices", end_indices)
.finish(),
}
}
}
/// Erase `LowCardinality` and `SimpleAggregateFunction` from the column type as inserts can be done
/// without them.
///
/// TODO: encode `LowCardinality`
fn erase_wrappers(data_type: DataTypeNode) -> DataTypeNode {
match data_type {
DataTypeNode::LowCardinality(inner) | DataTypeNode::SimpleAggregateFunction(_, inner) => {
erase_wrappers(*inner)
}
DataTypeNode::Nullable(mut inner) => {
*inner = erase_wrappers(*inner);
DataTypeNode::Nullable(inner)
}
DataTypeNode::Array(mut inner) => {
*inner = erase_wrappers(*inner);
DataTypeNode::Array(inner)
}
DataTypeNode::Tuple(types) => {
// Converting to a `VeqDeque` is an `O(1)` operation that then lets us
// iterate through `types` by-value and push them back into the same allocation.
let mut types = VecDeque::from(types);
for _ in 0..types.len() {
let ty = types.pop_front().unwrap();
types.push_back(erase_wrappers(ty));
}
// The vector should be linear again, so this conversion should also be trivial.
DataTypeNode::Tuple(types.into())
}
DataTypeNode::Map([mut key_ty, mut val_ty]) => {
*key_ty = erase_wrappers(*key_ty);
*val_ty = erase_wrappers(*val_ty);
DataTypeNode::Map([key_ty, val_ty])
}
other => other,
}
}
fn is_forbidden_nullable(data_type: &DataTypeNode) -> bool {
// https://clickhouse.com/docs/reference/data-types/nullable
matches!(data_type, DataTypeNode::Array(_) | DataTypeNode::Map(_))
}