Deprecate group in favor of chunks (#13377)

# Description
The name of the `group` command is a little unclear/ambiguous.
Everything I look at it, I think of `group-by`. I think `chunks` more
clearly conveys what the `group` command does. Namely, it divides the
input list into chunks of a certain size. For example,
[`slice::chunks`](https://doc.rust-lang.org/std/primitive.slice.html#method.chunks)
has the same name. So, this PR adds a new `chunks` command to replace
the now deprecated `group` command.

The `chunks` command is a refactored version of `group`. As such, there
is a small performance improvement:
```nushell
# $data is a very large list
> bench { $data | chunks 2 } --rounds 30 | get mean
474ms 921µs 190ns

# deprecation warning was disabled here for fairness
> bench { $data | group 2 } --rounds 30 | get mean
592ms 702µs 440ns



> bench { $data | chunks 200 } --rounds 30 | get mean
374ms 188µs 318ns

> bench { $data | group 200 } --rounds 30 | get mean
481ms 264µs 869ns 



> bench { $data | chunks 1 } --rounds 30 | get mean
642ms 574µs 42ns

> bench { $data | group 1 } --rounds 30 | get mean
981ms 602µs 513ns
```

# User-Facing Changes
- `group` command has been deprecated in favor of new `chunks` command.
- `chunks` errors when given a chunk size of `0` whereas `group` returns
chunks with one element.

# Tests + Formatting
Added tests for `chunks`, since `group` did not have any tests.

# After Submitting
Update book if necessary.
This commit is contained in:
Ian Manske
2024-07-16 03:49:00 +00:00
committed by GitHub
parent 3d1145e759
commit 0918050ac8
11 changed files with 269 additions and 4 deletions

View File

@ -47,6 +47,26 @@ pub fn report_error_new(
report_error(&working_set, error);
}
pub fn report_warning(
working_set: &StateWorkingSet,
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) {
eprintln!("Warning: {:?}", CliError(error, working_set));
// reset vt processing, aka ansi because illbehaved externals can break it
#[cfg(windows)]
{
let _ = nu_utils::enable_vt_processing();
}
}
pub fn report_warning_new(
engine_state: &EngineState,
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, error);
}
impl std::fmt::Debug for CliError<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let config = self.1.get_config();

View File

@ -5,7 +5,9 @@ mod parse_error;
mod parse_warning;
mod shell_error;
pub use cli_error::{format_error, report_error, report_error_new};
pub use cli_error::{
format_error, report_error, report_error_new, report_warning, report_warning_new,
};
pub use compile_error::CompileError;
pub use labeled_error::{ErrorLabel, LabeledError};
pub use parse_error::{DidYouMean, ParseError};

View File

@ -6,11 +6,11 @@ use thiserror::Error;
#[derive(Clone, Debug, Error, Diagnostic, Serialize, Deserialize)]
pub enum ParseWarning {
#[error("Deprecated: {old_command}")]
#[diagnostic(help("for more info: {url}"))]
#[diagnostic(help("for more info see {url}"))]
DeprecatedWarning {
old_command: String,
new_suggestion: String,
#[label("`{old_command}` is deprecated and will be removed in a future release. Please {new_suggestion} instead")]
#[label("`{old_command}` is deprecated and will be removed in a future release. Please {new_suggestion} instead.")]
span: Span,
url: String,
},

View File

@ -635,6 +635,34 @@ impl PipelineData {
Ok(None)
}
}
pub fn unsupported_input_error(
self,
expected_type: impl Into<String>,
span: Span,
) -> ShellError {
match self {
PipelineData::Empty => ShellError::PipelineEmpty { dst_span: span },
PipelineData::Value(value, ..) => ShellError::OnlySupportsThisInputType {
exp_input_type: expected_type.into(),
wrong_type: value.get_type().get_non_specified_string(),
dst_span: span,
src_span: value.span(),
},
PipelineData::ListStream(stream, ..) => ShellError::OnlySupportsThisInputType {
exp_input_type: expected_type.into(),
wrong_type: "list (stream)".into(),
dst_span: span,
src_span: stream.span(),
},
PipelineData::ByteStream(stream, ..) => ShellError::OnlySupportsThisInputType {
exp_input_type: expected_type.into(),
wrong_type: stream.type_().describe().into(),
dst_span: span,
src_span: stream.span(),
},
}
}
}
enum PipelineIteratorInner {

View File

@ -36,6 +36,10 @@ pub enum Type {
}
impl Type {
pub fn list(inner: Type) -> Self {
Self::List(Box::new(inner))
}
pub fn record() -> Self {
Self::Record([].into())
}