use std::fs::File;
use std::path::Path;
use anyhow::{Context, Result};
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use parquet::arrow::arrow_writer::ArrowWriter;
use parquet::basic::{Compression, ZstdLevel};
use parquet::file::properties::WriterProperties;
use crate::output::Table;
use crate::value::cell;
pub fn head(path: &Path, rows: usize) -> Result<Table> {
let file = File::open(path).with_context(|| format!("open {}", path.display()))?;
let builder = ParquetRecordBatchReaderBuilder::try_new(file)
.with_context(|| format!("read the footer of {}", path.display()))?;
let columns: Vec<String> = builder
.schema()
.fields()
.iter()
.map(|field| field.name().clone())
.collect();
let reader = builder.with_batch_size(rows.max(1)).build()?;
let mut table = Table::new(columns);
for batch in reader {
let batch = batch?;
for row in 0..batch.num_rows() {
if table.rows.len() >= rows {
return Ok(table);
}
table.push(
batch
.columns()
.iter()
.map(|column| cell(column, row))
.collect(),
);
}
}
Ok(table)
}
pub fn cat(paths: &[std::path::PathBuf]) -> Result<Table> {
let mut table = Table::default();
for path in paths {
let file = File::open(path).with_context(|| format!("open {}", path.display()))?;
let builder = ParquetRecordBatchReaderBuilder::try_new(file)
.with_context(|| format!("read the footer of {}", path.display()))?;
let names: Vec<String> = builder
.schema()
.fields()
.iter()
.map(|field| field.name().clone())
.collect();
if table.columns.is_empty() {
table.columns = names;
}
for batch in builder.build()? {
let batch = batch?;
for row in 0..batch.num_rows() {
table.push(
batch
.columns()
.iter()
.map(|column| cell(column, row))
.collect(),
);
}
}
}
Ok(table)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum ConvertFormat {
Csv,
Json,
Jsonl,
Parquet,
}
impl std::fmt::Display for ConvertFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
ConvertFormat::Csv => "csv",
ConvertFormat::Json => "json",
ConvertFormat::Jsonl => "jsonl",
ConvertFormat::Parquet => "parquet",
};
f.write_str(name)
}
}
pub fn convert(path: &Path, out: &Path, format: ConvertFormat) -> Result<()> {
let file = File::open(path).with_context(|| format!("open {}", path.display()))?;
let builder = ParquetRecordBatchReaderBuilder::try_new(file)
.with_context(|| format!("read the footer of {}", path.display()))?;
match format {
ConvertFormat::Parquet => {
let properties = WriterProperties::builder()
.set_compression(Compression::ZSTD(ZstdLevel::try_new(3)?))
.build();
let mut writer = ArrowWriter::try_new(
File::create(out).with_context(|| format!("create {}", out.display()))?,
builder.schema().clone(),
Some(properties),
)?;
for batch in builder.build()? {
writer.write(&batch?)?;
}
writer.close()?;
}
ConvertFormat::Csv | ConvertFormat::Json | ConvertFormat::Jsonl => {
let table = cat(&[path.to_path_buf()])?;
let mut handle =
File::create(out).with_context(|| format!("create {}", out.display()))?;
let format = match format {
ConvertFormat::Csv => crate::output::Format::Csv,
ConvertFormat::Json => crate::output::Format::Json,
_ => crate::output::Format::Jsonl,
};
crate::output::write_table(&mut handle, &table, format)?;
}
}
Ok(())
}