Skip to content

fix: read KEY, INDEX, EXCLUDE and OPTIONS as names where their element does not follow - #2970

Merged
manticore-projects merged 1 commit into
JSQLParser:masterfrom
minleejae:fix/contextual-keyword-names-20261202
Dec 2, 2026
Merged

manticore-projects merged 1 commit into
JSQLParser:masterfrom
minleejae:fix/contextual-keyword-names-20261202

Conversation

@minleejae

Copy link
Copy Markdown
Contributor

Some words start a table element or an ALTER TABLE action in one dialect but are plain names in another, and the parser took them as the start of that element even where it does not follow:

  • With Dialect.POSTGRESQL, KEY, INDEX, FULLTEXT and SPATIAL started MySQL's inline index definition, although PostgreSQL has none and does not reserve these words. CREATE TABLE kv (id INT, key VARCHAR(10)), ALTER TABLE t ADD key INT, ALTER TABLE kv ALTER key TYPE BIGINT and ALTER TABLE t ALTER index TYPE BIGINT were parse errors.
  • After CONSTRAINT, a word that could start a constraint was never taken as its name, so CONSTRAINT key UNIQUE (a) (PostgreSQL) and CONSTRAINT exclude UNIQUE (a) (MySQL and PostgreSQL) failed.
  • With Dialect.MYSQL or Dialect.POSTGRESQL, ALTER TABLE t ADD exclude INT started an exclusion constraint.
  • OPTIONS started PostgreSQL's foreign-data options without their parenthesized list, so MySQL's ALTER TABLE t ALTER INDEX options INVISIBLE, ALTER CHECK options NOT ENFORCED and ALTER CONSTRAINT options ENFORCED failed.

This change reads these words as names where their element does not follow:

  • isTableIndexAhead(): with Dialect.POSTGRESQL, KEY, INDEX, FULLTEXT and SPATIAL no longer start an index definition, and MySQL's ALTER INDEX name {VISIBLE | INVISIBLE} is not read there.
  • isConstraintNameBeforeKindAhead(): after CONSTRAINT, a word followed by PRIMARY KEY, UNIQUE, FOREIGN, CHECK, EXCLUDE or NOT NULL is the constraint's name. PRIMARY never is, as MySQL and PostgreSQL reserve it, and KEY, INDEX, FULLTEXT and SPATIAL are not with MySQL, which reserves them.
  • ADD with the two dialects uses isExcludeConstraintAhead(), as CREATE TABLE already does: EXCLUDE starts a constraint only before USING, its elements or WHERE.
  • isForeignDataOptionsAt(): OPTIONS starts foreign-data options only before (.

Compatibility: MySQL's index definitions and unnamed constraints (KEY k (a), CONSTRAINT UNIQUE KEY u (a), CONSTRAINT PRIMARY KEY (a)), PostgreSQL's exclusion constraints and foreign-data options parse as before. Parser result classes and public methods are unchanged. Without a dialect, KEY, INDEX, FULLTEXT and SPATIAL still start an index definition, so CREATE TABLE kv (id INT, key VARCHAR(10)) still needs Dialect.POSTGRESQL.

Not changed: reserved words that the parser accepts as names only in some positions (OFFSET, ONLY, RETURNING with MySQL; SET, BETWEEN with PostgreSQL) still fail after DROP, TO, INDEX or ADD.

Reference: MySQL 8.4 CREATE TABLE ([CONSTRAINT [symbol]] UNIQUE ...), ALTER TABLE (ALTER INDEX index_name {VISIBLE | INVISIBLE}, ALTER {CHECK | CONSTRAINT} symbol [NOT] ENFORCED), Keywords and Reserved Words; PostgreSQL 18 CREATE TABLE, ALTER TABLE, SQL Key Words.

Validation

  • ContextualKeywordNamesTest: with PostgreSQL, key, index, fulltext and spatial as columns in CREATE TABLE, ADD and ALTER ... TYPE; those words and exclude as constraint names in CREATE TABLE and ADD CONSTRAINT, with and without a dialect; ADD exclude INT with and without the dialects; options as index and constraint name with MySQL; and MySQL's index definitions, ADD key INT rejected with MySQL, PostgreSQL's exclusion constraint and foreign-data options as before. 15 of the 18 cases fail on the base (08e222fd6).
  • The keyword survey of fix: accept non-reserved keywords as existing names in ALTER TABLE and CREATE SCHEMA #2968 (1,101 words in 144 DDL and DML name positions): this change fixes 45 parse failures of the base and adds none.
  • MySQL 8.4.5 (PREPARE only) and PostgreSQL 18.6 agreed with the branch on all 21 statements I checked, including the rejected ADD key INT and CONSTRAINT key UNIQUE (a) (MySQL) and CONSTRAINT primary UNIQUE (a) (PostgreSQL).
  • Java 17 gradle check (Spotless, Checkstyle, PMD, JaCoCo verification) and checkGrammarAmbiguity: 16,879 tests, 0 failures, 25 skipped; no choice conflicts.

AI assistance: prepared with Claude Code.

…t does not follow

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

Copy link
Copy Markdown
Contributor Author

@manticore-projects Thanks for the reviews! Could you share when you plan to release 5.5?

@manticore-projects
manticore-projects merged commit 1038c30 into JSQLParser:master Dec 2, 2026
8 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

@manticore-projects Thanks for the reviews! Could you share when you plan to release 5.5?

Thank you for your effort and contribution!
On the 5.5 release, it should happen around Christmas? What do you think @wumpz?

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.

2 participants