Skip to content

[SPARK-57029][SQL][TESTS] Add byte-level visibility golden for ICU collation sort keys - #56096

Open
yaooqinn wants to merge 5 commits into
apache:masterfrom
yaooqinn:spark-icu-sortkey-golden
Open

yaooqinn wants to merge 5 commits into
apache:masterfrom
yaooqinn:spark-icu-sortkey-golden

Conversation

@yaooqinn

@yaooqinn yaooqinn commented May 25, 2026 •

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Add a test-only visibility golden suite for ICU collation sort keys:

  • New test: sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala
  • New golden: sql/core/src/test/resources/collations/ICU-collations-sort-keys.md (38 cells, ~1900 bytes)

The suite snapshots (collation, input) -> hex(CollationKey) for 14 dimensions covering the ICU surface Spark uses: UCA primary / tertiary case / secondary diacritic; NFC vs NFD canonical equivalence; combining-mark reorder visibility; SMP surrogate path; BMP precomposed Hangul; ASCII punct / space at primary; Turkish locale tailoring (en_USA + tr); CJK Han implicit weighting; empty string boundary; U+FFFD; C0 controls; variation selectors.

Each test asserts a contract on the recorded bytes: row existence, non-empty hex, level segmentation for NON_IGNORABLE alternate handling, prefix-share invariants for Turkish tailoring, and the ICU compressed-sortkey lead byte for CJK implicit weights. Drift-prone dims fire with named-condition messages if Spark's ICU configuration or library version changes the semantic; stable dims fire if a regression silently drops or folds a cell.

The pattern mirrors ICUCollationsMapSuite (which lists the ICU locale surface) and is scoped to ICU-backed collations only. UTF8_LCASE is out of scope -- it does not go through com.ibm.icu.text.Collator.getCollationKey() and is already covered by CollationFactorySuite.

Why are the changes needed?

icu4j upgrades silently change ORDER BY ... COLLATE semantics across Spark versions. Past upgrade PRs (e.g. SPARK-50189, SPARK-52038, SPARK-54447, SPARK-55308, SPARK-56397) touch only the dependency file and benchmark results -- they ship no byte-level regression on sort output, so a CLDR re-weighting can land in master without any reviewer signal.

Empirical evidence from a local cross-version probe (icu4j 72.1 through 78.3, 33 test cells covering Latin / Turkish / zh_CN): the icu4j 75 → 76 transition altered 23/33 cell sortkeys (UCA primary base shift, e.g. en_US 'a': 0x2a → 0x2b); 77.1 → 78.3 (Spark 4.1 → master, SPARK-52038 → SPARK-56397) altered 4/33 cells silently. None of these drifts surfaced in PR review.

This suite makes such drift visible during ICU upgrade review: any change to the recorded bytes shows up as a golden diff that a reviewer must explicitly accept. It is not a stability contract -- the disclaimer at golden line 1, the GOLDEN_DISCLAIMER constant (and the line-1 assert that pins it), and the suite scaladoc all state that downstream consumers MUST NOT rely on byte equality across Spark versions. The file is a review-trigger snapshot, nothing more.

Reviewer note: when this golden file changes on a PR that does not bump icu4j, please request a revert -- regeneration belongs in the ICU upgrade PR.

Does this PR introduce any user-facing change?

No. Test-only; no SQLConf, no public API, no production code path.

How was this patch tested?

  • New suite ICUCollationSortKeyGoldenSuite (16 tests). Local 16/16 PASS on master, two-pass deterministic: regenerate the golden, then re-run from disk -- bytes match.
  • Regenerate the golden with SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "sql/testOnly org.apache.spark.sql.ICUCollationSortKeyGoldenSuite"; the suite enforces idempotency and that on-disk bytes match the regen output.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.7

@yaooqinn
yaooqinn force-pushed the spark-icu-sortkey-golden branch from b0fe8cc to 8c38b17 Compare May 25, 2026 10:54
@yaooqinn
yaooqinn marked this pull request as ready for review May 25, 2026 14:01
@yaooqinn
yaooqinn requested a review from dongjoon-hyun May 25, 2026 14:55

@dongjoon-hyun dongjoon-hyun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way for GitHub not to consider this MD file as a binary, @yaooqinn ?

Image Image

@yaooqinn

Copy link
Copy Markdown
Member Author

Hi @dongjoon-hyun, I shared the same concern with you until copilot showed me this sibling file.
https://github.com/apache/spark/blob/master/sql/core/src/test/resources/collations/ICU-collations-map.md

If you think we shall use txt like sql golden files, I can switch to txt based.

@dongjoon-hyun

Copy link
Copy Markdown
Member

Got it. Never mind for my previous comment.

cc @cloud-fan

@ilicmarkodb ilicmarkodb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yaooqinn! Should we add a few more different collations (e.g., sr) for the same inputs to the golden file?

@yaooqinn

Copy link
Copy Markdown
Member Author

Thanks @ilicmarkodb. Were you thinking of specific cells / patterns sr would catch that the current matrix misses, or broader script coverage in general? Asking because the matrix is sized to keep ICU upgrade reviewers' diff-eyeball cost bounded — if there's a concrete drift pattern only sr surfaces, that's a strong case to add; if it's "more is better" coverage, I'd lean toward holding to keep the visibility signal sharp.

@yaooqinn

yaooqinn commented Jun 1, 2026

Copy link
Copy Markdown
Member Author

While building this golden suite I measured what the ICU4J 77.1 → 78.3 bump actually does to collation sort keys. Running Collator.getCollationKey over the 44 golden inputs here (same ROOT + strength mapping the suite uses), 4 cells differ between the two versions:

  • 😀 U+1F600 and 𐀀 U+10000 — SMP/emoji, expected
  • 北京 and 中国 — CJK, sort keys change (full keys in the golden file)

Spark 4.1 ships icu4j 77.1, 4.2 ships 78.3 (the bump landed via SPARK-56397). So ORDER BY col COLLATE UNICODE over CJK data can return a different row order on 4.2 vs 4.1 — user-visible. SPARK-56397 declared user-facing change = No and carried no collation regression test, so this rode in silently.

Not arguing the bump is wrong — newer ICU is the right call. Flagging it for a possible 4.2 release-note / known-issues line so users doing CJK ORDER BY aren't surprised. cc @huaxingao as 4.2 release manager for visibility.

@github-actions

Copy link
Copy Markdown

We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!

@github-actions github-actions Bot added the Stale label Sep 10, 2026
@github-actions github-actions Bot closed this Sep 10, 2026
…llation sort keys

Add a test-only visibility golden suite for ICU collation sort keys:

- New test: `sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala`
- New golden: `sql/core/src/test/resources/collations/ICU-collations-sort-keys.md` (38 cells, ~1900 bytes)

The suite snapshots `(collation, input) -> hex(CollationKey)` for 14 dimensions covering the ICU surface Spark uses: UCA primary / tertiary case / secondary diacritic; NFC vs NFD canonical equivalence; combining-mark reorder visibility; SMP surrogate path; BMP precomposed Hangul; ASCII punct / space at primary; Turkish locale tailoring (en_USA + tr); CJK Han implicit weighting; empty string boundary; U+FFFD; C0 controls; variation selectors.

Each test asserts a contract on the recorded bytes: row existence, non-empty hex, level segmentation for NON_IGNORABLE alternate handling, prefix-share invariants for Turkish tailoring, and the ICU compressed-sortkey lead byte for CJK implicit weights. Drift-prone dims fire with named-condition messages if Spark's ICU configuration or library version changes the semantic; stable dims fire if a regression silently drops or folds a cell.

The pattern mirrors `ICUCollationsMapSuite` (which lists the ICU locale surface) and is scoped to ICU-backed collations only. `UTF8_LCASE` is out of scope -- it does not go through `com.ibm.icu.text.Collator.getCollationKey()` and is already covered by `CollationFactorySuite`.

icu4j upgrades silently change `ORDER BY ... COLLATE` semantics across Spark versions. Past upgrade PRs (e.g. SPARK-50189, SPARK-52038, SPARK-54447, SPARK-55308, SPARK-56397) touch only the dependency file and benchmark results -- they ship no byte-level regression on sort output, so a CLDR re-weighting can land in master without any reviewer signal.

Empirical evidence from a local cross-version probe (icu4j 72.1 through 78.3, 33 test cells covering Latin / Turkish / zh_CN): the icu4j 75 → 76 transition altered 23/33 cell sortkeys (UCA primary base shift, e.g. `en_US 'a': 0x2a → 0x2b`); 77.1 → 78.3 (Spark 4.1 → master, SPARK-52038 → SPARK-56397) altered 4/33 cells silently. None of these drifts surfaced in PR review.

This suite makes such drift visible during ICU upgrade review: any change to the recorded bytes shows up as a golden diff that a reviewer must explicitly accept. It is **not** a stability contract -- the disclaimer at golden line 1, the `GOLDEN_DISCLAIMER` constant (and the line-1 assert that pins it), and the suite scaladoc all state that downstream consumers MUST NOT rely on byte equality across Spark versions. The file is a review-trigger snapshot, nothing more.

Reviewer note: when this golden file changes on a PR that does not bump `icu4j`, please request a revert -- regeneration belongs in the ICU upgrade PR.

No. Test-only; no SQLConf, no public API, no production code path.

- New suite `ICUCollationSortKeyGoldenSuite` (16 tests). Local 16/16 PASS on master, two-pass deterministic: regenerate the golden, then re-run from disk -- bytes match.
- Regenerate the golden with `SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "sql/testOnly org.apache.spark.sql.ICUCollationSortKeyGoldenSuite"`; the suite enforces idempotency and that on-disk bytes match the regen output.

Generated-by: Claude Opus 4.7
…ut column

A literal NUL in the golden file makes git and GitHub treat it as binary, so an
ICU upgrade PR would show 'binary file changed' instead of the sort-key rows
that drifted. Render control characters as backslash-u escapes and look rows up
through the same function.
@yaooqinn yaooqinn reopened this Nov 10, 2026
@yaooqinn
yaooqinn force-pushed the spark-icu-sortkey-golden branch from 8c38b17 to 8d52390 Compare November 10, 2026 06:26
… sv, de, hi, ko) to the sort-key golden

Each cell is an exemplar character of its locale; except sr/sr_Latn (kept as a
baseline), each one's sort key differs between ICU4J 78.3 and 79.1. For zh the
three characters also change position in ORDER BY.
@yaooqinn

Copy link
Copy Markdown
Member Author

Reopening. ICU4J 79.1 shipped on 2026-10-22 and master is still on 78.3.

Changes since the last round, rebased on current master:

  1. The golden had a raw NUL (C0 row), so git/GitHub treated it as binary and an ICU bump would have shown "binary file changed". Control characters in the input column are now written as escapes.
  2. Following @ilicmarkodb's suggestion, added locale tailoring cells: sr, sr_Latn, plus zh, zh_Hant, ja, sv, de, hi, ko. Except for the two Serbian locales, each input is an exemplar character of that locale whose sort key changes between 78.3 and 79.1. sr/sr_Latn show no change and are kept as a baseline.

Bumping icu4j.version to 79.1 locally makes the suite fail as intended; regenerating changes 15 of 59 rows, now as a text diff (e.g. UNICODE 😀, tr I/ı, UNICODE 北京, zh 匙/啦/哇, ja 々/ー, sv å/ö, de ß).

Correction to my earlier comment: a sort-key change is not always an order change. Sorting each locale's CLDR exemplar characters, 78.3 → 79.1 changes order only for zh (pinyin): 匙, 啦 and 哇 move. 77.1 → 78.3 swaps one CJK pair under root (劳/労).

@yaooqinn yaooqinn removed the Stale label Nov 10, 2026
@dongjoon-hyun

Copy link
Copy Markdown
Member

Thank you for refreshing.

@dongjoon-hyun dongjoon-hyun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the update, @yaooqinn. I left inline comments. Here is a summary.

  1. Several assertions cannot fail. ICU sort keys always contain level separators and a terminator (the empty string is 01 01 00), so the hex.nonEmpty checks always pass, and the NUL/US/VS-15/VS-16 cells are byte-identical to the empty-string cell.
  2. Ordering is not checked. ORDER BY uses comparator, not sortKeyFunction, and no test asserts relative order. Meanwhile, raw bytes also flag order-preserving weight shifts, which may lead to rubber-stamp regeneration on every ICU upgrade.
  3. The disclaimer understates the impact. sortKeyFunction bytes feed hash()/xxhash64() and the sketch aggregates, so sort-key drift is user-visible beyond ordering and should be documented when it happens.
  4. Some helpers and comments are inaccurate: the Turkish primaryPrefix, primarySegmentBytes, the NO_DECOMPOSITION and canonical-equivalence claims, and the 78.3 to 79.1 comment. In addition, several rows are visually indistinguishable in the golden diff.
  5. There is room to simplify. One golden comparison plus semantic assertions on live sort keys would cover most of the current tests.

Also, the PR description seems outdated (it says 38 cells and 16 tests, while there are 59 rows and 18 tests now).

val linearB = new String(Character.toChars(0x10000)) // Linear B B008 A
val emojiHex = hexFor("UNICODE", emoji)
val linearBHex = hexFor("UNICODE", linearB)
assert(emojiHex.nonEmpty && linearBHex.nonEmpty,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nonEmpty cannot fail here. ICU sort keys always include level separators and a terminator, so even the empty string produces 01 01 00 (golden L44). If the surrogate pair were dropped before reaching ICU, the key would become 01 01 00 and this assertion would still pass.

The same pattern appears at L219, L232, L323, L350, L362, L375 and L386. Shall we compare against the empty-string key instead?

assert(emojiHex != hexFor("UNICODE", ""))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, nonEmpty could never fail. In dfc8def the supplementary, Hangul and U+FFFD inputs are compared against the empty-string key instead ("UNICODE keeps supplementary, Hangul and U+FFFD code points"), and the other nonEmpty checks you listed are removed.

// C-style truncation anywhere in the UTF8String -> ICU chain would drop
// everything from NUL onwards. U+001F (UNIT SEPARATOR) is a
// representative non-NUL C0 control. Both should survive end-to-end.
val nulHex = hexFor("UNICODE", "\u0000")

@dongjoon-hyun dongjoon-hyun Nov 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cells cannot show the regressions described in the comments. \u0000, \u001f, \ufe0e and \ufe0f all produce 01 01 00 (golden L46-L49), which is byte-identical to the empty string (L44).

  • NUL: truncating "\u0000" at NUL yields "", whose key is the same, so a C-style truncation would not change the golden. Embedding it between letters, e.g. "a\u0000b", would make truncation visible.
  • VS-15/VS-16: dropping a fully-ignorable character is not observable in the sort key at all. Only promotion to a non-ignorable weight is detectable, and that shows up in the golden diff, not in the assertion.
  • U+FFFD (L349): "\ufffd" is well-formed, so it does not exercise the ill-formed UTF-8 to U+FFFD path in toValidString. That would need a cell like UTF8String.fromBytes(Array(0xff.toByte)), which GoldenCell(String) cannot express.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. In dfc8def the controls and variation selectors sit between letters. The test asserts each equals the key of ab and that a\u0000b differs from a, so truncation at NUL now fails. Promotion of a selector to a non-ignorable weight fails the == ab check too.

REGEN_HINT)
}

test("UNICODE primary order separates basic-Latin letters") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name says "separates basic-Latin letters", but it only checks that the rows exist. Nothing in this suite asserts relative order (e.g. a < b < c, or a and A sharing a primary weight).

Since this PR is motivated by ORDER BY ... COLLATE drift, could we assert the ordering directly with collation.comparator? Note that ORDER BY goes through comparator, not sortKeyFunction (CollationFactory.java L1037-L1040).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in dfc8def. The test now sorts with comparator and asserts a, A, b, B, c, and the CI/AI tests use comparator.compare instead of key bytes.


// Helper: strip the trailing tertiary/case section (the last
// "<weight> 00" terminator pair) to compare primary prefixes only.
def primaryPrefix(hex: String): String = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropRight(2) assumes that the key ends with a 1-byte tertiary weight plus the terminator.

  • The remaining prefix still includes the secondary level (e.g. 3b 01 05 01 for tr i), so this is not a primary-only comparison.
  • The assumption does not hold even within this golden: en_USA U+0130 is 3b 01 45 9c 01 dc 05 00 (golden L37), with a 2-byte tertiary.

Taking the bytes before the first 01 would compare primary weights only. Also, L275 says "Under en_USA no such re-pairing exists", but nothing asserts it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that helper was wrong. In dfc8def I removed it and the Turkish test compares with tr_CI: I = ı, İ = i, I != i, plus en_USA_CI I = i for the non-Turkish pairing.

"en_USA_AI should fold 'e' and 'e-acute' to identical sort keys")
}

test("UNICODE collates NFC and NFD canonically-equivalent forms identically") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildCollation does not set any decomposition mode; it only sets the ks/kc locale keywords (CollationFactory.java L1003-L1017). So "Spark's NO_DECOMPOSITION ICU option" here, at L188 and at L478 is ICU's default rather than a Spark setting.

The test name is also misleading. Canonical equivalence is not honored in general: D5 (golden L27/L28) shows that two canonically-equivalent inputs get different sort keys under UNICODE. The NFC/NFD equality here holds only because both inputs happen to be FCD. Shall we rename the test and fix the comments so that readers don't conclude that canonical equivalence is guaranteed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the comment was wrong. In dfc8def the NO_DECOMPOSITION wording is gone and the test is renamed to say what it checks: NFC and NFD e-acute share a key, and the two combining-mark orders do not. It now asserts both, so nobody should read it as general canonical equivalence.

s"got: ${goldenLines.head}")
}

test("writeGoldenFile is deterministic and writes no BOM") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the infrastructure checks look redundant with the on-disk comparison at L107. If the on-disk file equals the writeGoldenFile output, then the disclaimer on line 1, the absence of a BOM, the absence of control characters, and the existence of every row already follow. The semantic tests also re-parse the markdown (parseRow, stripSinglePadding, rowFor, hexOf) instead of calling sortKeyBytes directly.

Following ICUCollationsMapSuite, one golden comparison plus semantic assertions on live sort keys would cover the same ground in far fewer lines, and would avoid the parsing pitfalls (|, padding).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. In dfc8def I followed ICUCollationsMapSuite: one full-file comparison, and semantic tests that call sortKeyFunction and comparator directly. The disclaimer, BOM, control-character and row-existence tests are removed, and so is the markdown parsing.

s"UNICODE basic-Latin rows missing: ${missing.mkString(", ")}; $REGEN_HINT")
}

test("UNICODE_CI folds tertiary case; UNICODE and en_USA distinguish it") {

@dongjoon-hyun dongjoon-hyun Nov 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these semantic checks already exist in CollationFactorySuite: "collation aware equality and sort key" compares sortKeyFunction bytes, e.g. UNICODE_CI aaa/AAA and UNICODE "\u00e5" vs "a\u030A". Also:

  • withTempDir from SparkFunSuite could replace Utils.createTempDir() at L92.
  • The ICU version is available via Collation.version. Recording it in the golden header would make "the golden changed without an icu4j bump" visible in the diff itself.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. In dfc8def the header records the ICU version from Collation.version, so a golden change without an icu4j bump shows in the diff. The temp-dir code is gone. I kept a one-line case-folding check next to the golden rows on purpose, so each section has a live assertion beside it. Happy to drop it if you'd rather rely on CollationFactorySuite alone.


test("ICU locale tailorings for sr, zh, ja, sv, de, hi and ko are recorded") {
// One block per locale tailoring. Inputs are exemplar characters of each
// locale whose sort keys changed in a past ICU upgrade (78.3 to 79.1), so

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

master is still on icu4j 78.3 (as mentioned in your latest comment), so "changed in a past ICU upgrade (78.3 to 79.1)" reads as if Spark had already gone through that upgrade. Maybe "whose sort keys differ between ICU4J 78.3 and 79.1"?

Also, the rationale at L422-L424 ("trim also strips NUL ... U+0000 in the C0-controls row") no longer applies, since control characters are now written as escapes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. In dfc8def the comment reads "whose sort keys differ between ICU4J 78.3 and 79.1", and the trim rationale is gone along with the parsing code.

private[sql] case class GoldenCell(collation: String, input: String)

/** Parse a markdown table row "| a | b | c |" into Seq("a","b","c"). */
private[sql] def parseRow(row: String): Seq[String] = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseRow splits on |, but renderInput does not escape it. If someone adds a cell like GoldenCell("UNICODE", "a|b") to probe punctuation order, the row is parsed into 4 columns, rowFor cannot find it, and the "golden row missing; regenerate" error persists even after regeneration.

There is also no check for duplicate (collation, input) cells, so find in rowFor would silently use the first match.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in dfc8def. The suite no longer parses the markdown, the input column escapes | and \, and a new test fails on duplicate cells.

*
* To re-generate the golden file, run:
* {{{
* SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "sql/testOnly org.apache.spark.sql.ICUCollationSortKeyGoldenSuite"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This line is 111 characters and relies on scalastyle:off line.size.limit. ICUCollationsMapSuite does the same, so this is minor, but the wildcard form fits within 100 characters and would let us drop the scalastyle:off/on pair.

 *   SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "sql/testOnly *ICUCollationSortKeyGoldenSuite"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in dfc8def. The command uses the wildcard form and the line.size.limit toggle is removed.

@github-actions github-actions Bot added the SQL label Nov 11, 2026

@spark-test spark-test left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review summary

This review checked the new suite and its golden against the pinned source and the committed bytes. The change is test-only: every finding concerns what a test checks or what a comment says, and none changes runtime behavior.

Ten of the eleven findings are already open in the existing inline threads, and this review independently confirms each one; those findings link to their threads. The new one is on the NON_IGNORABLE test's L1 probe: besides the byte counting raised in its thread, its comment credits ICU's 01 level separator to UTS #10.

Two points refine suggestions in those threads. Comparing a key with the empty-string key fixes the non-empty checks for the SMP, Hangul and U+FFFD cells, but the lone NUL, US and variation-selector cells record that same 01 01 00 key, so the comparison would fail there; truncation shows only with an embedded probe such as 'a' + U+0000 + 'b', and dropping a completely ignorable character can never change a key. For the ordering test, the comparator and the sort-key bytes come from the same ICU collator applied to the same string, so either can carry the order assertion.

Three questions for the author follow under Decision challenges.

Cross-cutting observations

  • These fixes are needed whichever way the suite's structure is settled: moving the named assertions onto live sort keys would change where they run, not what they must compare, and the golden would still need a drift test of its own and an escaped '|' to keep its markdown table intact.

    The findings can be fixed in the current structure now, and the structure question can be answered separately without reopening them.

    Related:

    • Non-empty sort-key checks cannot fail
    • Basic-Latin primary-order test checks only that rows exist
    • Turkish primaryPrefix compares primary plus secondary bytes
    • A '|' in a cell input splits its golden row
    • Golden drift check is hidden in the determinism test
    • Assert the named properties on live sort keys instead of re-parsing the golden

Findings

11 total: 0 P0, 0 P1, 3 P2, 8 P3.

Non-blocking (P2)

  • Non-empty sort-key checks cannot fail — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:208 — unresolved in existing discussion.
  • NUL, US and variation-selector probes cannot show truncation or a drop — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:360 — unresolved in existing discussion.
  • Basic-Latin primary-order test checks only that rows exist — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:121 — unresolved in existing discussion.

Nit (P3)

  • Turkish primaryPrefix compares primary plus secondary bytes — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:292 — unresolved in existing discussion.
  • NFC/NFD comments and test name misstate how decomposition works — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:176 — unresolved in existing discussion.
  • Locale-tailoring comment calls 78.3 to 79.1 a past ICU upgrade — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:382 — unresolved in existing discussion.
  • parseRow's trim rationale predates control-character escaping — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:422 — unresolved in existing discussion.
  • Golden input column cannot tell seven rows apart — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:550 — unresolved in existing discussion.
  • A '|' in a cell input splits its golden row — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:425 — unresolved in existing discussion.
  • Golden drift check is hidden in the determinism test — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:107 — unresolved in existing discussion.
  • NON_IGNORABLE L1 probe relies on ICU's byte format but cites UTS #10 — sql/core/src/test/scala/org/apache/spark/sql/ICUCollationSortKeyGoldenSuite.scala:249 — see inline.

PR description suggestions

  • Update the stale counts: the golden now records 59 cells (the 14 dimensions plus a locale-tailoring block for sr, sr_Latn, zh, zh_Hant, ja, sv, de, hi and ko, about 2.5 KB), and the suite has 18 tests, not '38 cells, ~1900 bytes' and '16 tests'. This follows up on an existing discussion.
  • Reword 'stable dims fire if a regression silently drops or folds a cell' and the 'non-empty hex' contract: the nonEmpty checks cannot fail, and the C0 and variation-selector probes cannot detect a drop, so describe what the assertions actually check. This follows up on an existing discussion.

Decision challenges

Golden disclaimer: forbid relying on sort-key bytes, or prompt a migration note when they change

This review confirms the sketch half of the existing concern about the disclaimer and narrows the hash half. hll_sketch_agg, theta_sketch_agg and the tuple sketch aggregate update their sketches with collation.sortKeyFunction bytes, and users can store those sketches and union them later; given your measurement that the UNICODE keys of 北京 and 中国 changed between icu4j 77.1 and 78.3, a sketch built on Spark 4.1 and unioned with one built on 4.2 would count such a string twice. By default, though, hash() and xxhash64() hash the UTF-8 bytes: Murmur3Hash and XxHash64 set isCollationAware = false and read ICU keys only under the internal spark.sql.legacy.collationAwareHashFunctions conf, which defaults to false, and the collation-aware variants serve internal uses such as shuffle partitioning. What remains open is what the golden should say. The review leans toward scoping the disclaimer, because a behavior change that can hurt users, as a sort-key change does for stored sketches and for ORDER BY where keys reorder, should come with a migration note. Either keep golden line 1, GOLDEN_DISCLAIMER and the scaladoc as they are, so a changed UNICODE | 北京 row still says that no consumer may rely on the bytes and the upgrade PR's reviewer judges the user impact alone; or have them say that the file records the current bytes for review without specifying their layout, and that a changed row means ORDER BY results and sketch-aggregate outputs for that collation can change, which the upgrade PR should call out in docs/sql-migration-guide.md or the release notes, and drop the sentence that forbids relying on byte equality.

This follows up on existing discussions 1, 2.

Which PRs may regenerate the sort-key golden

This review agrees that the hint conflicts with the reviewer note, and finds that the note also conflicts with the rest of the PR. The description says the named assertions fire when Spark's ICU configuration changes, which a PR could land only by regenerating without an icu4j bump, and this PR itself added the locale cells in fc61330 without one, so an icu4j-only rule would also bar cell additions and deliberate collator changes from regenerating. The review leans toward allowing regeneration in any PR that explains its drift, for consistency with Spark's other goldens, such as ICU-collations-map.md and the SQL query goldens, which are regenerated in the PR that changes their output and reviewed there. The options are: keep the note and the 'after extending cells' hint as they are, and let reviewers judge cell additions and collator changes case by case; make regeneration icu4j-only, with REGEN_HINT and the scaladoc saying so, the golden header recording Collation.version so that a regeneration without a bump shows in the diff, and the description dropping its claim about Spark-side configuration changes, so a PR that adds a cell such as 'a' + U+0000 + 'b' could land only with an icu4j bump; or allow regeneration in a PR that bumps icu4j, deliberately changes Spark's collator configuration, or adds cells, with REGEN_HINT, the scaladoc and the reviewer note asking that PR to account for every changed row, so the same cell addition regenerates in its own PR and explains its new row.

This follows up on an existing discussion.

Assert the named properties on live sort keys instead of re-parsing the golden

This review confirms most of the redundancy and adds a timing point. Fifteen of the eighteen tests read their rows by re-parsing the committed markdown, and given the on-disk comparison and the writer's line-1 check, the disclaimer test, the BOM check and every row-presence check follow. The control-character check does not: after a regeneration it still guards the writer's escaping, so a simplified suite would keep it. Because the named assertions read the committed bytes, an icu4j bump that breaks a named property fails only the drift check until the golden is regenerated, while assertions on keys computed from the cells would fail in the same run. The review leans toward live keys, as the simpler design that meets every stated requirement without a parser. Either keep re-parsing the committed golden and fix the individual assertions, so an ICU change that gave a and b the same primary fails the fixed ordering test only after the golden is regenerated; or keep one golden comparison that names the changed (collation, input) rows, keep the disclaimer-line and control-character checks, assert each named property on sortKeyBytes computed from the cells, and drop rowFor, hexFor, parseRow, stripSinglePadding and hexOf, so the same change fails the ordering test in the same run as the drift check.

This follows up on existing discussions 1, 2.

Generated by Omnigent on Databricks

// segment for "a<sep>b" contains 3+ primary weight bytes (a, sep, b).
// Under SHIFTED the separator is removed from L1 (shifted to L4), so
// the L1 segment would contain only 2 primary weights ("ab"-shape).
def primarySegmentBytes(hex: String): Int = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (P3): Adding to the byte-count point already raised on this line: the comment above attributes the 01 level separator to UTS #10 sec 3.2, but UTS #10 separates levels with a zero weight. The 01 separator and the 00 terminator are ICU's sort-key byte format, the kind of implementation detail the CJK test's comment distinguishes from a UCA constant, so this probe measures ICU's encoding rather than a property the standard defines. That is also why the >= 3 threshold depends on byte widths: the hyphen already has a two-byte primary (05 0e), so an 'ab'-shaped key would pass under SHIFTED if a and b ever got multi-byte primaries. Since the hyphen-versus-space assertion above would already fail under SHIFTED at Spark's level-3 strength, this probe adds little as written. Comparing the bytes before the first 01 for 'a-b' and 'a b' with those of an 'ab' cell would catch SHIFTED at any strength regardless of byte widths, and the comment could attribute the separator to ICU's format.

This extends an existing discussion.

Verification:

  • Regression: Replacing the recorded key of 'a-b' or of 'a b' with the key of 'ab', as SHIFTED alternate handling would produce, makes the test fail with a message that names the separator.
  • Inspection: No comment in the suite attributes ICU's 01 level separator or 00 terminator to UTS Add Shortest-path computations to graphx.lib with unit tests. #10.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that was ICU's byte format and not UTS #10. In dfc8def the comment and the byte-count probe are removed. The test compares a-b and a b with ab directly, so it doesn't depend on the encoding.

… assertions checkable

- The golden now has two sections: sort-key bytes (they feed hash(), xxhash64()
  and sketch aggregates on collated strings) and, per collation, the inputs in
  comparator order (what ORDER BY uses). A weight shift changes only the first;
  a reorder shows in the second.
- One drift test compares the golden with the current output line by line and
  lists the changed lines; the header records the ICU version.
- Rows show code points, so look-alike inputs (NFC/NFD, combining orders,
  variation selectors, empty string) are distinct in a diff; | is escaped.
- Assertions use sortKeyFunction and comparator directly and can fail: controls
  and variation selectors sit between letters; a-b and a b are compared with ab;
  Turkish case pairs are checked with tr_CI.
- Removed checks implied by the full-file comparison, and the disclaimer now
  says these values are user-visible.
@yaooqinn

Copy link
Copy Markdown
Member Author

Thanks @dongjoon-hyun, all points addressed in dfc8def:

  • The golden now has two sections: sort-key bytes (they feed hash()/xxhash64() and sketch aggregates) and, per collation, the inputs in comparator order, i.e. what ORDER BY uses. A weight shift changes only the first; a reorder shows in the second.
  • One test compares the golden with the current output line by line and lists the changed lines; the header records the ICU version. Locally with icu4j 79.1 it reports 22 lines, including the zh order 持<拉<啦<誓<匙<收<挖<哇 → 持<匙<拉<啦<誓<收<哇<挖.
  • Assertions use sortKeyFunction/comparator directly and can fail: controls and variation selectors sit between letters (compared with ab), a-b/a b vs ab, Turkish pairs via tr_CI. Checks implied by the full-file comparison were removed.
  • Rows show code points, | is escaped, duplicate cells are rejected, and the comments you flagged (NO_DECOMPOSITION, UTS Add Shortest-path computations to graphx.lib with unit tests. #10, the 78.3/79.1 wording, trim rationale) are gone. The disclaimer now says these values are user-visible.

@yaooqinn

Copy link
Copy Markdown
Member Author

CI for dfc8def ran in my fork (the PR check shows ACTION_REQUIRED because Actions did not start a run for that push): https://github.com/yaooqinn/spark/actions/runs/45389127330 . 27 jobs passed and 2 were skipped. sql - other tests hit the job time limit after about 2.5 hours and was cancelled. Its log has no failed test, and ICUCollationSortKeyGoldenSuite passed all 10 tests. I also ran the suite locally on dfc8def with JDK 21: 10 succeeded, 0 failed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants