fv-compute-container 0.2.0

The container backend for fv-compute: a transform is a process or image that reads Arrow IPC (or row JSON) on stdin and writes it on stdout — for heavy, GPU or arbitrary-dependency transforms.
Documentation
//! Run a container transform (a Python script speaking the `json` protocol).
//!
//! ```bash
//! cargo run -p fv-compute-container --example quickstart
//! ```
//!
//! Loads `examples/transforms/riskBand` and runs one Arrow batch through it. Needs `python3` on
//! the PATH; the script itself has no dependencies.

use arrow::array::{Float64Array, Int64Array, RecordBatch};
use arrow::datatypes::{DataType, Field, Schema};
use fv_compute::Runtime;
use fv_compute_container::ContainerBackend;
use std::path::Path;
use std::sync::Arc;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let transforms = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../examples/transforms");
    let runtime = Runtime::builder()
        .root(transforms)
        .backend(ContainerBackend::new())
        .build()?;

    let schema = Arc::new(Schema::new(vec![
        Field::new("id", DataType::Int64, true),
        Field::new("score", DataType::Float64, true),
    ]));
    let input = RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(vec![1, 2, 3])),
            Arc::new(Float64Array::from(vec![0.1, 0.5, 0.9])),
        ],
    )?;

    // Each run spawns the process, streams the batch in, and reads the result back.
    let output = runtime.run("riskBand@1.0.0", &[input])?;

    print_batch(&output)?;
    Ok(())
}

/// Print a batch as aligned text without pulling in arrow's `prettyprint` feature (whose table
/// crate needs a newer Rust than this crate's minimum).
fn print_batch(batch: &RecordBatch) -> Result<(), Box<dyn std::error::Error>> {
    use arrow::util::display::{ArrayFormatter, FormatOptions};
    let options = FormatOptions::default();
    let formatters: Vec<ArrayFormatter> = batch
        .columns()
        .iter()
        .map(|c| ArrayFormatter::try_new(c.as_ref(), &options))
        .collect::<Result<_, _>>()?;
    let names: Vec<String> = batch.schema().fields().iter().map(|f| f.name().clone()).collect();
    println!("{}", names.join("\t"));
    for row in 0..batch.num_rows() {
        let cells: Vec<String> = formatters.iter().map(|f| f.value(row).to_string()).collect();
        println!("{}", cells.join("\t"));
    }
    Ok(())
}