All Products
Search
Document Center

Simple Log Service:Lambda expressions

Last Updated:Jun 20, 2026

Log Service allows you to define lambda expressions in SQL and Simple Log Service Processing Language (SPL) statements and pass them to specific functions to extend their capabilities. This topic describes the syntax of lambda expressions and provides usage examples.

Syntax

A lambda expression must be used with a higher-order function, such as the filter function, reduce function, transform function, zip_with function, or map_filter function. The syntax for a lambda expression is as follows:

parameter -> expression

Parameter

Description

parameter

An identifier for the input parameters.

expression

The expression to evaluate. Most MySQL expressions are valid in a lambda expression. Examples:

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)

Examples

Example 1: Filter null elements

Returns the non-null elements from the array [5, null, 7, null].

  • Query and analysis statement

    * | SELECT filter(array[5, null, 7, null], x -> x is not null)
  • The result is [5,7].

Example 2: Sum array elements

Returns the sum of all elements in the array [5, 20, 50].

  • Query and analysis statement

    * | SELECT reduce(array[5, 20, 50], 0, (s, x) -> s + x, s -> s)
  • The result is 75 (the cumulative sum of 5+20+50).

Example 3: Filter a map by value

Creates a map from two arrays and keeps only the key-value pairs with values greater than 10.

  • Query and analysis statement

    * | SELECT map_filter(map(array['class01', 'class02', 'class03'], array[11, 10, 9]), (k,v) -> v > 10)
  • The result is {"class01":11}. The function retains only key-value pairs with values greater than 10.

Example 4: Swap paired elements

This example pairs elements from two arrays at the same index. It then applies a lambda expression to swap the elements in each pair, creating a new nested array.

  • Query and analysis statement

    * | SELECT zip_with(array[1, 3, 5], array['a', 'b', 'c'], (x, y) -> (y, x))
  • The result is [["a",1],["b",3],["c",5]]. The lambda expression swaps the order of each pair of elements and combines them into a nested array.

Example 5: Transform elements with null handling

Adds 1 to each element in the array [5, NULL, 6]. If an element is null, it is first converted to 0.

  • Query and analysis statement

    * | SELECT transform(array[5, NULL, 6], x -> coalesce(x, 0) + 1)
  • The result is [6,1,7].

Other examples

* | 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))
* | SELECT zip_with(array['a', 'b', 'c'], array['d', 'e', 'f'], (x, y) -> concat(x, y))