Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate Microsoft Fabric, Power BI, SQL, and AI community-led event. March 31 to April 2, 2025.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to: Databricks SQL Databricks Runtime
A parameterized expression that can be passed to a function to control its behavior.
For example, array_sort function accepts a lambda function as an argument to define a custom sort order.
{ param -> expr |
(param1 [, ...] ) -> expr }
paramN
: An identifier used by the parent function to pass arguments for the lambda function.expr
: Any simple expression referencing paramN
, which does not contain a subquery or a SQL user-defined function.The result type is defined by the result type of expr
.
If there is more than one paramN
, the parameter names must be unique. The types of the parameters are set by the invoking function.
The expression
must be valid for these types and the result type must match the defined expectations of the invoking functions.
The array_sort function function expects a lambda function with two parameters.
The parameter types will be the type of the elements of the array to be sorted.
The expression is expected to return an INTEGER where -1 means param1
< param2
, 0 means param1
= param2
, and 1 otherwise.
To sort an ARRAY of STRING in a right to left lexical order, you can use the following lambda function.
(p1, p2) -> CASE WHEN p1 = p2 THEN 0
WHEN reverse(p1) < reverse(p2) THEN -1
ELSE 1 END
Lambda functions are defined and used ad hoc. So the function definition is the argument:
> SELECT array_sort(array('Hello', 'World'),
(p1, p2) -> CASE WHEN p1 = p2 THEN 0
WHEN reverse(p1) < reverse(p2) THEN -1
ELSE 1 END);
[World, Hello]
Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate Microsoft Fabric, Power BI, SQL, and AI community-led event. March 31 to April 2, 2025.
Register today