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.
Syntax
lambda <param1>[, <param2>, ...]: <expression>
Arguments
<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.
Considerations
- Lambda expressions can only be used as arguments to functions that accept them, such as
ARRAY_TRANSFORMandARRAY_FILTER. - The parameter names are local to the lambda and don't conflict with column names in the outer query.
- Lambda expressions can't reference columns or variables from the outer query scope.
- The return type of the lambda expression is inferred from the expression. If the expression returns only NULL, you must cast it to a specific type.
- Data 360 uses the Python / DuckDB-style lambda syntax (
lambda x: expr), not the Spark / Snowflake-style arrow syntax (x -> expr).
Examples
Single Parameter Lambda
Use a single parameter when operating on one array.
SELECT array_transform(ARRAY[1, 2, 3], lambda x: x * 2) AS doubled;
Returns [2, 4, 6].
Multiple Parameter Lambda
Use multiple parameters when operating on multiple arrays in parallel.
SELECT array_transform(ARRAY[1, 2, 3], ARRAY[10, 20, 30], lambda x, y: x + y) AS sums;
Returns [11, 22, 33].
Lambda with Conditional Logic
Use CASE expressions within lambdas for conditional transformations.
SELECT array_transform(ARRAY[1, 5, 3], ARRAY[4, 2, 6], lambda x, y:
CASE WHEN x > y THEN x ELSE y END
) AS maximums;
Returns [4, 5, 6].
Lambda for Filtering
Return a boolean expression to filter array elements.
SELECT array_filter(ARRAY[1, 2, 3, 4, 5, 6], lambda x: x % 2 = 0) AS evens;
Returns [2, 4, 6].
Lambda with String Operations
Lambdas work with any data type.
SELECT array_transform(
ARRAY['hello', 'world'],
ARRAY['!', '?'],
lambda x, y: upper(x) || y
) AS result;
Returns ["HELLO!", "WORLD?"].