Skip to content

feat: keep MySQL key-part prefix lengths in KeyElement - #2971

Open
minleejae wants to merge 1 commit into
JSQLParser:masterfrom
minleejae:feat/mysql-key-prefix-length-20261201
Open

minleejae wants to merge 1 commit into
JSQLParser:masterfrom
minleejae:feat/mysql-key-prefix-length-20261201

Conversation

@minleejae

Copy link
Copy Markdown
Contributor

MySQL key parts may index a prefix of a column, as in KEY k (label(10)). KeyElement kept the prefix as the first entry of getParams(), the text (10), next to DESC and other legacy parameters, and printed it with a space (label (10)). Callers had to recognise the prefix by matching that text.

KeyElement now has getPrefixLength(), setPrefixLength(Integer) and withPrefixLength(Integer). IndexColumnWithParams sets it for a column name followed by one integer in parentheses, in every dialect except PostgreSQL, which reads the same text as a function call. isMySqlKeyPartPrefixAhead(), which #2967 added to keep such a prefix from being read as a function call, now takes the position of the parenthesis and decides both cases. The prefix is no longer repeated in getParams(). KeyElement prints it right after the name, as MySQL does: label(10) DESC.

Compatibility: KeyElement was added in #2747 and is not in a release yet. Statements parse as before, and only the representation changes: getParams() no longer contains (10), and toString() prints label(10) instead of label (10). Other parenthesized parameters, such as lower(label) with MySQL or Oracle, stay in getParams(). The tests that pinned the old representation (CreateIndexTest, MySqlIndexSpecOptionTest, DeparseWhitespaceTest, and PostgreSqlDdlKeyOptionsTest from #2967) are updated.

Reference: MySQL 8.4 CREATE INDEX (key_part: {col_name [(length)] | (expr)} [ASC | DESC], "Column Prefix Key Parts").

Validation

  • MySqlKeyPartPrefixLengthTest covers CREATE INDEX, KEY, UNIQUE KEY and PRIMARY KEY in CREATE TABLE, and ADD INDEX and ADD UNIQUE KEY in ALTER TABLE. Without a dialect and with MySQL and MariaDB, each prefix length is kept, the statement is printed unchanged and round-trips through the deparser. A changed or removed prefix length is printed. MySQL and Oracle keep other parentheses as parameters, and PostgreSQL reads label(10) as a function call.
  • MySQL 8.4.11 accepted every statement in the tests.
  • Java 17 gradle check (Spotless, Checkstyle, PMD, JaCoCo verification) and checkGrammarAmbiguity: 18,715 tests, 0 failures, 25 skipped; no choice conflicts.

AI assistance: prepared with Claude Code.

Signed-off-by: minleejae <mmj9808@gmail.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant