fluss-rs 1.0.0

The official rust client of Apache Fluss
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

pub mod binary_array;
pub mod binary_map;
mod column;
pub(crate) mod column_vector;
pub mod columnar;
pub mod view;

pub(crate) mod datum;
mod decimal;

pub(crate) mod aligned;
pub mod binary;
pub(crate) mod column_writer;
pub mod compacted;
pub mod encode;
pub mod field_getter;
mod fixed_schema_decoder;
mod lookup_row;
pub mod paimon;
mod projected_row;
mod row_decoder;

use crate::client::WriteFormat;
pub use binary_array::{FlussArray, FlussArrayWriter};
pub use binary_map::{FlussMap, FlussMapWriter};
use bytes::Bytes;
pub use column::*;
pub use compacted::CompactedRow;
pub use datum::*;
pub use decimal::{Decimal, MAX_COMPACT_PRECISION};
pub use encode::KeyEncoder;
pub(crate) use fixed_schema_decoder::FixedSchemaDecoder;
pub use lookup_row::LookupRow;
pub(crate) use projected_row::ProjectedRow;
pub use row_decoder::{CompactedRowDecoder, RowDecoder, RowDecoderFactory};
use serde::Serialize;
pub use view::{ArrayView, MapView, RowView};

pub struct BinaryRow<'a> {
    data: BinaryDataWrapper<'a>,
}

pub enum BinaryDataWrapper<'a> {
    Bytes(Bytes),
    Ref(&'a [u8]),
}

impl<'a> BinaryRow<'a> {
    /// Returns the binary representation of this row as a byte slice.
    pub fn as_bytes(&'a self) -> &'a [u8] {
        match &self.data {
            BinaryDataWrapper::Bytes(bytes) => bytes.as_ref(),
            BinaryDataWrapper::Ref(r) => r,
        }
    }
}

use crate::error::Error::IllegalArgument;
use crate::error::Result;

/// Positional typed reads shared by rows and arrays. Lets one writer accept
/// either shape without materializing.
pub trait DataGetters: Send + Sync {
    fn is_null_at(&self, pos: usize) -> Result<bool>;

    fn get_boolean(&self, pos: usize) -> Result<bool>;
    fn get_byte(&self, pos: usize) -> Result<i8>;
    fn get_short(&self, pos: usize) -> Result<i16>;
    fn get_int(&self, pos: usize) -> Result<i32>;
    fn get_long(&self, pos: usize) -> Result<i64>;
    fn get_float(&self, pos: usize) -> Result<f32>;
    fn get_double(&self, pos: usize) -> Result<f64>;
    fn get_char(&self, pos: usize, length: usize) -> Result<&str>;
    fn get_string(&self, pos: usize) -> Result<&str>;
    fn get_decimal(&self, pos: usize, precision: usize, scale: usize) -> Result<Decimal>;
    fn get_date(&self, pos: usize) -> Result<Date>;
    fn get_time(&self, pos: usize) -> Result<Time>;
    fn get_timestamp_ntz(&self, pos: usize, precision: u32) -> Result<TimestampNtz>;
    fn get_timestamp_ltz(&self, pos: usize, precision: u32) -> Result<TimestampLtz>;
    fn get_binary(&self, pos: usize, length: usize) -> Result<&[u8]>;
    fn get_bytes(&self, pos: usize) -> Result<&[u8]>;

    fn get_array(&self, pos: usize) -> Result<view::ArrayView<'_>> {
        Err(IllegalArgument {
            message: format!("get_array not supported at position {pos}"),
        })
    }

    fn get_map(&self, pos: usize) -> Result<view::MapView<'_>> {
        Err(IllegalArgument {
            message: format!("get_map not supported at position {pos}"),
        })
    }

    fn get_row(&self, pos: usize) -> Result<view::RowView<'_>> {
        Err(IllegalArgument {
            message: format!("get_row not supported at position {pos}"),
        })
    }
}

pub trait InternalRow: DataGetters {
    /// Returns the number of fields in this row
    fn get_field_count(&self) -> usize;

    /// Returns encoded bytes if already encoded
    fn as_encoded_bytes(&self, _write_format: WriteFormat) -> Option<&[u8]> {
        None
    }
}

/// Read-side accessor for an ARRAY column.
pub trait InternalArray: DataGetters {
    /// Number of elements (including nulls).
    fn size(&self) -> usize;
}

/// Read-side accessor for a MAP column. Keys and values are parallel
/// [`InternalArray`]s indexed in lockstep.
pub trait InternalMap: Send + Sync {
    /// Number of key/value entries.
    fn size(&self) -> usize;

    /// Keys in entry order; the binary-map invariant guarantees no nulls.
    fn key_array(&self) -> &dyn InternalArray;

    /// Values in entry order, paired with [`Self::key_array`] by position.
    fn value_array(&self) -> &dyn InternalArray;
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct GenericRow<'a> {
    pub values: Vec<Datum<'a>>,
}

impl<'a> GenericRow<'a> {
    fn get_value(&self, pos: usize) -> Result<&Datum<'a>> {
        self.values.get(pos).ok_or_else(|| IllegalArgument {
            message: format!(
                "position {pos} out of bounds (row has {} fields)",
                self.values.len()
            ),
        })
    }

    fn try_convert<T: TryFrom<&'a Datum<'a>>>(
        &'a self,
        pos: usize,
        expected_type: &str,
    ) -> Result<T> {
        let datum = self.get_value(pos)?;
        T::try_from(datum).map_err(|_| IllegalArgument {
            message: format!(
                "type mismatch at position {pos}: expected {expected_type}, got {datum:?}"
            ),
        })
    }
}

impl<'a> InternalRow for GenericRow<'a> {
    fn get_field_count(&self) -> usize {
        self.values.len()
    }
}

impl<'a> DataGetters for GenericRow<'a> {
    fn is_null_at(&self, pos: usize) -> Result<bool> {
        Ok(self.get_value(pos)?.is_null())
    }

    fn get_boolean(&self, pos: usize) -> Result<bool> {
        self.try_convert(pos, "Boolean")
    }

    fn get_byte(&self, pos: usize) -> Result<i8> {
        self.try_convert(pos, "TinyInt")
    }

    fn get_short(&self, pos: usize) -> Result<i16> {
        self.try_convert(pos, "SmallInt")
    }

    fn get_int(&self, pos: usize) -> Result<i32> {
        self.try_convert(pos, "Int")
    }

    fn get_long(&self, pos: usize) -> Result<i64> {
        self.try_convert(pos, "BigInt")
    }

    fn get_float(&self, pos: usize) -> Result<f32> {
        self.try_convert(pos, "Float")
    }

    fn get_double(&self, pos: usize) -> Result<f64> {
        self.try_convert(pos, "Double")
    }

    fn get_char(&self, pos: usize, _length: usize) -> Result<&str> {
        // don't check length, following java client
        self.get_string(pos)
    }

    fn get_string(&self, pos: usize) -> Result<&str> {
        self.try_convert(pos, "String")
    }

    fn get_decimal(&self, pos: usize, _precision: usize, _scale: usize) -> Result<Decimal> {
        match self.get_value(pos)? {
            Datum::Decimal(d) => Ok(d.clone()),
            other => Err(IllegalArgument {
                message: format!(
                    "type mismatch at position {pos}: expected Decimal, got {other:?}"
                ),
            }),
        }
    }

    fn get_date(&self, pos: usize) -> Result<Date> {
        match self.get_value(pos)? {
            Datum::Date(d) => Ok(*d),
            Datum::Int32(i) => Ok(Date::new(*i)),
            other => Err(IllegalArgument {
                message: format!(
                    "type mismatch at position {pos}: expected Date or Int32, got {other:?}"
                ),
            }),
        }
    }

    fn get_time(&self, pos: usize) -> Result<Time> {
        match self.get_value(pos)? {
            Datum::Time(t) => Ok(*t),
            Datum::Int32(i) => Ok(Time::new(*i)),
            other => Err(IllegalArgument {
                message: format!(
                    "type mismatch at position {pos}: expected Time or Int32, got {other:?}"
                ),
            }),
        }
    }

    fn get_timestamp_ntz(&self, pos: usize, _precision: u32) -> Result<TimestampNtz> {
        match self.get_value(pos)? {
            Datum::TimestampNtz(t) => Ok(*t),
            other => Err(IllegalArgument {
                message: format!(
                    "type mismatch at position {pos}: expected TimestampNtz, got {other:?}"
                ),
            }),
        }
    }

    fn get_timestamp_ltz(&self, pos: usize, _precision: u32) -> Result<TimestampLtz> {
        match self.get_value(pos)? {
            Datum::TimestampLtz(t) => Ok(*t),
            other => Err(IllegalArgument {
                message: format!(
                    "type mismatch at position {pos}: expected TimestampLtz, got {other:?}"
                ),
            }),
        }
    }

    fn get_binary(&self, pos: usize, _length: usize) -> Result<&[u8]> {
        match self.get_value(pos)? {
            Datum::Blob(b) => Ok(b.as_ref()),
            other => Err(IllegalArgument {
                message: format!("type mismatch at position {pos}: expected Binary, got {other:?}"),
            }),
        }
    }

    fn get_bytes(&self, pos: usize) -> Result<&[u8]> {
        match self.get_value(pos)? {
            Datum::Blob(b) => Ok(b.as_ref()),
            other => Err(IllegalArgument {
                message: format!("type mismatch at position {pos}: expected Bytes, got {other:?}"),
            }),
        }
    }

    fn get_array(&self, pos: usize) -> Result<view::ArrayView<'_>> {
        match self.get_value(pos)? {
            Datum::Array(a) => Ok(view::ArrayView::Binary(a.clone())),
            other => Err(IllegalArgument {
                message: format!("type mismatch at position {pos}: expected Array, got {other:?}"),
            }),
        }
    }

    fn get_map(&self, pos: usize) -> Result<view::MapView<'_>> {
        match self.get_value(pos)? {
            Datum::Map(m) => Ok(view::MapView::Binary(m.clone())),
            other => Err(IllegalArgument {
                message: format!("type mismatch at position {pos}: expected Map, got {other:?}"),
            }),
        }
    }

    fn get_row(&self, pos: usize) -> Result<view::RowView<'_>> {
        match self.get_value(pos)? {
            Datum::Row(r) => Ok(view::RowView::Generic(r.as_ref())),
            other => Err(IllegalArgument {
                message: format!("type mismatch at position {pos}: expected Row, got {other:?}"),
            }),
        }
    }
}

impl<'a> GenericRow<'a> {
    /// Consumes this row and returns one whose `Datum` values are all
    /// `'static` (borrowed `Cow`s are promoted to owned, nested rows recurse).
    /// Lets a row outlive the bytes it was decoded from.
    pub fn into_owned(self) -> GenericRow<'static> {
        GenericRow {
            values: self.values.into_iter().map(Datum::into_owned).collect(),
        }
    }
}

impl<'a> GenericRow<'a> {
    pub fn from_data(data: Vec<impl Into<Datum<'a>>>) -> GenericRow<'a> {
        GenericRow {
            values: data.into_iter().map(Into::into).collect(),
        }
    }

    /// Creates a GenericRow with the specified number of fields, all initialized to null.
    ///
    /// This is useful when you need to create a row with a specific field count
    /// but only want to set some fields (e.g., for KV delete operations where
    /// only primary key fields need to be set).
    ///
    /// # Example
    /// ```
    /// use fluss::row::GenericRow;
    ///
    /// let mut row = GenericRow::new(3);
    /// row.set_field(0, 42); // Only set the primary key
    /// // Fields 1 and 2 remain null
    /// ```
    pub fn new(field_count: usize) -> GenericRow<'a> {
        GenericRow {
            values: vec![Datum::Null; field_count],
        }
    }

    /// Sets the field at the given position to the specified value.
    ///
    /// # Panics
    /// Panics if `pos` is out of bounds (>= field count).
    pub fn set_field(&mut self, pos: usize, value: impl Into<Datum<'a>>) {
        self.values[pos] = value.into();
    }
}

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

    #[test]
    fn is_null_at_checks_datum_nullity() {
        let mut row = GenericRow::new(2);
        row.set_field(0, Datum::Null);
        row.set_field(1, 42_i32);

        assert!(row.is_null_at(0).unwrap());
        assert!(!row.is_null_at(1).unwrap());
    }

    #[test]
    fn is_null_at_out_of_bounds_returns_error() {
        let row = GenericRow::from_data(vec![42_i32]);
        let err = row.is_null_at(5).unwrap_err();
        assert!(
            err.to_string().contains("out of bounds"),
            "Expected out of bounds error, got: {err}"
        );
    }

    #[test]
    fn new_initializes_nulls() {
        let row = GenericRow::new(3);
        assert_eq!(row.get_field_count(), 3);
        assert!(row.is_null_at(0).unwrap());
        assert!(row.is_null_at(1).unwrap());
        assert!(row.is_null_at(2).unwrap());
    }

    #[test]
    fn partial_row_for_delete() {
        // Simulates delete scenario: only primary key (field 0) is set
        let mut row = GenericRow::new(3);
        row.set_field(0, 123_i32);
        // Fields 1 and 2 remain null
        assert_eq!(row.get_field_count(), 3);
        assert_eq!(row.get_int(0).unwrap(), 123);
        assert!(row.is_null_at(1).unwrap());
        assert!(row.is_null_at(2).unwrap());
    }

    #[test]
    fn type_mismatch_returns_error() {
        let row = GenericRow::from_data(vec![Datum::Int64(999)]);
        let err = row.get_string(0).unwrap_err();
        assert!(
            err.to_string().contains("type mismatch"),
            "Expected type mismatch error, got: {err}"
        );
    }

    #[test]
    fn out_of_bounds_returns_error() {
        let row = GenericRow::from_data(vec![42_i32]);
        let err = row.get_int(5).unwrap_err();
        assert!(
            err.to_string().contains("out of bounds"),
            "Expected out of bounds error, got: {err}"
        );
    }
}