<?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=Collation_(Spark_SQL)</id>
	<title>Collation (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=Collation_(Spark_SQL)"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Collation_(Spark_SQL)&amp;action=history"/>
	<updated>2026-12-06T00:07:39Z</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=Collation_(Spark_SQL)&amp;diff=133&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=Collation_(Spark_SQL)&amp;diff=133&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:37Z</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            = Collation (Spark SQL)&lt;br /&gt;
| also_known_as   = String collation, collated string type&lt;br /&gt;
| field           = SQL query engines&lt;br /&gt;
| key_parts       = &amp;lt;code&amp;gt;COLLATE&amp;lt;/code&amp;gt; clause, &amp;lt;code&amp;gt;UTF8_BINARY&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;UTF8_LCASE&amp;lt;/code&amp;gt;, ICU-based &amp;lt;code&amp;gt;UNICODE&amp;lt;/code&amp;gt; and locale collations, &amp;lt;code&amp;gt;_CI&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;_AI&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;_RTRIM&amp;lt;/code&amp;gt; modifiers, &amp;lt;code&amp;gt;DEFAULT COLLATION&amp;lt;/code&amp;gt;&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Spark error classes]], [[Delta Lake]], [[Apache Parquet]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-ref-syntax-aux-show-collations.md, SqlBaseParser.g4, CollationFactory.java, collationExpressions.scala, RewriteCollationJoin.scala, SQLConf.scala, error-conditions.json, sql-tests collations-basic results), the Spark 4.0.0, 4.1.0, 4.2.0 and 4.3.0 release notes and the PostgreSQL collation documentation}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], a &amp;#039;&amp;#039;&amp;#039;collation&amp;#039;&amp;#039;&amp;#039; is a property of the &amp;lt;code&amp;gt;STRING&amp;lt;/code&amp;gt; type that sets how Spark SQL compares, sorts, groups and joins string values, for example whether &amp;lt;code&amp;gt;&amp;#039;a&amp;#039;&amp;lt;/code&amp;gt; equals &amp;lt;code&amp;gt;&amp;#039;A&amp;#039;&amp;lt;/code&amp;gt;. Spark SQL added string collation support in Spark 4.0.0 (SPARK-46830).&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 default collation, &amp;lt;code&amp;gt;UTF8_BINARY&amp;lt;/code&amp;gt;, compares the raw UTF-8 bytes; other collations make comparisons case-insensitive, accent-insensitive, trailing-space-insensitive or locale-aware.&amp;lt;ref name=&amp;quot;show&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-syntax-aux-show-collations.html |title=SHOW COLLATIONS (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What collations does Spark SQL support? ==&lt;br /&gt;
Spark SQL has two kinds of collation. Two, with their &amp;lt;code&amp;gt;_RTRIM&amp;lt;/code&amp;gt; variants, are built into Spark and do not depend on a locale: &amp;lt;code&amp;gt;UTF8_BINARY&amp;lt;/code&amp;gt; (case- and accent-sensitive byte comparison) and &amp;lt;code&amp;gt;UTF8_LCASE&amp;lt;/code&amp;gt; (case-insensitive). The others come from the International Components for Unicode (ICU) library: &amp;lt;code&amp;gt;UNICODE&amp;lt;/code&amp;gt;, which uses the locale-independent Unicode rules, and locale collations such as &amp;lt;code&amp;gt;en_USA&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;show&amp;quot; /&amp;gt; The Spark 4.3.0 build uses ICU4J 78.3.&amp;lt;ref name=&amp;quot;pom&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/pom.xml |title=pom.xml (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A collation name can carry modifiers. &amp;lt;code&amp;gt;_CI&amp;lt;/code&amp;gt; makes it case-insensitive, &amp;lt;code&amp;gt;_AI&amp;lt;/code&amp;gt; accent-insensitive and &amp;lt;code&amp;gt;_RTRIM&amp;lt;/code&amp;gt; ignores trailing spaces, giving names such as &amp;lt;code&amp;gt;UNICODE_CI_AI&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;en_USA_CI&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;UTF8_BINARY_RTRIM&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;show&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;factory&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/common/unsafe/src/main/java/org/apache/spark/sql/catalyst/util/CollationFactory.java |title=CollationFactory.java (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The &amp;lt;code&amp;gt;SHOW COLLATIONS [LIKE pattern]&amp;lt;/code&amp;gt; command, added in Spark 4.2.0 (SPARK-49543), lists every collation with its language, country, case and accent sensitivity, pad attribute and ICU version.&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;&amp;lt;ref name=&amp;quot;show&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to set a collation in Spark SQL ==&lt;br /&gt;
A collation can be attached at several levels, according to the Spark 4.3.0 grammar:&amp;lt;ref name=&amp;quot;grammar&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4 |title=SqlBaseParser.g4 (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* on a column type: &amp;lt;code&amp;gt;STRING COLLATE UTF8_LCASE&amp;lt;/code&amp;gt;, and also on &amp;lt;code&amp;gt;CHAR&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;VARCHAR&amp;lt;/code&amp;gt;;&lt;br /&gt;
* on an expression: &amp;lt;code&amp;gt;name COLLATE UNICODE_CI&amp;lt;/code&amp;gt;, or the function &amp;lt;code&amp;gt;collate(expr, &amp;#039;UTF8_LCASE&amp;#039;)&amp;lt;/code&amp;gt;;&lt;br /&gt;
* as a default for new objects: a &amp;lt;code&amp;gt;DEFAULT COLLATION&amp;lt;/code&amp;gt; clause on &amp;lt;code&amp;gt;CREATE TABLE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CREATE SCHEMA&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ALTER TABLE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ALTER SCHEMA&amp;lt;/code&amp;gt; and SQL functions.&lt;br /&gt;
&lt;br /&gt;
Schema-level default collation for tables arrived in Spark 4.1.0 (SPARK-52219), and Spark 4.2.0 extended collation support to &amp;lt;code&amp;gt;CHAR&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;VARCHAR&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CREATE TABLE AS SELECT&amp;lt;/code&amp;gt; and SQL user-defined functions.&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;r420&amp;quot; /&amp;gt; The function &amp;lt;code&amp;gt;collation(expr)&amp;lt;/code&amp;gt; returns the fully qualified collation of a value; for a plain string literal it returns &amp;lt;code&amp;gt;SYSTEM.BUILTIN.UTF8_BINARY&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;expr&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/collationExpressions.scala |title=collationExpressions.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does collation affect comparison, grouping and joins? ==&lt;br /&gt;
Collation changes the result of every operation that compares strings. In Spark&amp;#039;s own test suite, a table with the values &amp;lt;code&amp;gt;aaa&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;AAA&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;bbb&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;BBB&amp;lt;/code&amp;gt; produces four groups when grouped by a &amp;lt;code&amp;gt;UTF8_BINARY&amp;lt;/code&amp;gt; column and two groups when grouped by a &amp;lt;code&amp;gt;UTF8_LCASE&amp;lt;/code&amp;gt; column. A filter &amp;lt;code&amp;gt;= &amp;#039;aaa&amp;#039;&amp;lt;/code&amp;gt; on the &amp;lt;code&amp;gt;UTF8_LCASE&amp;lt;/code&amp;gt; column returns both &amp;lt;code&amp;gt;aaa&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;AAA&amp;lt;/code&amp;gt;, and a self-join on that column matches each value with both spellings.&amp;lt;ref name=&amp;quot;tests&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/test/resources/sql-tests/results/collations-basic.sql.out |title=collations-basic.sql.out (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Spark&amp;#039;s hash joins compare keys by their bytes, which is wrong for a collation in which different bytes can be equal. The analyzer rule &amp;lt;code&amp;gt;RewriteCollationJoin&amp;lt;/code&amp;gt; therefore wraps equality join keys of non-binary collations, including those nested in structs and arrays, in a &amp;lt;code&amp;gt;CollationKey&amp;lt;/code&amp;gt; expression, so that equal values under the collation produce equal keys and hash joins still work.&amp;lt;ref name=&amp;quot;join&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/RewriteCollationJoin.scala |title=RewriteCollationJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both sides of a comparison must agree on one collation. Spark picks it by strength: a &amp;lt;code&amp;gt;COLLATE&amp;lt;/code&amp;gt; clause is explicit and wins; a column, variable or subquery result carries an implicit collation; a literal carries the default collation and loses to either. A literal compared with a &amp;lt;code&amp;gt;UTF8_LCASE&amp;lt;/code&amp;gt; column is therefore compared case-insensitively.&amp;lt;ref name=&amp;quot;coercion&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/CollationTypeCoercion.scala |title=CollationTypeCoercion.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If two explicit collations conflict, or two columns with different implicit collations meet, the query fails with &amp;lt;code&amp;gt;COLLATION_MISMATCH&amp;lt;/code&amp;gt; (SQLSTATE &amp;lt;code&amp;gt;42P21&amp;lt;/code&amp;gt;); its &amp;lt;code&amp;gt;IMPLICIT&amp;lt;/code&amp;gt; sub-condition suggests adding a &amp;lt;code&amp;gt;COLLATE&amp;lt;/code&amp;gt; clause.&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; See [[Spark error classes]] for how such conditions are structured.&lt;br /&gt;
&lt;br /&gt;
== Limitations ==&lt;br /&gt;
* Non-&amp;lt;code&amp;gt;UTF8_BINARY&amp;lt;/code&amp;gt; strings are not allowed in map keys unless &amp;lt;code&amp;gt;spark.sql.collation.allowInMapKeys&amp;lt;/code&amp;gt; is set; it defaults to false.&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;
* Some functions do not support every collation and fail with &amp;lt;code&amp;gt;UNSUPPORTED_COLLATION.FOR_FUNCTION&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;errjson&amp;quot; /&amp;gt;&lt;br /&gt;
* Since Spark 4.1.0 the user-facing hash functions ignore collation (SPARK-52828); &amp;lt;code&amp;gt;spark.sql.legacy.collationAwareHashFunctions&amp;lt;/code&amp;gt; restores the earlier behaviour.&amp;lt;ref name=&amp;quot;r410&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&lt;br /&gt;
* Since Spark 4.3, a streaming file sink rejects a partition column that is a string with a non-default collation such as &amp;lt;code&amp;gt;UTF8_LCASE&amp;lt;/code&amp;gt;.&amp;lt;ref&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/ss-migration-guide.html |title=Structured Streaming Migration Guide (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* Several correctness bugs involving non-binary collations have been fixed in recent releases, for example in &amp;lt;code&amp;gt;NOT IN&amp;lt;/code&amp;gt; subqueries and constraint inference (SPARK-54852 and SPARK-55647 in 4.2.0, SPARK-57727 in 4.3.0).&amp;lt;ref name=&amp;quot;r420&amp;quot; /&amp;gt;&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;
== Example ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE users (name STRING COLLATE UTF8_LCASE) USING parquet;&lt;br /&gt;
INSERT INTO users VALUES (&amp;#039;Alice&amp;#039;), (&amp;#039;ALICE&amp;#039;), (&amp;#039;bob&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
SELECT count(*) FROM users WHERE name = &amp;#039;alice&amp;#039;;   -- 2&lt;br /&gt;
SELECT name, count(*) FROM users GROUP BY name;    -- 2 groups: alice/ALICE and bob&lt;br /&gt;
&lt;br /&gt;
SELECT &amp;#039;a&amp;#039; = &amp;#039;A&amp;#039;;                                  -- false (UTF8_BINARY)&lt;br /&gt;
SELECT &amp;#039;a&amp;#039; COLLATE UTF8_LCASE = &amp;#039;A&amp;#039;;               -- true&lt;br /&gt;
SELECT collation(&amp;#039;a&amp;#039; COLLATE UNICODE_CI);          -- SYSTEM.BUILTIN.UNICODE_CI&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Collation in other SQL engines ==&lt;br /&gt;
Collation is a general SQL concept, and engines implement it differently. PostgreSQL offers a &amp;lt;code&amp;gt;libc&amp;lt;/code&amp;gt; provider based on the operating system locales and an &amp;lt;code&amp;gt;icu&amp;lt;/code&amp;gt; provider, and it supports case-insensitive and accent-insensitive comparison through user-defined &amp;quot;nondeterministic&amp;quot; collations; its documentation notes that these carry a performance cost and do not support some pattern-matching operations.&amp;lt;ref name=&amp;quot;pg&amp;quot;&amp;gt;{{Cite web |url=https://www.postgresql.org/docs/current/collation.html |title=Collation Support (PostgreSQL 19 documentation) |publisher=PostgreSQL Global Development Group |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In Spark, the built-in &amp;lt;code&amp;gt;UTF8_LCASE&amp;lt;/code&amp;gt; collation gives case-insensitive comparison without ICU.&amp;lt;ref name=&amp;quot;show&amp;quot; /&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;
* [[Delta Lake]]&lt;br /&gt;
* [[Apache Parquet]]&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>