Add SQL array::group() function

This commit is contained in:
Tobie Morgan Hitchcock 2023-01-07 19:40:56 +00:00
parent 513f213a4a
commit b53103c9c7
3 changed files with 10 additions and 0 deletions

View file

@ -51,6 +51,13 @@ pub fn flatten((arg,): (Value,)) -> Result<Value, Error> {
}) })
} }
pub fn group((arg,): (Value,)) -> Result<Value, Error> {
Ok(match arg {
Value::Array(v) => v.uniq().flatten().into(),
_ => Value::None,
})
}
pub fn insert((array, value, index): (Value, Value, Option<Value>)) -> Result<Value, Error> { pub fn insert((array, value, index): (Value, Value, Option<Value>)) -> Result<Value, Error> {
match (array, index) { match (array, index) {
(Value::Array(mut v), Some(Value::Number(i))) => { (Value::Array(mut v), Some(Value::Number(i))) => {

View file

@ -66,6 +66,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"array::difference" => array::difference, "array::difference" => array::difference,
"array::distinct" => array::distinct, "array::distinct" => array::distinct,
"array::flatten" => array::flatten, "array::flatten" => array::flatten,
"array::group" => array::group,
"array::insert" => array::insert, "array::insert" => array::insert,
"array::intersect" => array::intersect, "array::intersect" => array::intersect,
"array::len" => array::len, "array::len" => array::len,

View file

@ -79,6 +79,7 @@ impl Function {
match self { match self {
Self::Normal(f, _) if f == "array::concat" => true, Self::Normal(f, _) if f == "array::concat" => true,
Self::Normal(f, _) if f == "array::distinct" => 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 == "array::union" => true,
Self::Normal(f, _) if f == "count" => true, Self::Normal(f, _) if f == "count" => true,
Self::Normal(f, _) if f == "math::bottom" => 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::difference"),
tag("array::distinct"), tag("array::distinct"),
tag("array::flatten"), tag("array::flatten"),
tag("array::group"),
tag("array::insert"), tag("array::insert"),
tag("array::intersect"), tag("array::intersect"),
tag("array::len"), tag("array::len"),