Skip to content

Commit

Permalink
Polars command reorg (nushell#13798)
Browse files Browse the repository at this point in the history
# Description
House keeping. Restructures polars modules as discussed in:
https://docs.google.com/spreadsheets/d/1gyA58i_yTXKCJ5DbO_RxBNAlK6S7C1M22ppKwVLZltc/edit?usp=sharing
  • Loading branch information
ayax79 committed Sep 6, 2024
1 parent dd843fa commit 249e1ef
Show file tree
Hide file tree
Showing 127 changed files with 958 additions and 918 deletions.
Original file line number Diff line number Diff line change
@@ -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<Example> {
$examples
}

fn run(
&self,
plugin: &Self::Plugin,
engine: &EngineInterface,
call: &EvaluatedCall,
input: PipelineData,
) -> Result<PipelineData, LabeledError> {
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<PipelineData, LabeledError> {
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<Example> {
$examples
}

fn run(
&self,
plugin: &Self::Plugin,
engine: &EngineInterface,
call: &EvaluatedCall,
input: PipelineData,
) -> Result<PipelineData, LabeledError> {
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<Example> {
$examples
}

fn run(
&self,
plugin: &Self::Plugin,
engine: &EngineInterface,
call: &EvaluatedCall,
input: PipelineData,
) -> Result<PipelineData, LabeledError> {
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!(
Expand Down Expand Up @@ -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!(
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
44 changes: 44 additions & 0 deletions crates/nu_plugin_polars/src/dataframe/command/aggregation/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn PluginCommand<Plugin = PolarsPlugin>>> {
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),
]
}
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
Loading

0 comments on commit 249e1ef

Please sign in to comment.