Skip to main content

copybook_arrow/
ipc.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2//! Arrow IPC (Feather v2) file writer
3
4use arrow::array::RecordBatch;
5use arrow::datatypes::Schema as ArrowSchema;
6use arrow::ipc::writer::FileWriter;
7use std::fs::File;
8use std::io::BufWriter;
9use std::path::Path;
10use std::sync::Arc;
11
12use crate::{ArrowError, Result};
13
14/// Write Arrow `RecordBatch` objects to an Arrow IPC file.
15///
16/// # Errors
17///
18/// Returns an error if file creation or writing fails.
19#[inline]
20pub fn write_ipc<P: AsRef<Path>>(
21    path: P,
22    batches: &[RecordBatch],
23    schema: &ArrowSchema,
24) -> Result<()> {
25    let file = File::create(path)?;
26    let buf_writer = BufWriter::new(file);
27
28    let mut writer = FileWriter::try_new(buf_writer, &Arc::new(schema.clone()))
29        .map_err(|e| ArrowError::IpcWrite(format!("Failed to create IPC writer: {e}")))?;
30
31    for batch in batches {
32        writer
33            .write(batch)
34            .map_err(|e| ArrowError::IpcWrite(format!("Failed to write batch: {e}")))?;
35    }
36
37    writer
38        .finish()
39        .map_err(|e| ArrowError::IpcWrite(format!("Failed to finish IPC writer: {e}")))?;
40
41    Ok(())
42}