<?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=Spark_SQL_scripting</id>
	<title>Spark SQL scripting - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Spark_SQL_scripting"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Spark_SQL_scripting&amp;action=history"/>
	<updated>2026-12-06T00:25:12Z</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=Spark_SQL_scripting&amp;diff=198&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=Spark_SQL_scripting&amp;diff=198&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:57Z</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            = Spark SQL scripting&lt;br /&gt;
| also_known_as   = SQL Scripting, procedural SQL in Spark&lt;br /&gt;
| field           = SQL query engines&lt;br /&gt;
| key_parts       = Compound statement (&amp;lt;code&amp;gt;BEGIN ... END&amp;lt;/code&amp;gt;), local variables, &amp;lt;code&amp;gt;IF&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CASE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;WHILE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;REPEAT&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;LOOP&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;FOR&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;LEAVE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ITERATE&amp;lt;/code&amp;gt;, condition handlers, cursors&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Spark error classes]], [[Spark SQL ANSI mode]], [[SQL pipe syntax]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-ref-scripting.md, docs/control-flow/*.md, SqlBaseParser.g4, AstBuilder.scala, SQLConf.scala, classic SparkSession.scala), SPARK-48338, SPARK-54499 and the Spark 4.1.0 and 4.2.0 release notes}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], &amp;#039;&amp;#039;&amp;#039;SQL scripting&amp;#039;&amp;#039;&amp;#039; is the procedural extension of Spark SQL that runs a block of statements written between &amp;lt;code&amp;gt;BEGIN&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;END&amp;lt;/code&amp;gt;, with local variables, loops, conditional logic and error handlers, as a single request. The Spark documentation describes the syntax as based on the SQL/PSM standard.&amp;lt;ref name=&amp;quot;doc&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-scripting.html |title=SQL Scripting (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; SQL scripting lets ETL and analysis logic that used to need Python or Scala driver code stay entirely in SQL; the umbrella ticket also names it as a step toward stored procedures and data warehouse migrations.&amp;lt;ref name=&amp;quot;jira48338&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-48338 |title=SPARK-48338: Sql Scripting support for Spark SQL |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Is SQL scripting enabled by default? ==&lt;br /&gt;
Yes, since Spark 4.1.0. The Spark 4.1.0 release notes state that SQL Scripting is generally available (GA) and enabled by default (SPARK-54499).&amp;lt;ref name=&amp;quot;r410&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4.1.0.html |title=Spark Release 4.1.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;jira54499&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-54499 |title=SPARK-54499: Enable SQL Scripting by default for Spark 4.1 release |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The switch is &amp;lt;code&amp;gt;spark.sql.scripting.enabled&amp;lt;/code&amp;gt;, a configuration added in Spark 4.0.0 whose default in Spark 4.3.0 is &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;. Its description in the 4.3.0 source still calls the feature &amp;quot;under development&amp;quot;, a wording older than the GA decision.&amp;lt;ref name=&amp;quot;sqlconf&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/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Two parts of SQL scripting stay behind internal flags in Spark 4.3.0, both off by default. &amp;lt;code&amp;gt;CONTINUE&amp;lt;/code&amp;gt; handlers need &amp;lt;code&amp;gt;spark.sql.scripting.continueHandlerEnabled&amp;lt;/code&amp;gt; (added in 4.1.0), and cursors (&amp;lt;code&amp;gt;DECLARE CURSOR&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;OPEN&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;FETCH&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CLOSE&amp;lt;/code&amp;gt;) need &amp;lt;code&amp;gt;spark.sql.scripting.cursorEnabled&amp;lt;/code&amp;gt; (added in 4.2.0). Without them the parser rejects these statements.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;astbuilder&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/parser/AstBuilder.scala |title=AstBuilder.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The release notes list &amp;lt;code&amp;gt;CONTINUE HANDLER&amp;lt;/code&amp;gt; support under 4.1.0 (SPARK-53621) and cursor support under 4.2.0 (SPARK-54759).&amp;lt;ref name=&amp;quot;r410&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;r420&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-2-0.html |title=Spark Release 4.2.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What does Spark SQL scripting support? ==&lt;br /&gt;
Every SQL script is a compound statement: &amp;lt;code&amp;gt;BEGIN&amp;lt;/code&amp;gt;, an optional declaration section, a body of statements separated by semicolons, and &amp;lt;code&amp;gt;END&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;doc&amp;quot; /&amp;gt; The declaration section can declare local variables (with an optional &amp;lt;code&amp;gt;DEFAULT&amp;lt;/code&amp;gt;), named conditions, cursors and condition handlers.&amp;lt;ref name=&amp;quot;compound&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/control-flow/compound-stmt.html |title=Compound Statement (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The body of a Spark SQL script can contain:&amp;lt;ref name=&amp;quot;doc&amp;quot; /&amp;gt;&lt;br /&gt;
* control flow: &amp;lt;code&amp;gt;IF&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CASE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;WHILE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;REPEAT&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;LOOP&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;FOR&amp;lt;/code&amp;gt; loops over query results, and &amp;lt;code&amp;gt;LEAVE&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ITERATE&amp;lt;/code&amp;gt; to exit or continue a labelled loop;&lt;br /&gt;
* DDL such as &amp;lt;code&amp;gt;CREATE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ALTER&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DROP&amp;lt;/code&amp;gt;, and DML such as &amp;lt;code&amp;gt;INSERT&amp;lt;/code&amp;gt;;&lt;br /&gt;
* queries, whose result sets are returned to the caller;&lt;br /&gt;
* &amp;lt;code&amp;gt;SET&amp;lt;/code&amp;gt; for local and session variables, and &amp;lt;code&amp;gt;EXECUTE IMMEDIATE&amp;lt;/code&amp;gt;;&lt;br /&gt;
* nested compound statements, each with its own scope for variables, cursors, conditions and handlers.&lt;br /&gt;
&lt;br /&gt;
Compound statements are not atomic. The only supported mode is &amp;lt;code&amp;gt;NOT ATOMIC&amp;lt;/code&amp;gt;: when a statement fails, earlier statements in the block are not rolled back.&amp;lt;ref name=&amp;quot;compound&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does error handling work in SQL scripts? ==&lt;br /&gt;
A condition handler catches errors raised inside its compound statement. A handler can name a Spark error condition such as &amp;lt;code&amp;gt;DIVIDE_BY_ZERO&amp;lt;/code&amp;gt;, a user-declared condition, one or more SQLSTATE values, &amp;lt;code&amp;gt;SQLEXCEPTION&amp;lt;/code&amp;gt; (any SQLSTATE except the &amp;lt;code&amp;gt;XX&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;02&amp;lt;/code&amp;gt; classes) or &amp;lt;code&amp;gt;NOT FOUND&amp;lt;/code&amp;gt; (the &amp;lt;code&amp;gt;02&amp;lt;/code&amp;gt; class).&amp;lt;ref name=&amp;quot;doc&amp;quot; /&amp;gt; When several handlers apply, Spark uses the one in the innermost compound statement and, among those, the most specific: a named condition beats a SQLSTATE, which beats a generic handler.&amp;lt;ref name=&amp;quot;doc&amp;quot; /&amp;gt; The error names and SQLSTATEs are the ones described in [[Spark error classes]].&lt;br /&gt;
&lt;br /&gt;
An &amp;lt;code&amp;gt;EXIT&amp;lt;/code&amp;gt; handler runs its action and then leaves the compound statement that declared it. A &amp;lt;code&amp;gt;CONTINUE&amp;lt;/code&amp;gt; handler runs its action and resumes with the statement after the one that failed.&amp;lt;ref name=&amp;quot;doc&amp;quot; /&amp;gt; A user-declared condition without an explicit SQLSTATE gets &amp;lt;code&amp;gt;45000&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;compound&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following script, adapted from the Spark compound statement reference, catches a division by zero with an &amp;lt;code&amp;gt;EXIT&amp;lt;/code&amp;gt; handler. It runs with the Spark 4.3.0 defaults, because &amp;lt;code&amp;gt;EXIT&amp;lt;/code&amp;gt; handlers do not need an extra flag.&amp;lt;ref name=&amp;quot;compound&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
BEGIN&lt;br /&gt;
  DECLARE a INT DEFAULT 1;&lt;br /&gt;
  DECLARE b INT DEFAULT 5;&lt;br /&gt;
  DECLARE EXIT HANDLER FOR DIVIDE_BY_ZERO&lt;br /&gt;
    div0: BEGIN&lt;br /&gt;
      VALUES (15);&lt;br /&gt;
    END div0;&lt;br /&gt;
  SET a = 10;&lt;br /&gt;
  SET a = b / 0;      -- raises DIVIDE_BY_ZERO under ANSI mode&lt;br /&gt;
  VALUES (a);         -- not reached&lt;br /&gt;
END;&lt;br /&gt;
-- returns 15&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;FOR&amp;lt;/code&amp;gt; loop iterates over the rows of a query:&amp;lt;ref name=&amp;quot;for&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/control-flow/for-stmt.html |title=FOR Statement (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
BEGIN&lt;br /&gt;
  DECLARE total INT DEFAULT 0;&lt;br /&gt;
  sumOdd: FOR r AS SELECT num FROM range(1, 20) AS t(num) DO&lt;br /&gt;
    IF r.num &amp;gt; 10 THEN&lt;br /&gt;
      LEAVE sumOdd;&lt;br /&gt;
    ELSEIF r.num % 2 = 0 THEN&lt;br /&gt;
      ITERATE sumOdd;&lt;br /&gt;
    END IF;&lt;br /&gt;
    SET total = total + r.num;&lt;br /&gt;
  END FOR sumOdd;&lt;br /&gt;
  VALUES (total);     -- 25&lt;br /&gt;
END;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Spark documentation notes that such loops can often be replaced by a single relational query, which is usually more efficient; here &amp;lt;code&amp;gt;SELECT sum(num) FROM range(1, 10) AS t(num) WHERE num % 2 = 1&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;for&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to run a SQL script from PySpark or Scala ==&lt;br /&gt;
A script is passed as one string to &amp;lt;code&amp;gt;spark.sql()&amp;lt;/code&amp;gt;; the parser returns a compound body instead of a single query, and the returned DataFrame holds the result set of the last query the script ran.&amp;lt;ref name=&amp;quot;e2e&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingE2eSuite.scala |title=SqlScriptingE2eSuite.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Named parameter markers can be passed in, but positional parameters are rejected for SQL scripts, and a script cannot be run through &amp;lt;code&amp;gt;EXECUTE IMMEDIATE&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;session&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/classic/SparkSession.scala |title=classic/SparkSession.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The documentation describes two ways to pass data in and out of a script: session variables and parameter markers.&amp;lt;ref name=&amp;quot;doc&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
df = spark.sql(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
BEGIN&lt;br /&gt;
  DECLARE n INT DEFAULT 0;&lt;br /&gt;
  SET n = :iterations;&lt;br /&gt;
  WHILE n &amp;gt; 0 DO&lt;br /&gt;
    SET n = n - 1;&lt;br /&gt;
  END WHILE;&lt;br /&gt;
  VALUES (n);&lt;br /&gt;
END&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;, args={&amp;quot;iterations&amp;quot;: 3})&lt;br /&gt;
df.show()   # one row: 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Apache Spark]]&lt;br /&gt;
* [[Spark error classes]]&lt;br /&gt;
* [[Spark SQL ANSI mode]]&lt;br /&gt;
* [[SQL pipe syntax]]&lt;br /&gt;
* [[Spark Declarative Pipelines]]&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>