clickhouse_rowbinary 0.1.0

RowBinary encoder/decoder for ClickHouse
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
//! This module provides two readers:
//! - `RowBinaryValueReader` decodes rows into `Value`s.
//! - `RowBinaryReader` scans seekable streams and exposes raw row bytes.

use std::io::{self, Read, Seek, SeekFrom};

use zeekstd::{Decoder, Seekable};

use crate::{
    error::{Error, Result},
    io::{read_string, read_uvarint},
    types::{TypeDesc, parse_type_desc},
};

use super::{
    format::RowBinaryFormat,
    scan::{CaptureReader, skip_value_optional, skip_value_required},
    schema::{Field, Row, Schema},
    value_rw::{read_value_optional, read_value_required},
};

/// `RowBinary` reader that streams rows from the provided reader.
pub struct RowBinaryValueReader<R: Read> {
    inner: R,
    schema: Schema,
    header: Option<RowBinaryHeader>,
}

impl<R: Read> RowBinaryValueReader<R> {
    /// Creates a reader without a pre-defined schema.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when header parsing fails.
    pub fn new(inner: R, format: RowBinaryFormat) -> Result<Self> {
        Self::with_schema_optional(inner, format, None)
    }

    /// Creates a reader with an expected schema.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when header parsing fails.
    pub fn with_schema(inner: R, format: RowBinaryFormat, schema: Schema) -> Result<Self> {
        Self::with_schema_optional(inner, format, Some(schema))
    }

    /// Reads the next row.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when decoding fails or the stream ends
    /// unexpectedly.
    pub fn read_row(&mut self) -> Result<Option<Row>> {
        if self.schema.is_empty() {
            return Ok(None);
        }

        if matches!(self.schema.fields()[0].ty, crate::types::TypeDesc::Nothing) {
            return Err(Error::UnsupportedCombination(
                "RowBinary cannot stream Nothing as the leading column".into(),
            ));
        }
        let mut row = Vec::with_capacity(self.schema.len());
        for (index, field) in self.schema.fields().iter().enumerate() {
            let value = if index == 0 {
                match read_value_optional(&field.ty, &mut self.inner)? {
                    Some(value) => value,
                    None => return Ok(None),
                }
            } else {
                read_value_required(&field.ty, &mut self.inner)?
            };
            row.push(value);
        }
        Ok(Some(row))
    }

    /// Reads the next row into the provided buffer.
    ///
    /// Returns `Ok(true)` when a row was read, or `Ok(false)` on EOF.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when decoding fails or the stream ends
    /// unexpectedly.
    pub fn read_row_into(&mut self, row: &mut Row) -> Result<bool> {
        if self.schema.is_empty() {
            row.clear();
            return Ok(false);
        }

        if matches!(self.schema.fields()[0].ty, crate::types::TypeDesc::Nothing) {
            return Err(Error::UnsupportedCombination(
                "RowBinary cannot stream Nothing as the leading column".into(),
            ));
        }
        row.clear();
        row.reserve(self.schema.len());
        for (index, field) in self.schema.fields().iter().enumerate() {
            let value = if index == 0 {
                match read_value_optional(&field.ty, &mut self.inner)? {
                    Some(value) => value,
                    None => return Ok(false),
                }
            } else {
                read_value_required(&field.ty, &mut self.inner)?
            };
            row.push(value);
        }
        Ok(true)
    }

    /// Returns an iterator over decoded rows.
    pub fn rows(self) -> RowBinaryRows<R> {
        RowBinaryRows { reader: self }
    }

    /// Returns the parsed header, if present.
    #[must_use]
    pub fn header(&self) -> Option<&RowBinaryHeader> {
        self.header.as_ref()
    }

    fn with_schema_optional(
        mut inner: R,
        format: RowBinaryFormat,
        schema: Option<Schema>,
    ) -> Result<Self> {
        let (schema, header) = parse_header_from_reader(&mut inner, format, schema)?;
        Ok(Self {
            inner,
            schema,
            header,
        })
    }
}

/// Iterator over `RowBinary` rows.
pub struct RowBinaryRows<R: Read> {
    reader: RowBinaryValueReader<R>,
}

impl<R: Read> Iterator for RowBinaryRows<R> {
    type Item = Result<Row>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.reader.read_row() {
            Ok(Some(row)) => Some(Ok(row)),
            Ok(None) => None,
            Err(err) => Some(Err(err)),
        }
    }
}

/// Header metadata for `RowBinary` formats with names and/or types.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RowBinaryHeader {
    /// Column names in stream order.
    pub names: Vec<String>,
    /// Column types when present in the header.
    pub types: Option<Vec<TypeDesc>>,
}

fn parse_header<S: Seekable>(
    decoder: &mut Decoder<'static, S>,
    format: RowBinaryFormat,
    schema: Option<Schema>,
) -> Result<(Schema, Option<RowBinaryHeader>, u64)> {
    decoder.seek(SeekFrom::Start(0))?;
    let (schema, header) = parse_header_from_reader(decoder, format, schema)?;
    let offset = decoder.offset();
    Ok((schema, header, offset))
}

fn parse_header_from_reader<R: Read + ?Sized>(
    reader: &mut R,
    format: RowBinaryFormat,
    schema: Option<Schema>,
) -> Result<(Schema, Option<RowBinaryHeader>)> {
    let has_schema = schema.is_some();
    let mut schema = schema.unwrap_or_else(|| Schema::new(Vec::new()));

    match format {
        RowBinaryFormat::RowBinary => {
            if !has_schema {
                return Err(Error::InvalidValue("schema required for RowBinary reader"));
            }
            if schema.is_empty() {
                return Err(Error::InvalidValue(
                    "schema must contain at least one column",
                ));
            }
            return Ok((schema, None));
        }
        RowBinaryFormat::RowBinaryWithNames | RowBinaryFormat::RowBinaryWithNamesAndTypes => {}
    }

    let column_count = read_uvarint(reader)?.ok_or_else(|| {
        Error::Io(io::Error::new(
            io::ErrorKind::UnexpectedEof,
            "missing header",
        ))
    })?;
    let column_count = usize::try_from(column_count)
        .map_err(|_| Error::Overflow("header column count too large"))?;
    if column_count == 0 {
        return Err(Error::InvalidValue("header column count must be > 0"));
    }

    let mut names = Vec::with_capacity(column_count);
    for _ in 0..column_count {
        let name = match read_string(reader) {
            Ok(Some(value)) => value,
            Ok(None) => return Err(Error::InvalidValue("missing header")),
            Err(Error::Io(err)) if err.kind() == io::ErrorKind::UnexpectedEof => {
                return Err(Error::InvalidValue("missing header"));
            }
            Err(err) => return Err(err),
        };
        names.push(name);
    }

    let types = if format == RowBinaryFormat::RowBinaryWithNamesAndTypes {
        let mut types = Vec::with_capacity(column_count);
        for _ in 0..column_count {
            let type_name = match read_string(reader) {
                Ok(Some(value)) => value,
                Ok(None) => return Err(Error::InvalidValue("missing header")),
                Err(Error::Io(err)) if err.kind() == io::ErrorKind::UnexpectedEof => {
                    return Err(Error::InvalidValue("missing header"));
                }
                Err(err) => return Err(err),
            };
            types.push(parse_type_desc(&type_name)?);
        }
        Some(types)
    } else {
        None
    };

    if has_schema {
        if schema.len() != names.len() {
            return Err(Error::InvalidValue("header column count mismatch"));
        }
        if format == RowBinaryFormat::RowBinaryWithNames
            && schema
                .fields()
                .iter()
                .map(|field| field.name.as_str())
                .ne(names.iter().map(String::as_str))
        {
            return Err(Error::InvalidValue("header column names mismatch"));
        }
    } else if let Some(types) = types.clone() {
        schema = Schema::new(
            names
                .iter()
                .cloned()
                .zip(types)
                .map(|(name, ty)| Field { name, ty })
                .collect(),
        );
    } else {
        return Err(Error::InvalidValue(
            "schema required for RowBinaryWithNames reader",
        ));
    }

    if schema.is_empty() {
        return Err(Error::InvalidValue(
            "schema must contain at least one column",
        ));
    }

    let header = Some(RowBinaryHeader { names, types });
    Ok((schema, header))
}

/// Seekable Zstd reader for `RowBinary` payloads.
pub struct RowBinaryReader<S: Seekable> {
    /// Parsed schema for the stream.
    schema: Schema,
    /// Parsed header metadata when the format includes names and/or types.
    header: Option<RowBinaryHeader>,
    /// Seekable decoder for the compressed payload.
    decoder: Decoder<'static, S>,
    /// Row offset stride for the sparse in-memory index.
    row_stride: usize,
    /// Sparse row offsets: entry `i` is the offset for row `i * row_stride`.
    row_offsets: Vec<u64>,
    /// Current row index.
    current_row: usize,
    /// Buffer holding the current row bytes (empty when unloaded).
    row_buf: Vec<u8>,
}

const DEFAULT_ROW_OFFSET_STRIDE: usize = 1024;

impl<S: Seekable> RowBinaryReader<S> {
    /// Creates a new seekable reader for the specified format.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when the decoder cannot be created.
    pub fn new(source: S, format: RowBinaryFormat, schema: Option<Schema>) -> Result<Self> {
        Self::new_with_stride(source, format, schema, DEFAULT_ROW_OFFSET_STRIDE)
    }

    /// Creates a new seekable reader with a custom row index stride.
    ///
    /// Smaller strides use more memory but make backward seeks faster.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when the decoder cannot be created or
    /// the stride is zero.
    pub fn new_with_stride(
        source: S,
        format: RowBinaryFormat,
        schema: Option<Schema>,
        row_stride: usize,
    ) -> Result<Self> {
        if row_stride == 0 {
            return Err(Error::InvalidValue("row stride must be greater than 0"));
        }
        let mut decoder = Decoder::new(source).map_err(Error::from)?;
        let (schema, header, data_start_offset) = parse_header(&mut decoder, format, schema)?;
        let mut reader = Self {
            schema,
            header,
            decoder,
            row_stride,
            row_offsets: Vec::new(),
            current_row: 0,
            row_buf: Vec::new(),
        };
        reader.row_offsets.push(data_start_offset);
        let maybe_len = read_row_bytes(&reader.schema, &mut reader.decoder, &mut reader.row_buf)?;
        if maybe_len.is_none() {
            reader.row_buf.clear();
        } else {
            reader.record_next_offset_if_needed();
        }
        Ok(reader)
    }

    /// Returns the parsed header, if present.
    #[must_use]
    pub fn header(&self) -> Option<&RowBinaryHeader> {
        self.header.as_ref()
    }

    /// Seeks to a specific row index.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when the row is out of range.
    pub fn seek_row(&mut self, index: usize) -> Result<()> {
        if index != self.current_row + 1 || self.row_buf.is_empty() {
            self.seek_to_row(index)?;
        }
        self.load_current_row()?;
        self.current_row = index;
        Ok(())
    }

    /// Seeks to a row relative to the current row index.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when the target row is out of range.
    pub fn seek_relative(&mut self, delta: i64) -> Result<()> {
        let base = i64::try_from(self.current_row)
            .map_err(|_| Error::Overflow("current row index too large"))?;
        let target = base
            .checked_add(delta)
            .ok_or(Error::InvalidValue("row index out of range"))?;
        if target < 0 {
            return Err(Error::InvalidValue("row index out of range"));
        }
        let target = usize::try_from(target).map_err(|_| Error::Overflow("row index too large"))?;
        self.seek_row(target)
    }

    /// Returns the current row as an uncompressed byte slice.
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::Error`] when decoding fails.
    pub fn current_row(&mut self) -> Result<Option<&[u8]>> {
        if self.row_buf.is_empty() {
            return Ok(None);
        }
        Ok(Some(&self.row_buf))
    }

    fn ensure_offsets_for(&mut self, index: usize) -> Result<()> {
        let block = index / self.row_stride;
        if block < self.row_offsets.len() {
            return Ok(());
        }
        let mut offset = self.row_offsets[self.row_offsets.len().saturating_sub(1)];
        self.decoder.seek(SeekFrom::Start(offset))?;
        while self.row_offsets.len() <= block {
            for _ in 0..self.row_stride {
                if let Err(err) = skip_row(&self.schema, &mut self.decoder) {
                    if matches!(
                        err,
                        Error::Io(ref io_err) if io_err.kind() == io::ErrorKind::UnexpectedEof
                    ) {
                        return Err(Error::InvalidValue("row index out of range"));
                    }
                    return Err(err);
                }
            }
            offset = self.decoder.offset();
            self.row_offsets.push(offset);
        }
        Ok(())
    }

    fn seek_to_row(&mut self, index: usize) -> Result<()> {
        self.ensure_offsets_for(index)?;
        let block = index / self.row_stride;
        let offset = self.row_offsets[block];
        self.decoder.seek(SeekFrom::Start(offset))?;
        let start = block * self.row_stride;
        if index > start {
            let mut row = start;
            while row < index {
                if let Err(err) = skip_row(&self.schema, &mut self.decoder) {
                    if matches!(
                        err,
                        Error::Io(ref io_err) if io_err.kind() == io::ErrorKind::UnexpectedEof
                    ) {
                        return Err(Error::InvalidValue("row index out of range"));
                    }
                    return Err(err);
                }
                row += 1;
            }
        }
        Ok(())
    }

    fn load_current_row(&mut self) -> Result<()> {
        let maybe_len = read_row_bytes(&self.schema, &mut self.decoder, &mut self.row_buf)?;
        if maybe_len.is_none() {
            self.row_buf.clear();
            return Err(Error::InvalidValue("row index out of range"));
        }
        self.record_next_offset_if_needed();
        Ok(())
    }

    fn record_next_offset_if_needed(&mut self) {
        let next_row = self.current_row + 1;
        if next_row.is_multiple_of(self.row_stride) {
            let next_block = next_row / self.row_stride;
            if self.row_offsets.len() == next_block {
                self.row_offsets.push(self.decoder.offset());
            }
        }
    }
}

fn read_row_bytes<R: Read + ?Sized>(
    schema: &Schema,
    reader: &mut R,
    buf: &mut Vec<u8>,
) -> Result<Option<usize>> {
    if matches!(schema.fields()[0].ty, TypeDesc::Nothing) {
        return Err(Error::UnsupportedCombination(
            "RowBinary cannot stream Nothing as the leading column".into(),
        ));
    }
    buf.clear();
    let mut capture = CaptureReader::new(reader, buf);
    let mut iter = schema.fields().iter();
    let Some(first) = iter.next() else {
        return Ok(None);
    };
    if let Some(()) = skip_value_optional(&first.ty, &mut capture)? {
    } else {
        buf.clear();
        return Ok(None);
    }
    for field in iter {
        skip_value_required(&field.ty, &mut capture)?;
    }
    Ok(Some(buf.len()))
}

fn skip_row<R: Read + ?Sized>(schema: &Schema, reader: &mut R) -> Result<()> {
    if schema.is_empty() {
        return Ok(());
    }
    let mut iter = schema.fields().iter();
    let Some(first) = iter.next() else {
        return Ok(());
    };
    let Some(()) = skip_value_optional(&first.ty, reader)? else {
        return Err(Error::Io(io::Error::new(
            io::ErrorKind::UnexpectedEof,
            "unexpected EOF while reading row",
        )));
    };
    for field in iter {
        skip_value_required(&field.ty, reader)?;
    }
    Ok(())
}