diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/aggregate.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/aggregate.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/aggregate.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/aggregate.rs diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/expressions_macro.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/aggregation_commands.rs similarity index 56% rename from crates/nu_plugin_polars/src/dataframe/expressions/expressions_macro.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/aggregation_commands.rs index 2a18c31c4d..4f2a4875da 100644 --- a/crates/nu_plugin_polars/src/dataframe/expressions/expressions_macro.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/aggregation/aggregation_commands.rs @@ -1,263 +1,10 @@ -/// Definition of multiple Expression commands using a macro rule -/// All of these expressions have an identical body and only require -/// to have a change in the name, description and expression function use crate::dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame}; use crate::values::CustomValueSupport; use crate::PolarsPlugin; +use crate::{expr_command, lazy_expr_command}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Span, Type, Value}; -// The structs defined in this file are structs that form part of other commands -// since they share a similar name -macro_rules! expr_command { - ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident) => { - #[derive(Clone)] - pub struct $command; - - impl PluginCommand for $command { - type Plugin = PolarsPlugin; - - fn name(&self) -> &str { - $name - } - - fn description(&self) -> &str { - $desc - } - - fn signature(&self) -> Signature { - Signature::build(self.name()) - .description($desc) - .input_output_type( - Type::Custom("expression".into()), - Type::Custom("expression".into()), - ) - .category(Category::Custom("expression".into())) - } - - fn examples(&self) -> Vec { - $examples - } - - fn run( - &self, - plugin: &Self::Plugin, - engine: &EngineInterface, - call: &EvaluatedCall, - input: PipelineData, - ) -> Result { - let expr = NuExpression::try_from_pipeline(plugin, input, call.head) - .map_err(LabeledError::from)?; - let expr: NuExpression = expr.into_polars().$func().into(); - expr.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } - } - - #[cfg(test)] - mod $test { - use super::*; - use crate::test::test_polars_plugin_command; - use nu_protocol::ShellError; - - #[test] - fn test_examples() -> Result<(), ShellError> { - test_polars_plugin_command(&$command) - } - } - }; - - ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident, $ddof: expr) => { - #[derive(Clone)] - pub struct $command; - - impl PluginCommand for $command { - type Plugin = PolarsPlugin; - - fn signature(&self) -> Signature { - Signature::build(self.name()) - .description($desc) - .input_output_type( - Type::Custom("expression".into()), - Type::Custom("expression".into()), - ) - .category(Category::Custom("expression".into())) - .plugin_examples($examples) - } - - fn run( - &self, - _plugin: &Self::Plugin, - engine: &EngineInterface, - call: &EvaluatedCall, - input: PipelineData, - ) -> Result { - let expr = NuExpression::try_from_pipeline(input, call.head) - .map_err(LabeledError::from)?; - let expr: NuExpression = expr.into_polars().$func($ddof).into(); - expr.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } - } - - #[cfg(test)] - mod $test { - use super::*; - use crate::test::test_polars_plugin_command; - - #[test] - fn test_examples() -> Result<(), ShellError> { - test_polars_plugin_command(&$command) - } - } - }; -} - -// The structs defined in this file are structs that form part of other commands -// since they share a similar name -macro_rules! lazy_expr_command { - ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident) => { - #[derive(Clone)] - pub struct $command; - - impl PluginCommand for $command { - type Plugin = PolarsPlugin; - - fn name(&self) -> &str { - $name - } - - fn description(&self) -> &str { - $desc - } - - fn signature(&self) -> Signature { - Signature::build(self.name()) - .description($desc) - .input_output_types(vec![ - ( - Type::Custom("expression".into()), - Type::Custom("expression".into()), - ), - ( - Type::Custom("dataframe".into()), - Type::Custom("dataframe".into()), - ), - ]) - .category(Category::Custom("expression".into())) - } - - fn examples(&self) -> Vec { - $examples - } - - fn run( - &self, - plugin: &Self::Plugin, - engine: &EngineInterface, - call: &EvaluatedCall, - input: PipelineData, - ) -> Result { - let value = input.into_value(call.head)?; - if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { - let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value) - .map_err(LabeledError::from)?; - let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().$func()); - lazy.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } else { - let expr = - NuExpression::try_from_value(plugin, &value).map_err(LabeledError::from)?; - let expr: NuExpression = expr.into_polars().$func().into(); - expr.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } - } - } - - #[cfg(test)] - mod $test { - use super::*; - use crate::test::test_polars_plugin_command; - use nu_protocol::ShellError; - - #[test] - fn test_examples() -> Result<(), ShellError> { - test_polars_plugin_command(&$command) - } - } - }; - - ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident, $ddof: expr) => { - #[derive(Clone)] - pub struct $command; - - impl PluginCommand for $command { - type Plugin = PolarsPlugin; - - fn name(&self) -> &str { - $name - } - - fn description(&self) -> &str { - $desc - } - fn signature(&self) -> Signature { - Signature::build(self.name()) - .input_output_types(vec![ - ( - Type::Custom("expression".into()), - Type::Custom("expression".into()), - ), - ( - Type::Custom("dataframe".into()), - Type::Custom("dataframe".into()), - ), - ]) - .category(Category::Custom("expression".into())) - } - - fn examples(&self) -> Vec { - $examples - } - - fn run( - &self, - plugin: &Self::Plugin, - engine: &EngineInterface, - call: &EvaluatedCall, - input: PipelineData, - ) -> Result { - let value = input.into_value(call.head)?; - if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { - let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value) - .map_err(LabeledError::from)?; - let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().$func($ddof)); - lazy.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } else { - let expr = NuExpression::try_from_value(plugin, &value)?; - let expr: NuExpression = expr.into_polars().$func($ddof).into(); - expr.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } - } - } - - #[cfg(test)] - mod $test { - use super::*; - use crate::test::test_polars_plugin_command; - use nu_protocol::ShellError; - - #[test] - fn test_examples() -> Result<(), ShellError> { - test_polars_plugin_command(&$command) - } - } - }; -} - // ExprList command // Expands to a command definition for a list expression expr_command!( @@ -303,21 +50,6 @@ expr_command!( test_count ); -// ExprNot command -// Expands to a command definition for a not expression -expr_command!( - ExprNot, - "polars expr-not", - "Creates a not expression.", - vec![Example { - description: "Creates a not expression", - example: "(polars col a) > 2) | polars expr-not", - result: None, - },], - not, - test_not -); - // ExprMax command // Expands to a command definition for max aggregation lazy_expr_command!( diff --git a/crates/nu_plugin_polars/src/dataframe/series/cumulative.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/cumulative.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/series/cumulative.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/cumulative.rs index da7081128d..e745bf5432 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/cumulative.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/aggregation/cumulative.rs @@ -1,6 +1,6 @@ use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/groupby.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/groupby.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/groupby.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/groupby.rs diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/median.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/median.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/median.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/median.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/aggregation/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/mod.rs new file mode 100644 index 0000000000..ea14500075 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/aggregation/mod.rs @@ -0,0 +1,44 @@ +mod aggregate; +mod aggregation_commands; +mod cumulative; +pub mod groupby; +mod median; +mod n_null; +mod n_unique; +mod quantile; +mod rolling; +mod value_counts; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use aggregate::LazyAggregate; +pub use aggregation_commands::*; +pub use cumulative::Cumulative; +pub use n_null::NNull; +pub use n_unique::NUnique; +pub use rolling::Rolling; +pub use value_counts::ValueCount; + +pub(crate) fn aggregation_commands() -> Vec>> { + vec![ + Box::new(Cumulative), + Box::new(ExprAggGroups), + Box::new(ExprCount), + Box::new(ExprList), + Box::new(ExprMax), + Box::new(ExprMin), + Box::new(ExprSum), + Box::new(ExprMean), + Box::new(ExprStd), + Box::new(ExprVar), + Box::new(LazyAggregate), + Box::new(median::LazyMedian), + Box::new(quantile::LazyQuantile), + Box::new(groupby::ToLazyGroupBy), + Box::new(Rolling), + Box::new(ValueCount), + Box::new(NNull), + Box::new(NUnique), + ] +} diff --git a/crates/nu_plugin_polars/src/dataframe/series/n_null.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/n_null.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/series/n_null.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/n_null.rs index cfe69853a7..61507d9d69 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/n_null.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/aggregation/n_null.rs @@ -1,6 +1,6 @@ use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/series/n_unique.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/n_unique.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/series/n_unique.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/n_unique.rs index 381017f9e7..3c696ee371 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/n_unique.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/aggregation/n_unique.rs @@ -3,7 +3,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{Column, NuDataFrame, NuExpression}; +use crate::values::{Column, NuDataFrame, NuExpression}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/quantile.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/quantile.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/quantile.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/quantile.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/rolling.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/rolling.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/series/rolling.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/rolling.rs index b63b5048be..05e7df6040 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/rolling.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/aggregation/rolling.rs @@ -1,7 +1,6 @@ +use crate::values::{Column, NuDataFrame}; use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::{Column, NuDataFrame}; - use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Spanned, diff --git a/crates/nu_plugin_polars/src/dataframe/series/value_counts.rs b/crates/nu_plugin_polars/src/dataframe/command/aggregation/value_counts.rs similarity index 97% rename from crates/nu_plugin_polars/src/dataframe/series/value_counts.rs rename to crates/nu_plugin_polars/src/dataframe/command/aggregation/value_counts.rs index bb228af8bb..e05a1c401c 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/value_counts.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/aggregation/value_counts.rs @@ -1,6 +1,5 @@ -use crate::{values::CustomValueSupport, PolarsPlugin}; - -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, CustomValueSupport, NuDataFrame}; +use crate::PolarsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/series/all_false.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/all_false.rs similarity index 97% rename from crates/nu_plugin_polars/src/dataframe/series/all_false.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/all_false.rs index 36e31dfb98..f5cc7acf11 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/all_false.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/boolean/all_false.rs @@ -1,6 +1,5 @@ -use crate::{values::CustomValueSupport, PolarsPlugin}; - -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, CustomValueSupport, NuDataFrame}; +use crate::PolarsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/series/all_true.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/all_true.rs similarity index 96% rename from crates/nu_plugin_polars/src/dataframe/series/all_true.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/all_true.rs index cf3d7a79fe..bcca2f1562 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/all_true.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/boolean/all_true.rs @@ -1,6 +1,5 @@ -use crate::{values::CustomValueSupport, PolarsPlugin}; - -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, CustomValueSupport, NuDataFrame}; +use crate::PolarsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/series/indexes/arg_true.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/arg_true.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/indexes/arg_true.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/arg_true.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/boolean/expr_not.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/expr_not.rs new file mode 100644 index 0000000000..886d89eeab --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/boolean/expr_not.rs @@ -0,0 +1,21 @@ +use crate::expr_command; +use crate::values::CustomValueSupport; +use crate::values::NuExpression; +use crate::PolarsPlugin; +use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; +use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Type}; + +// ExprNot command +// Expands to a command definition for a not expression +expr_command!( + ExprNot, + "polars expr-not", + "Creates a not expression.", + vec![Example { + description: "Creates a not expression", + example: "(polars col a) > 2) | polars expr-not", + result: None, + },], + not, + test_not +); diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/is_duplicated.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/is_duplicated.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/masks/is_duplicated.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/is_duplicated.rs diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/is_in.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/is_in.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/expressions/is_in.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/is_in.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/is_not_null.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/is_not_null.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/masks/is_not_null.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/is_not_null.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/is_null.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/is_null.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/masks/is_null.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/is_null.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/is_unique.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/is_unique.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/masks/is_unique.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/is_unique.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/boolean/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/mod.rs new file mode 100644 index 0000000000..e57229fe3a --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/boolean/mod.rs @@ -0,0 +1,46 @@ +mod all_false; +mod all_true; +mod arg_true; +mod expr_not; +mod is_duplicated; +mod is_in; +mod is_not_null; +mod is_null; +mod is_unique; +mod not; +pub(crate) mod otherwise; +mod set; +mod when; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use all_false::AllFalse; +pub use arg_true::ArgTrue; +pub use is_duplicated::IsDuplicated; +pub use is_in::ExprIsIn; +pub use is_not_null::IsNotNull; +pub use is_null::IsNull; +pub use is_unique::IsUnique; +pub use not::NotSeries; +pub use otherwise::ExprOtherwise; +pub use set::SetSeries; +pub use when::ExprWhen; + +pub(crate) fn boolean_commands() -> Vec>> { + vec![ + Box::new(AllFalse), + Box::new(all_true::AllTrue), + Box::new(ArgTrue), + Box::new(ExprIsIn), + Box::new(ExprOtherwise), + Box::new(ExprWhen), + Box::new(expr_not::ExprNot), + Box::new(IsDuplicated), + Box::new(IsNotNull), + Box::new(IsNull), + Box::new(IsUnique), + Box::new(NotSeries), + Box::new(SetSeries), + ] +} diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/not.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/not.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/masks/not.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/not.rs diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/otherwise.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/otherwise.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/expressions/otherwise.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/otherwise.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/set.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/set.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/masks/set.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/set.rs diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/when.rs b/crates/nu_plugin_polars/src/dataframe/command/boolean/when.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/expressions/when.rs rename to crates/nu_plugin_polars/src/dataframe/command/boolean/when.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/core/cache.rs b/crates/nu_plugin_polars/src/dataframe/command/core/cache.rs new file mode 100644 index 0000000000..ce68f7712b --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/core/cache.rs @@ -0,0 +1,16 @@ +use crate::lazy_command; + +// LazyCache command +// Expands to a command definition for cache +lazy_command!( + LazyCache, + "polars cache", + "Caches operations in a new LazyFrame.", + vec![Example { + description: "Caches the result into a new LazyFrame", + example: "[[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars reverse | polars cache", + result: None, + }], + cache, + test_cache +); diff --git a/crates/nu_plugin_polars/src/dataframe/eager/columns.rs b/crates/nu_plugin_polars/src/dataframe/command/core/columns.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/eager/columns.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/columns.rs index 5fa4fb82a8..ceb9975dfe 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/columns.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/columns.rs @@ -1,6 +1,6 @@ +use crate::values::NuDataFrame; use crate::PolarsPlugin; -use super::super::values::NuDataFrame; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/fetch.rs b/crates/nu_plugin_polars/src/dataframe/command/core/fetch.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/fetch.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/fetch.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/core/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/core/mod.rs new file mode 100644 index 0000000000..4acf6778e1 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/core/mod.rs @@ -0,0 +1,39 @@ +mod cache; +mod columns; +mod fetch; +mod open; +mod save; +mod schema; +mod shape; +mod summary; +mod to_df; +mod to_lazy; +mod to_nu; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use self::open::OpenDataFrame; +use fetch::LazyFetch; +pub use schema::SchemaCmd; +pub use shape::ShapeDF; +pub use summary::Summary; +pub use to_df::ToDataFrame; +pub use to_lazy::ToLazyFrame; +pub use to_nu::ToNu; + +pub(crate) fn core_commands() -> Vec>> { + vec![ + Box::new(columns::ColumnsDF), + Box::new(cache::LazyCache), + Box::new(LazyFetch), + Box::new(OpenDataFrame), + Box::new(Summary), + Box::new(ShapeDF), + Box::new(SchemaCmd), + Box::new(ToNu), + Box::new(ToDataFrame), + Box::new(save::SaveDF), + Box::new(ToLazyFrame), + ] +} diff --git a/crates/nu_plugin_polars/src/dataframe/eager/open.rs b/crates/nu_plugin_polars/src/dataframe/command/core/open.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/open.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/open.rs index c2894d64fe..a897114999 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/open.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/open.rs @@ -1,12 +1,11 @@ use crate::{ dataframe::values::NuSchema, - values::{CustomValueSupport, NuLazyFrame, PolarsFileType}, + values::{CustomValueSupport, NuDataFrame, NuLazyFrame, PolarsFileType}, EngineWrapper, PolarsPlugin, }; use nu_path::expand_path_with; use nu_utils::perf; -use super::super::values::NuDataFrame; use nu_plugin::PluginCommand; use nu_protocol::{ Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Spanned, diff --git a/crates/nu_plugin_polars/src/dataframe/eager/save/arrow.rs b/crates/nu_plugin_polars/src/dataframe/command/core/save/arrow.rs similarity index 95% rename from crates/nu_plugin_polars/src/dataframe/eager/save/arrow.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/save/arrow.rs index 2c972c4d4f..54317edea3 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/save/arrow.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/save/arrow.rs @@ -48,7 +48,7 @@ pub(crate) fn command_eager( #[cfg(test)] pub mod test { - use crate::eager::save::test::{test_eager_save, test_lazy_save}; + use crate::command::core::save::test::{test_eager_save, test_lazy_save}; #[test] pub fn test_arrow_eager_save() -> Result<(), Box> { diff --git a/crates/nu_plugin_polars/src/dataframe/eager/save/avro.rs b/crates/nu_plugin_polars/src/dataframe/command/core/save/avro.rs similarity index 96% rename from crates/nu_plugin_polars/src/dataframe/eager/save/avro.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/save/avro.rs index 58463ec36c..ef5c0cdca2 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/save/avro.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/save/avro.rs @@ -60,7 +60,7 @@ pub(crate) fn command_eager( #[cfg(test)] pub mod test { - use crate::eager::save::test::{test_eager_save, test_lazy_save}; + use crate::command::core::save::test::{test_eager_save, test_lazy_save}; #[test] pub fn test_avro_eager_save() -> Result<(), Box> { diff --git a/crates/nu_plugin_polars/src/dataframe/eager/save/csv.rs b/crates/nu_plugin_polars/src/dataframe/command/core/save/csv.rs similarity index 97% rename from crates/nu_plugin_polars/src/dataframe/eager/save/csv.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/save/csv.rs index f43b1b023b..941967b469 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/save/csv.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/save/csv.rs @@ -97,7 +97,7 @@ pub(crate) fn command_eager( #[cfg(test)] pub mod test { - use crate::eager::save::test::{test_eager_save, test_lazy_save}; + use crate::command::core::save::test::{test_eager_save, test_lazy_save}; #[test] pub fn test_csv_eager_save() -> Result<(), Box> { diff --git a/crates/nu_plugin_polars/src/dataframe/eager/save/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/core/save/mod.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/eager/save/mod.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/save/mod.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/save/ndjson.rs b/crates/nu_plugin_polars/src/dataframe/command/core/save/ndjson.rs similarity index 95% rename from crates/nu_plugin_polars/src/dataframe/eager/save/ndjson.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/save/ndjson.rs index 7840103079..55204436d8 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/save/ndjson.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/save/ndjson.rs @@ -49,7 +49,7 @@ pub(crate) fn command_eager( #[cfg(test)] pub mod test { - use crate::eager::save::test::{test_eager_save, test_lazy_save}; + use crate::command::core::save::test::{test_eager_save, test_lazy_save}; #[test] pub fn test_arrow_eager_save() -> Result<(), Box> { diff --git a/crates/nu_plugin_polars/src/dataframe/eager/save/parquet.rs b/crates/nu_plugin_polars/src/dataframe/command/core/save/parquet.rs similarity index 95% rename from crates/nu_plugin_polars/src/dataframe/eager/save/parquet.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/save/parquet.rs index 7200e23500..f77e7c4028 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/save/parquet.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/save/parquet.rs @@ -48,7 +48,7 @@ pub(crate) fn command_eager( #[cfg(test)] pub(crate) mod test { - use crate::eager::save::test::{test_eager_save, test_lazy_save}; + use crate::command::core::save::test::{test_eager_save, test_lazy_save}; #[test] pub fn test_parquet_eager_save() -> Result<(), Box> { diff --git a/crates/nu_plugin_polars/src/dataframe/eager/schema.rs b/crates/nu_plugin_polars/src/dataframe/command/core/schema.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/eager/schema.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/schema.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/shape.rs b/crates/nu_plugin_polars/src/dataframe/command/core/shape.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/eager/shape.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/shape.rs index 60f96e8b9f..bbd8821e18 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/shape.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/shape.rs @@ -5,7 +5,7 @@ use nu_protocol::{ use crate::{dataframe::values::Column, values::CustomValueSupport, PolarsPlugin}; -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; #[derive(Clone)] pub struct ShapeDF; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/summary.rs b/crates/nu_plugin_polars/src/dataframe/command/core/summary.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/summary.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/summary.rs index 863fb891b7..f9156560d2 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/summary.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/summary.rs @@ -1,6 +1,6 @@ use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/eager/to_df.rs b/crates/nu_plugin_polars/src/dataframe/command/core/to_df.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/to_df.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/to_df.rs index 39d54f6e10..e3a9491411 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/to_df.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/to_df.rs @@ -4,7 +4,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/to_lazy.rs b/crates/nu_plugin_polars/src/dataframe/command/core/to_lazy.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/lazy/to_lazy.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/to_lazy.rs index 8d81bd1a8a..1b42d310f3 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/to_lazy.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/to_lazy.rs @@ -1,6 +1,6 @@ use crate::{dataframe::values::NuSchema, values::CustomValueSupport, Cacheable, PolarsPlugin}; -use super::super::values::{NuDataFrame, NuLazyFrame}; +use crate::values::{NuDataFrame, NuLazyFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, SyntaxShape, Type}; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/to_nu.rs b/crates/nu_plugin_polars/src/dataframe/command/core/to_nu.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/to_nu.rs rename to crates/nu_plugin_polars/src/dataframe/command/core/to_nu.rs index a8b4aa3976..07427ca63d 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/to_nu.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/core/to_nu.rs @@ -10,7 +10,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; #[derive(Clone)] pub struct ToNu; diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/alias.rs b/crates/nu_plugin_polars/src/dataframe/command/data/alias.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/expressions/alias.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/alias.rs index 0c12be144e..1d1c547073 100644 --- a/crates/nu_plugin_polars/src/dataframe/expressions/alias.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/alias.rs @@ -1,6 +1,6 @@ use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::NuExpression; +use crate::values::NuExpression; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/eager/append.rs b/crates/nu_plugin_polars/src/dataframe/command/data/append.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/eager/append.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/append.rs diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/arg_where.rs b/crates/nu_plugin_polars/src/dataframe/command/data/arg_where.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/expressions/arg_where.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/arg_where.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/cast.rs b/crates/nu_plugin_polars/src/dataframe/command/data/cast.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/cast.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/cast.rs index 5787d961e1..c1fc08c3c8 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/cast.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/cast.rs @@ -4,7 +4,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ record, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/col.rs b/crates/nu_plugin_polars/src/dataframe/command/data/col.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/expressions/col.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/col.rs diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/collect.rs b/crates/nu_plugin_polars/src/dataframe/command/data/collect.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/collect.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/collect.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/drop.rs b/crates/nu_plugin_polars/src/dataframe/command/data/drop.rs similarity index 97% rename from crates/nu_plugin_polars/src/dataframe/eager/drop.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/drop.rs index 4e2a2b7136..852e42e52c 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/drop.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/drop.rs @@ -7,8 +7,8 @@ use nu_protocol::{ use crate::values::CustomValueSupport; use crate::PolarsPlugin; -use super::super::values::utils::convert_columns; -use super::super::values::{Column, NuDataFrame}; +use crate::values::utils::convert_columns; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct DropDF; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/drop_duplicates.rs b/crates/nu_plugin_polars/src/dataframe/command/data/drop_duplicates.rs similarity index 97% rename from crates/nu_plugin_polars/src/dataframe/eager/drop_duplicates.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/drop_duplicates.rs index 2d311867ea..5f82ce13f6 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/drop_duplicates.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/drop_duplicates.rs @@ -8,8 +8,8 @@ use polars::prelude::UniqueKeepStrategy; use crate::values::CustomValueSupport; use crate::PolarsPlugin; -use super::super::values::utils::convert_columns_string; -use super::super::values::{Column, NuDataFrame}; +use crate::values::utils::convert_columns_string; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct DropDuplicates; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/drop_nulls.rs b/crates/nu_plugin_polars/src/dataframe/command/data/drop_nulls.rs similarity index 97% rename from crates/nu_plugin_polars/src/dataframe/eager/drop_nulls.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/drop_nulls.rs index 43dbf29f3d..b99ea4223d 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/drop_nulls.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/drop_nulls.rs @@ -7,8 +7,8 @@ use nu_protocol::{ use crate::values::CustomValueSupport; use crate::PolarsPlugin; -use super::super::values::utils::convert_columns_string; -use super::super::values::{Column, NuDataFrame}; +use crate::values::utils::convert_columns_string; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct DropNulls; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/dummies.rs b/crates/nu_plugin_polars/src/dataframe/command/data/dummies.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/eager/dummies.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/dummies.rs index 6346ebfb60..684cda772d 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/dummies.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/dummies.rs @@ -1,4 +1,4 @@ -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; use crate::{values::CustomValueSupport, PolarsPlugin}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/explode.rs b/crates/nu_plugin_polars/src/dataframe/command/data/explode.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/explode.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/explode.rs diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/fill_nan.rs b/crates/nu_plugin_polars/src/dataframe/command/data/fill_nan.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/fill_nan.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/fill_nan.rs diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/fill_null.rs b/crates/nu_plugin_polars/src/dataframe/command/data/fill_null.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/fill_null.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/fill_null.rs diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/filter.rs b/crates/nu_plugin_polars/src/dataframe/command/data/filter.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/filter.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/filter.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/filter_with.rs b/crates/nu_plugin_polars/src/dataframe/command/data/filter_with.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/filter_with.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/filter_with.rs index acfe2251cd..bc222459e1 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/filter_with.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/filter_with.rs @@ -11,7 +11,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct FilterWith; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/first.rs b/crates/nu_plugin_polars/src/dataframe/command/data/first.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/eager/first.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/first.rs index e75aa04428..ca5205ff6a 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/first.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/first.rs @@ -3,7 +3,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{NuDataFrame, NuExpression}; +use crate::values::{NuDataFrame, NuExpression}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/flatten.rs b/crates/nu_plugin_polars/src/dataframe/command/data/flatten.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/flatten.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/flatten.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/get.rs b/crates/nu_plugin_polars/src/dataframe/command/data/get.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/eager/get.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/get.rs index 330b027b0d..09269589a3 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/get.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/get.rs @@ -8,7 +8,7 @@ use crate::{ dataframe::values::utils::convert_columns_string, values::CustomValueSupport, PolarsPlugin, }; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct GetDF; diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/join.rs b/crates/nu_plugin_polars/src/dataframe/command/data/join.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/join.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/join.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/last.rs b/crates/nu_plugin_polars/src/dataframe/command/data/last.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/eager/last.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/last.rs index 88f1041c25..43e4fbc232 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/last.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/last.rs @@ -3,7 +3,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{utils::DEFAULT_ROWS, NuDataFrame, NuExpression}; +use crate::values::{utils::DEFAULT_ROWS, NuDataFrame, NuExpression}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/lit.rs b/crates/nu_plugin_polars/src/dataframe/command/data/lit.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/expressions/lit.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/lit.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/data/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/data/mod.rs new file mode 100644 index 0000000000..302b5f4685 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/data/mod.rs @@ -0,0 +1,109 @@ +mod alias; +mod append; +mod arg_where; +mod cast; +mod col; +mod collect; +mod drop; +mod drop_duplicates; +mod drop_nulls; +mod dummies; +mod explode; +mod fill_nan; +mod fill_null; +mod filter; +mod filter_with; +mod first; +mod flatten; +mod get; +mod join; +mod last; +mod lit; +mod pivot; +mod query_df; +mod rename; +mod reverse; +mod sample; +mod select; +mod slice; +mod sort_by_expr; +pub mod sql_context; +pub mod sql_expr; +mod take; +mod unpivot; +mod with_column; +use filter::LazyFilter; +mod shift; +mod unique; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use alias::ExprAlias; +pub use append::AppendDF; +pub use arg_where::ExprArgWhere; +pub use cast::CastDF; +pub use col::ExprCol; +pub use collect::LazyCollect; +pub use drop::DropDF; +pub use drop_duplicates::DropDuplicates; +pub use drop_nulls::DropNulls; +pub use dummies::Dummies; +pub use explode::LazyExplode; +use fill_nan::LazyFillNA; +pub use fill_null::LazyFillNull; +pub use first::FirstDF; +use flatten::LazyFlatten; +pub use get::GetDF; +use join::LazyJoin; +pub use last::LastDF; +pub use lit::ExprLit; +use query_df::QueryDf; +pub use rename::RenameDF; +pub use sample::SampleDF; +pub use shift::Shift; +pub use slice::SliceDF; +use sort_by_expr::LazySortBy; +pub use take::TakeDF; +pub use unique::Unique; +pub use unpivot::UnpivotDF; +pub use with_column::WithColumn; + +pub(crate) fn data_commands() -> Vec>> { + vec![ + Box::new(AppendDF), + Box::new(CastDF), + Box::new(DropDF), + Box::new(DropDuplicates), + Box::new(DropNulls), + Box::new(Dummies), + Box::new(filter_with::FilterWith), + Box::new(GetDF), + Box::new(pivot::PivotDF), + Box::new(UnpivotDF), + Box::new(FirstDF), + Box::new(LastDF), + Box::new(RenameDF), + Box::new(SampleDF), + Box::new(SliceDF), + Box::new(TakeDF), + Box::new(QueryDf), + Box::new(WithColumn), + Box::new(ExprAlias), + Box::new(ExprArgWhere), + Box::new(ExprLit), + Box::new(ExprCol), + Box::new(LazyCollect), + Box::new(LazyExplode), + Box::new(LazyFillNA), + Box::new(LazyFillNull), + Box::new(LazyFlatten), + Box::new(LazyJoin), + Box::new(reverse::LazyReverse), + Box::new(select::LazySelect), + Box::new(LazySortBy), + Box::new(LazyFilter), + Box::new(Shift), + Box::new(Unique), + ] +} diff --git a/crates/nu_plugin_polars/src/dataframe/eager/pivot.rs b/crates/nu_plugin_polars/src/dataframe/command/data/pivot.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/pivot.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/pivot.rs index c541d16520..946868e6ad 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/pivot.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/pivot.rs @@ -12,7 +12,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; #[derive(Clone)] pub struct PivotDF; @@ -75,7 +75,7 @@ impl PluginCommand for PivotDF { vec![ Example { example: "[[name subject test_1 test_2]; [Cady maths 98 100] [Cady physics 99 100] [Karen maths 61 60] [Karen physics 58 60]] | polars into-df | polars pivot --on [subject] --index [name] --values [test_1]", - description: "Perform a pivot in order to show individuals test score by subject", + description: "Perform a pivot in order to show individuals test score by subject", result: Some( NuDataFrame::try_from_columns( vec![ diff --git a/crates/nu_plugin_polars/src/dataframe/eager/query_df.rs b/crates/nu_plugin_polars/src/dataframe/command/data/query_df.rs similarity index 96% rename from crates/nu_plugin_polars/src/dataframe/eager/query_df.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/query_df.rs index 799a3b4d64..3d0c218796 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/query_df.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/query_df.rs @@ -1,7 +1,8 @@ -use super::super::values::NuDataFrame; +use super::sql_context::SQLContext; use crate::dataframe::values::Column; -use crate::dataframe::{eager::SQLContext, values::NuLazyFrame}; +use crate::dataframe::values::NuLazyFrame; use crate::values::CustomValueSupport; +use crate::values::NuDataFrame; use crate::PolarsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/eager/rename.rs b/crates/nu_plugin_polars/src/dataframe/command/data/rename.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/rename.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/rename.rs index 197d347b97..485c440818 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/rename.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/rename.rs @@ -10,7 +10,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct RenameDF; diff --git a/crates/nu_plugin_polars/src/dataframe/command/data/reverse.rs b/crates/nu_plugin_polars/src/dataframe/command/data/reverse.rs new file mode 100644 index 0000000000..cb1e6af35f --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/data/reverse.rs @@ -0,0 +1,37 @@ +use nu_protocol::{Span, Value}; + +use crate::{ + lazy_command, + values::{Column, NuDataFrame}, +}; + +// LazyReverse command +// Expands to a command definition for reverse +lazy_command!( + LazyReverse, + "polars reverse", + "Reverses the LazyFrame", + vec![Example { + description: "Reverses the dataframe.", + example: "[[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars reverse", + result: Some( + NuDataFrame::try_from_columns( + vec![ + Column::new( + "a".to_string(), + vec![Value::test_int(2), Value::test_int(4), Value::test_int(6),], + ), + Column::new( + "b".to_string(), + vec![Value::test_int(2), Value::test_int(2), Value::test_int(2),], + ), + ], + None + ) + .expect("simple df for test should not fail") + .into_value(Span::test_data()), + ), + },], + reverse, + test_reverse +); diff --git a/crates/nu_plugin_polars/src/dataframe/eager/sample.rs b/crates/nu_plugin_polars/src/dataframe/command/data/sample.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/sample.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/sample.rs index 8e110feef9..b334c2cbc4 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/sample.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/sample.rs @@ -8,7 +8,7 @@ use polars::series::Series; use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct SampleDF; diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/select.rs b/crates/nu_plugin_polars/src/dataframe/command/data/select.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/lazy/select.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/select.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/shift.rs b/crates/nu_plugin_polars/src/dataframe/command/data/shift.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/series/shift.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/shift.rs index 51c8242e51..6565ad92b2 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/shift.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/shift.rs @@ -4,7 +4,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/eager/slice.rs b/crates/nu_plugin_polars/src/dataframe/command/data/slice.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/eager/slice.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/slice.rs index feeeab5df0..422d17db49 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/slice.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/slice.rs @@ -6,7 +6,7 @@ use nu_protocol::{ use crate::{dataframe::values::Column, values::CustomValueSupport, PolarsPlugin}; -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; #[derive(Clone)] pub struct SliceDF; diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/sort_by_expr.rs b/crates/nu_plugin_polars/src/dataframe/command/data/sort_by_expr.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/lazy/sort_by_expr.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/sort_by_expr.rs index 952388a2dc..3a0f2dd2d0 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/sort_by_expr.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/sort_by_expr.rs @@ -1,4 +1,4 @@ -use super::super::values::NuLazyFrame; +use crate::values::NuLazyFrame; use crate::{ dataframe::values::{Column, NuDataFrame, NuExpression}, values::CustomValueSupport, diff --git a/crates/nu_plugin_polars/src/dataframe/eager/sql_context.rs b/crates/nu_plugin_polars/src/dataframe/command/data/sql_context.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/sql_context.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/sql_context.rs index f558904344..3349736702 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/sql_context.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/sql_context.rs @@ -1,4 +1,4 @@ -use crate::dataframe::eager::sql_expr::parse_sql_expr; +use crate::dataframe::command::data::sql_expr::parse_sql_expr; use polars::error::{ErrString, PolarsError}; use polars::prelude::{col, DataFrame, DataType, IntoLazy, LazyFrame}; use sqlparser::ast::{ diff --git a/crates/nu_plugin_polars/src/dataframe/eager/sql_expr.rs b/crates/nu_plugin_polars/src/dataframe/command/data/sql_expr.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/eager/sql_expr.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/sql_expr.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/take.rs b/crates/nu_plugin_polars/src/dataframe/command/data/take.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/take.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/take.rs index a03093b607..e6c3c3c9b6 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/take.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/take.rs @@ -7,7 +7,7 @@ use polars::prelude::DataType; use crate::{dataframe::values::Column, values::CustomValueSupport, PolarsPlugin}; -use super::super::values::NuDataFrame; +use crate::values::NuDataFrame; #[derive(Clone)] pub struct TakeDF; diff --git a/crates/nu_plugin_polars/src/dataframe/series/unique.rs b/crates/nu_plugin_polars/src/dataframe/command/data/unique.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/series/unique.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/unique.rs index 991d22b096..ebc854a211 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/unique.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/unique.rs @@ -4,7 +4,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/eager/unpivot.rs b/crates/nu_plugin_polars/src/dataframe/command/data/unpivot.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/unpivot.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/unpivot.rs index 0d61951ec2..5235305de2 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/unpivot.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/unpivot.rs @@ -11,7 +11,7 @@ use crate::{ PolarsPlugin, }; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct UnpivotDF; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/with_column.rs b/crates/nu_plugin_polars/src/dataframe/command/data/with_column.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/eager/with_column.rs rename to crates/nu_plugin_polars/src/dataframe/command/data/with_column.rs index 1c29f90c3f..f7c6deccf8 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/with_column.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/with_column.rs @@ -1,4 +1,4 @@ -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use crate::{ dataframe::values::{NuExpression, NuLazyFrame}, values::{CustomValueSupport, PolarsPluginObject}, diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/as_date.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/as_date.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/as_date.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/as_date.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/as_datetime.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/as_datetime.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/as_datetime.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/as_datetime.rs diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/datepart.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/datepart.rs similarity index 99% rename from crates/nu_plugin_polars/src/dataframe/expressions/datepart.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/datepart.rs index 1c7b7e7005..12841db43f 100644 --- a/crates/nu_plugin_polars/src/dataframe/expressions/datepart.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/datetime/datepart.rs @@ -1,4 +1,4 @@ -use super::super::values::NuExpression; +use crate::values::NuExpression; use crate::{ dataframe::values::{Column, NuDataFrame}, diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_day.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_day.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_day.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_day.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_hour.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_hour.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_hour.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_hour.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_minute.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_minute.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_minute.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_minute.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_month.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_month.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_month.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_month.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_nanosecond.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_nanosecond.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_nanosecond.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_nanosecond.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_ordinal.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_ordinal.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_ordinal.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_ordinal.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_second.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_second.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_second.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_second.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_week.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_week.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_week.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_week.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_weekday.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_weekday.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_weekday.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_weekday.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/get_year.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/get_year.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/date/get_year.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/get_year.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/datetime/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/mod.rs new file mode 100644 index 0000000000..44c5d5a53a --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/datetime/mod.rs @@ -0,0 +1,52 @@ +mod as_date; +mod as_datetime; +mod datepart; +mod get_day; +mod get_hour; +mod get_minute; +mod get_month; +mod get_nanosecond; +mod get_ordinal; +mod get_second; +mod get_week; +mod get_weekday; +mod get_year; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use as_date::AsDate; +pub use as_datetime::AsDateTime; +pub use datepart::ExprDatePart; +pub use get_day::GetDay; +pub use get_hour::GetHour; +pub use get_minute::GetMinute; +pub use get_month::GetMonth; +pub use get_nanosecond::GetNanosecond; +pub use get_ordinal::GetOrdinal; +pub use get_second::GetSecond; +pub use get_week::GetWeek; +pub use get_weekday::GetWeekDay; +pub use get_year::GetYear; +mod strftime; + +pub use strftime::StrFTime; + +pub(crate) fn datetime_commands() -> Vec>> { + vec![ + Box::new(ExprDatePart), + Box::new(AsDate), + Box::new(AsDateTime), + Box::new(GetDay), + Box::new(GetHour), + Box::new(GetMinute), + Box::new(GetMonth), + Box::new(GetNanosecond), + Box::new(GetOrdinal), + Box::new(GetSecond), + Box::new(GetWeek), + Box::new(GetWeekDay), + Box::new(GetYear), + Box::new(StrFTime), + ] +} diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/strftime.rs b/crates/nu_plugin_polars/src/dataframe/command/datetime/strftime.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/strftime.rs rename to crates/nu_plugin_polars/src/dataframe/command/datetime/strftime.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/arg_max.rs b/crates/nu_plugin_polars/src/dataframe/command/index/arg_max.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/series/arg_max.rs rename to crates/nu_plugin_polars/src/dataframe/command/index/arg_max.rs index e7c67cdc32..de81281bda 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/arg_max.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/index/arg_max.rs @@ -1,6 +1,6 @@ use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/series/arg_min.rs b/crates/nu_plugin_polars/src/dataframe/command/index/arg_min.rs similarity index 98% rename from crates/nu_plugin_polars/src/dataframe/series/arg_min.rs rename to crates/nu_plugin_polars/src/dataframe/command/index/arg_min.rs index 268ba401da..5df4cbaa17 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/arg_min.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/index/arg_min.rs @@ -1,6 +1,6 @@ use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::{Column, NuDataFrame}; +use crate::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ diff --git a/crates/nu_plugin_polars/src/dataframe/series/indexes/arg_sort.rs b/crates/nu_plugin_polars/src/dataframe/command/index/arg_sort.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/indexes/arg_sort.rs rename to crates/nu_plugin_polars/src/dataframe/command/index/arg_sort.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/indexes/arg_unique.rs b/crates/nu_plugin_polars/src/dataframe/command/index/arg_unique.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/indexes/arg_unique.rs rename to crates/nu_plugin_polars/src/dataframe/command/index/arg_unique.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/index/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/index/mod.rs new file mode 100644 index 0000000000..e0439f13f4 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/index/mod.rs @@ -0,0 +1,24 @@ +mod arg_max; +mod arg_min; +mod arg_sort; +mod arg_unique; +mod set_with_idx; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use arg_max::ArgMax; +pub use arg_min::ArgMin; +pub use arg_sort::ArgSort; +pub use arg_unique::ArgUnique; +pub use set_with_idx::SetWithIndex; + +pub(crate) fn index_commands() -> Vec>> { + vec![ + Box::new(ArgMax), + Box::new(ArgMin), + Box::new(ArgSort), + Box::new(ArgUnique), + Box::new(SetWithIndex), + ] +} diff --git a/crates/nu_plugin_polars/src/dataframe/series/indexes/set_with_idx.rs b/crates/nu_plugin_polars/src/dataframe/command/index/set_with_idx.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/indexes/set_with_idx.rs rename to crates/nu_plugin_polars/src/dataframe/command/index/set_with_idx.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/integer/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/integer/mod.rs new file mode 100644 index 0000000000..080b7141b8 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/integer/mod.rs @@ -0,0 +1,12 @@ +mod to_decimal; +mod to_integer; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use to_decimal::ToDecimal; +pub use to_integer::ToInteger; + +pub(crate) fn integer_commands() -> Vec>> { + vec![Box::new(ToDecimal), Box::new(ToInteger)] +} diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/to_decimal.rs b/crates/nu_plugin_polars/src/dataframe/command/integer/to_decimal.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/to_decimal.rs rename to crates/nu_plugin_polars/src/dataframe/command/integer/to_decimal.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/to_integer.rs b/crates/nu_plugin_polars/src/dataframe/command/integer/to_integer.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/to_integer.rs rename to crates/nu_plugin_polars/src/dataframe/command/integer/to_integer.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/macro_commands.rs b/crates/nu_plugin_polars/src/dataframe/command/macro_commands.rs new file mode 100644 index 0000000000..3986a53037 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/macro_commands.rs @@ -0,0 +1,440 @@ +/// Definition of multiple Expression commands using a macro rule +/// All of these expressions have an identical body and only require +/// to have a change in the name, description and expression function + +#[macro_export] +macro_rules! lazy_command { + ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident) => { + use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; + use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Type}; + /// Definition of multiple lazyframe commands using a macro rule + /// All of these commands have an identical body and only require + /// to have a change in the name, description and function + use $crate::dataframe::values::NuLazyFrame; + use $crate::values::CustomValueSupport; + use $crate::PolarsPlugin; + + #[derive(Clone)] + pub struct $command; + + impl PluginCommand for $command { + type Plugin = PolarsPlugin; + + fn name(&self) -> &str { + $name + } + + fn description(&self) -> &str { + $desc + } + + fn signature(&self) -> Signature { + Signature::build(self.name()) + .description($desc) + .input_output_type( + Type::Custom("dataframe".into()), + Type::Custom("dataframe".into()), + ) + .category(Category::Custom("lazyframe".into())) + } + + fn examples(&self) -> Vec { + $examples + } + + fn run( + &self, + plugin: &Self::Plugin, + engine: &EngineInterface, + call: &EvaluatedCall, + input: PipelineData, + ) -> Result { + let lazy = NuLazyFrame::try_from_pipeline_coerce(plugin, input, call.head) + .map_err(LabeledError::from)?; + let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().$func()); + lazy.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } + } + + #[cfg(test)] + mod $test { + use super::*; + use nu_protocol::ShellError; + use $crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&$command) + } + } + }; + + ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident, $ddot: expr) => { + #[derive(Clone)] + pub struct $command; + + impl PluginCommand for $command { + type Plugin = PolarsPlugin; + + fn signature(&self) -> Signature { + Signature::build($name) + .description($desc) + .input_output_type( + Type::Custom("dataframe".into()), + Type::Custom("dataframe".into()), + ) + .category(Category::Custom("lazyframe".into())) + .plugin_examples($examples) + } + + fn run( + &self, + _plugin: &Self::Plugin, + engine: &EngineInterface, + call: &EvaluatedCall, + input: PipelineData, + ) -> Result { + let lazy = NuLazyFrame::try_from_pipeline_coerce(plugin, input, call.head) + .map_err(LabeledError::from)?; + let lazy = NuLazyFrame::new(lazy.from_eager, lazy.into_polars().$func($ddot)); + lazy.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } + } + + #[cfg(test)] + mod $test { + use super::*; + use nu_protocol::ShellError; + use $crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&$command) + } + } + }; + + ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident?, $test: ident) => { + #[derive(Clone)] + pub struct $command; + + impl PluginCommand for $command { + type Plugin = PolarsPlugin; + + fn name(&self) -> &str { + $name + } + + fn description(&self) -> &str { + $desc + } + fn signature(&self) -> Signature { + Signature::build(self.name()) + .input_output_type( + Type::Custom("dataframe".into()), + Type::Custom("dataframe".into()), + ) + .category(Category::Custom("lazyframe".into())) + } + + fn examples(&self) -> Vec { + $examples + } + + fn run( + &self, + plugin: &Self::Plugin, + engine: &EngineInterface, + call: &EvaluatedCall, + input: PipelineData, + ) -> Result { + let lazy = NuLazyFrame::try_from_pipeline_coerce(plugin, input, call.head) + .map_err(LabeledError::from)?; + + let lazy = NuLazyFrame::new( + lazy.from_eager, + lazy.to_polars() + .$func() + .map_err(|e| ShellError::GenericError { + error: "Dataframe Error".into(), + msg: e.to_string(), + help: None, + span: None, + inner: vec![], + }) + .map_err(LabeledError::from)?, + ); + + lazy.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } + } + + #[cfg(test)] + mod $test { + use super::*; + use nu_protocol::ShellError; + use $crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&$command) + } + } + }; +} + +// The structs defined in this file are structs that form part of other commands +// since they share a similar name +#[macro_export] +macro_rules! expr_command { + ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident) => { + #[derive(Clone)] + pub struct $command; + + impl PluginCommand for $command { + type Plugin = PolarsPlugin; + + fn name(&self) -> &str { + $name + } + + fn description(&self) -> &str { + $desc + } + + fn signature(&self) -> Signature { + Signature::build(self.name()) + .description($desc) + .input_output_type( + Type::Custom("expression".into()), + Type::Custom("expression".into()), + ) + .category(Category::Custom("expression".into())) + } + + fn examples(&self) -> Vec { + $examples + } + + fn run( + &self, + plugin: &Self::Plugin, + engine: &EngineInterface, + call: &EvaluatedCall, + input: PipelineData, + ) -> Result { + let expr = NuExpression::try_from_pipeline(plugin, input, call.head) + .map_err(LabeledError::from)?; + let expr: NuExpression = expr.into_polars().$func().into(); + expr.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } + } + + #[cfg(test)] + mod $test { + use super::*; + use nu_protocol::ShellError; + use $crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&$command) + } + } + }; + + ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident, $ddof: expr) => { + #[derive(Clone)] + pub struct $command; + + impl PluginCommand for $command { + type Plugin = PolarsPlugin; + + fn signature(&self) -> Signature { + Signature::build(self.name()) + .description($desc) + .input_output_type( + Type::Custom("expression".into()), + Type::Custom("expression".into()), + ) + .category(Category::Custom("expression".into())) + .plugin_examples($examples) + } + + fn run( + &self, + _plugin: &Self::Plugin, + engine: &EngineInterface, + call: &EvaluatedCall, + input: PipelineData, + ) -> Result { + let expr = NuExpression::try_from_pipeline(input, call.head) + .map_err(LabeledError::from)?; + let expr: NuExpression = expr.into_polars().$func($ddof).into(); + expr.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } + } + + #[cfg(test)] + mod $test { + use super::*; + use $crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&$command) + } + } + }; +} + +// The structs defined in this file are structs that form part of other commands +// since they share a similar name +#[macro_export] +macro_rules! lazy_expr_command { + ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident) => { + #[derive(Clone)] + pub struct $command; + + impl PluginCommand for $command { + type Plugin = PolarsPlugin; + + fn name(&self) -> &str { + $name + } + + fn description(&self) -> &str { + $desc + } + + fn signature(&self) -> Signature { + Signature::build(self.name()) + .description($desc) + .input_output_types(vec![ + ( + Type::Custom("expression".into()), + Type::Custom("expression".into()), + ), + ( + Type::Custom("dataframe".into()), + Type::Custom("dataframe".into()), + ), + ]) + .category(Category::Custom("expression".into())) + } + + fn examples(&self) -> Vec { + $examples + } + + fn run( + &self, + plugin: &Self::Plugin, + engine: &EngineInterface, + call: &EvaluatedCall, + input: PipelineData, + ) -> Result { + let value = input.into_value(call.head)?; + if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { + let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value) + .map_err(LabeledError::from)?; + let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().$func()); + lazy.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } else { + let expr = + NuExpression::try_from_value(plugin, &value).map_err(LabeledError::from)?; + let expr: NuExpression = expr.into_polars().$func().into(); + expr.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } + } + } + + #[cfg(test)] + mod $test { + use super::*; + use nu_protocol::ShellError; + use $crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&$command) + } + } + }; + + ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident, $ddof: expr) => { + #[derive(Clone)] + pub struct $command; + + impl PluginCommand for $command { + type Plugin = PolarsPlugin; + + fn name(&self) -> &str { + $name + } + + fn description(&self) -> &str { + $desc + } + fn signature(&self) -> Signature { + Signature::build(self.name()) + .input_output_types(vec![ + ( + Type::Custom("expression".into()), + Type::Custom("expression".into()), + ), + ( + Type::Custom("dataframe".into()), + Type::Custom("dataframe".into()), + ), + ]) + .category(Category::Custom("expression".into())) + } + + fn examples(&self) -> Vec { + $examples + } + + fn run( + &self, + plugin: &Self::Plugin, + engine: &EngineInterface, + call: &EvaluatedCall, + input: PipelineData, + ) -> Result { + let value = input.into_value(call.head)?; + if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { + let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value) + .map_err(LabeledError::from)?; + let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().$func($ddof)); + lazy.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } else { + let expr = NuExpression::try_from_value(plugin, &value)?; + let expr: NuExpression = expr.into_polars().$func($ddof).into(); + expr.to_pipeline_data(plugin, engine, call.head) + .map_err(LabeledError::from) + } + } + } + + #[cfg(test)] + mod $test { + use super::*; + use nu_protocol::ShellError; + use $crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&$command) + } + } + }; +} diff --git a/crates/nu_plugin_polars/src/dataframe/command/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/mod.rs new file mode 100644 index 0000000000..bab0f3c9f2 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/mod.rs @@ -0,0 +1,10 @@ +pub mod aggregation; +pub mod boolean; +pub mod core; +pub mod data; +pub mod datetime; +pub mod index; +pub mod integer; +pub mod macro_commands; +pub mod string; +pub mod stub; diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/concat_str.rs b/crates/nu_plugin_polars/src/dataframe/command/string/concat_str.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/expressions/concat_str.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/concat_str.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/contains.rs b/crates/nu_plugin_polars/src/dataframe/command/string/contains.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/contains.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/contains.rs diff --git a/crates/nu_plugin_polars/src/dataframe/command/string/mod.rs b/crates/nu_plugin_polars/src/dataframe/command/string/mod.rs new file mode 100644 index 0000000000..c921fa2d79 --- /dev/null +++ b/crates/nu_plugin_polars/src/dataframe/command/string/mod.rs @@ -0,0 +1,36 @@ +mod concat_str; +mod contains; +mod replace; +mod replace_all; +mod str_join; +mod str_lengths; +mod str_slice; +mod to_lowercase; +mod to_uppercase; + +use crate::PolarsPlugin; +use nu_plugin::PluginCommand; + +pub use concat_str::ExprConcatStr; +pub use contains::Contains; +pub use replace::Replace; +pub use replace_all::ReplaceAll; +pub use str_join::StrJoin; +pub use str_lengths::StrLengths; +pub use str_slice::StrSlice; +pub use to_lowercase::ToLowerCase; +pub use to_uppercase::ToUpperCase; + +pub(crate) fn string_commands() -> Vec>> { + vec![ + Box::new(ExprConcatStr), + Box::new(Contains), + Box::new(Replace), + Box::new(ReplaceAll), + Box::new(StrJoin), + Box::new(StrLengths), + Box::new(StrSlice), + Box::new(ToLowerCase), + Box::new(ToUpperCase), + ] +} diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/replace.rs b/crates/nu_plugin_polars/src/dataframe/command/string/replace.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/replace.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/replace.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/replace_all.rs b/crates/nu_plugin_polars/src/dataframe/command/string/replace_all.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/replace_all.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/replace_all.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/str_join.rs b/crates/nu_plugin_polars/src/dataframe/command/string/str_join.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/str_join.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/str_join.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/str_lengths.rs b/crates/nu_plugin_polars/src/dataframe/command/string/str_lengths.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/str_lengths.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/str_lengths.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/str_slice.rs b/crates/nu_plugin_polars/src/dataframe/command/string/str_slice.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/str_slice.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/str_slice.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/to_lowercase.rs b/crates/nu_plugin_polars/src/dataframe/command/string/to_lowercase.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/to_lowercase.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/to_lowercase.rs diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/to_uppercase.rs b/crates/nu_plugin_polars/src/dataframe/command/string/to_uppercase.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/series/string/to_uppercase.rs rename to crates/nu_plugin_polars/src/dataframe/command/string/to_uppercase.rs diff --git a/crates/nu_plugin_polars/src/dataframe/stub.rs b/crates/nu_plugin_polars/src/dataframe/command/stub.rs similarity index 100% rename from crates/nu_plugin_polars/src/dataframe/stub.rs rename to crates/nu_plugin_polars/src/dataframe/command/stub.rs diff --git a/crates/nu_plugin_polars/src/dataframe/eager/mod.rs b/crates/nu_plugin_polars/src/dataframe/eager/mod.rs deleted file mode 100644 index 1dea968efe..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/eager/mod.rs +++ /dev/null @@ -1,88 +0,0 @@ -mod append; -mod cast; -mod columns; -mod drop; -mod drop_duplicates; -mod drop_nulls; -mod dummies; -mod filter_with; -mod first; -mod get; -mod last; -mod open; -mod pivot; -mod query_df; -mod rename; -mod sample; -mod save; -mod schema; -mod shape; -mod slice; -mod sql_context; -mod sql_expr; -mod summary; -mod take; -mod to_df; -mod to_nu; -mod unpivot; -mod with_column; - -use crate::PolarsPlugin; - -pub use self::open::OpenDataFrame; -pub use append::AppendDF; -pub use cast::CastDF; -pub use columns::ColumnsDF; -pub use drop::DropDF; -pub use drop_duplicates::DropDuplicates; -pub use drop_nulls::DropNulls; -pub use dummies::Dummies; -pub use filter_with::FilterWith; -pub use first::FirstDF; -pub use get::GetDF; -pub use last::LastDF; -use nu_plugin::PluginCommand; -pub use query_df::QueryDf; -pub use rename::RenameDF; -pub use sample::SampleDF; -pub use schema::SchemaCmd; -pub use shape::ShapeDF; -pub use slice::SliceDF; -pub use sql_context::SQLContext; -pub use summary::Summary; -pub use take::TakeDF; -pub use to_df::ToDataFrame; -pub use to_nu::ToNu; -pub use unpivot::UnpivotDF; -pub use with_column::WithColumn; - -pub(crate) fn eager_commands() -> Vec>> { - vec![ - Box::new(AppendDF), - Box::new(CastDF), - Box::new(ColumnsDF), - Box::new(DropDF), - Box::new(DropDuplicates), - Box::new(DropNulls), - Box::new(Dummies), - Box::new(FilterWith), - Box::new(GetDF), - Box::new(OpenDataFrame), - Box::new(pivot::PivotDF), - Box::new(UnpivotDF), - Box::new(Summary), - Box::new(FirstDF), - Box::new(LastDF), - Box::new(RenameDF), - Box::new(SampleDF), - Box::new(ShapeDF), - Box::new(SliceDF), - Box::new(SchemaCmd), - Box::new(TakeDF), - Box::new(ToNu), - Box::new(ToDataFrame), - Box::new(QueryDf), - Box::new(WithColumn), - Box::new(save::SaveDF), - ] -} diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/mod.rs b/crates/nu_plugin_polars/src/dataframe/expressions/mod.rs deleted file mode 100644 index 055b836dac..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/expressions/mod.rs +++ /dev/null @@ -1,48 +0,0 @@ -mod alias; -mod arg_where; -mod col; -mod concat_str; -mod datepart; -mod expressions_macro; -mod is_in; -mod lit; -mod otherwise; -mod when; - -use nu_plugin::PluginCommand; - -pub use crate::dataframe::expressions::alias::ExprAlias; -pub use crate::dataframe::expressions::arg_where::ExprArgWhere; -pub use crate::dataframe::expressions::col::ExprCol; -pub use crate::dataframe::expressions::concat_str::ExprConcatStr; -pub use crate::dataframe::expressions::datepart::ExprDatePart; -pub use crate::dataframe::expressions::expressions_macro::*; -pub use crate::dataframe::expressions::is_in::ExprIsIn; -pub use crate::dataframe::expressions::lit::ExprLit; -pub use crate::dataframe::expressions::otherwise::ExprOtherwise; -pub use crate::dataframe::expressions::when::ExprWhen; -use crate::PolarsPlugin; - -pub(crate) fn expr_commands() -> Vec>> { - vec![ - Box::new(ExprAlias), - Box::new(ExprArgWhere), - Box::new(ExprAggGroups), - Box::new(ExprCol), - Box::new(ExprConcatStr), - Box::new(ExprCount), - Box::new(ExprDatePart), - Box::new(ExprIsIn), - Box::new(ExprList), - Box::new(ExprLit), - Box::new(ExprNot), - Box::new(ExprMax), - Box::new(ExprMin), - Box::new(ExprOtherwise), - Box::new(ExprSum), - Box::new(ExprMean), - Box::new(ExprStd), - Box::new(ExprVar), - Box::new(ExprWhen), - ] -} diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/macro_commands.rs b/crates/nu_plugin_polars/src/dataframe/lazy/macro_commands.rs deleted file mode 100644 index eb63f8b9f2..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/lazy/macro_commands.rs +++ /dev/null @@ -1,228 +0,0 @@ -/// Definition of multiple lazyframe commands using a macro rule -/// All of these commands have an identical body and only require -/// to have a change in the name, description and function -use crate::dataframe::values::{Column, NuDataFrame, NuLazyFrame}; -use crate::values::CustomValueSupport; -use crate::PolarsPlugin; -use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; -use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Span, Type, Value}; - -macro_rules! lazy_command { - ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident) => { - #[derive(Clone)] - pub struct $command; - - impl PluginCommand for $command { - type Plugin = PolarsPlugin; - - fn name(&self) -> &str { - $name - } - - fn description(&self) -> &str { - $desc - } - - fn signature(&self) -> Signature { - Signature::build(self.name()) - .description($desc) - .input_output_type( - Type::Custom("dataframe".into()), - Type::Custom("dataframe".into()), - ) - .category(Category::Custom("lazyframe".into())) - } - - fn examples(&self) -> Vec { - $examples - } - - fn run( - &self, - plugin: &Self::Plugin, - engine: &EngineInterface, - call: &EvaluatedCall, - input: PipelineData, - ) -> Result { - let lazy = NuLazyFrame::try_from_pipeline_coerce(plugin, input, call.head) - .map_err(LabeledError::from)?; - let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().$func()); - lazy.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } - } - - #[cfg(test)] - mod $test { - use super::*; - use crate::test::test_polars_plugin_command; - use nu_protocol::ShellError; - - #[test] - fn test_examples() -> Result<(), ShellError> { - test_polars_plugin_command(&$command) - } - } - }; - - ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident, $test: ident, $ddot: expr) => { - #[derive(Clone)] - pub struct $command; - - impl PluginCommand for $command { - type Plugin = PolarsPlugin; - - fn signature(&self) -> Signature { - Signature::build($name) - .description($desc) - .input_output_type( - Type::Custom("dataframe".into()), - Type::Custom("dataframe".into()), - ) - .category(Category::Custom("lazyframe".into())) - .plugin_examples($examples) - } - - fn run( - &self, - _plugin: &Self::Plugin, - engine: &EngineInterface, - call: &EvaluatedCall, - input: PipelineData, - ) -> Result { - let lazy = NuLazyFrame::try_from_pipeline_coerce(plugin, input, call.head) - .map_err(LabeledError::from)?; - let lazy = NuLazyFrame::new(lazy.from_eager, lazy.into_polars().$func($ddot)); - lazy.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } - } - - #[cfg(test)] - mod $test { - use super::*; - use crate::test::test_polars_plugin_command; - use nu_protocol::ShellError; - - #[test] - fn test_examples() -> Result<(), ShellError> { - test_polars_plugin_command(&$command) - } - } - }; - - ($command: ident, $name: expr, $desc: expr, $examples: expr, $func: ident?, $test: ident) => { - #[derive(Clone)] - pub struct $command; - - impl PluginCommand for $command { - type Plugin = PolarsPlugin; - - fn name(&self) -> &str { - $name - } - - fn description(&self) -> &str { - $desc - } - fn signature(&self) -> Signature { - Signature::build(self.name()) - .input_output_type( - Type::Custom("dataframe".into()), - Type::Custom("dataframe".into()), - ) - .category(Category::Custom("lazyframe".into())) - } - - fn examples(&self) -> Vec { - $examples - } - - fn run( - &self, - plugin: &Self::Plugin, - engine: &EngineInterface, - call: &EvaluatedCall, - input: PipelineData, - ) -> Result { - let lazy = NuLazyFrame::try_from_pipeline_coerce(plugin, input, call.head) - .map_err(LabeledError::from)?; - - let lazy = NuLazyFrame::new( - lazy.from_eager, - lazy.to_polars() - .$func() - .map_err(|e| ShellError::GenericError { - error: "Dataframe Error".into(), - msg: e.to_string(), - help: None, - span: None, - inner: vec![], - }) - .map_err(LabeledError::from)?, - ); - - lazy.to_pipeline_data(plugin, engine, call.head) - .map_err(LabeledError::from) - } - } - - #[cfg(test)] - mod $test { - use super::*; - use crate::test::test_polars_plugin_command; - use nu_protocol::ShellError; - - #[test] - fn test_examples() -> Result<(), ShellError> { - test_polars_plugin_command(&$command) - } - } - }; -} - -// LazyReverse command -// Expands to a command definition for reverse -lazy_command!( - LazyReverse, - "polars reverse", - "Reverses the LazyFrame", - vec![Example { - description: "Reverses the dataframe.", - example: "[[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars reverse", - result: Some( - NuDataFrame::try_from_columns( - vec![ - Column::new( - "a".to_string(), - vec![Value::test_int(2), Value::test_int(4), Value::test_int(6),], - ), - Column::new( - "b".to_string(), - vec![Value::test_int(2), Value::test_int(2), Value::test_int(2),], - ), - ], - None - ) - .expect("simple df for test should not fail") - .into_value(Span::test_data()), - ), - },], - reverse, - test_reverse -); - -// LazyCache command -// Expands to a command definition for cache -lazy_command!( - LazyCache, - "polars cache", - "Caches operations in a new LazyFrame.", - vec![Example { - description: "Caches the result into a new LazyFrame", - example: "[[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars reverse | polars cache", - result: None, - }], - cache, - test_cache -); diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/mod.rs b/crates/nu_plugin_polars/src/dataframe/lazy/mod.rs deleted file mode 100644 index e70143e6ce..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/lazy/mod.rs +++ /dev/null @@ -1,57 +0,0 @@ -mod aggregate; -mod collect; -mod explode; -mod fetch; -mod fill_nan; -mod fill_null; -mod filter; -mod flatten; -pub mod groupby; -mod join; -mod macro_commands; -mod median; -mod quantile; -mod select; -mod sort_by_expr; -mod to_lazy; - -use nu_plugin::PluginCommand; - -pub use crate::dataframe::lazy::aggregate::LazyAggregate; -pub use crate::dataframe::lazy::collect::LazyCollect; -use crate::dataframe::lazy::fetch::LazyFetch; -use crate::dataframe::lazy::fill_nan::LazyFillNA; -pub use crate::dataframe::lazy::fill_null::LazyFillNull; -use crate::dataframe::lazy::filter::LazyFilter; -use crate::dataframe::lazy::groupby::ToLazyGroupBy; -use crate::dataframe::lazy::join::LazyJoin; -pub(crate) use crate::dataframe::lazy::macro_commands::*; -use crate::dataframe::lazy::quantile::LazyQuantile; -pub(crate) use crate::dataframe::lazy::select::LazySelect; -use crate::dataframe::lazy::sort_by_expr::LazySortBy; -pub use crate::dataframe::lazy::to_lazy::ToLazyFrame; -use crate::PolarsPlugin; -pub use explode::LazyExplode; -pub use flatten::LazyFlatten; - -pub(crate) fn lazy_commands() -> Vec>> { - vec![ - Box::new(LazyAggregate), - Box::new(LazyCache), - Box::new(LazyCollect), - Box::new(LazyExplode), - Box::new(LazyFetch), - Box::new(LazyFillNA), - Box::new(LazyFillNull), - Box::new(LazyFilter), - Box::new(LazyFlatten), - Box::new(LazyJoin), - Box::new(median::LazyMedian), - Box::new(LazyReverse), - Box::new(LazySelect), - Box::new(LazySortBy), - Box::new(LazyQuantile), - Box::new(ToLazyFrame), - Box::new(ToLazyGroupBy), - ] -} diff --git a/crates/nu_plugin_polars/src/dataframe/mod.rs b/crates/nu_plugin_polars/src/dataframe/mod.rs index e41ef5bc1a..6899b8beee 100644 --- a/crates/nu_plugin_polars/src/dataframe/mod.rs +++ b/crates/nu_plugin_polars/src/dataframe/mod.rs @@ -1,10 +1,6 @@ use nu_protocol::{ShellError, Span}; -pub mod eager; -pub mod expressions; -pub mod lazy; -pub mod series; -pub mod stub; +pub mod command; mod utils; pub mod values; diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/mod.rs b/crates/nu_plugin_polars/src/dataframe/series/date/mod.rs deleted file mode 100644 index ed3895a172..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/series/date/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -mod as_date; -mod as_datetime; -mod get_day; -mod get_hour; -mod get_minute; -mod get_month; -mod get_nanosecond; -mod get_ordinal; -mod get_second; -mod get_week; -mod get_weekday; -mod get_year; - -pub use as_date::AsDate; -pub use as_datetime::AsDateTime; -pub use get_day::GetDay; -pub use get_hour::GetHour; -pub use get_minute::GetMinute; -pub use get_month::GetMonth; -pub use get_nanosecond::GetNanosecond; -pub use get_ordinal::GetOrdinal; -pub use get_second::GetSecond; -pub use get_week::GetWeek; -pub use get_weekday::GetWeekDay; -pub use get_year::GetYear; diff --git a/crates/nu_plugin_polars/src/dataframe/series/indexes/mod.rs b/crates/nu_plugin_polars/src/dataframe/series/indexes/mod.rs deleted file mode 100644 index c0af8c8653..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/series/indexes/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -mod arg_sort; -mod arg_true; -mod arg_unique; -mod set_with_idx; - -pub use arg_sort::ArgSort; -pub use arg_true::ArgTrue; -pub use arg_unique::ArgUnique; -pub use set_with_idx::SetWithIndex; diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/mod.rs b/crates/nu_plugin_polars/src/dataframe/series/masks/mod.rs deleted file mode 100644 index 985b14eaec..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/series/masks/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -mod is_duplicated; -mod is_not_null; -mod is_null; -mod is_unique; -mod not; -mod set; - -pub use is_duplicated::IsDuplicated; -pub use is_not_null::IsNotNull; -pub use is_null::IsNull; -pub use is_unique::IsUnique; -pub use not::NotSeries; -pub use set::SetSeries; diff --git a/crates/nu_plugin_polars/src/dataframe/series/mod.rs b/crates/nu_plugin_polars/src/dataframe/series/mod.rs deleted file mode 100644 index d4328937f6..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/series/mod.rs +++ /dev/null @@ -1,87 +0,0 @@ -mod date; -pub use date::*; - -mod string; -pub use string::*; - -mod masks; -pub use masks::*; - -mod indexes; -pub use indexes::*; - -mod all_false; -mod all_true; -mod arg_max; -mod arg_min; -mod cumulative; -mod n_null; -mod n_unique; -mod rolling; -mod shift; -mod unique; -mod value_counts; - -pub use all_false::AllFalse; -use nu_plugin::PluginCommand; - -use crate::PolarsPlugin; -pub use all_true::AllTrue; -pub use arg_max::ArgMax; -pub use arg_min::ArgMin; -pub use cumulative::Cumulative; -pub use n_null::NNull; -pub use n_unique::NUnique; -pub use rolling::Rolling; -pub use shift::Shift; -pub use unique::Unique; -pub use value_counts::ValueCount; - -pub(crate) fn series_commands() -> Vec>> { - vec![ - Box::new(AllFalse), - Box::new(AllTrue), - Box::new(ArgMax), - Box::new(ArgMin), - Box::new(ArgSort), - Box::new(ArgTrue), - Box::new(ArgUnique), - Box::new(AsDate), - Box::new(AsDateTime), - Box::new(Contains), - Box::new(Cumulative), - Box::new(GetDay), - Box::new(GetHour), - Box::new(GetMinute), - Box::new(GetMonth), - Box::new(GetNanosecond), - Box::new(GetOrdinal), - Box::new(GetSecond), - Box::new(GetWeek), - Box::new(GetWeekDay), - Box::new(GetYear), - Box::new(IsDuplicated), - Box::new(IsNotNull), - Box::new(IsNull), - Box::new(IsUnique), - Box::new(NNull), - Box::new(NUnique), - Box::new(NotSeries), - Box::new(Replace), - Box::new(ReplaceAll), - Box::new(Rolling), - Box::new(SetSeries), - Box::new(SetWithIndex), - Box::new(Shift), - Box::new(StrJoin), - Box::new(StrLengths), - Box::new(StrSlice), - Box::new(StrFTime), - Box::new(ToDecimal), - Box::new(ToInteger), - Box::new(ToLowerCase), - Box::new(ToUpperCase), - Box::new(Unique), - Box::new(ValueCount), - ] -} diff --git a/crates/nu_plugin_polars/src/dataframe/series/string/mod.rs b/crates/nu_plugin_polars/src/dataframe/series/string/mod.rs deleted file mode 100644 index 0aa758c7aa..0000000000 --- a/crates/nu_plugin_polars/src/dataframe/series/string/mod.rs +++ /dev/null @@ -1,23 +0,0 @@ -mod contains; -mod replace; -mod replace_all; -mod str_join; -mod str_lengths; -mod str_slice; -mod strftime; -mod to_decimal; -mod to_integer; -mod to_lowercase; -mod to_uppercase; - -pub use contains::Contains; -pub use replace::Replace; -pub use replace_all::ReplaceAll; -pub use str_join::StrJoin; -pub use str_lengths::StrLengths; -pub use str_slice::StrSlice; -pub use strftime::StrFTime; -pub use to_decimal::ToDecimal; -pub use to_integer::ToInteger; -pub use to_lowercase::ToLowerCase; -pub use to_uppercase::ToUpperCase; diff --git a/crates/nu_plugin_polars/src/lib.rs b/crates/nu_plugin_polars/src/lib.rs index e4d627e4db..c537ef53fe 100644 --- a/crates/nu_plugin_polars/src/lib.rs +++ b/crates/nu_plugin_polars/src/lib.rs @@ -2,7 +2,11 @@ use std::cmp::Ordering; use cache::cache_commands; pub use cache::{Cache, Cacheable}; -use dataframe::{stub::PolarsCmd, values::CustomValueType}; +use command::{ + aggregation::aggregation_commands, boolean::boolean_commands, core::core_commands, + data::data_commands, datetime::datetime_commands, index::index_commands, + integer::integer_commands, string::string_commands, stub::PolarsCmd, +}; use log::debug; use nu_plugin::{EngineInterface, Plugin, PluginCommand}; @@ -10,11 +14,9 @@ mod cache; pub mod dataframe; pub use dataframe::*; use nu_protocol::{ast::Operator, CustomValue, LabeledError, ShellError, Span, Spanned, Value}; +use values::CustomValueType; -use crate::{ - eager::eager_commands, expressions::expr_commands, lazy::lazy_commands, - series::series_commands, values::PolarsPluginCustomValue, -}; +use crate::values::PolarsPluginCustomValue; pub trait EngineWrapper { fn get_env_var(&self, key: &str) -> Option; @@ -61,10 +63,16 @@ impl Plugin for PolarsPlugin { fn commands(&self) -> Vec>> { let mut commands: Vec>> = vec![Box::new(PolarsCmd)]; - commands.append(&mut eager_commands()); - commands.append(&mut lazy_commands()); - commands.append(&mut expr_commands()); - commands.append(&mut series_commands()); + + commands.append(&mut aggregation_commands()); + commands.append(&mut boolean_commands()); + commands.append(&mut core_commands()); + commands.append(&mut data_commands()); + commands.append(&mut datetime_commands()); + commands.append(&mut index_commands()); + commands.append(&mut integer_commands()); + commands.append(&mut string_commands()); + commands.append(&mut cache_commands()); commands }