clickhouse-native-client 0.1.0

Async ClickHouse client using the native TCP protocol with LZ4/ZSTD compression and TLS support
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
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
use crate::{
    column::{
        Column,
        ColumnRef,
    },
    types::Type,
    Error,
    Result,
};
use std::sync::Arc;

/// Block metadata used by ClickHouse for distributed query processing.
#[derive(Debug, Clone, Default)]
pub struct BlockInfo {
    /// Whether this block is an overflow block (1 = yes, 0 = no).
    pub is_overflows: u8,
    /// Bucket number for distributed GROUP BY (-1 if not applicable).
    pub bucket_num: i32,
}

/// A block is a collection of named columns with the same number of rows
#[derive(Clone)]
pub struct Block {
    columns: Vec<ColumnItem>,
    rows: usize,
    info: BlockInfo,
}

#[derive(Clone)]
struct ColumnItem {
    name: String,
    column: ColumnRef,
}

impl Block {
    /// Create a new empty block
    pub fn new() -> Self {
        Self { columns: Vec::new(), rows: 0, info: BlockInfo::default() }
    }

    /// Create a block with reserved capacity
    pub fn with_capacity(cols: usize, rows: usize) -> Self {
        Self {
            columns: Vec::with_capacity(cols),
            rows,
            info: BlockInfo::default(),
        }
    }

    /// Append a named column to the block
    pub fn append_column(
        &mut self,
        name: impl Into<String>,
        column: ColumnRef,
    ) -> Result<()> {
        let name = name.into();

        if self.columns.is_empty() {
            self.rows = column.size();
        } else if column.size() != self.rows {
            return Err(Error::Validation(format!(
                "All columns in block must have same count of rows. Name: '{}', expected rows: {}, got: {}",
                name,
                self.rows,
                column.size()
            )));
        }

        self.columns.push(ColumnItem { name, column });
        Ok(())
    }

    /// Get the number of columns in the block
    pub fn column_count(&self) -> usize {
        self.columns.len()
    }

    /// Get the number of rows in the block
    pub fn row_count(&self) -> usize {
        self.rows
    }

    /// Get column by index
    pub fn column(&self, index: usize) -> Option<ColumnRef> {
        self.columns.get(index).map(|item| item.column.clone())
    }

    /// Get mutable access to column by index
    /// Returns None if index is out of bounds
    /// Panics if the column has multiple references
    pub fn column_mut(
        &mut self,
        index: usize,
    ) -> Option<&mut (dyn Column + '_)> {
        let item = self.columns.get_mut(index)?;
        Some(Arc::get_mut(&mut item.column)
            .expect("Cannot get mutable access to shared column - column has multiple references"))
    }

    /// Get column name by index
    pub fn column_name(&self, index: usize) -> Option<&str> {
        self.columns.get(index).map(|item| item.name.as_str())
    }

    /// Get column by name
    pub fn column_by_name(&self, name: &str) -> Option<ColumnRef> {
        self.columns
            .iter()
            .find(|item| item.name == name)
            .map(|item| item.column.clone())
    }

    /// Get mutable access to column by name
    /// Returns None if column with given name is not found
    /// Panics if the column has multiple references
    pub fn column_by_name_mut(
        &mut self,
        name: &str,
    ) -> Option<&mut (dyn Column + '_)> {
        let item = self.columns.iter_mut().find(|item| item.name == name)?;
        Some(Arc::get_mut(&mut item.column)
            .expect("Cannot get mutable access to shared column - column has multiple references"))
    }

    /// Get block info
    pub fn info(&self) -> &BlockInfo {
        &self.info
    }

    /// Set block info
    pub fn set_info(&mut self, info: BlockInfo) {
        self.info = info;
    }

    /// Clear all data from all columns
    pub fn clear(&mut self) {
        // Clear by removing all columns from the block
        self.columns.clear();
        self.rows = 0;
    }

    /// Reserve capacity in all columns
    pub fn reserve(&mut self, new_cap: usize) {
        for item in &mut self.columns {
            // Must panic if can't reserve to prevent inconsistent state
            Arc::get_mut(&mut item.column)
                .expect("Cannot reserve on shared column - column has multiple references")
                .reserve(new_cap);
        }
    }

    /// Refresh and validate row count
    pub fn refresh_row_count(&mut self) -> Result<usize> {
        if self.columns.is_empty() {
            self.rows = 0;
            return Ok(0);
        }

        let first_rows = self.columns[0].column.size();

        for item in &self.columns {
            let col_rows = item.column.size();
            if col_rows != first_rows {
                return Err(Error::Validation(format!(
                    "All columns in block must have same count of rows. Name: '{}', expected: {}, got: {}",
                    item.name, first_rows, col_rows
                )));
            }
        }

        self.rows = first_rows;
        Ok(first_rows)
    }

    /// Iterate over columns
    pub fn iter(&self) -> BlockIterator<'_> {
        BlockIterator { block: self, index: 0 }
    }

    /// Check if block is empty
    pub fn is_empty(&self) -> bool {
        self.rows == 0 || self.columns.is_empty()
    }
}

impl Default for Block {
    fn default() -> Self {
        Self::new()
    }
}

/// Iterator over block columns
pub struct BlockIterator<'a> {
    block: &'a Block,
    index: usize,
}

impl<'a> Iterator for BlockIterator<'a> {
    type Item = (&'a str, &'a Type, ColumnRef);

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.block.columns.len() {
            let item = &self.block.columns[self.index];
            self.index += 1;
            Some((&item.name, item.column.column_type(), item.column.clone()))
        } else {
            None
        }
    }
}

impl<'a> IntoIterator for &'a Block {
    type Item = (&'a str, &'a Type, ColumnRef);
    type IntoIter = BlockIterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    use crate::column::numeric::ColumnUInt64;
    use std::sync::Arc;

    #[test]
    fn test_block_creation() {
        let block = Block::new();
        assert_eq!(block.column_count(), 0);
        assert_eq!(block.row_count(), 0);
        assert!(block.is_empty());
    }

    #[test]
    fn test_block_append_column() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(1);
        col1.append(2);
        col1.append(3);

        block.append_column("id", Arc::new(col1)).unwrap();

        assert_eq!(block.column_count(), 1);
        assert_eq!(block.row_count(), 3);
        assert!(!block.is_empty());
    }

    #[test]
    fn test_block_multiple_columns() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(1);
        col1.append(2);

        let mut col2 = ColumnUInt64::new();
        col2.append(100);
        col2.append(200);

        block.append_column("id", Arc::new(col1)).unwrap();
        block.append_column("value", Arc::new(col2)).unwrap();

        assert_eq!(block.column_count(), 2);
        assert_eq!(block.row_count(), 2);
    }

    #[test]
    fn test_block_mismatched_rows() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(1);
        col1.append(2);

        let mut col2 = ColumnUInt64::new();
        col2.append(100);
        col2.append(200);
        col2.append(300); // Extra row!

        block.append_column("id", Arc::new(col1)).unwrap();
        let result = block.append_column("value", Arc::new(col2));

        assert!(result.is_err());
    }

    #[test]
    fn test_block_get_column() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(42);

        block.append_column("test", Arc::new(col1)).unwrap();

        let col = block.column(0).unwrap();
        assert_eq!(col.size(), 1);

        assert!(block.column(1).is_none());
    }

    #[test]
    fn test_block_get_column_by_name() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(42);

        block.append_column("my_column", Arc::new(col1)).unwrap();

        let col = block.column_by_name("my_column").unwrap();
        assert_eq!(col.size(), 1);

        assert!(block.column_by_name("nonexistent").is_none());
    }

    #[test]
    fn test_block_column_name() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(1);

        block.append_column("test_name", Arc::new(col1)).unwrap();

        assert_eq!(block.column_name(0), Some("test_name"));
        assert_eq!(block.column_name(1), None);
    }

    #[test]
    fn test_block_iterator() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(1);

        let mut col2 = ColumnUInt64::new();
        col2.append(2);

        block.append_column("first", Arc::new(col1)).unwrap();
        block.append_column("second", Arc::new(col2)).unwrap();

        let names: Vec<&str> = block.iter().map(|(name, _, _)| name).collect();
        assert_eq!(names, vec!["first", "second"]);
    }

    #[test]
    fn test_block_info() {
        let mut block = Block::new();

        let info = BlockInfo { is_overflows: 1, bucket_num: 42 };

        block.set_info(info.clone());

        assert_eq!(block.info().is_overflows, 1);
        assert_eq!(block.info().bucket_num, 42);
    }

    #[test]
    fn test_block_column_mut_exclusive_access() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(42);

        block.append_column("test", Arc::new(col1)).unwrap();

        // Should get mutable access since block has exclusive ownership
        let col_mut = block.column_mut(0).expect("Should get mutable access");

        // Can use it to append data
        let col_u64 =
            col_mut.as_any_mut().downcast_mut::<ColumnUInt64>().unwrap();
        col_u64.append(100);

        // Verify data was appended
        let col = block.column(0).unwrap();
        let col_u64 = col.as_any().downcast_ref::<ColumnUInt64>().unwrap();
        assert_eq!(col_u64.len(), 2);
        assert_eq!(col_u64.at(0), 42);
        assert_eq!(col_u64.at(1), 100);
    }

    #[test]
    #[should_panic(expected = "Cannot get mutable access to shared column")]
    fn test_block_column_mut_panics_on_shared() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(42);

        block.append_column("test", Arc::new(col1)).unwrap();

        // Clone the column to create a shared reference
        let _shared_ref = block.column(0).unwrap();

        // Should panic because column now has multiple references
        let _ = block.column_mut(0);
    }

    #[test]
    fn test_block_column_mut_out_of_bounds() {
        let mut block = Block::new();

        // Should return None for out of bounds index
        assert!(block.column_mut(0).is_none());
        assert!(block.column_mut(100).is_none());
    }

    #[test]
    fn test_block_column_by_name_mut_exclusive_access() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(42);

        block.append_column("my_column", Arc::new(col1)).unwrap();

        // Should get mutable access since block has exclusive ownership
        let col_mut = block
            .column_by_name_mut("my_column")
            .expect("Should get mutable access");

        // Can use it to append data
        let col_u64 =
            col_mut.as_any_mut().downcast_mut::<ColumnUInt64>().unwrap();
        col_u64.append(100);

        // Verify data was appended
        let col = block.column_by_name("my_column").unwrap();
        let col_u64 = col.as_any().downcast_ref::<ColumnUInt64>().unwrap();
        assert_eq!(col_u64.len(), 2);
        assert_eq!(col_u64.at(0), 42);
        assert_eq!(col_u64.at(1), 100);
    }

    #[test]
    #[should_panic(expected = "Cannot get mutable access to shared column")]
    fn test_block_column_by_name_mut_panics_on_shared() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(42);

        block.append_column("my_column", Arc::new(col1)).unwrap();

        // Clone the column to create a shared reference
        let _shared_ref = block.column_by_name("my_column").unwrap();

        // Should panic because column now has multiple references
        let _ = block.column_by_name_mut("my_column");
    }

    #[test]
    fn test_block_column_by_name_mut_not_found() {
        let mut block = Block::new();

        let mut col1 = ColumnUInt64::new();
        col1.append(42);

        block.append_column("my_column", Arc::new(col1)).unwrap();

        // Should return None for non-existent column name
        assert!(block.column_by_name_mut("nonexistent").is_none());
    }
}