Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement(stdlib): Allow zip function to take two array parameters #1159

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions benches/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2938,8 +2938,16 @@ bench_function! {
bench_function! {
zip => vrl::stdlib::Zip;

default {
args: func_args![array: value!([["one", "two", "three", "four"], ["one", 2, null, true]])],
one_parameter {
args: func_args![array0: value!([["one", "two", "three", "four"], ["one", 2, null, true]])],
want: Ok(value!([["one","one"], ["two",2], ["three",null], ["four",true]])),
}

two_parameters {
args: func_args![
array0: value!(["one", "two", "three", "four"]),
array1: value!(["one", 2, null, true]),
],
want: Ok(value!([["one","one"], ["two",2], ["three",null], ["four",true]])),
}
}
79 changes: 57 additions & 22 deletions src/stdlib/zip.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use crate::compiler::prelude::*;

fn zip(value: Value) -> Resolved {
fn zip2(value0: Value, value1: Value) -> Resolved {
Ok(value0
.try_array()?
.into_iter()
.zip(value1.try_array()?)
.map(|(v0, v1)| Value::Array(vec![v0, v1]))
.collect())
}

fn zip_all(value: Value) -> Resolved {
Ok(MultiZip(
value
.try_array()?
Expand Down Expand Up @@ -30,19 +39,33 @@ impl Function for Zip {
}

fn parameters(&self) -> &'static [Parameter] {
&[Parameter {
keyword: "array",
kind: kind::ARRAY,
required: true,
}]
&[
Parameter {
keyword: "array0",
kind: kind::ARRAY,
required: true,
},
Parameter {
keyword: "array1",
kind: kind::ARRAY,
required: false,
},
]
}

fn examples(&self) -> &'static [Example] {
&[Example {
title: "merge three arrays into an array of 3-tuples",
source: r#"zip([["a", "b", "c"], [1, null, true], [4, 5, 6]])"#,
result: Ok(r#"[["a", 1, 4], ["b", null, 5], ["c", true, 6]]"#),
}]
&[
Example {
title: "merge an array of three arrays into an array of 3-tuples",
source: r#"zip([["a", "b", "c"], [1, null, true], [4, 5, 6]])"#,
result: Ok(r#"[["a", 1, 4], ["b", null, 5], ["c", true, 6]]"#),
},
Example {
title: "merge two array parameters",
source: "zip([1, 2, 3, 4], [5, 6, 7])",
result: Ok("[[1, 5], [2, 6], [3, 7]]"),
},
]
}

fn compile(
Expand All @@ -51,19 +74,25 @@ impl Function for Zip {
_ctx: &mut FunctionCompileContext,
arguments: ArgumentList,
) -> Compiled {
let array = arguments.required("array");
Ok(ZipFn { array }.as_expr())
let array0 = arguments.required("array0");
let array1 = arguments.optional("array1");
Ok(ZipFn { array0, array1 }.as_expr())
}
}

#[derive(Debug, Clone)]
struct ZipFn {
array: Box<dyn Expression>,
array0: Box<dyn Expression>,
array1: Option<Box<dyn Expression>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
array0: Box<dyn Expression>,
array1: Option<Box<dyn Expression>>,
array_0: Box<dyn Expression>,
array_1: Option<Box<dyn Expression>>,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you say so. Fixed in 5935e89

}

impl FunctionExpression for ZipFn {
fn resolve(&self, ctx: &mut Context) -> Resolved {
zip(self.array.resolve(ctx)?)
let array0 = self.array0.resolve(ctx)?;
match &self.array1 {
None => zip_all(array0),
Some(array1) => zip2(array0, array1.resolve(ctx)?),
}
}

fn type_def(&self, _state: &TypeState) -> TypeDef {
Expand All @@ -81,43 +110,49 @@ mod tests {
zip => Zip;

zips_two_arrays {
args: func_args![array: value!([[1, 2, 3], [4, 5, 6]])],
args: func_args![array0: value!([[1, 2, 3], [4, 5, 6]])],
want: Ok(value!([[1, 4], [2, 5], [3, 6]])),
tdef: TypeDef::array(Collection::any()),
}

zips_three_arrays {
args: func_args![array: value!([[1, 2, 3], [4, 5, 6], [7, 8, 9]])],
args: func_args![array0: value!([[1, 2, 3], [4, 5, 6], [7, 8, 9]])],
want: Ok(value!([[1, 4, 7], [2, 5, 8], [3, 6, 9]])),
tdef: TypeDef::array(Collection::any()),
}

zips_two_parameters {
args: func_args![array0: value!([1, 2, 3]), array1: value!([4, 5, 6])],
want: Ok(value!([[1, 4], [2, 5], [3, 6]])),
tdef: TypeDef::array(Collection::any()),
}

uses_shortest_length1 {
args: func_args![array: value!([[1, 2, 3], [4, 5]])],
args: func_args![array0: value!([[1, 2, 3], [4, 5]])],
want: Ok(value!([[1, 4], [2, 5]])),
tdef: TypeDef::array(Collection::any()),
}

uses_shortest_length2 {
args: func_args![array: value!([[1, 2], [4, 5, 6]])],
args: func_args![array0: value!([[1, 2], [4, 5, 6]])],
want: Ok(value!([[1, 4], [2, 5]])),
tdef: TypeDef::array(Collection::any()),
}

requires_outer_array {
args: func_args![array: 1],
args: func_args![array0: 1],
want: Err("expected array, got integer"),
tdef: TypeDef::array(Collection::any()),
}

requires_inner_arrays1 {
args: func_args![array: value!([true, []])],
args: func_args![array0: value!([true, []])],
want: Err("expected array, got boolean"),
tdef: TypeDef::array(Collection::any()),
}

requires_inner_arrays2 {
args: func_args![array: value!([[], null])],
args: func_args![array0: value!([[], null])],
want: Err("expected array, got null"),
tdef: TypeDef::array(Collection::any()),
}
Expand Down
Loading