Update merge to also take single records (#6919)

This commit is contained in:
Leon 2022-10-28 02:00:26 +10:00 committed by GitHub
parent 5add5cbd12
commit f281cd5aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 152 additions and 41 deletions

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
Signature, Span, SyntaxShape, Value, ShellError, Signature, Span, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -15,15 +15,25 @@ impl Command for Merge {
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Merge a table into an input table" "Merge the input with a record or table, overwriting values in matching columns."
}
fn extra_usage(&self) -> &str {
r#"You may provide a column structure to merge, or a block
that generates a column structure.
When merging tables, row 0 of the input table is overwritten
with values from row 0 of the provided table, then
repeating this process with row 1, and so on."#
} }
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("merge") Signature::build("merge")
.required( .required(
"block", "block",
SyntaxShape::Block(Some(vec![])), // Both this and `update` should have a shape more like <record> | <table> | <block> than just <any>. -Leon 2022-10-27
"the block to run and merge into the table", SyntaxShape::Any,
"the new value to merge with, or a block that produces it",
) )
.category(Category::Filters) .category(Category::Filters)
} }
@ -32,7 +42,7 @@ impl Command for Merge {
vec![ vec![
Example { Example {
example: "[a b c] | wrap name | merge { [1 2 3] | wrap index }", example: "[a b c] | wrap name | merge { [1 2 3] | wrap index }",
description: "Merge an index column into the input table", description: "Add an 'index' column to the input table",
result: Some(Value::List { result: Some(Value::List {
vals: vec![ vals: vec![
Value::test_record( Value::test_record(
@ -52,7 +62,7 @@ impl Command for Merge {
}), }),
}, },
Example { Example {
example: "{a: 1, b: 2} | merge { {c: 3} }", example: "{a: 1, b: 2} | merge {c: 3}",
description: "Merge two records", description: "Merge two records",
result: Some(Value::test_record( result: Some(Value::test_record(
vec!["a", "b", "c"], vec!["a", "b", "c"],
@ -60,8 +70,8 @@ impl Command for Merge {
)), )),
}, },
Example { Example {
example: "{a: 1, b: 3} | merge { {b: 2, c: 4} }", example: "{a: 1, b: 3} | merge { { b: 2 } | merge { c: 4 } }",
description: "Merge two records with overlap key", description: "Merge records, overwriting overlapping values",
result: Some(Value::test_record( result: Some(Value::test_record(
vec!["a", "b", "c"], vec!["a", "b", "c"],
vec![Value::test_int(1), Value::test_int(2), Value::test_int(4)], vec![Value::test_int(1), Value::test_int(2), Value::test_int(4)],
@ -77,35 +87,47 @@ impl Command for Merge {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let block: CaptureBlock = call.req(engine_state, stack, 0)?; let replacement: Value = call.req(engine_state, stack, 0)?;
let mut stack = stack.captures_to_stack(&block.captures);
let metadata = input.metadata(); let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
let block = engine_state.get_block(block.block_id);
let call = call.clone(); let call = call.clone();
let result = eval_block( let argument_was_block = replacement.as_block().is_ok();
engine_state,
&mut stack,
block,
PipelineData::new(call.head),
call.redirect_stdout,
call.redirect_stderr,
);
let table = match result { let merge_value: Value = if argument_was_block {
Ok(res) => res, // When given a block, run it to obtain the matching value.
Err(e) => return Err(e), let capture_block: CaptureBlock = FromValue::from_value(&replacement)?;
let mut stack = stack.captures_to_stack(&capture_block.captures);
stack.with_env(&stack.env_vars.clone(), &stack.env_hidden.clone());
let block = engine_state.get_block(capture_block.block_id);
let result = eval_block(
engine_state,
&mut stack,
block,
PipelineData::new(call.head),
call.redirect_stdout,
call.redirect_stderr,
);
match result {
Ok(res) => res.into_value(call.head),
Err(e) => return Err(e),
}
} else {
// Otherwise, just use the passed-in value as-is.
replacement
}; };
match (&input, &table) { match (&input, merge_value) {
// table (list of records) // table (list of records)
( (
PipelineData::Value(Value::List { .. }, ..) | PipelineData::ListStream { .. }, PipelineData::Value(Value::List { .. }, ..) | PipelineData::ListStream { .. },
PipelineData::Value(Value::List { .. }, ..) | PipelineData::ListStream { .. }, Value::List { vals, .. },
) => { ) => {
let mut table_iter = table.into_iter(); let mut table_iter = vals.into_iter();
let res = let res =
input input
@ -147,14 +169,11 @@ impl Command for Merge {
}, },
.., ..,
), ),
PipelineData::Value( Value::Record {
Value::Record { cols: to_merge_cols,
cols: to_merge_cols, vals: to_merge_vals,
vals: to_merge_vals, ..
.. },
},
..,
),
) => { ) => {
let (cols, vals) = do_merge( let (cols, vals) = do_merge(
(inp_cols.to_vec(), inp_vals.to_vec()), (inp_cols.to_vec(), inp_vals.to_vec()),
@ -167,21 +186,29 @@ impl Command for Merge {
} }
.into_pipeline_data()) .into_pipeline_data())
} }
(_, PipelineData::Value(val, ..)) | (PipelineData::Value(val, ..), _) => { (PipelineData::Value(val, ..), merge_value) => {
let span = if val.span()? == Span::test_data() { // Only point the "value originates here" arrow at the merge value
// if it was generated from a block. Otherwise, point at the pipeline value. -Leon 2022-10-27
let span = if argument_was_block {
if merge_value.span()? == Span::test_data() {
Span::new(call.head.start, call.head.start)
} else {
merge_value.span()?
}
} else if val.span()? == Span::test_data() {
Span::new(call.head.start, call.head.start) Span::new(call.head.start, call.head.start)
} else { } else {
val.span()? val.span()?
}; };
Err(ShellError::PipelineMismatch( Err(ShellError::PipelineMismatch(
"record or table in both the input and the argument block".to_string(), "input, and argument, to be both record or both table".to_string(),
call.head, call.head,
span, span,
)) ))
} }
_ => Err(ShellError::PipelineMismatch( _ => Err(ShellError::PipelineMismatch(
"record or table in both the input and the argument block".to_string(), "input, and argument, to be both record or both table".to_string(),
call.head, call.head,
Span::new(call.head.start, call.head.start), Span::new(call.head.start, call.head.start),
)), )),

View File

@ -40,7 +40,7 @@ impl Command for Update {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
upsert(engine_state, stack, call, input) update(engine_state, stack, call, input)
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
@ -70,7 +70,7 @@ impl Command for Update {
} }
} }
fn upsert( fn update(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
call: &Call, call: &Call,

View File

@ -38,5 +38,89 @@ fn row() {
)); ));
assert_eq!(actual.out, "2"); assert_eq!(actual.out, "2");
}) });
}
#[test]
fn single_record_no_overwrite() {
assert_eq!(
nu!(
cwd: ".", pipeline(
r#"
{a: 1, b: 5} | merge {c: 2} | to nuon
"#
))
.out,
"{a: 1, b: 5, c: 2}"
);
}
#[test]
fn single_record_overwrite() {
assert_eq!(
nu!(
cwd: ".", pipeline(
r#"
{a: 1, b: 2} | merge {a: 2} | to nuon
"#
))
.out,
"{a: 2, b: 2}"
);
}
#[test]
fn single_row_table_overwrite() {
assert_eq!(
nu!(
cwd: ".", pipeline(
r#"
[[a b]; [1 4]] | merge [[a b]; [2 4]] | to nuon
"#
))
.out,
"[[a, b]; [2, 4]]"
);
}
#[test]
fn single_row_table_no_overwrite() {
assert_eq!(
nu!(
cwd: ".", pipeline(
r#"
[[a b]; [1 4]] | merge [[c d]; [2 4]] | to nuon
"#
))
.out,
"[[a, b, c, d]; [1, 4, 2, 4]]"
);
}
#[test]
fn multi_row_table_no_overwrite() {
assert_eq!(
nu!(
cwd: ".", pipeline(
r#"
[[a b]; [1 4] [8 9] [9 9]] | merge [[c d]; [2 4]] | to nuon
"#
))
.out,
"[{a: 1, b: 4, c: 2, d: 4}, {a: 8, b: 9}, {a: 9, b: 9}]"
);
}
#[test]
fn multi_row_table_overwrite() {
assert_eq!(
nu!(
cwd: ".", pipeline(
r#"
[[a b]; [1 4] [8 9] [9 9]] | merge [[a b]; [7 7]] | to nuon
"#
))
.out,
"[[a, b]; [7, 7], [8, 9], [9, 9]]"
);
} }