Skip to content

#3822: generated repositories keep the mainframe's key order - #3874

Merged
squid-protocol merged 1 commit into
mainfrom
fleet/3822-key-collation
Sep 27, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
fleet/3822-key-collation

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Closes #3822 (epic #3819).

Problem: a KSDS browse or a DB2 ORDER BY follows the key's EBCDIC bytes: lower case before upper case, letters before digits. The generated repositories left ordering to the database's default collation, which folds case and accents. On MySQL (utf8mb4_0900_ai_ci), ABC, abc and ÀBC are the same key, so distinct COBOL keys collided on the primary key. On every database, browses came back in a different order from the mainframe's.

What changes (culture.key_collation, declared in #3849 and wired here):

  • ebcdic (the default).
    • A String key gets a <KEY>_SORT column: the key's code-page bytes in hex (A001 in cp037 is C1F0F0F1). It is filled on @PrePersist / @PreUpdate by the new CobolRecords.sortKey(key, codePage).
    • The STARTBR / READNEXT / READPREV finders range over that column and order by it: findByAcctIdSortGreaterThanEqualOrderByAcctIdSortAsc.
    • Generated services pass CobolRecords.sortKey(from, "cp037").
    • Hex, two characters per byte, keeps byte order under any collation, and the ordering stays in the database, so Pageable still works.
  • ebcdic and binary. String key columns, including the text parts of a group key, get a byte-wise collation: COLLATE "C" on PostgreSQL, utf8mb4_bin on MySQL. H2 compares code points by default. DB2 and Oracle are left unchanged, because their defaults (the table's code page, NLS_SORT=BINARY) are already byte-wise.
  • database. Output identical to before.
  • Porting rule. Order and compare keys by the code page's bytes, never by String.compareTo or the database's default collation.

Limits

  • Only single-field String keys get the sort column. A group key's browse was already a TODO (a range over an @EmbeddedId isn't a derived query), and numeric keys already order correctly.
  • Rows inserted behind JPA's back (raw SQL) won't have a sort key.

Tests

  • New tests/cobol_mainframe/test_key_collation.py, on the Java conversion: CICS FILE (VSAM) operations + FILE-CONTROL / IDCAMS -> Spring Data repositories #3617 estate re-keyed to a PIC X(8) key. It covers the generated entity, repository, service and key class for postgresql, mysql, binary and database. It also runs CobolRecords.sortKey under javac/java for cp037, cp273 and cp500 and compares the result against Python's byte order. The cp037 order is the one the issue gives: Ä001 a001 A 01 A-01 A001 1001.
  • tests/cobol_mainframe: 794 passed.
  • java_target_matrix.py --scan --only default plain-classes mysql on that estate: all three generated projects compile with Maven.
  • pr_gates.py --fast: 5/5 pass.
  • No refraction snapshot contains a VSAM entity, so no snapshot moves.

Gemini's earlier draft only added COLLATE "C" and kept the database's order. That order is ASCII, not EBCDIC: digits before letters, upper case before lower. This PR is a rewrite.

🤖 Generated with Claude Code

A KSDS browse follows the key's EBCDIC bytes (lower case before upper,
letters before digits); databases' default collations fold case and
accents, so MySQL merged distinct COBOL keys on the primary key and every
database browsed in another order.

culture.key_collation:
- ebcdic (default): a String key gets a <KEY>_SORT column -- its code-page
  bytes in hex, set by @PrePersist/@PreUpdate through the new
  CobolRecords.sortKey(key, codePage) -- and the STARTBR/READNEXT/READPREV
  finders range over and order by it; the services pass the sort key.
- ebcdic and binary: String key columns (and group-key text parts) collate
  byte-wise: COLLATE "C" on PostgreSQL, utf8mb4_bin on MySQL.
- database: the output of before.
A porting rule orders keys by code-page bytes, never compareTo or the
database's collation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit 99f5e11 into main Sep 27, 2026
37 checks passed
@squid-protocol
squid-protocol deleted the fleet/3822-key-collation branch September 27, 2026 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Key order: EBCDIC vs the target database's collation -- generated repositories browse and sort differently, MySQL collides case-variant keys

1 participant