lightstream 0.5.0

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
// Copyright Peter G. Bower 2025-2026.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! # Arrow IPC Table Stream Encoder
//!
//! Serialises `Table` values into Arrow IPC metadata and body pairs.
//! Handles schema, dictionary, and record batch encoding with state
//! tracking so schema and dictionaries are emitted once.
//!
//! The encoder produces `(meta, body)` pairs. Writing, framing, queuing,
//! and protocol-level concerns like EOS markers and file footers are
//! handled by the writer layer in `writers/ipc/`.

use std::collections::{HashMap, HashSet};
use std::io;

use minarrow::{ArrowType, Field, TableV};

use crate::arrow::message::org::apache::arrow::flatbuf as fbm;
use crate::compression::{Compression, compress};
use crate::constants::DEFAULT_FRAME_ALLOCATION_SIZE;
use crate::enums::{IPCMessageProtocol, WriterState};
use crate::models::encoders::ipc::schema::{
    build_flatbuf_recordbatch, build_flatbuf_schema, encode_flatbuf_dictionary,
};
use crate::traits::stream_buffer::StreamBuffer;
use crate::utils::align_to;

/// Arrow IPC table encoder.
///
/// Serialises schema, dictionaries, and record batches as `(meta, body)`
/// pairs. Tracks encoding state so schema and dictionaries are emitted
/// once. Does not handle framing, queuing, or I/O - those responsibilities
/// belong to the writer layer.
pub struct TableStreamEncoder<B: StreamBuffer + 'static> {
    /// Arrow IPC protocol - affects alignment calculations
    pub protocol: IPCMessageProtocol,
    /// Current encoding state
    pub state: WriterState,
    /// Compression codec for record batch bodies. `None` writes uncompressed.
    pub compression: Option<Compression>,
    /// Arrow schema for this stream
    pub schema: Vec<Field>,
    /// Set of dictionary IDs already encoded
    pub written_dict_ids: HashSet<i64>,
    /// Registered dictionaries for categorical columns
    pub dictionaries: HashMap<i64, Vec<String>>,
    /// FlatBuffer builder instance for serialisation
    pub fbb: flatbuffers::FlatBufferBuilder<'static>,
    /// B controls wire alignment for frame boundary calculations
    _alignment: std::marker::PhantomData<B>,
}

impl<B: StreamBuffer> TableStreamEncoder<B> {
    /// Create a new encoder. `None` for `compression` writes uncompressed
    /// batches; `Some(codec)` compresses every record-batch body.
    pub fn new(
        schema: Vec<Field>,
        protocol: IPCMessageProtocol,
        compression: Option<Compression>,
    ) -> Self {
        Self {
            protocol,
            state: WriterState::Fresh,
            compression,
            schema,
            written_dict_ids: HashSet::new(),
            dictionaries: HashMap::new(),
            fbb: flatbuffers::FlatBufferBuilder::with_capacity(4096),
            _alignment: std::marker::PhantomData,
        }
    }

    /// Register a dictionary for a categorical column.
    pub fn register_dictionary(&mut self, id: i64, uniques: Vec<String>) {
        self.dictionaries.insert(id, uniques);
    }

    /// Encode the schema as a flatbuffer metadata blob.
    /// Updates state to SchemaDone.
    pub fn encode_schema(&mut self) -> io::Result<Vec<u8>> {
        let meta = build_flatbuf_schema(&mut self.fbb, &self.schema)?;
        self.state = WriterState::SchemaDone;
        Ok(meta)
    }

    /// Encode a dictionary batch, returning `(meta, body)`.
    /// Returns `Ok(None)` if this dictionary was already encoded.
    pub fn encode_dictionary(&mut self, id: i64) -> io::Result<Option<(Vec<u8>, Vec<u8>)>> {
        if self.written_dict_ids.contains(&id) {
            return Ok(None);
        }
        let uniques = self.dictionaries.get(&id).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidInput,
                format!("dictionary id {id} not registered"),
            )
        })?;
        let (meta, body) = encode_flatbuf_dictionary(&mut self.fbb, id, uniques)?;
        self.written_dict_ids.insert(id);
        Ok(Some((meta, body)))
    }

    /// Collect dictionary IDs from the schema that need encoding for categorical columns.
    pub fn pending_dict_ids(&self) -> Vec<i64> {
        self.schema
            .iter()
            .enumerate()
            .filter_map(|(col_idx, field)| {
                if let ArrowType::Dictionary(_) = field.dtype {
                    Some(col_idx as i64)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Serialise a table view as an Arrow IPC record batch, returning (metadata, body) buffers.
    ///
    /// Uses `compute_body_layout` to collect column data as zero-copy slices,
    /// then writes them into a single body buffer.
    pub fn encode_record_batch(&mut self, view: &TableV) -> io::Result<(Vec<u8>, B)> {
        use crate::models::encoders::ipc::record_batch::compute_body_layout;

        let layout = compute_body_layout::<B>(view)?;

        // When compression is active, compress each buffer with a u64 LE
        // uncompressed length prefix and rebuild the buffer metadata against
        // the compressed layout, per the Arrow IPC BodyCompression spec.
        let (body, fb_buffers, body_size) = if let Some(codec) = self.compression {
            let mut compressed: Vec<Vec<u8>> = Vec::with_capacity(layout.regions.len());
            for region in &layout.regions {
                if region.data.is_empty() {
                    compressed.push(Vec::new());
                } else {
                    let raw = region.data.compression_bytes();
                    let c = compress(&raw, codec)
                        .map_err(|e| io::Error::other(format!("{}", e)))?;
                    let mut wire = Vec::with_capacity(8 + c.len());
                    wire.extend_from_slice(&(raw.len() as u64).to_le_bytes());
                    wire.extend_from_slice(&c);
                    compressed.push(wire);
                }
            }
            let mut fb_buffers = Vec::with_capacity(compressed.len());
            let mut body = B::with_capacity(DEFAULT_FRAME_ALLOCATION_SIZE);
            let mut offset = 0usize;
            for c in &compressed {
                fb_buffers.push(fbm::Buffer::new(offset as i64, c.len() as i64));
                body.extend_from_slice(c);
                let pad = align_to::<B>(c.len());
                if pad > 0 {
                    body.extend_from_slice(&[0u8; 64][..pad]);
                }
                offset += c.len() + pad;
            }
            (body, fb_buffers, offset)
        } else {
            let mut body = B::with_capacity(layout.body_size.max(DEFAULT_FRAME_ALLOCATION_SIZE));
            for region in &layout.regions {
                region.data.write_into(&mut body);
                if region.pad > 0 {
                    body.extend_from_slice(&[0u8; 64][..region.pad]);
                }
            }
            (body, layout.fb_buffers, layout.body_size)
        };

        let fb_compression = match self.compression {
            Some(c) => Some(c.to_arrow_ipc_type()?),
            None => None,
        };
        let meta = build_flatbuf_recordbatch(
            &mut self.fbb,
            view.len,
            &layout.fb_field_nodes,
            &fb_buffers,
            body_size,
            fb_compression,
            None,
        )?;
        Ok((meta, body))
    }
}

/// The below are 'logical' unit tests confirming:
/// - Unique dictionary IDs are respected.
/// - The macro populates buffers and field nodes as intended.
/// - Null handling works as expected for all supported index types.
///
/// For full roundtrip IO on the Stream reader and writer, see "../tests".
#[cfg(test)]
mod tests {
    use std::fs::File as StdFile;
    use std::io::{Read, Write};
    use std::sync::Arc;

    use minarrow::ffi::arrow_dtype::CategoricalIndexType;
    use minarrow::{
        Array, Bitmask, Buffer, CategoricalArray, Field, FieldArray, IntegerArray, NumericArray,
        Table, TextArray, Vec64,
    };
    use tempfile::NamedTempFile;

    use crate::constants::{ARROW_MAGIC_NUMBER, ARROW_MAGIC_NUMBER_PADDED};

    use super::*;
    use crate::models::writers::ipc::table_stream::TableStreamWriter;

    fn make_bitmask(valid: &[bool]) -> Bitmask {
        let mut bits = vec![0u8; (valid.len() + 7) / 8];
        for (i, v) in valid.iter().enumerate() {
            if *v {
                bits[i / 8] |= 1 << (i % 8);
            }
        }
        Bitmask::new(Buffer::from(Vec64::from_slice(&bits[..])), valid.len())
    }

    fn dict_strs() -> Vec<String> {
        vec![
            "apple".to_string(),
            "banana".to_string(),
            "pear".to_string(),
        ]
    }

    fn make_schema(idx_ty: CategoricalIndexType, nullable: bool) -> Vec<Field> {
        vec![Field {
            name: "col".to_string(),
            dtype: ArrowType::Dictionary(idx_ty),
            nullable,
            metadata: Default::default(),
        }]
    }

    fn make_table(arr: FieldArray, n_rows: usize) -> Table {
        Table {
            cols: vec![arr],
            n_rows,
            name: "tbl".to_string(),
            ..Default::default()
        }
    }

    fn read_file_bytes(path: &std::path::Path) -> Vec<u8> {
        let mut f = StdFile::open(path).expect("file open");
        let mut buf = Vec::new();
        f.read_to_end(&mut buf).expect("file read");
        buf
    }

    fn check_ipc_padding(buf: &[u8]) {
        // Checks: Flatbuffers message header (8 bytes) + meta + 64-byte padding + data
        // Only a minimal check here - you can parse the header and verify offset alignment
        // but at minimum ensure buffer length is a multiple of 8 and/or 64 as Arrow requires.
        assert_eq!(buf.len() % 8, 0, "Arrow IPC frame should be 8-byte aligned");
    }

    #[cfg(not(feature = "default_categorical_8"))]
    #[test]
    fn test_write_categorical_column_u32_to_file() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        let mut writer: TableStreamWriter = TableStreamWriter::new(
            make_schema(CategoricalIndexType::UInt32, true),
            IPCMessageProtocol::Stream,
            None,
        );

        let arr = CategoricalArray {
            data: Buffer::from(Vec64::from_slice(&[1u32, 0, 2, 1])),
            unique_values: Vec64::from(dict_strs()),
            null_mask: Some(make_bitmask(&[true, false, true, true])),
        };

        writer.register_dictionary(0, dict_strs());

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Dictionary(CategoricalIndexType::UInt32),
                    nullable: true,
                    metadata: Default::default(),
                },
                Array::TextArray(TextArray::Categorical32(Arc::new(arr))),
            ),
            4,
        );

        writer.write(&tbl.clone().into()).unwrap();
        writer.finish().unwrap();

        let mut file = StdFile::create(&path).unwrap();
        for frame in writer.drain_all_frames() {
            use std::io::Write;
            file.write_all(&frame).unwrap();
        }
        file.flush().unwrap();
        drop(file);

        let buf = read_file_bytes(&path);
        assert!(!buf.is_empty());
        check_ipc_padding(&buf);
    }

    #[cfg(feature = "default_categorical_8")]
    #[test]
    fn test_write_categorical_column_u8_to_file() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        let mut writer = TableStreamWriter::<Vec<u8>>::new(
            make_schema(CategoricalIndexType::UInt8, true),
            IPCMessageProtocol::Stream,
            None,
        );

        let arr = CategoricalArray {
            data: Buffer::from(Vec64::from_slice(&[1u8, 0, 2, 1])),
            unique_values: Vec64::from(dict_strs()),
            null_mask: Some(make_bitmask(&[true, false, true, true])),
        };

        writer.register_dictionary(0, dict_strs());

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Dictionary(CategoricalIndexType::UInt8),
                    nullable: true,
                    metadata: Default::default(),
                },
                Array::TextArray(TextArray::Categorical8(Arc::new(arr))),
            ),
            4,
        );

        writer.write(&tbl.clone().into()).unwrap();
        writer.finish().unwrap();

        let mut file = StdFile::create(&path).unwrap();
        for frame in writer.drain_all_frames() {
            use std::io::Write;
            file.write_all(&frame).unwrap();
        }
        file.flush().unwrap();
        drop(file);

        let buf = read_file_bytes(&path);
        assert!(!buf.is_empty());
        check_ipc_padding(&buf);
    }

    #[cfg(feature = "extended_categorical")]
    #[test]
    fn test_write_categorical_column_u8() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        let mut writer: TableStreamWriter = TableStreamWriter::new(
            make_schema(CategoricalIndexType::UInt8, true),
            IPCMessageProtocol::Stream,
            None,
        );

        let arr = CategoricalArray {
            data: Buffer::from(Vec64::from_slice(&[1u8, 0, 2, 1])),
            unique_values: Vec64::from(dict_strs()),
            null_mask: Some(make_bitmask(&[true, true, false, true])),
        };

        writer.register_dictionary(0, dict_strs());

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Dictionary(CategoricalIndexType::UInt8),
                    nullable: true,
                    metadata: Default::default(),
                },
                Array::TextArray(TextArray::Categorical8(Arc::new(arr))),
            ),
            4,
        );

        writer.write(&tbl.into()).unwrap();
        writer.finish().unwrap();

        let mut buf = Vec::new();
        for frame in writer.drain_all_frames() {
            buf.extend_from_slice(&frame);
        }

        assert!(!buf.is_empty());
        check_ipc_padding(&buf);
    }

    #[cfg(feature = "extended_categorical")]
    #[test]
    fn test_write_categorical_column_u16() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        let mut writer: TableStreamWriter = TableStreamWriter::new(
            make_schema(CategoricalIndexType::UInt16, false),
            IPCMessageProtocol::Stream,
            None,
        );

        let arr = CategoricalArray {
            data: Buffer::from(Vec64::from_slice(&[2u16, 1, 0, 2])),
            unique_values: Vec64::from(dict_strs()),
            null_mask: None,
        };

        writer.register_dictionary(0, dict_strs());

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Dictionary(CategoricalIndexType::UInt16),
                    nullable: false,
                    metadata: Default::default(),
                },
                Array::TextArray(TextArray::Categorical16(Arc::new(arr))),
            ),
            4,
        );

        writer.write(&tbl.into()).unwrap();
        writer.finish().unwrap();

        let mut buf = Vec::new();
        for frame in writer.drain_all_frames() {
            buf.extend_from_slice(&frame);
        }

        assert!(!buf.is_empty());
        check_ipc_padding(&buf);
    }

    #[cfg(feature = "extended_categorical")]
    #[test]
    fn test_write_categorical_column_u64() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        let mut writer: TableStreamWriter = TableStreamWriter::new(
            make_schema(CategoricalIndexType::UInt64, false),
            IPCMessageProtocol::Stream,
            None,
        );

        let arr = CategoricalArray {
            data: Buffer::from(Vec64::from_slice(&[0u64, 2, 1, 0])),
            unique_values: Vec64::from(dict_strs()),
            null_mask: None,
        };

        writer.register_dictionary(0, dict_strs());

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Dictionary(CategoricalIndexType::UInt64),
                    nullable: false,
                    metadata: Default::default(),
                },
                Array::TextArray(TextArray::Categorical64(Arc::new(arr))),
            ),
            4,
        );

        writer.write(&tbl.into()).unwrap();
        writer.finish().unwrap();

        let mut buf = Vec::new();
        for frame in writer.drain_all_frames() {
            buf.extend_from_slice(&frame);
        }

        assert!(!buf.is_empty());
        check_ipc_padding(&buf);
    }

    #[cfg(not(feature = "default_categorical_8"))]
    #[test]
    fn test_ipc_file_write_read_dict() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        let mut writer: TableStreamWriter = TableStreamWriter::new(
            make_schema(CategoricalIndexType::UInt32, true),
            IPCMessageProtocol::File,
            None,
        );

        let arr = CategoricalArray {
            data: Buffer::from(Vec64::from_slice(&[0u32, 1, 1, 2])),
            unique_values: Vec64::from(dict_strs()),
            null_mask: Some(make_bitmask(&[true, false, true, true])),
        };

        writer.register_dictionary(0, dict_strs());

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Dictionary(CategoricalIndexType::UInt32),
                    nullable: true,
                    metadata: Default::default(),
                },
                Array::TextArray(TextArray::Categorical32(Arc::new(arr))),
            ),
            4,
        );

        writer.write(&tbl.clone().into()).unwrap();
        writer.finish().unwrap();

        // Write to temp file
        let mut file = StdFile::create(&path).unwrap();
        for frame in writer.drain_all_frames() {
            use std::io::Write;
            file.write_all(&frame).unwrap();
        }
        file.flush().unwrap();
        drop(file);

        let buf = read_file_bytes(&path);
        println!("Written buffer:\n{:?}", buf);
        assert!(
            buf.starts_with(ARROW_MAGIC_NUMBER_PADDED),
            "file must start with Arrow magic"
        );
        assert!(
            buf.ends_with(ARROW_MAGIC_NUMBER),
            "file must end with Arrow magic"
        );
        println!("Written buffer len : {:?}", buf.len());
        // We add 2 for the end magic marker, and the 3rd 4-byte contination marker.
        // This is more of a sanity check than a starting alignment check.
        assert!(
            (buf.len() + 4 + 2) % 8 == 0,
            "Arrow IPC file must be a multiple of 8 bytes"
        );
    }

    #[cfg(feature = "default_categorical_8")]
    #[test]
    fn test_ipc_file_write_read_dict() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        let mut writer = TableStreamWriter::<Vec<u8>>::new(
            make_schema(CategoricalIndexType::UInt8, true),
            IPCMessageProtocol::File,
            None,
        );

        let arr = CategoricalArray {
            data: Buffer::from(Vec64::from_slice(&[0u8, 1, 1, 2])),
            unique_values: Vec64::from(dict_strs()),
            null_mask: Some(make_bitmask(&[true, false, true, true])),
        };

        writer.register_dictionary(0, dict_strs());

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Dictionary(CategoricalIndexType::UInt8),
                    nullable: true,
                    metadata: Default::default(),
                },
                Array::TextArray(TextArray::Categorical8(Arc::new(arr))),
            ),
            4,
        );

        writer.write(&tbl.clone().into()).unwrap();
        writer.finish().unwrap();

        let mut file = StdFile::create(&path).unwrap();
        for frame in writer.drain_all_frames() {
            use std::io::Write;
            file.write_all(&frame).unwrap();
        }
        file.flush().unwrap();
        drop(file);

        let buf = read_file_bytes(&path);
        assert!(
            buf.starts_with(ARROW_MAGIC_NUMBER_PADDED),
            "file must start with Arrow magic"
        );
        assert!(
            buf.ends_with(ARROW_MAGIC_NUMBER),
            "file must end with Arrow magic"
        );
        assert!(
            (buf.len() + 4 + 2) % 8 == 0,
            "Arrow IPC file must be a multiple of 8 bytes"
        );
    }

    #[test]
    fn test_ipc_file_write_read_std() {
        let temp = NamedTempFile::new().unwrap();
        let path = temp.path().to_path_buf();

        // Create schema with a single Int32 (non-dictionary) column
        let schema = vec![Field {
            name: "col".to_string(),
            dtype: ArrowType::Int32,
            nullable: true,
            metadata: Default::default(),
        }];

        let mut writer =
            TableStreamWriter::<Vec64<u8>>::new(schema.clone(), IPCMessageProtocol::File, None);

        // Create a simple Int32 array with null mask
        let data = vec![10i32, 20, 30, 40];
        let mask = make_bitmask(&[true, false, true, true]);
        let arr = NumericArray::Int32(Arc::new(IntegerArray {
            data: Buffer::from(Vec64::from_slice(&data)),
            null_mask: Some(mask),
        }));

        let tbl = make_table(
            FieldArray::new(
                Field {
                    name: "col".to_string(),
                    dtype: ArrowType::Int32,
                    nullable: true,
                    metadata: Default::default(),
                },
                Array::NumericArray(arr),
            ),
            4,
        );

        writer.write(&tbl.clone().into()).unwrap();
        writer.finish().unwrap();

        // Write to temp file
        let mut file = StdFile::create(&path).unwrap();
        for frame in writer.drain_all_frames() {
            use std::io::Write;
            file.write_all(&frame).unwrap();
        }
        file.flush().unwrap();
        drop(file);

        let buf = read_file_bytes(&path);
        println!("Written buffer:\n{:?}", buf);
        assert!(
            buf.starts_with(ARROW_MAGIC_NUMBER_PADDED),
            "file must start with Arrow magic"
        );
        assert!(
            buf.ends_with(ARROW_MAGIC_NUMBER),
            "file must end with Arrow magic"
        );
    }
}