forked from extern/nushell
# Description This doesn't really do much that the user could see, but it helps get us ready to do the steps of the refactor to split the span off of Value, so that values can be spanless. This allows us to have top-level values that can hold both a Value and a Span, without requiring that all values have them. We expect to see significant memory reduction by removing so many unnecessary spans from values. For example, a table of 100,000 rows and 5 columns would have a savings of ~8megs in just spans that are almost always duplicated. # User-Facing Changes Nothing yet # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
196 lines
5.7 KiB
Rust
196 lines
5.7 KiB
Rust
mod custom_value;
|
|
|
|
use super::{NuDataFrame, NuExpression};
|
|
use core::fmt;
|
|
use nu_protocol::{PipelineData, ShellError, Span, Value};
|
|
use polars::prelude::{Expr, IntoLazy, LazyFrame, Schema};
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
// Lazyframe wrapper for Nushell operations
|
|
// Polars LazyFrame is behind and Option to allow easy implementation of
|
|
// the Deserialize trait
|
|
#[derive(Default)]
|
|
pub struct NuLazyFrame {
|
|
pub lazy: Option<LazyFrame>,
|
|
pub schema: Option<Schema>,
|
|
pub from_eager: bool,
|
|
}
|
|
|
|
// Mocked serialization of the LazyFrame object
|
|
impl Serialize for NuLazyFrame {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
serializer.serialize_none()
|
|
}
|
|
}
|
|
|
|
// Mocked deserialization of the LazyFrame object
|
|
impl<'de> Deserialize<'de> for NuLazyFrame {
|
|
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
{
|
|
Ok(NuLazyFrame::default())
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for NuLazyFrame {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "NuLazyframe")
|
|
}
|
|
}
|
|
|
|
// Referenced access to the real LazyFrame
|
|
impl AsRef<LazyFrame> for NuLazyFrame {
|
|
fn as_ref(&self) -> &polars::prelude::LazyFrame {
|
|
// The only case when there cannot be a lazy frame is if it is created
|
|
// using the default function or if created by deserializing something
|
|
self.lazy.as_ref().expect("there should always be a frame")
|
|
}
|
|
}
|
|
|
|
impl AsMut<LazyFrame> for NuLazyFrame {
|
|
fn as_mut(&mut self) -> &mut polars::prelude::LazyFrame {
|
|
// The only case when there cannot be a lazy frame is if it is created
|
|
// using the default function or if created by deserializing something
|
|
self.lazy.as_mut().expect("there should always be a frame")
|
|
}
|
|
}
|
|
|
|
impl From<LazyFrame> for NuLazyFrame {
|
|
fn from(lazy_frame: LazyFrame) -> Self {
|
|
Self {
|
|
lazy: Some(lazy_frame),
|
|
from_eager: false,
|
|
schema: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NuLazyFrame {
|
|
pub fn new(from_eager: bool, lazy: LazyFrame) -> Self {
|
|
Self {
|
|
lazy: Some(lazy),
|
|
from_eager,
|
|
schema: None,
|
|
}
|
|
}
|
|
|
|
pub fn from_dataframe(df: NuDataFrame) -> Self {
|
|
let lazy = df.as_ref().clone().lazy();
|
|
Self {
|
|
lazy: Some(lazy),
|
|
from_eager: true,
|
|
schema: Some(df.as_ref().schema()),
|
|
}
|
|
}
|
|
|
|
pub fn into_value(self, span: Span) -> Result<Value, ShellError> {
|
|
if self.from_eager {
|
|
let df = self.collect(span)?;
|
|
Ok(Value::CustomValue {
|
|
val: Box::new(df),
|
|
span,
|
|
})
|
|
} else {
|
|
Ok(Value::CustomValue {
|
|
val: Box::new(self),
|
|
span,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub fn into_polars(self) -> LazyFrame {
|
|
self.lazy.expect("lazyframe cannot be none to convert")
|
|
}
|
|
|
|
pub fn collect(self, span: Span) -> Result<NuDataFrame, ShellError> {
|
|
self.lazy
|
|
.expect("No empty lazy for collect")
|
|
.collect()
|
|
.map_err(|e| {
|
|
ShellError::GenericError(
|
|
"Error collecting lazy frame".to_string(),
|
|
e.to_string(),
|
|
Some(span),
|
|
None,
|
|
Vec::new(),
|
|
)
|
|
})
|
|
.map(|df| NuDataFrame {
|
|
df,
|
|
from_lazy: !self.from_eager,
|
|
})
|
|
}
|
|
|
|
pub fn try_from_value(value: Value) -> Result<Self, ShellError> {
|
|
if Self::can_downcast(&value) {
|
|
Ok(Self::get_lazy_df(value)?)
|
|
} else if NuDataFrame::can_downcast(&value) {
|
|
let df = NuDataFrame::try_from_value(value)?;
|
|
Ok(NuLazyFrame::from_dataframe(df))
|
|
} else {
|
|
Err(ShellError::CantConvert {
|
|
to_type: "lazy or eager dataframe".into(),
|
|
from_type: value.get_type().to_string(),
|
|
span: value.span(),
|
|
help: None,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub fn try_from_pipeline(input: PipelineData, span: Span) -> Result<Self, ShellError> {
|
|
let value = input.into_value(span);
|
|
Self::try_from_value(value)
|
|
}
|
|
|
|
pub fn get_lazy_df(value: Value) -> Result<Self, ShellError> {
|
|
match value {
|
|
Value::CustomValue { val, span } => match val.as_any().downcast_ref::<Self>() {
|
|
Some(expr) => Ok(Self {
|
|
lazy: expr.lazy.clone(),
|
|
from_eager: false,
|
|
schema: None,
|
|
}),
|
|
None => Err(ShellError::CantConvert {
|
|
to_type: "lazy frame".into(),
|
|
from_type: "non-dataframe".into(),
|
|
span,
|
|
help: None,
|
|
}),
|
|
},
|
|
x => Err(ShellError::CantConvert {
|
|
to_type: "lazy frame".into(),
|
|
from_type: x.get_type().to_string(),
|
|
span: x.span(),
|
|
help: None,
|
|
}),
|
|
}
|
|
}
|
|
|
|
pub fn can_downcast(value: &Value) -> bool {
|
|
if let Value::CustomValue { val, .. } = value {
|
|
val.as_any().downcast_ref::<Self>().is_some()
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
pub fn apply_with_expr<F>(self, expr: NuExpression, f: F) -> Self
|
|
where
|
|
F: Fn(LazyFrame, Expr) -> LazyFrame,
|
|
{
|
|
let df = self.lazy.expect("Lazy frame must not be empty to apply");
|
|
let expr = expr.into_polars();
|
|
let new_frame = f(df, expr);
|
|
|
|
Self {
|
|
from_eager: self.from_eager,
|
|
lazy: Some(new_frame),
|
|
schema: None,
|
|
}
|
|
}
|
|
}
|