<?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_ANSI_mode</id>
	<title>Spark SQL ANSI mode - 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_ANSI_mode"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Spark_SQL_ANSI_mode&amp;action=history"/>
	<updated>2026-12-06T00:13:27Z</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_ANSI_mode&amp;diff=197&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_ANSI_mode&amp;diff=197&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 ANSI mode&lt;br /&gt;
| also_known_as   = ANSI SQL mode, ANSI dialect&lt;br /&gt;
| field           = SQL query engines&lt;br /&gt;
| key_parts       = &amp;lt;code&amp;gt;spark.sql.ansi.enabled&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spark.sql.storeAssignmentPolicy&amp;lt;/code&amp;gt;, runtime errors on overflow and invalid casts, &amp;lt;code&amp;gt;try_*&amp;lt;/code&amp;gt; functions&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Spark error classes]], [[Spark 3 to Spark 4 migration]], [[Spark SQL scripting]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-ref-ansi-compliance.md, docs/sql-migration-guide.md, SQLConf.scala, error-conditions.json) and the Spark 4.0.0, 4.1.0 and 4.3.0 release notes}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], &amp;#039;&amp;#039;&amp;#039;ANSI mode&amp;#039;&amp;#039;&amp;#039; is the Spark SQL dialect, controlled by &amp;lt;code&amp;gt;spark.sql.ansi.enabled&amp;lt;/code&amp;gt;, in which invalid operations such as integer overflow, division by zero and malformed casts raise runtime errors instead of returning null or a wrapped value, and type coercion follows rules aligned with the ANSI SQL standard.&amp;lt;ref name=&amp;quot;ansi&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-ansi-compliance.html |title=ANSI Compliance (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; ANSI mode has been the default since Spark 4.0.&amp;lt;ref name=&amp;quot;migration&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-migration-guide.html |title=Migration Guide: SQL, Datasets and DataFrame (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; With ANSI mode off, Spark SQL uses a Hive-compatible dialect that returns null for many invalid operations.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt; The change matters most when moving workloads from Spark 3 to Spark 4.&lt;br /&gt;
&lt;br /&gt;
== What is ANSI mode in Spark SQL? ==&lt;br /&gt;
Spark SQL has two settings that bring it closer to the SQL standard. &amp;lt;code&amp;gt;spark.sql.ansi.enabled&amp;lt;/code&amp;gt; switches the dialect: with it on, Spark throws runtime exceptions on invalid operations and uses type coercion rules based on a data type precedence list. &amp;lt;code&amp;gt;spark.sql.storeAssignmentPolicy&amp;lt;/code&amp;gt; separately controls implicit casts when rows are inserted into a table. Both settings exist since Spark 3.0.0; in Spark 4.3.0 their defaults are &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ANSI&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Spark documentation notes that some ANSI-mode behaviours do not come from the ANSI SQL standard itself but follow its style.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt; ANSI mode is therefore a Spark dialect, not a certification of full standard compliance.&lt;br /&gt;
&lt;br /&gt;
== Is ANSI mode on by default in Spark 4? ==&lt;br /&gt;
Yes. SPARK-44444, listed in the Spark 4.0.0 release notes, made ANSI mode the default.&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; The migration guide says that since Spark 4.0 &amp;lt;code&amp;gt;spark.sql.ansi.enabled&amp;lt;/code&amp;gt; is on by default, and that the previous behaviour returns when either the configuration or the environment variable &amp;lt;code&amp;gt;SPARK_ANSI_SQL_MODE&amp;lt;/code&amp;gt; is set to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;migration&amp;quot; /&amp;gt; In the v4.3.0 source the default is computed as true unless &amp;lt;code&amp;gt;SPARK_ANSI_SQL_MODE&amp;lt;/code&amp;gt; equals &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&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; The [[Spark 3 to Spark 4 migration]] page covers the upgrade work around this change.&lt;br /&gt;
&lt;br /&gt;
Spark 4.1.0 added two related changes: views now always persist the ANSI setting in force when they were created (SPARK-53348), and ANSI mode became the default for the pandas API on Spark (SPARK-53295).&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; Spark 4.3.0 made &amp;lt;code&amp;gt;ceil&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;floor&amp;lt;/code&amp;gt; throw overflow errors in ANSI mode (SPARK-58052).&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;
== What does ANSI mode change? ==&lt;br /&gt;
=== Arithmetic overflow ===&lt;br /&gt;
With ANSI mode on, Spark throws an arithmetic exception when a numeric or interval operation overflows. With it off, decimal overflow produces null and other numeric types wrap around as they do in Java or Scala, so &amp;lt;code&amp;gt;2147483647 + 1&amp;lt;/code&amp;gt; returns &amp;lt;code&amp;gt;-2147483648&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt; In ANSI mode the same query fails with the error condition &amp;lt;code&amp;gt;ARITHMETIC_OVERFLOW&amp;lt;/code&amp;gt;, whose SQLSTATE is &amp;lt;code&amp;gt;22003&amp;lt;/code&amp;gt;; division by zero fails with &amp;lt;code&amp;gt;DIVIDE_BY_ZERO&amp;lt;/code&amp;gt; (SQLSTATE &amp;lt;code&amp;gt;22012&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;errjson&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 (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Casts ===&lt;br /&gt;
In ANSI mode an explicit &amp;lt;code&amp;gt;CAST&amp;lt;/code&amp;gt; that cannot succeed raises an error: a string that does not parse as the target type, or a numeric value out of the target type&amp;#039;s range. ANSI mode also forbids some conversions that the non-ANSI dialect allows: numeric to and from binary, date to and from boolean, timestamp to and from boolean, and date to numeric.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt; &amp;lt;code&amp;gt;CAST(&amp;#039;a&amp;#039; AS INT)&amp;lt;/code&amp;gt; fails with &amp;lt;code&amp;gt;CAST_INVALID_INPUT&amp;lt;/code&amp;gt; (SQLSTATE &amp;lt;code&amp;gt;22018&amp;lt;/code&amp;gt;) and &amp;lt;code&amp;gt;CAST(2147483648L AS INT)&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;CAST_OVERFLOW&amp;lt;/code&amp;gt; (SQLSTATE &amp;lt;code&amp;gt;22003&amp;lt;/code&amp;gt;); with ANSI mode off both return a value (null and &amp;lt;code&amp;gt;-2147483648&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;errjson&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Table inserts ===&lt;br /&gt;
Under the default &amp;lt;code&amp;gt;ANSI&amp;lt;/code&amp;gt; store assignment policy, inserting a value of an incompatible type, such as a string into an &amp;lt;code&amp;gt;INT&amp;lt;/code&amp;gt; column, fails at analysis time, and inserting an out-of-range number fails with &amp;lt;code&amp;gt;CAST_OVERFLOW_IN_TABLE_INSERT&amp;lt;/code&amp;gt;. The &amp;lt;code&amp;gt;LEGACY&amp;lt;/code&amp;gt; policy allows any valid cast, as Spark 2.x did, and the &amp;lt;code&amp;gt;STRICT&amp;lt;/code&amp;gt; policy forbids any cast that could lose precision.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Functions and operators ===&lt;br /&gt;
Several functions behave differently under ANSI mode. &amp;lt;code&amp;gt;element_at&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;elt&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;array[index]&amp;lt;/code&amp;gt; operator throw on an invalid index; &amp;lt;code&amp;gt;to_date&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;to_timestamp&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;unix_timestamp&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;make_date&amp;lt;/code&amp;gt; throw when input cannot be parsed or the result is invalid.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Keywords ===&lt;br /&gt;
ANSI mode does not by itself reserve SQL keywords. Spark uses the ANSI keyword parser, which forbids reserved keywords as identifiers, only when both &amp;lt;code&amp;gt;spark.sql.ansi.enabled&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;spark.sql.ansi.enforceReservedKeywords&amp;lt;/code&amp;gt; are true, and the second defaults to false.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Summary of ANSI and non-ANSI behaviour ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Expression !! ANSI mode on (Spark 4 default) !! ANSI mode off&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;2147483647 + 1&amp;lt;/code&amp;gt; || Error &amp;lt;code&amp;gt;ARITHMETIC_OVERFLOW&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;-2147483648&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;1 / 0&amp;lt;/code&amp;gt; || Error &amp;lt;code&amp;gt;DIVIDE_BY_ZERO&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt;&amp;lt;ref&amp;gt;The non-ANSI divide behaves like &amp;lt;code&amp;gt;try_divide&amp;lt;/code&amp;gt;, per a comment in {{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala |title=arithmetic.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CAST(&amp;#039;a&amp;#039; AS INT)&amp;lt;/code&amp;gt; || Error &amp;lt;code&amp;gt;CAST_INVALID_INPUT&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CAST(2147483648L AS INT)&amp;lt;/code&amp;gt; || Error &amp;lt;code&amp;gt;CAST_OVERFLOW&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;-2147483648&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CAST(DATE&amp;#039;2020-01-01&amp;#039; AS INT)&amp;lt;/code&amp;gt; || Analysis error || &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
Sources: Spark ANSI compliance documentation and error condition list.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;errjson&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to tolerate bad data in ANSI mode ==&lt;br /&gt;
Spark provides &amp;lt;code&amp;gt;try_*&amp;lt;/code&amp;gt; variants that return &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; instead of raising an error. They include &amp;lt;code&amp;gt;try_cast&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_add&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_subtract&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_multiply&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_divide&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_mod&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_sum&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_avg&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_element_at&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_to_timestamp&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try_to_date&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;try_parse_url&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;ansi&amp;quot; /&amp;gt; Using these functions on the columns that may hold bad values keeps the rest of a query strict, which is narrower than turning ANSI mode off for a whole session.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
-- Spark 4.3 default: spark.sql.ansi.enabled = true&lt;br /&gt;
SELECT CAST(&amp;#039;a&amp;#039; AS INT);             -- fails with CAST_INVALID_INPUT&lt;br /&gt;
SELECT try_cast(&amp;#039;a&amp;#039; AS INT);         -- NULL&lt;br /&gt;
SELECT try_add(2147483647, 1);       -- NULL&lt;br /&gt;
SELECT try_divide(10, 0);            -- NULL&lt;br /&gt;
&lt;br /&gt;
-- Restore the Spark 3 behaviour for one session only&lt;br /&gt;
SET spark.sql.ansi.enabled = false;&lt;br /&gt;
SELECT CAST(&amp;#039;a&amp;#039; AS INT);             -- NULL&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 3 to Spark 4 migration]]&lt;br /&gt;
* [[Spark error classes]]&lt;br /&gt;
* [[Spark SQL scripting]]&lt;br /&gt;
* [[Variant type]]&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>