一、语法
Lambda 表达式需与函数一起使用,例如 filter 函数、reduce 函数、transform 函数、zip_with 函数。Lambda 表达式的语法如下:
parameter -> expression
| 参数 | 说明 |
| parameter | 用于传递参数的标识符。 |
| expression | 表达式,大多数的 Mysql 表达式都可以在 Lambda 表达式使用。例如:x -> x + 1 (x, y) -> x + y x -> regexp_like(x, 'a+') x -> x[1] / x[2] x -> if(x > 0, x, -x) x -> coalesce(x, 0) x -> cast(x AS JSON) x -> x + try(1 / 0) |
二、示例1:使用 Lambda 表达式 x-> x is not null
返回数组[5,null,7,null]中非 null 的元素。
·查询和分析语句
* | SELECT filter(array[5, null, 7, null], x -> x is not null)
·查询和分析结果
[5,7]
三、示例2:使用 Lambda 表达式0,(s, x) -> s + x, s -> s
返回数组[5,20,50]中各个元素相加的结果。
·查询和分析语句
* | SELECT reduce(array[5, 20, 50], 0, (s, x) -> s + x, s -> s)
·查询和分析结果
75
四、示例3:使用 Lambda 表达式(k, v) -> v> 10
将两个数组映射为一个 Map 且 Map 中的键值大于10。
·查询和分析语句
* | SELECT map_filter(map(array['class01', 'class02', 'class03'], array[11, 10, 9]), (k,v) -> v > 10)
·查询和分析结果
{"class01":11}
五、示例4:使用 Lambda 表达式(x, y) -> (y, x)
将对换两个数组的元素位置,并提取数组中索引相同的元素组成一个新的二维数组。
·查询和分析语句
* | SELECT zip_with(array['a', 'b', 'c'], array['d', 'e', 'f'], (x, y) -> concat(x, y))
·查询和分析结果
["ad","be","cf"]
六、示例5:使用 Lambda 表达式 x -> coalesce(x, 0)+1
将数组[5, null, 6]的各个元素加1,然后返回。如果数组中包含 null 元素,则转换为0,再加1。
·查询和分析语句
* | SELECT transform(array[5, NULL, 6], x -> coalesce(x, 0) + 1)
·查询和分析结果
[6,1,7]
七、其他示例
* | SELECT filter(array[], x -> true)* | SELECT map_filter(map(array[],array[]), (k, v) -> true)* | SELECT reduce(array[5, 6, 10, 20], — calculates arithmetic average: 10.25 cast(row(0.0, 0) AS row(sum double, count integer)), (s, x) -> cast(row(x + s.sum, s.count + 1) AS row(sum double, count integer)), s -> if(s.count = 0, null, s.sum / s.count))* | SELECT reduce(array[2147483647, 1], cast(0 AS bigint), (s, x) -> s + x, s -> s)* | SELECT reduce(array[5, 20, null, 50], 0, (s, x) -> s + x, s -> s)* | SELECT transform(array[array[1, null, 2], array[3, null]], a -> filter(a, x -> x is not null))