2021-12-21 19:32:09 +01:00
|
|
|
use std::{fs::File, path::PathBuf};
|
|
|
|
|
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
|
|
|
engine::{Command, EngineState, Stack},
|
2022-06-12 21:18:00 +02:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
2021-12-21 19:32:09 +01:00
|
|
|
};
|
|
|
|
use polars::prelude::ParquetWriter;
|
|
|
|
|
|
|
|
use super::super::values::NuDataFrame;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ToParquet;
|
|
|
|
|
|
|
|
impl Command for ToParquet {
|
|
|
|
fn name(&self) -> &str {
|
2022-06-12 21:18:00 +02:00
|
|
|
"to-parquet"
|
2021-12-21 19:32:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Saves dataframe to parquet file"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name())
|
|
|
|
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
|
|
|
.category(Category::Custom("dataframe".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2022-06-12 21:18:00 +02:00
|
|
|
description: "Saves dataframe to parquet file",
|
|
|
|
example: "[[a b]; [1 2] [3 4]] | to-df | to-parquet test.parquet",
|
2021-12-21 19:32:09 +01:00
|
|
|
result: None,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:18:00 +02:00
|
|
|
fn input_type(&self) -> Type {
|
|
|
|
Type::Custom("dataframe".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output_type(&self) -> Type {
|
|
|
|
Type::Any
|
|
|
|
}
|
|
|
|
|
2021-12-21 19:32:09 +01:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
command(engine_state, stack, call, input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn command(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let file_name: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
|
|
|
|
2022-03-19 12:13:34 +01:00
|
|
|
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
2021-12-21 19:32:09 +01:00
|
|
|
|
|
|
|
let file = File::create(&file_name.item).map_err(|e| {
|
2022-04-18 14:34:10 +02:00
|
|
|
ShellError::GenericError(
|
2021-12-21 19:32:09 +01:00
|
|
|
"Error with file name".into(),
|
|
|
|
e.to_string(),
|
2022-04-18 14:34:10 +02:00
|
|
|
Some(file_name.span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
2021-12-21 19:32:09 +01:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
2022-03-19 12:13:34 +01:00
|
|
|
ParquetWriter::new(file).finish(df.as_mut()).map_err(|e| {
|
2022-04-18 14:34:10 +02:00
|
|
|
ShellError::GenericError(
|
|
|
|
"Error saving file".into(),
|
|
|
|
e.to_string(),
|
|
|
|
Some(file_name.span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
)
|
2021-12-21 19:32:09 +01:00
|
|
|
})?;
|
|
|
|
|
|
|
|
let file_value = Value::String {
|
|
|
|
val: format!("saved {:?}", &file_name.item),
|
|
|
|
span: file_name.span,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(PipelineData::Value(
|
|
|
|
Value::List {
|
|
|
|
vals: vec![file_value],
|
|
|
|
span: call.head,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
}
|