SEPURI-SAI-KRISHNA opened a new pull request, #60443:
URL: https://github.com/apache/spark/pull/60443
### What changes were proposed in this pull request?
For the case-sensitive, accent-insensitive ICU collations (`UNICODE_AI`,
`en_AI`, `de_AI`, ..., and their `_RTRIM` variants), the comparator that
`CollationFactory` builds no longer calls `Collator.compare` on the collator of
the collation. It orders two strings with a collator of primary strength for
the same locale, and when they are equal at the primary level it compares their
collation keys. The other collations are unchanged.
### Why are the changes needed?
Spark compares the strings of an ICU collation in two ways: with the
comparator of the collation (`Collator.compare`: `=`, `<`, sorting, sort-based
aggregation, `In`), and by its sort key (`Collator.getCollationKey`: hash
joins, hash aggregation, shuffles, `InSet`). The two have to agree, but for the
CS_AI collations they do not when a string contains U+00DF (sharp s). So an
equi-join returns rows for which its own condition is false, and equality is
not transitive:
```sql
CREATE TEMPORARY VIEW t AS SELECT id, CAST(s AS STRING COLLATE de_AI) AS s
FROM VALUES (1, 'Straße'), (2, 'Strasse') AS v(id, s);
SELECT a.id, b.id, a.s = b.s FROM t a CROSS JOIN t b;
-- (1,1,true) (1,2,false) (2,1,false) (2,2,true)
SELECT a.id, b.id FROM t a JOIN t b ON a.s = b.s;
-- (1,1) (1,2) (2,1) (2,2)
SELECT count(DISTINCT s) FROM t; -- 2
SELECT id FROM t WHERE s IN ('Strasse', 'x'); -- 2
SELECT id FROM t WHERE s IN ('Strasse', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6',
'x7', 'x8', 'x9', 'x10');
-- 1 and 2 (InSet)
-- U+FF53 is the fullwidth s
SELECT 'ß' COLLATE UNICODE_AI = 'ss' COLLATE UNICODE_AI, -- false
'ß' COLLATE UNICODE_AI = 'ss' COLLATE UNICODE_AI, -- true
'ss' COLLATE UNICODE_AI = 'ss' COLLATE UNICODE_AI; -- true
```
These collations are built with primary strength and the case level
(`ks=level1`, `kc=true`). For this setting ICU4J itself is inconsistent. With
plain icu4j 78.3 (the version on master; 76.1 behaves the same):
```
"ß" vs "ss" compare = 1 collation keys equal
"ß" vs "SS" compare = 1 collation key of "ß" is less
"ßあ" vs "ssあ" compare = 0 collation keys equal
"ßあ" vs "SSあ" compare = -1 collation key of "ßあ" is less
```
The cause is in the fast path that `Collator.compare` takes for Latin text,
`com.ibm.icu.impl.coll.CollationFastLatin`. It holds a character that maps to
two collation elements as two 16-bit "mini CEs" in one `int`, and `getCases`
(also `getSecondaries`, `getTertiaries` and `getQuaternaries`) tells one
element from two with `if (pair <= 0xffff)`. The code is a port of ICU4C, where
`pair` is a `uint32_t`. In Java the `int` is negative when the second mini CE
has its top bit set, so the two elements are taken for one, and their case
weights are compared unmasked. That is the case for a sharp s (s + s) in the
fast Latin table of all 145 locales that have one (and for U+0149 in 20 of
them, where I found no pair of strings that it changes). ICU4C returns equal
for `"ß"` and `"ss"` (`ucol_strcoll`, checked with 74.2), and so does ICU4J
when the strings do not take the fast path (the last two lines above). So the
collation keys are right, and `'Straße' = 'Strasse'` should be true in `d
e_AI`, as it is in `de_CI_AI`. The code is the same on the main branch of ICU.
The primary level of the fast path is not affected (`getPrimaries` masks the
value first), and the other levels do not matter at primary strength. So the
comparator of these collations can stay on the fast path for the primary level,
and needs the collation keys only for strings that are equal there, that is,
differ by case at most.
I compared the result with the collation keys on all pairs of 8,897 one- and
two-character strings (the letters up to U+024F, some apostrophes and other
characters) for the root locale and for `hu`, `az`, `hr`, `de`, `tr`, `da`,
`sv`, `es` and `cs` (79 million pairs each). `Collator.compare` of the
collation differs from the keys on 22 to 28 pairs for equality and 84 to 102
for the order, all of them with U+00DF. The new comparator never differs.
Sorting 200,000 strings of 8, 32 and 128 lowercase letters (best of 5, ms):
| strings | `Collator.compare` (before) | this PR | collation keys only |
|---|---|---|---|
| all distinct | 193 / 162 / 176 | 200 / 165 / 197 | 1473 / 3161 / 11494 |
| 1000 distinct values | 96 / 104 / 85 | 103 / 104 / 95 | 1048 / 2744 / 8097
|
| 1000 values, first letter in either case | 115 / 155 / 300 | 220 / 384 /
945 | 946 / 2169 / 7341 |
So comparing by the collation keys only, which is the simplest way to make
the two agree, would be 8 to 65 times slower. With this change the cost is the
same unless many strings differ only by case, and then it is 2 to 3 times.
For the other strengths, `Collator.compare` and the collation keys agree on
equality for all pairs (36 million pairs each). Only the order of `"ß"` and
`"ſs"` differs, which has no effect in Spark, because the sort key is used for
equality and hashing only. So `UNICODE`, `UNICODE_CI` and `UNICODE_CI_AI` are
left as they are.
SPARK-49667 already disallowed the CS_AI collations in the functions that
use the ICU `StringSearch`. Equality, ordering, grouping, joins and `IN` still
accept them.
### Does this PR introduce _any_ user-facing change?
Yes. Under the case-sensitive, accent-insensitive ICU collations, `=`, `<`,
`IN`, `DISTINCT`, `GROUP BY` and `ORDER BY` now treat a sharp s as "ss" of the
same case, as joins already did. For example `'Straße' COLLATE de_AI =
'Strasse' COLLATE de_AI` returns true instead of false. The released versions
since 4.0.0 are affected (reproduced on 4.0.3).
### How was this patch tested?
- A new test in `CollationFactorySuite`: for 12 collations and all pairs of
20 strings around the sharp s, the comparator, the equality function and the
sort key agree. It fails without the change.
- A new test in `CollationSuite`: filters, `IN` (`In` and `InSet`),
`DISTINCT`, `GROUP BY`, hash and nested-loop joins and `ORDER BY` agree on
`'Straße'`, `'Strasse'` and `'STRASSE'`. It fails without the change.
- Ran the tests of the `unsafe` module and the suites in
`org.apache.spark.sql.collation`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Fable 5.2)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]