sqlload 0.11.1

SQL data loading tooling. Pre-release, under development.
//! A yielded Arrow batch in the types the loader carries.

use std::sync::Arc;

use arrow_array::{ArrayRef, RecordBatch, RecordBatchOptions};
use arrow_schema::{Field, Schema};

use crate::row_batches::_helpers::arrow_json::normalise_column;

/// `batch` with 64-bit ints and floats, UTC microsecond timestamps, plain strings and binary, and nested columns as `json` text; other columns are kept as they are (zero copy), and so is the schema's metadata.
pub fn normalise_batch(batch: &RecordBatch) -> Result<RecordBatch, String> {
    let schema = batch.schema();
    let (fields, arrays): (Vec<Field>, Vec<ArrayRef>) = schema
        .fields()
        .iter()
        .zip(batch.columns())
        .map(|(field, array)| normalise_column(field.name(), array))
        .collect::<Result<Vec<_>, String>>()?
        .into_iter()
        .unzip();
    let options = RecordBatchOptions::new().with_row_count(Some(batch.num_rows()));
    let schema = Schema::new(fields).with_metadata(schema.metadata().clone());
    RecordBatch::try_new_with_options(Arc::new(schema), arrays, &options)
        .map_err(|error| error.to_string())
}