From b53103c9c7311e2e9f3cb621a6188c0ba0399faf Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 7 Jan 2023 19:40:56 +0000 Subject: [PATCH] Add SQL `array::group()` function --- lib/src/fnc/array.rs | 7 +++++++ lib/src/fnc/mod.rs | 1 + lib/src/sql/function.rs | 2 ++ 3 files changed, 10 insertions(+) diff --git a/lib/src/fnc/array.rs b/lib/src/fnc/array.rs index c42d7d5b..4c6a3ea5 100644 --- a/lib/src/fnc/array.rs +++ b/lib/src/fnc/array.rs @@ -51,6 +51,13 @@ pub fn flatten((arg,): (Value,)) -> Result { }) } +pub fn group((arg,): (Value,)) -> Result { + Ok(match arg { + Value::Array(v) => v.uniq().flatten().into(), + _ => Value::None, + }) +} + pub fn insert((array, value, index): (Value, Value, Option)) -> Result { match (array, index) { (Value::Array(mut v), Some(Value::Number(i))) => { diff --git a/lib/src/fnc/mod.rs b/lib/src/fnc/mod.rs index e13dd1a8..b9749237 100644 --- a/lib/src/fnc/mod.rs +++ b/lib/src/fnc/mod.rs @@ -66,6 +66,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec) -> Result array::difference, "array::distinct" => array::distinct, "array::flatten" => array::flatten, + "array::group" => array::group, "array::insert" => array::insert, "array::intersect" => array::intersect, "array::len" => array::len, diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index a8036124..7fbb0e34 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -79,6 +79,7 @@ impl Function { match self { Self::Normal(f, _) if f == "array::concat" => true, Self::Normal(f, _) if f == "array::distinct" => true, + Self::Normal(f, _) if f == "array::group" => true, Self::Normal(f, _) if f == "array::union" => true, Self::Normal(f, _) if f == "count" => true, Self::Normal(f, _) if f == "math::bottom" => true, @@ -241,6 +242,7 @@ fn function_array(i: &str) -> IResult<&str, &str> { tag("array::difference"), tag("array::distinct"), tag("array::flatten"), + tag("array::group"), tag("array::insert"), tag("array::intersect"), tag("array::len"),