lightstream 0.4.3

Composable, zero-copy Arrow IPC and native data streaming for Rust with SIMD-aligned I/O, async support, and memory-mapping.
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
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
//! # IPC FlatBuffers Builders (internal)
//!
//! Helpers that construct FlatBuffer-encoded Arrow IPC artefacts used by the encoder.
//! This includes schema messages, record batches, dictionary batches, and the file footer with block metadata.
//!
//! Follows the IPC protocol as outlined
//! [here](https://arrow.apache.org/docs/format/Columnar.html#serialisation-and-interprocess-communication-ipc).
//!
//! Uses the generated FlatBuffers types under `src/arrow/*` (built from `src/flatb/*`).

use std::io::{self};

use minarrow::ffi::arrow_dtype::CategoricalIndexType;
use minarrow::{ArrowType, Field};

use crate::arrow::file::org::apache::arrow::flatbuf as fbf;
use crate::arrow::message::org::apache::arrow::flatbuf as fbm;
use crate::debug_println;
use crate::traits::stream_buffer::StreamBuffer;

/// Build the Arrow schema as a FlatBuffer-encoded IPC message.
///
/// # Arguments
/// - `fields`: Slice of Arrow [`Field`] definitions.
///
/// # Returns
/// IPC-encoded schema message (suitable for inclusion in an IPC frame).
///
/// # Errors
/// Returns error for any unsupported Arrow type.
pub fn build_flatbuf_schema<'a>(
    fbb: &'a mut flatbuffers::FlatBufferBuilder<'static>,
    schema: &[Field],
) -> io::Result<Vec<u8>> {
    fbb.reset();
    let fb_fields = build_flatbuf_fields(fbb, schema)?;
    let fields_vec = fbb.create_vector(&fb_fields);

    // let features_vec = fbb.create_vector(&[fbm::Feature::UNUSED]);
    let schema_obj = fbm::Schema::create(
        fbb,
        &fbm::SchemaArgs {
            endianness: fbm::Endianness::Little,
            fields: Some(fields_vec),
            custom_metadata: None,
            features: None, // features: Some(features_vec)
        },
    );
    let msg = fbm::Message::create(
        fbb,
        &fbm::MessageArgs {
            version: fbm::MetadataVersion::V5,
            header_type: fbm::MessageHeader::Schema,
            header: Some(schema_obj.as_union_value()),
            bodyLength: 0,
            custom_metadata: None,
        },
    );
    fbb.finish(msg, None);
    let flatbuffers = fbb.finished_data().to_vec();

    Ok(flatbuffers)
}

/// Build a vector of Arrow schema `Field` objects (for Arrow IPC message section),
/// using the message-specific FlatBuffers definitions.
///
/// This constructs FlatBuffers offsets for each Arrow field, with each field
/// encoded using the `fbm` (message) schema types.
///
/// # Arguments
/// - `fbb`: The FlatBufferBuilder used to allocate objects.
/// - `schema`: Slice of Arrow [`Field`] definitions.
///
/// # Returns
/// Vector of WIPOffset<fbm::Field> suitable for inclusion in an IPC message schema.
///
/// # Errors
/// Returns error for any unsupported Arrow type.
fn build_flatbuf_fields<'fbb>(
    fbb: &mut flatbuffers::FlatBufferBuilder<'fbb>,
    schema: &[Field],
) -> io::Result<Vec<flatbuffers::WIPOffset<fbm::Field<'fbb>>>> {
    let mut fb_fields = Vec::with_capacity(schema.len());

    // Build all fields
    for (idx, field) in schema.iter().enumerate() {
        fb_fields.push(build_flatbuf_field(fbb, field, idx)?);
    }

    Ok(fb_fields)
}

/// Build a single Arrow schema `Field` object (for Arrow IPC message section),
/// using message-specific FlatBuffers definitions.
///
/// This encodes all Arrow logical types as a FlatBuffer offset, for use in Arrow IPC message metadata.
///
/// # Arguments
/// - `fbb`: The FlatBufferBuilder used to allocate objects.
/// - `field`: The Arrow [`Field`] schema element.
///
/// # Returns
/// FlatBuffers WIPOffset<fbm::Field> describing the field for the IPC message schema.
///
/// # Errors
/// Returns error for any unsupported Arrow type.
fn build_flatbuf_field<'fbb>(
    fbb: &mut flatbuffers::FlatBufferBuilder<'fbb>,
    field: &Field,
    col_idx: usize,
) -> io::Result<flatbuffers::WIPOffset<fbm::Field<'fbb>>> {
    // Create field name
    let fb_name = fbb.create_string(&field.name);

    // Create empty children vector (we don't use nested types)
    let children = fbb.create_vector::<flatbuffers::WIPOffset<fbm::Field>>(&[]);

    // No custom metadata
    let custom_metadata = None;

    // Build the type and optional dictionary encoding
    let (fb_type_type, fb_type_offset, fb_dict) = match &field.dtype {
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::Int8 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 8,
                    is_signed: true,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::Int16 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 16,
                    is_signed: true,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::Int32 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 32,
                    is_signed: true,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::Int64 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 64,
                    is_signed: true,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::UInt8 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 8,
                    is_signed: false,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::UInt16 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 16,
                    is_signed: false,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::UInt32 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 32,
                    is_signed: false,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::UInt64 => {
            let int = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: 64,
                    is_signed: false,
                },
            );
            (fbm::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::Float32 => {
            let fp = fbm::FloatingPoint::create(
                fbb,
                &fbm::FloatingPointArgs {
                    precision: fbm::Precision::SINGLE,
                },
            );
            (fbm::Type::FloatingPoint, Some(fp.as_union_value()), None)
        }
        ArrowType::Float64 => {
            let fp = fbm::FloatingPoint::create(
                fbb,
                &fbm::FloatingPointArgs {
                    precision: fbm::Precision::DOUBLE,
                },
            );
            (fbm::Type::FloatingPoint, Some(fp.as_union_value()), None)
        }
        ArrowType::Boolean => {
            let bl = fbm::Bool::create(fbb, &fbm::BoolArgs {});
            (fbm::Type::Bool, Some(bl.as_union_value()), None)
        }
        ArrowType::String => {
            let s = fbm::Utf8::create(fbb, &fbm::Utf8Args {});
            (fbm::Type::Utf8, Some(s.as_union_value()), None)
        }
        #[cfg(feature = "large_string")]
        ArrowType::LargeString => {
            let s = fbm::LargeUtf8::create(fbb, &fbm::LargeUtf8Args {});
            (fbm::Type::Utf8, Some(s.as_union_value()), None)
        }
        #[cfg(feature = "datetime")]
        ArrowType::Date32 => {
            let date = fbm::Date::create(
                fbb,
                &fbm::DateArgs {
                    unit: fbm::DateUnit::DAY,
                },
            );
            (fbm::Type::Date, Some(date.as_union_value()), None)
        }
        #[cfg(feature = "datetime")]
        ArrowType::Date64 => {
            let date = fbm::Date::create(
                fbb,
                &fbm::DateArgs {
                    unit: fbm::DateUnit::MILLISECOND,
                },
            );
            (fbm::Type::Date, Some(date.as_union_value()), None)
        }
        ArrowType::Dictionary(idx_ty) => {
            // Build index type for dictionary
            let idx_width = match idx_ty {
                CategoricalIndexType::UInt32 => 32,
                #[cfg(feature = "extended_categorical")]
                CategoricalIndexType::UInt64 => 64,
                #[cfg(feature = "extended_categorical")]
                CategoricalIndexType::UInt16 => 16,
                #[cfg(feature = "extended_categorical")]
                CategoricalIndexType::UInt8 => 8,
            };

            // Create the index type Int
            let index_type = fbm::Int::create(
                fbb,
                &fbm::IntArgs {
                    bitWidth: idx_width,
                    is_signed: false,
                },
            );

            // Create dictionary encoding with column index as ID
            let dict = fbm::DictionaryEncoding::create(
                fbb,
                &fbm::DictionaryEncodingArgs {
                    id: col_idx as i64,
                    indexType: Some(index_type),
                    isOrdered: false,
                    dictionaryKind: fbm::DictionaryKind::DenseArray,
                },
            );

            // For dictionary-encoded fields, the type is Utf8 (string dictionary)
            // but the physical storage is Int (indices)
            let utf8_type = fbm::Utf8::create(fbb, &fbm::Utf8Args {});
            (
                fbm::Type::Utf8,
                Some(utf8_type.as_union_value()),
                Some(dict),
            )
        }
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!("unsupported type in schema: {}", field.name),
            ));
        }
    };

    // Create the field with all components
    Ok(fbm::Field::create(
        fbb,
        &fbm::FieldArgs {
            name: Some(fb_name),
            nullable: field.nullable,
            type_type: fb_type_type,
            type_: fb_type_offset,
            dictionary: fb_dict,
            children: Some(children), // Always include children, even if empty
            custom_metadata,
        },
    ))
}

/// Build an Arrow record batch flatbuffers message for the given table.
///
/// # Arguments
/// - `tbl`: Table to encode.
/// - `fields`: Fields describing schema.
/// - `dicts`: Dictionary mapping for categorical columns.
///
/// # Returns
/// FlatBuffer message
///
/// # Errors
/// Returns error if unsupported column type or shape mismatch.
pub(crate) fn build_flatbuf_recordbatch<'a>(
    fbb: &'a mut flatbuffers::FlatBufferBuilder<'static>,
    n_rows: usize,
    fb_field_nodes: &[fbm::FieldNode],
    fb_buffers: &[fbm::Buffer],
    body_len: usize,
) -> io::Result<Vec<u8>> {
    fbb.reset();
    let fb_nodes_vec = fbb.create_vector(fb_field_nodes);
    let fb_buffers_vec = fbb.create_vector(fb_buffers);
    let rb = fbm::RecordBatch::create(
        fbb,
        &fbm::RecordBatchArgs {
            length: n_rows as i64,
            nodes: Some(fb_nodes_vec),
            buffers: Some(fb_buffers_vec),
            compression: None,
            variadicBufferCounts: None,
        },
    );
    let meta = fbm::Message::create(
        fbb,
        &fbm::MessageArgs {
            version: fbm::MetadataVersion::V5,
            header_type: fbm::MessageHeader::RecordBatch,
            header: Some(rb.as_union_value()),
            bodyLength: body_len as i64,
            custom_metadata: None,
        },
    );
    fbb.finish(meta, None);
    Ok(fbb.finished_data().to_vec())
}

/// Build a dictionary batch (metadata and body) for the given dictionary ID and values.
///
/// # Arguments
/// - `dict_id`: Dictionary ID (matches column index).
/// - `uniques`: List of unique values for the dictionary.
///
/// # Returns
/// Tuple: (`FlatBuffer message`, `body buffer`)
///
/// # Errors
/// Returns error if input is inconsistent or unsupported.
pub fn encode_flatbuf_dictionary<'a, B: StreamBuffer>(
    fbb: &'a mut flatbuffers::FlatBufferBuilder<'static>,
    id: i64,
    uniques: &[String],
) -> io::Result<(Vec<u8>, B)> {
    debug_println!("Encoding flatbuffers dictionary.");
    fbb.reset();

    // Build the string data and offsets
    let mut data_buf = Vec::<u8>::new();
    let mut offs = Vec::<u32>::with_capacity(uniques.len() + 1);
    offs.push(0);

    for s in uniques {
        data_buf.extend_from_slice(s.as_bytes());
        offs.push(data_buf.len() as u32);
    }

    // Create the body buffer with proper alignment
    let mut body = B::with_capacity(4 + offs.len() * 4 + data_buf.len());

    // Important: Arrow expects buffers to be aligned, but the actual data layout is:
    // 1. Null buffer (empty for dictionary)
    // 2. Offsets buffer
    // 3. Data buffer

    // The offsets need to be written as raw bytes
    let offs_bytes =
        unsafe { std::slice::from_raw_parts(offs.as_ptr() as *const u8, offs.len() * 4) };

    // Write body: offsets then data
    body.extend_from_slice(offs_bytes);
    body.extend_from_slice(&data_buf);

    // Create field node
    let field_nodes = vec![fbm::FieldNode::new(uniques.len() as i64, 0)];

    // Create buffers metadata
    // Buffer 0: null buffer (empty)
    // Buffer 1: offsets
    // Buffer 2: string data
    let buffers = vec![
        fbm::Buffer::new(0, 0),                       // null buffer
        fbm::Buffer::new(0, offs_bytes.len() as i64), // offsets
        fbm::Buffer::new(offs_bytes.len() as i64, data_buf.len() as i64), // data
    ];

    // Build the FlatBuffer message
    let field_nodes_vec = fbb.create_vector(&field_nodes);
    let buffers_vec = fbb.create_vector(&buffers);

    let rb = fbm::RecordBatch::create(
        fbb,
        &fbm::RecordBatchArgs {
            length: uniques.len() as i64,
            nodes: Some(field_nodes_vec),
            buffers: Some(buffers_vec),
            compression: None,
            variadicBufferCounts: None,
        },
    );

    let dict_batch = fbm::DictionaryBatch::create(
        fbb,
        &fbm::DictionaryBatchArgs {
            id,
            data: Some(rb),
            isDelta: false,
        },
    );

    let msg = fbm::Message::create(
        fbb,
        &fbm::MessageArgs {
            version: fbm::MetadataVersion::V5,
            header_type: fbm::MessageHeader::DictionaryBatch,
            header: Some(dict_batch.as_union_value()),
            bodyLength: body.len() as i64,
            custom_metadata: None,
        },
    );

    fbb.finish(msg, None);
    Ok((fbb.finished_data().to_vec(), body))
}

/// Build a vector of Arrow schema `Field` objects (for the Arrow File footer),
/// using the file-specific FlatBuffers definitions.
///
/// This constructs FlatBuffers offsets for each Arrow field, with each field
/// encoded using the `fbf` (file) schema types.
///
/// # Arguments
/// - `fbb`: The FlatBufferBuilder used to allocate objects.
/// - `schema`: Slice of Arrow [`Field`] definitions (schema).
///
/// # Returns
/// Vector of WIPOffset<fbf::Field> for inclusion in the file footer schema.
///
/// # Errors
/// Returns error for any unsupported Arrow type.
fn build_flatbuf_fields_file<'fbb>(
    fbb: &mut flatbuffers::FlatBufferBuilder<'fbb>,
    schema: &[Field],
) -> io::Result<Vec<flatbuffers::WIPOffset<fbf::Field<'fbb>>>> {
    let mut fb_fields = Vec::with_capacity(schema.len());
    for (col_idx, f) in schema.iter().enumerate() {
        fb_fields.push(build_flatbuf_field_file(fbb, f, col_idx)?);
    }
    Ok(fb_fields)
}

/// Build a single Arrow schema `Field` object (for the Arrow File footer),
/// using file-specific FlatBuffers definitions.
///
/// This encodes all Arrow logical types as a FlatBuffer offset, for use in Arrow File metadata.
///
/// # Arguments
/// - `fbb`: The FlatBufferBuilder used to allocate objects.
/// - `field`: The Arrow [`Field`] schema element.
///
/// # Returns
/// FlatBuffers WIPOffset<fbf::Field> describing the field for the file footer.
///
/// # Errors
/// Returns error for any unsupported Arrow type.
fn build_flatbuf_field_file<'fbb>(
    fbb: &mut flatbuffers::FlatBufferBuilder<'fbb>,
    field: &Field,
    col_idx: usize,
) -> io::Result<flatbuffers::WIPOffset<fbf::Field<'fbb>>> {
    let fb_name = fbb.create_string(&field.name);
    let children = fbb.create_vector::<flatbuffers::WIPOffset<fbf::Field>>(&[]);
    let custom_metadata = None;

    let (fb_type_type, fb_type_offset, fb_dict) = match &field.dtype {
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::Int8 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 8,
                    is_signed: true,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::Int16 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 16,
                    is_signed: true,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::Int32 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 32,
                    is_signed: true,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::Int64 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 64,
                    is_signed: true,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::UInt8 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 8,
                    is_signed: false,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        #[cfg(feature = "extended_numeric_types")]
        ArrowType::UInt16 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 16,
                    is_signed: false,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::UInt32 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 32,
                    is_signed: false,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::UInt64 => {
            let int = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: 64,
                    is_signed: false,
                },
            );
            (fbf::Type::Int, Some(int.as_union_value()), None)
        }
        ArrowType::Float32 => {
            let fp = fbf::FloatingPoint::create(
                fbb,
                &fbf::FloatingPointArgs {
                    precision: fbf::Precision::SINGLE,
                },
            );
            (fbf::Type::FloatingPoint, Some(fp.as_union_value()), None)
        }
        ArrowType::Float64 => {
            let fp = fbf::FloatingPoint::create(
                fbb,
                &fbf::FloatingPointArgs {
                    precision: fbf::Precision::DOUBLE,
                },
            );
            (fbf::Type::FloatingPoint, Some(fp.as_union_value()), None)
        }
        ArrowType::Boolean => {
            let bl = fbf::Bool::create(fbb, &fbf::BoolArgs {});
            (fbf::Type::Bool, Some(bl.as_union_value()), None)
        }
        ArrowType::String => {
            let s = fbf::Utf8::create(fbb, &fbf::Utf8Args {});
            (fbf::Type::Utf8, Some(s.as_union_value()), None)
        }
        #[cfg(feature = "large_string")]
        ArrowType::LargeString => {
            let s = fbf::LargeUtf8::create(fbb, &fbf::LargeUtf8Args {});
            (fbf::Type::Utf8, Some(s.as_union_value()), None)
        }
        #[cfg(feature = "datetime")]
        ArrowType::Date32 => {
            let date = fbf::Date::create(
                fbb,
                &fbf::DateArgs {
                    unit: fbf::DateUnit::DAY,
                },
            );
            (fbf::Type::Date, Some(date.as_union_value()), None)
        }
        #[cfg(feature = "datetime")]
        ArrowType::Date64 => {
            let date = fbf::Date::create(
                fbb,
                &fbf::DateArgs {
                    unit: fbf::DateUnit::MILLISECOND,
                },
            );
            (fbf::Type::Date, Some(date.as_union_value()), None)
        }
        ArrowType::Dictionary(idx_ty) => {
            let idx_width = match idx_ty {
                CategoricalIndexType::UInt32 => 32,
                #[cfg(feature = "extended_categorical")]
                CategoricalIndexType::UInt64 => 64,
                #[cfg(feature = "extended_categorical")]
                CategoricalIndexType::UInt16 => 16,
                #[cfg(feature = "extended_categorical")]
                CategoricalIndexType::UInt8 => 8,
            };

            let index_type = fbf::Int::create(
                fbb,
                &fbf::IntArgs {
                    bitWidth: idx_width,
                    is_signed: false,
                },
            );

            let dict = fbf::DictionaryEncoding::create(
                fbb,
                &fbf::DictionaryEncodingArgs {
                    id: col_idx as i64,
                    indexType: Some(index_type),
                    isOrdered: false,
                    dictionaryKind: fbf::DictionaryKind::DenseArray,
                },
            );

            // File footer uses Utf8 type for dictionary fields
            let utf8_type = fbf::Utf8::create(fbb, &fbf::Utf8Args {});
            (
                fbf::Type::Utf8,
                Some(utf8_type.as_union_value()),
                Some(dict),
            )
        }
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!("unsupported type in schema: {}", field.name),
            ));
        }
    };

    Ok(fbf::Field::create(
        fbb,
        &fbf::FieldArgs {
            name: Some(fb_name),
            nullable: field.nullable,
            type_type: fb_type_type,
            type_: fb_type_offset,
            dictionary: fb_dict,
            children: Some(children),
            custom_metadata,
        },
    ))
}

/// Arrow IPC block meta for file footer
#[derive(Debug, Clone)]
pub struct FooterBlockMeta {
    pub offset: u64,
    pub metadata_len: u32,
    pub body_len: u64,
}

/// Build the Arrow file footer FlatBuffer message, including batch and dictionary block metadata.
///
/// # Arguments
/// - `fields`: Table schema.
/// - `dict_blocks`: Dictionary block metadata.
/// - `batch_blocks`: Record batch block metadata.
///
/// # Returns
/// FlatBuffer-encoded Arrow file footer.
///
/// # Errors
/// Returns error for any unsupported Arrow type.
pub fn build_flatbuf_footer<'a>(
    fbb: &'a mut flatbuffers::FlatBufferBuilder<'static>,
    schema: &[Field],
    blocks_dictionaries: &[FooterBlockMeta],
    blocks_record_batches: &[FooterBlockMeta],
) -> io::Result<Vec<u8>> {
    fbb.reset();
    let fb_dict_blocks: Vec<_> = blocks_dictionaries
        .iter()
        .map(|b| fbf::Block::new(b.offset as i64, b.metadata_len as i32, b.body_len as i64))
        .collect();
    let fb_batch_blocks: Vec<_> = blocks_record_batches
        .iter()
        .map(|b| fbf::Block::new(b.offset as i64, b.metadata_len as i32, b.body_len as i64))
        .collect();
    let dict_vec = fbb.create_vector(&fb_dict_blocks);
    let batch_vec = fbb.create_vector(&fb_batch_blocks);
    let fb_fields = build_flatbuf_fields_file(fbb, schema)?;
    let fields_vec = fbb.create_vector(&fb_fields);
    let schema_obj = fbf::Schema::create(
        fbb,
        &fbf::SchemaArgs {
            endianness: fbf::Endianness::Little,
            fields: Some(fields_vec),
            custom_metadata: None,
            features: None,
        },
    );
    let footer = fbf::Footer::create(
        fbb,
        &fbf::FooterArgs {
            version: fbf::MetadataVersion::V5,
            schema: Some(schema_obj),
            dictionaries: Some(dict_vec),
            recordBatches: Some(batch_vec),
            custom_metadata: None,
        },
    );
    fbb.finish(footer, None);
    Ok(fbb.finished_data().to_vec())
}