Cleaning up to_pipe_line_data and cache_and_to_value, making them part of CustomValueSupport (#12528)

# Description

This is just some cleanup. I moved to_pipeline_data and to_cache_value
to the CustomValueSupport trait, where I should've put them to begin
with.

Co-authored-by: Jack Wright <jack.wright@disqo.com>
This commit is contained in:
Jack Wright
2024-04-16 04:35:52 -07:00
committed by GitHub
parent c9e9b138eb
commit 1661bb68f9
88 changed files with 275 additions and 419 deletions

View File

@ -1,6 +1,6 @@
use crate::{
dataframe::values::{NuExpression, NuLazyFrame, NuLazyGroupBy},
values::{to_pipeline_data, Column, CustomValueSupport, NuDataFrame},
values::{Column, CustomValueSupport, NuDataFrame},
PolarsPlugin,
};
@ -148,7 +148,8 @@ impl PluginCommand for LazyAggregate {
let polars = group_by.to_polars();
let lazy = NuLazyFrame::new(false, polars.agg(&expressions));
to_pipeline_data(plugin, engine, call.head, lazy).map_err(LabeledError::from)
lazy.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}

View File

@ -1,5 +1,5 @@
use crate::dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame};
use crate::values::{to_pipeline_data, CustomValueSupport, PolarsPluginObject};
use crate::values::{CustomValueSupport, PolarsPluginObject};
use crate::PolarsPlugin;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
@ -151,7 +151,7 @@ pub(crate) fn explode_lazy(
.explode(columns.iter().map(AsRef::as_ref).collect::<Vec<&str>>());
let lazy = NuLazyFrame::from(exploded);
to_pipeline_data(plugin, engine, call.head, lazy)
lazy.to_pipeline_data(plugin, engine, call.head)
}
pub(crate) fn explode_expr(
@ -161,8 +161,9 @@ pub(crate) fn explode_expr(
expr: NuExpression,
) -> Result<PipelineData, ShellError> {
let expr: NuExpression = expr.to_polars().explode().into();
to_pipeline_data(plugin, engine, call.head, expr)
expr.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -1,5 +1,5 @@
use crate::dataframe::values::{Column, NuDataFrame};
use crate::values::{to_pipeline_data, CustomValueSupport, NuLazyFrame};
use crate::values::{CustomValueSupport, NuLazyFrame};
use crate::PolarsPlugin;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
@ -84,7 +84,9 @@ impl PluginCommand for LazyFetch {
// mark this as not from lazy so it doesn't get converted back to a lazy frame
eager.from_lazy = false;
to_pipeline_data(plugin, engine, call.head, eager).map_err(LabeledError::from)
eager
.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}

View File

@ -1,9 +1,6 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuExpression},
values::{
cant_convert_err, to_pipeline_data, CustomValueSupport, PolarsPluginObject,
PolarsPluginType,
},
values::{cant_convert_err, CustomValueSupport, PolarsPluginObject, PolarsPluginType},
PolarsPlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
@ -161,7 +158,7 @@ fn cmd_df(
})
.collect::<Vec<Column>>();
let df = NuDataFrame::try_from_columns(dataframe, None)?;
to_pipeline_data(plugin, engine, call.head, df)
df.to_pipeline_data(plugin, engine, call.head)
}
fn cmd_expr(
@ -173,8 +170,7 @@ fn cmd_expr(
) -> Result<PipelineData, ShellError> {
let fill = NuExpression::try_from_value(plugin, &fill)?.to_polars();
let expr: NuExpression = expr.to_polars().fill_nan(fill).into();
to_pipeline_data(plugin, engine, call.head, expr)
expr.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]

View File

@ -1,9 +1,6 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame},
values::{
cant_convert_err, to_pipeline_data, CustomValueSupport, PolarsPluginObject,
PolarsPluginType,
},
values::{cant_convert_err, CustomValueSupport, PolarsPluginObject, PolarsPluginType},
PolarsPlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
@ -100,7 +97,7 @@ fn cmd_lazy(
) -> Result<PipelineData, ShellError> {
let expr = NuExpression::try_from_value(plugin, &fill)?.to_polars();
let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().fill_null(expr));
to_pipeline_data(plugin, engine, call.head, lazy)
lazy.to_pipeline_data(plugin, engine, call.head)
}
fn cmd_expr(
@ -112,7 +109,7 @@ fn cmd_expr(
) -> Result<PipelineData, ShellError> {
let fill = NuExpression::try_from_value(plugin, &fill)?.to_polars();
let expr: NuExpression = expr.to_polars().fill_null(fill).into();
to_pipeline_data(plugin, engine, call.head, expr)
expr.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]

View File

@ -1,6 +1,6 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame},
values::{to_pipeline_data, CustomValueSupport},
values::CustomValueSupport,
PolarsPlugin,
};
@ -89,7 +89,7 @@ fn command(
lazy.from_eager,
lazy.to_polars().filter(filter_expr.to_polars()),
);
to_pipeline_data(plugin, engine, call.head, lazy)
lazy.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]

View File

@ -1,6 +1,6 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame, NuLazyGroupBy},
values::{to_pipeline_data, CustomValueSupport},
values::CustomValueSupport,
PolarsPlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
@ -153,7 +153,7 @@ fn command(
) -> Result<PipelineData, ShellError> {
let group_by = lazy.to_polars().group_by(expressions);
let group_by = NuLazyGroupBy::new(group_by, lazy.from_eager, lazy.schema()?);
to_pipeline_data(plugin, engine, call.head, group_by)
group_by.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]

View File

@ -1,6 +1,6 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame},
values::{to_pipeline_data, CustomValueSupport},
values::CustomValueSupport,
PolarsPlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
@ -244,7 +244,8 @@ impl PluginCommand for LazyJoin {
.finish();
let lazy = NuLazyFrame::new(from_eager, lazy);
to_pipeline_data(plugin, engine, call.head, lazy).map_err(LabeledError::from)
lazy.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}

View File

@ -2,7 +2,7 @@
/// 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::{to_pipeline_data, CustomValueSupport};
use crate::values::CustomValueSupport;
use crate::PolarsPlugin;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Span, Type, Value};
@ -47,7 +47,8 @@ macro_rules! lazy_command {
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());
to_pipeline_data(plugin, engine, call.head, lazy).map_err(LabeledError::from)
lazy.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}
@ -92,7 +93,8 @@ macro_rules! lazy_command {
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));
to_pipeline_data(plugin, engine, call.head, lazy).map_err(LabeledError::from)
lazy.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}
@ -160,7 +162,8 @@ macro_rules! lazy_command {
.map_err(LabeledError::from)?,
);
to_pipeline_data(plugin, engine, call.head, lazy).map_err(LabeledError::from)
lazy.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}

View File

@ -1,8 +1,7 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuLazyFrame},
values::{
cant_convert_err, to_pipeline_data, CustomValueSupport, NuExpression, PolarsPluginObject,
PolarsPluginType,
cant_convert_err, CustomValueSupport, NuExpression, PolarsPluginObject, PolarsPluginType,
},
PolarsPlugin,
};
@ -96,7 +95,7 @@ impl PluginCommand for LazyMedian {
PolarsPluginObject::NuLazyFrame(lazy) => command(plugin, engine, call, lazy),
PolarsPluginObject::NuExpression(expr) => {
let expr: NuExpression = expr.to_polars().median().into();
to_pipeline_data(plugin, engine, call.head, expr)
expr.to_pipeline_data(plugin, engine, call.head)
}
_ => Err(cant_convert_err(
&value,
@ -128,7 +127,7 @@ fn command(
inner: vec![],
})?;
let lazy = NuLazyFrame::new(lazy.from_eager, polars_lazy);
to_pipeline_data(plugin, engine, call.head, lazy)
lazy.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]

View File

@ -1,8 +1,7 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuLazyFrame},
values::{
cant_convert_err, to_pipeline_data, CustomValueSupport, NuExpression, PolarsPluginObject,
PolarsPluginType,
cant_convert_err, CustomValueSupport, NuExpression, PolarsPluginObject, PolarsPluginType,
},
PolarsPlugin,
};
@ -110,7 +109,7 @@ impl PluginCommand for LazyQuantile {
.to_polars()
.quantile(lit(quantile), QuantileInterpolOptions::default())
.into();
to_pipeline_data(plugin, engine, call.head, expr)
expr.to_pipeline_data(plugin, engine, call.head)
}
_ => Err(cant_convert_err(
&value,
@ -145,7 +144,7 @@ fn command(
})?,
);
to_pipeline_data(plugin, engine, call.head, lazy)
lazy.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]

View File

@ -1,6 +1,6 @@
use crate::{
dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame},
values::{to_pipeline_data, CustomValueSupport},
values::CustomValueSupport,
PolarsPlugin,
};
@ -68,7 +68,8 @@ impl PluginCommand for LazySelect {
let pipeline_value = input.into_value(call.head);
let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?;
let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().select(&expressions));
to_pipeline_data(plugin, engine, call.head, lazy).map_err(LabeledError::from)
lazy.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}

View File

@ -1,7 +1,7 @@
use super::super::values::NuLazyFrame;
use crate::{
dataframe::values::{Column, NuDataFrame, NuExpression},
values::{to_pipeline_data, CustomValueSupport},
values::CustomValueSupport,
PolarsPlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
@ -144,7 +144,8 @@ impl PluginCommand for LazySortBy {
lazy.to_polars()
.sort_by_exprs(&expressions, reverse, nulls_last, maintain_order),
);
to_pipeline_data(plugin, engine, call.head, lazy).map_err(LabeledError::from)
lazy.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
}
}