paimon 0.2.0

The rust implementation of Apache Paimon
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
// 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.

use super::cursor::AvroCursor;
use super::decode::{neg_count_to_usize, AvroRecordDecode};
use super::decode_helpers::{
    extract_record_schema, normalize_partition, read_bytes_field, read_int_field, read_long_field,
    read_string_field,
};
use super::manifest_file_meta_decode::decode_nullable_binary_table_stats;
use super::schema::{skip_nullable_field, FieldSchema, WriterSchema};
use crate::spec::manifest_common::FileKind;
use crate::spec::stats::BinaryTableStats;
use crate::spec::DataFileMeta;
use crate::spec::ManifestEntry;
use chrono::{DateTime, Utc};

impl AvroRecordDecode for ManifestEntry {
    fn decode(cursor: &mut AvroCursor, writer_schema: &WriterSchema) -> crate::Result<Self> {
        let mut kind: Option<FileKind> = None;
        let mut partition: Option<Vec<u8>> = None;
        let mut bucket: Option<i32> = None;
        let mut total_buckets: Option<i32> = None;
        let mut file: Option<DataFileMeta> = None;
        let mut version: Option<i32> = None;

        for field in &writer_schema.fields {
            match field.name.as_str() {
                "_KIND" => {
                    let v = read_int_field(cursor, field.nullable)?;
                    kind = Some(match v {
                        0 => FileKind::Add,
                        1 => FileKind::Delete,
                        _ => {
                            return Err(crate::Error::UnexpectedError {
                                message: format!("unknown FileKind: {v}"),
                                source: None,
                            })
                        }
                    });
                }
                "_PARTITION" => partition = Some(read_bytes_field(cursor, field.nullable)?),
                "_BUCKET" => bucket = Some(read_int_field(cursor, field.nullable)?),
                "_TOTAL_BUCKETS" => total_buckets = Some(read_int_field(cursor, field.nullable)?),
                "_FILE" => {
                    file = decode_nullable_data_file_meta(cursor, &field.schema, field.nullable)?;
                }
                "_VERSION" => version = Some(read_int_field(cursor, field.nullable)?),
                _ => skip_nullable_field(cursor, &field.schema, field.nullable)?,
            }
        }

        Ok(ManifestEntry::new(
            kind.unwrap_or(FileKind::Add),
            normalize_partition(partition),
            bucket.unwrap_or(0),
            total_buckets.unwrap_or(0),
            file.unwrap_or_else(default_data_file_meta),
            version.unwrap_or(0),
        ))
    }
}

/// Decode ManifestEntry records with a filter applied on lightweight fields.
///
/// Decodes only _KIND, _PARTITION, _BUCKET, _TOTAL_BUCKETS, _VERSION first.
/// If `filter` returns false, skips the expensive _FILE (DataFileMeta) decoding.
/// Returns only entries that pass the filter.
pub(crate) fn decode_manifest_entries_filtered<F>(
    cursor: &mut AvroCursor,
    writer_schema: &WriterSchema,
    is_union_wrapped: bool,
    filter: &mut F,
) -> crate::Result<Option<ManifestEntry>>
where
    F: FnMut(FileKind, &[u8], i32, i32) -> bool,
{
    if is_union_wrapped {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Err(crate::Error::UnexpectedError {
                message: "avro decode: unexpected null in top-level union".into(),
                source: None,
            });
        }
    }

    // Two-pass decode: first collect lightweight fields and record _FILE position,
    // then conditionally decode _FILE.
    let mut kind: Option<FileKind> = None;
    let mut partition: Option<Vec<u8>> = None;
    let mut bucket: Option<i32> = None;
    let mut total_buckets: Option<i32> = None;
    let mut version: Option<i32> = None;
    let mut file: Option<DataFileMeta> = None;
    let mut file_skipped = false;

    for field in &writer_schema.fields {
        match field.name.as_str() {
            "_KIND" => {
                let v = read_int_field(cursor, field.nullable)?;
                kind = Some(match v {
                    0 => FileKind::Add,
                    1 => FileKind::Delete,
                    _ => {
                        return Err(crate::Error::UnexpectedError {
                            message: format!("unknown FileKind: {v}"),
                            source: None,
                        })
                    }
                });
            }
            "_PARTITION" => partition = Some(read_bytes_field(cursor, field.nullable)?),
            "_BUCKET" => bucket = Some(read_int_field(cursor, field.nullable)?),
            "_TOTAL_BUCKETS" => total_buckets = Some(read_int_field(cursor, field.nullable)?),
            "_FILE" => {
                let can_filter = kind.is_some()
                    && partition.is_some()
                    && bucket.is_some()
                    && total_buckets.is_some();
                if can_filter {
                    let k = kind.unwrap_or(FileKind::Add);
                    let p = partition.as_deref().unwrap_or(&[]);
                    let b = bucket.unwrap_or(0);
                    let tb = total_buckets.unwrap_or(0);
                    if filter(k, p, b, tb) {
                        file =
                            decode_nullable_data_file_meta(cursor, &field.schema, field.nullable)?;
                    } else {
                        skip_nullable_field(cursor, &field.schema, field.nullable)?;
                        file_skipped = true;
                    }
                } else {
                    file = decode_nullable_data_file_meta(cursor, &field.schema, field.nullable)?;
                }
            }
            "_VERSION" => version = Some(read_int_field(cursor, field.nullable)?),
            _ => skip_nullable_field(cursor, &field.schema, field.nullable)?,
        }
    }

    if file_skipped {
        return Ok(None);
    }

    Ok(Some(ManifestEntry::new(
        kind.unwrap_or(FileKind::Add),
        normalize_partition(partition),
        bucket.unwrap_or(0),
        total_buckets.unwrap_or(0),
        file.unwrap_or_else(default_data_file_meta),
        version.unwrap_or(0),
    )))
}

fn decode_nullable_data_file_meta(
    cursor: &mut AvroCursor,
    field_schema: &FieldSchema,
    nullable: bool,
) -> crate::Result<Option<DataFileMeta>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(None);
        }
    }
    let record_schema =
        extract_record_schema(field_schema).ok_or_else(|| crate::Error::UnexpectedError {
            message: "avro decode: _FILE field is not a record".into(),
            source: None,
        })?;
    decode_data_file_meta(cursor, record_schema).map(Some)
}

/// Read string array, handling both `{"type":"array",...}` and `["null", {"type":"array",...}]`.
fn read_string_array_field(cursor: &mut AvroCursor, nullable: bool) -> crate::Result<Vec<String>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(vec![]);
        }
    }
    decode_string_array(cursor)
}

fn decode_data_file_meta(
    cursor: &mut AvroCursor,
    writer_schema: &WriterSchema,
) -> crate::Result<DataFileMeta> {
    let mut file_name: Option<String> = None;
    let mut file_size: Option<i64> = None;
    let mut row_count: Option<i64> = None;
    let mut min_key: Option<Vec<u8>> = None;
    let mut max_key: Option<Vec<u8>> = None;
    let mut key_stats: Option<BinaryTableStats> = None;
    let mut value_stats: Option<BinaryTableStats> = None;
    let mut min_sequence_number: Option<i64> = None;
    let mut max_sequence_number: Option<i64> = None;
    let mut schema_id: Option<i64> = None;
    let mut level: Option<i32> = None;
    let mut extra_files: Option<Vec<String>> = None;
    let mut creation_time: Option<DateTime<Utc>> = None;
    let mut delete_row_count: Option<i64> = None;
    let mut embedded_index: Option<Vec<u8>> = None;
    let mut file_source: Option<i32> = None;
    let mut value_stats_cols: Option<Vec<String>> = None;
    let mut external_path: Option<String> = None;
    let mut first_row_id: Option<i64> = None;
    let mut write_cols: Option<Vec<String>> = None;

    for field in &writer_schema.fields {
        match field.name.as_str() {
            "_FILE_NAME" => file_name = Some(read_string_field(cursor, field.nullable)?),
            "_FILE_SIZE" => file_size = Some(read_long_field(cursor, field.nullable)?),
            "_ROW_COUNT" => row_count = Some(read_long_field(cursor, field.nullable)?),
            "_MIN_KEY" => min_key = Some(read_bytes_field(cursor, field.nullable)?),
            "_MAX_KEY" => max_key = Some(read_bytes_field(cursor, field.nullable)?),
            "_KEY_STATS" => {
                key_stats =
                    decode_nullable_binary_table_stats(cursor, &field.schema, field.nullable)?
            }
            "_VALUE_STATS" => {
                value_stats =
                    decode_nullable_binary_table_stats(cursor, &field.schema, field.nullable)?
            }
            "_MIN_SEQUENCE_NUMBER" => {
                min_sequence_number = Some(read_long_field(cursor, field.nullable)?)
            }
            "_MAX_SEQUENCE_NUMBER" => {
                max_sequence_number = Some(read_long_field(cursor, field.nullable)?)
            }
            "_SCHEMA_ID" => schema_id = Some(read_long_field(cursor, field.nullable)?),
            "_LEVEL" => level = Some(read_int_field(cursor, field.nullable)?),
            "_EXTRA_FILES" => extra_files = Some(read_string_array_field(cursor, field.nullable)?),
            "_CREATION_TIME" => {
                creation_time = decode_nullable_timestamp_millis(cursor, field.nullable)?
            }
            "_DELETE_ROW_COUNT" => delete_row_count = decode_nullable_long(cursor, field.nullable)?,
            "_EMBEDDED_FILE_INDEX" => {
                embedded_index = decode_nullable_bytes(cursor, field.nullable)?
            }
            "_FILE_SOURCE" => file_source = decode_nullable_int(cursor, field.nullable)?,
            "_VALUE_STATS_COLS" => {
                value_stats_cols = decode_nullable_string_array(cursor, field.nullable)?
            }
            "_EXTERNAL_PATH" => external_path = decode_nullable_string(cursor, field.nullable)?,
            "_FIRST_ROW_ID" => first_row_id = decode_nullable_long(cursor, field.nullable)?,
            "_WRITE_COLS" => write_cols = decode_nullable_string_array(cursor, field.nullable)?,
            _ => skip_nullable_field(cursor, &field.schema, field.nullable)?,
        }
    }

    Ok(DataFileMeta {
        file_name: file_name.unwrap_or_default(),
        file_size: file_size.unwrap_or(0),
        row_count: row_count.unwrap_or(0),
        min_key: min_key.unwrap_or_default(),
        max_key: max_key.unwrap_or_default(),
        key_stats: key_stats.unwrap_or_else(|| BinaryTableStats::new(vec![], vec![], vec![])),
        value_stats: value_stats.unwrap_or_else(|| BinaryTableStats::new(vec![], vec![], vec![])),
        min_sequence_number: min_sequence_number.unwrap_or(0),
        max_sequence_number: max_sequence_number.unwrap_or(0),
        schema_id: schema_id.unwrap_or(0),
        level: level.unwrap_or(0),
        extra_files: extra_files.unwrap_or_default(),
        creation_time,
        delete_row_count,
        embedded_index,
        file_source,
        value_stats_cols,
        external_path,
        first_row_id,
        write_cols,
    })
}

fn decode_string_array(cursor: &mut AvroCursor) -> crate::Result<Vec<String>> {
    let mut result = Vec::new();
    loop {
        let count = cursor.read_long()?;
        if count == 0 {
            break;
        }
        let count = if count < 0 {
            cursor.skip_long()?;
            neg_count_to_usize(count)?
        } else {
            count as usize
        };
        result.reserve(count);
        for _ in 0..count {
            result.push(cursor.read_string()?.to_string());
        }
    }
    Ok(result)
}

fn decode_nullable_long(cursor: &mut AvroCursor, nullable: bool) -> crate::Result<Option<i64>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(None);
        }
    }
    Ok(Some(cursor.read_long()?))
}

fn decode_nullable_int(cursor: &mut AvroCursor, nullable: bool) -> crate::Result<Option<i32>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(None);
        }
    }
    Ok(Some(cursor.read_int()?))
}

fn decode_nullable_bytes(
    cursor: &mut AvroCursor,
    nullable: bool,
) -> crate::Result<Option<Vec<u8>>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(None);
        }
    }
    Ok(Some(cursor.read_bytes()?.to_vec()))
}

fn decode_nullable_string(
    cursor: &mut AvroCursor,
    nullable: bool,
) -> crate::Result<Option<String>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(None);
        }
    }
    Ok(Some(cursor.read_string()?.to_string()))
}

fn decode_nullable_string_array(
    cursor: &mut AvroCursor,
    nullable: bool,
) -> crate::Result<Option<Vec<String>>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(None);
        }
    }
    Ok(Some(decode_string_array(cursor)?))
}

fn decode_nullable_timestamp_millis(
    cursor: &mut AvroCursor,
    nullable: bool,
) -> crate::Result<Option<DateTime<Utc>>> {
    if nullable {
        let idx = cursor.read_union_index()?;
        if idx == 0 {
            return Ok(None);
        }
    }
    let millis = cursor.read_long()?;
    let secs = millis.div_euclid(1000);
    let nanos = (millis.rem_euclid(1000) * 1_000_000) as u32;
    Ok(DateTime::from_timestamp(secs, nanos))
}

fn default_data_file_meta() -> DataFileMeta {
    DataFileMeta {
        file_name: String::new(),
        file_size: 0,
        row_count: 0,
        min_key: vec![],
        max_key: vec![],
        key_stats: BinaryTableStats::new(vec![], vec![], vec![]),
        value_stats: BinaryTableStats::new(vec![], vec![], vec![]),
        min_sequence_number: 0,
        max_sequence_number: 0,
        schema_id: 0,
        level: 0,
        extra_files: vec![],
        creation_time: None,
        delete_row_count: None,
        embedded_index: None,
        file_source: None,
        value_stats_cols: None,
        external_path: None,
        first_row_id: None,
        write_cols: None,
    }
}