DID THIS ARTICLE SOLVE YOUR ISSUE?
Let us know so we can improve!
Let us know so we can improve!
Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Lambda expressions are anonymous functions you pass as arguments to higher-order functions such as ARRAY_TRANSFORM and ARRAY_FILTER. They enable element-wise operations on arrays without writing separate function definitions.
1lambda <param1>[, <param2>, ...]: <expression><param1>, <param2>, ...: One or more parameter names that represent the elements from the input arrays. The number of parameters must match the number of array arguments passed to the function.<expression>: Any valid SQL expression that uses the parameters. The expression is evaluated once for each element (or tuple of elements) in the input arrays.ARRAY_TRANSFORM and ARRAY_FILTER.lambda x: expr), not the Spark / Snowflake-style arrow syntax (x -> expr).Use a single parameter when operating on one array.
1SELECT ARRAY_TRANSFORM(ARRAY[1, 2, 3], lambda x: x * 2) AS doubled;Returns [2, 4, 6].
Use multiple parameters when operating on multiple arrays in parallel.
1SELECT ARRAY_TRANSFORM(ARRAY[1, 2, 3], ARRAY[10, 20, 30], lambda x, y: x + y) AS sums;Returns [11, 22, 33].
Use CASE expressions within lambdas for conditional transformations.
1SELECT ARRAY_TRANSFORM(ARRAY[1, 5, 3], ARRAY[4, 2, 6], lambda x, y:
2 CASE WHEN x > y THEN x ELSE y END
3) AS maximums;Returns [4, 5, 6].
Return a boolean expression to filter array elements.
1SELECT ARRAY_FILTER(ARRAY[1, 2, 3, 4, 5, 6], lambda x: x % 2 = 0) AS evens;Returns [2, 4, 6].
Lambdas work with any data type.
1SELECT ARRAY_TRANSFORM(
2 ARRAY['hello', 'world'],
3 ARRAY['!', '?'],
4 lambda x, y: UPPER(x) || y
5) AS result;Returns ["HELLO!", "WORLD?"].