Collation (Spark SQL)
| Also known as | String collation, collated string type |
|---|---|
| Field | SQL query engines |
| Key parts | COLLATE clause, UTF8_BINARY, UTF8_LCASE, ICU-based UNICODE and locale collations, _CI, _AI and _RTRIM modifiers, DEFAULT COLLATION
|
| Open source implementations | Apache Spark |
| Related | Spark error classes, Delta Lake, Apache Parquet |
In Apache Spark, a collation is a property of the STRING type that sets how Spark SQL compares, sorts, groups and joins string values, for example whether 'a' equals 'A'. Spark SQL added string collation support in Spark 4.0.0 (SPARK-46830).[1] The default collation, UTF8_BINARY, compares the raw UTF-8 bytes; other collations make comparisons case-insensitive, accent-insensitive, trailing-space-insensitive or locale-aware.[2]
What collations does Spark SQL support?
Spark SQL has two kinds of collation. Two, with their _RTRIM variants, are built into Spark and do not depend on a locale: UTF8_BINARY (case- and accent-sensitive byte comparison) and UTF8_LCASE (case-insensitive). The others come from the International Components for Unicode (ICU) library: UNICODE, which uses the locale-independent Unicode rules, and locale collations such as en_USA.[2] The Spark 4.3.0 build uses ICU4J 78.3.[3]
A collation name can carry modifiers. _CI makes it case-insensitive, _AI accent-insensitive and _RTRIM ignores trailing spaces, giving names such as UNICODE_CI_AI, en_USA_CI or UTF8_BINARY_RTRIM.[2][4] The SHOW COLLATIONS [LIKE pattern] 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.[5][2]
How to set a collation in Spark SQL
A collation can be attached at several levels, according to the Spark 4.3.0 grammar:[6]
- on a column type:
STRING COLLATE UTF8_LCASE, and also onCHARandVARCHAR; - on an expression:
name COLLATE UNICODE_CI, or the functioncollate(expr, 'UTF8_LCASE'); - as a default for new objects: a
DEFAULT COLLATIONclause onCREATE TABLE,CREATE SCHEMA,ALTER TABLE,ALTER SCHEMAand SQL functions.
Schema-level default collation for tables arrived in Spark 4.1.0 (SPARK-52219), and Spark 4.2.0 extended collation support to CHAR/VARCHAR, CREATE TABLE AS SELECT and SQL user-defined functions.[7][5] The function collation(expr) returns the fully qualified collation of a value; for a plain string literal it returns SYSTEM.BUILTIN.UTF8_BINARY.[8]
How does collation affect comparison, grouping and joins?
Collation changes the result of every operation that compares strings. In Spark's own test suite, a table with the values aaa, AAA, bbb and BBB produces four groups when grouped by a UTF8_BINARY column and two groups when grouped by a UTF8_LCASE column. A filter = 'aaa' on the UTF8_LCASE column returns both aaa and AAA, and a self-join on that column matches each value with both spellings.[9]
Spark's hash joins compare keys by their bytes, which is wrong for a collation in which different bytes can be equal. The analyzer rule RewriteCollationJoin therefore wraps equality join keys of non-binary collations, including those nested in structs and arrays, in a CollationKey expression, so that equal values under the collation produce equal keys and hash joins still work.[10]
Both sides of a comparison must agree on one collation. Spark picks it by strength: a COLLATE 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 UTF8_LCASE column is therefore compared case-insensitively.[11]
If two explicit collations conflict, or two columns with different implicit collations meet, the query fails with COLLATION_MISMATCH (SQLSTATE 42P21); its IMPLICIT sub-condition suggests adding a COLLATE clause.[12] See Spark error classes for how such conditions are structured.
Limitations
- Non-
UTF8_BINARYstrings are not allowed in map keys unlessspark.sql.collation.allowInMapKeysis set; it defaults to false.[13] - Some functions do not support every collation and fail with
UNSUPPORTED_COLLATION.FOR_FUNCTION.[12] - Since Spark 4.1.0 the user-facing hash functions ignore collation (SPARK-52828);
spark.sql.legacy.collationAwareHashFunctionsrestores the earlier behaviour.[7][13] - Since Spark 4.3, a streaming file sink rejects a partition column that is a string with a non-default collation such as
UTF8_LCASE.[14] - Several correctness bugs involving non-binary collations have been fixed in recent releases, for example in
NOT INsubqueries and constraint inference (SPARK-54852 and SPARK-55647 in 4.2.0, SPARK-57727 in 4.3.0).[5][15]
Example
CREATE TABLE users (name STRING COLLATE UTF8_LCASE) USING parquet;
INSERT INTO users VALUES ('Alice'), ('ALICE'), ('bob');
SELECT count(*) FROM users WHERE name = 'alice'; -- 2
SELECT name, count(*) FROM users GROUP BY name; -- 2 groups: alice/ALICE and bob
SELECT 'a' = 'A'; -- false (UTF8_BINARY)
SELECT 'a' COLLATE UTF8_LCASE = 'A'; -- true
SELECT collation('a' COLLATE UNICODE_CI); -- SYSTEM.BUILTIN.UNICODE_CI
Collation in other SQL engines
Collation is a general SQL concept, and engines implement it differently. PostgreSQL offers a libc provider based on the operating system locales and an icu provider, and it supports case-insensitive and accent-insensitive comparison through user-defined "nondeterministic" collations; its documentation notes that these carry a performance cost and do not support some pattern-matching operations.[16] In Spark, the built-in UTF8_LCASE collation gives case-insensitive comparison without ICU.[2]
See also
References
- ↑ Spark Release 4.0.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 2.2 2.3 2.4 SHOW COLLATIONS (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ pom.xml (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ CollationFactory.java (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 5.0 5.1 5.2 Spark Release 4.2.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SqlBaseParser.g4 (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 7.0 7.1 Spark Release 4.1.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ collationExpressions.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ collations-basic.sql.out (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ RewriteCollationJoin.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ CollationTypeCoercion.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 12.0 12.1 error-conditions.json (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 13.0 13.1 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Structured Streaming Migration Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 4.3.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Collation Support (PostgreSQL 19 documentation). PostgreSQL Global Development Group.Retrieved 2026-11-23.