<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Higher-order_functions_(Spark_SQL)</id>
	<title>Higher-order functions (Spark SQL) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Higher-order_functions_(Spark_SQL)"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Higher-order_functions_(Spark_SQL)&amp;action=history"/>
	<updated>2026-12-06T00:07:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Higher-order_functions_(Spark_SQL)&amp;diff=152&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Higher-order_functions_(Spark_SQL)&amp;diff=152&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:43Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Higher-order functions (Spark SQL)&lt;br /&gt;
| also_known_as   = Lambda functions, HOFs&lt;br /&gt;
| field           = Query languages, nested data processing&lt;br /&gt;
| introduced      = Apache Spark 2.4.0 (SQL); 3.1.0 (PySpark functions)&lt;br /&gt;
| key_parts       = Lambda expressions (&amp;lt;code&amp;gt;x -&amp;gt; expr&amp;lt;/code&amp;gt;), array functions (&amp;lt;code&amp;gt;transform&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;aggregate&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;exists&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;forall&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;zip_with&amp;lt;/code&amp;gt;), map functions (&amp;lt;code&amp;gt;transform_keys&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;transform_values&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;map_filter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;map_zip_with&amp;lt;/code&amp;gt;)&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Spark SQL]], [[Delta Lake]], [[Parameterized queries (Spark SQL)]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (higherOrderFunctions.scala, FunctionRegistry.scala, python/pyspark/sql/functions/builtin.py, error-conditions.json, the PySpark Arrow guide) and the Spark 2.4.0, 3.1.1, 4.0.0 and 4.3.0 release notes}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]] SQL, a &amp;#039;&amp;#039;&amp;#039;higher-order function&amp;#039;&amp;#039;&amp;#039; is a built-in function that takes a lambda function as an argument and applies it to the elements of an array or the entries of a map, such as &amp;lt;code&amp;gt;transform&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;aggregate&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;hof&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala |title=higherOrderFunctions.scala (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Higher-order functions let a query work on nested collections in place, without exploding them into rows and grouping them back, and without a user-defined function. Spark 2.4.0 introduced them in SQL, and Spark 3.1 added them to the PySpark functions API.&amp;lt;ref name=&amp;quot;r240&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-2-4-0.html |title=Spark Release 2.4.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;r311&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-1-1.html |title=Spark Release 3.1.1 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What is a lambda function in Spark SQL? ==&lt;br /&gt;
A Spark SQL lambda function is written as parameters, an arrow and a body: &amp;lt;code&amp;gt;x -&amp;gt; x + 1&amp;lt;/code&amp;gt; for one parameter, or &amp;lt;code&amp;gt;(acc, x) -&amp;gt; acc + x&amp;lt;/code&amp;gt; for several. Spark allows a lambda only as an argument of a higher-order function; using one elsewhere raises an error that says a lambda function should only be used in a higher order function.&amp;lt;ref name=&amp;quot;errors&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/common/utils/src/main/resources/error/error-conditions.json |title=error-conditions.json (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In the Spark source, the built-in higher-order functions are documented in the function group &amp;lt;code&amp;gt;lambda_funcs&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Subqueries are not allowed inside higher-order functions. Spark 4.0.0 blocked them for correctness (SPARK-47509), and the error message asks the user to remove all subquery expressions from higher-order functions.&amp;lt;ref name=&amp;quot;r400&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-0-0.html |title=Spark Release 4.0.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;errors&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Which higher-order functions does Spark SQL have? ==&lt;br /&gt;
The following table lists the built-in higher-order functions registered in Spark 4.3, with the version given in each function&amp;#039;s documentation:&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;registry&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala |title=FunctionRegistry.scala (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Function !! Works on !! Lambda !! What it returns !! Since&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;transform(arr, f)&amp;lt;/code&amp;gt; || Array || &amp;lt;code&amp;gt;x -&amp;gt; ...&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;(x, i) -&amp;gt; ...&amp;lt;/code&amp;gt; (i is the index) || A new array with &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; applied to each element || 2.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;filter(arr, f)&amp;lt;/code&amp;gt; || Array || &amp;lt;code&amp;gt;x -&amp;gt; bool&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;(x, i) -&amp;gt; bool&amp;lt;/code&amp;gt; || The elements for which &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; is true; the index argument is available since 3.0.0 || 2.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;aggregate(arr, start, merge [, finish])&amp;lt;/code&amp;gt; || Array || &amp;lt;code&amp;gt;(acc, x) -&amp;gt; ...&amp;lt;/code&amp;gt;, optional &amp;lt;code&amp;gt;acc -&amp;gt; ...&amp;lt;/code&amp;gt; || One value, folded from &amp;lt;code&amp;gt;start&amp;lt;/code&amp;gt; over all elements || 2.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;reduce(arr, start, merge [, finish])&amp;lt;/code&amp;gt; || Array || As &amp;lt;code&amp;gt;aggregate&amp;lt;/code&amp;gt; || Alias of &amp;lt;code&amp;gt;aggregate&amp;lt;/code&amp;gt;, registered since 3.4.0 || 3.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;exists(arr, f)&amp;lt;/code&amp;gt; || Array || &amp;lt;code&amp;gt;x -&amp;gt; bool&amp;lt;/code&amp;gt; || True if &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; holds for at least one element || 2.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;forall(arr, f)&amp;lt;/code&amp;gt; || Array || &amp;lt;code&amp;gt;x -&amp;gt; bool&amp;lt;/code&amp;gt; || True if &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; holds for every element || 3.0.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;zip_with(a, b, f)&amp;lt;/code&amp;gt; || Two arrays || &amp;lt;code&amp;gt;(x, y) -&amp;gt; ...&amp;lt;/code&amp;gt; || An element-wise merge; the shorter array is padded with nulls || 2.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;array_sort(arr, f)&amp;lt;/code&amp;gt; || Array || &amp;lt;code&amp;gt;(left, right) -&amp;gt; int&amp;lt;/code&amp;gt; || The array sorted with a comparator; the comparator form exists since 3.0.0 || 2.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;transform_keys(m, f)&amp;lt;/code&amp;gt; || Map || &amp;lt;code&amp;gt;(k, v) -&amp;gt; newKey&amp;lt;/code&amp;gt; || A map with new keys and unchanged values || 3.0.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;transform_values(m, f)&amp;lt;/code&amp;gt; || Map || &amp;lt;code&amp;gt;(k, v) -&amp;gt; newValue&amp;lt;/code&amp;gt; || A map with unchanged keys and new values || 3.0.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;map_filter(m, f)&amp;lt;/code&amp;gt; || Map || &amp;lt;code&amp;gt;(k, v) -&amp;gt; bool&amp;lt;/code&amp;gt; || The entries for which &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; is true || 3.0.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;map_zip_with(m1, m2, f)&amp;lt;/code&amp;gt; || Two maps || &amp;lt;code&amp;gt;(k, v1, v2) -&amp;gt; ...&amp;lt;/code&amp;gt; || A merged map; a value is NULL when its key is missing from one map || 3.0.0&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Null handling follows three-valued logic. For example, &amp;lt;code&amp;gt;exists(array(1, null, 3), x -&amp;gt; x % 2 == 0)&amp;lt;/code&amp;gt; returns NULL, while &amp;lt;code&amp;gt;forall(array(1, null, 3), x -&amp;gt; x % 2 == 0)&amp;lt;/code&amp;gt; returns false, because the element 1 already fails the predicate.&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do transform, filter and aggregate work? ==&lt;br /&gt;
The following Spark SQL query applies three array functions and one map function to a single row:&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT&lt;br /&gt;
  transform(amounts, x -&amp;gt; x * 2)             AS doubled,      -- [20,50,80]&lt;br /&gt;
  filter(amounts, x -&amp;gt; x &amp;gt; 15)               AS large,        -- [25,40]&lt;br /&gt;
  aggregate(amounts, 0, (acc, x) -&amp;gt; acc + x) AS total,        -- 75&lt;br /&gt;
  exists(amounts, x -&amp;gt; x &amp;gt; 30)               AS any_over_30,  -- true&lt;br /&gt;
  transform_values(tags, (k, v) -&amp;gt; v * 10)   AS tags_x10      -- {&amp;quot;a&amp;quot;:10,&amp;quot;b&amp;quot;:20}&lt;br /&gt;
FROM VALUES (array(10, 25, 40), map(&amp;#039;a&amp;#039;, 1, &amp;#039;b&amp;#039;, 2)) AS t(amounts, tags);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;aggregate&amp;lt;/code&amp;gt; starts from the initial state, applies &amp;lt;code&amp;gt;merge&amp;lt;/code&amp;gt; to the state and each element in order, and then applies &amp;lt;code&amp;gt;finish&amp;lt;/code&amp;gt; to the final state; without &amp;lt;code&amp;gt;finish&amp;lt;/code&amp;gt;, the identity function is used.&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt; The result type of &amp;lt;code&amp;gt;merge&amp;lt;/code&amp;gt; must match the type of the start value. If it does not, Spark fails the query with a &amp;lt;code&amp;gt;DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE&amp;lt;/code&amp;gt; error that names the type required by the start value.&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt; This is a common mistake when the start value is the integer literal &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; and the elements are &amp;lt;code&amp;gt;BIGINT&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DOUBLE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DECIMAL&amp;lt;/code&amp;gt;; the fix is to cast the start value, for example &amp;lt;code&amp;gt;CAST(0 AS BIGINT)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== How are higher-order functions used from PySpark? ==&lt;br /&gt;
PySpark exposes the same functions in &amp;lt;code&amp;gt;pyspark.sql.functions&amp;lt;/code&amp;gt;, where the lambda is a Python function that takes and returns &amp;lt;code&amp;gt;Column&amp;lt;/code&amp;gt; objects. The functions were added in PySpark 3.1.0 and support [[Spark Connect]] since 3.4.0.&amp;lt;ref name=&amp;quot;builtin&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/functions/builtin.py |title=builtin.py (pyspark.sql.functions, tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The Python function is not run per element. PySpark calls it once with placeholder columns to build a Spark expression, so the body may use &amp;lt;code&amp;gt;Column&amp;lt;/code&amp;gt; methods, functions from &amp;lt;code&amp;gt;pyspark.sql.functions&amp;lt;/code&amp;gt; and Scala user-defined functions, but not Python user-defined functions (SPARK-27052).&amp;lt;ref name=&amp;quot;builtin&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql import functions as F&lt;br /&gt;
&lt;br /&gt;
df = spark.createDataFrame([(1, [10, 25, 40])], [&amp;quot;id&amp;quot;, &amp;quot;amounts&amp;quot;])  # array&amp;lt;bigint&amp;gt;&lt;br /&gt;
&lt;br /&gt;
df.select(&lt;br /&gt;
    F.transform(&amp;quot;amounts&amp;quot;, lambda x: x * 2).alias(&amp;quot;doubled&amp;quot;),&lt;br /&gt;
    F.filter(&amp;quot;amounts&amp;quot;, lambda x, i: (x &amp;gt; 15) &amp;amp; (i &amp;gt; 0)).alias(&amp;quot;large_after_first&amp;quot;),&lt;br /&gt;
    # The start value is cast to bigint to match the element type.&lt;br /&gt;
    F.aggregate(&amp;quot;amounts&amp;quot;, F.lit(0).cast(&amp;quot;bigint&amp;quot;), lambda acc, x: acc + x).alias(&amp;quot;total&amp;quot;),&lt;br /&gt;
).show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PySpark also provides &amp;lt;code&amp;gt;reduce&amp;lt;/code&amp;gt;, added in 3.5.0, with the same arguments as &amp;lt;code&amp;gt;aggregate&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;builtin&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Higher-order functions vs explode and user-defined functions ==&lt;br /&gt;
Before Spark 2.4.0, the usual ways to compute over an array column were to explode it into rows and aggregate them back, or to write a user-defined function (UDF). Spark 2.4.0 added higher-order functions to make complex data types easier to process (SPARK-23899).&amp;lt;ref name=&amp;quot;r240&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Higher-order function !! &amp;lt;code&amp;gt;explode&amp;lt;/code&amp;gt; then &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt; !! Python UDF&lt;br /&gt;
|-&lt;br /&gt;
! Stays within one row&lt;br /&gt;
| Yes&lt;br /&gt;
| No; one row per element, then a regrouping step&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
! Runs in the JVM as a Catalyst expression&lt;br /&gt;
| Yes&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| No; data is transferred between the JVM and Python worker processes&amp;lt;ref name=&amp;quot;arrow&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/tutorial/sql/arrow_pandas.html |title=Apache Arrow in PySpark (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;config&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/configuration.html |title=Spark Configuration (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Element order and index&lt;br /&gt;
| Available (&amp;lt;code&amp;gt;(x, i) -&amp;gt; ...&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;transform&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;)&amp;lt;ref name=&amp;quot;hof&amp;quot; /&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;posexplode&amp;lt;/code&amp;gt; returns the position with each element&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/generators.scala |title=generators.scala (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| Available&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Spark 4.3.0 added code generation for array higher-order functions (SPARK-37019) and fixed wrong results from semantic equality checks of higher-order functions that capture lambda variables (SPARK-60508).&amp;lt;ref name=&amp;quot;r430&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-3-0.html |title=Spark Release 4.3.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Use in an open lakehouse ==&lt;br /&gt;
Tables in [[Delta Lake]] and [[Apache Iceberg]] often store nested arrays, maps and structs, for example line items in an order or attributes in an event. Higher-order functions let Spark SQL clean, filter and summarize these nested values in a single &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; or in a &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; source query, without changing the table&amp;#039;s row granularity.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Spark SQL]]&lt;br /&gt;
* [[Parameterized queries (Spark SQL)]]&lt;br /&gt;
* [[Delta Lake]]&lt;br /&gt;
* [[Apache Spark]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>