use datafusion::prelude::{CsvReadOptions, JsonReadOptions, ParquetReadOptions, SessionContext};
use crate::convert::batches_to_rows;
use crate::row::Row;
async fn collect(df: datafusion::dataframe::DataFrame) -> Result<Vec<Row>, String> {
let batches = df.collect().await.map_err(|e| e.to_string())?;
batches_to_rows(&batches)
}
pub async fn read_csv(path: &str, has_header: bool) -> Result<Vec<Row>, String> {
let ctx = SessionContext::new();
let df = ctx
.read_csv(path, CsvReadOptions::new().has_header(has_header))
.await
.map_err(|e| e.to_string())?;
collect(df).await
}
pub async fn read_ndjson(path: &str) -> Result<Vec<Row>, String> {
let ctx = SessionContext::new();
let df = ctx
.read_json(path, JsonReadOptions::default())
.await
.map_err(|e| e.to_string())?;
collect(df).await
}
pub async fn read_parquet(path: &str) -> Result<Vec<Row>, String> {
let ctx = SessionContext::new();
let df = ctx
.read_parquet(path, ParquetReadOptions::default())
.await
.map_err(|e| e.to_string())?;
collect(df).await
}