Skip to content

fix: accept non-reserved keywords as existing names in ALTER TABLE and CREATE SCHEMA - #2968

Merged
manticore-projects merged 1 commit into
JSQLParser:masterfrom
minleejae:fix/alter-name-keywords-20261202
Dec 2, 2026
Merged

manticore-projects merged 1 commit into
JSQLParser:masterfrom
minleejae:fix/alter-name-keywords-20261202

Conversation

@minleejae

Copy link
Copy Markdown
Contributor

ALTER TABLE read the name of an existing column, index or constraint through KeywordOrIdentifier() or a bare <S_IDENTIFIER> | <S_QUOTED_IDENTIFIER> choice, and CREATE SCHEMA read its names the same way. These took plain identifiers plus nine keywords, or no keyword at all, so a column, index or constraint named by any other non-reserved keyword or by a type name such as number, enum or bool could be created but not dropped or renamed. ALTER TABLE t DROP COLUMN link, ALTER TABLE t CHANGE level level INT, ALTER TABLE t RENAME COLUMN action TO cycle, ALTER TABLE t DROP INDEX link, ALTER TABLE t RENAME INDEX link TO status, ALTER TABLE t DROP CONSTRAINT IF EXISTS level and CREATE SCHEMA link were parse errors, while ADD COLUMN link INT and AFTER link parsed, as they use RelObjectName().

This change reads these names with RelObjectName(), the production of the other name positions:

  • DROP [COLUMN] [IF EXISTS] name, CHANGE [COLUMN] old new ..., RENAME [COLUMN] old TO new, DROP {INDEX | KEY} name, RENAME {INDEX | KEY | CONSTRAINT} old TO new, DROP CONSTRAINT [IF EXISTS] name, and the catalog, schema and AUTHORIZATION names of CREATE SCHEMA.
  • INDEX, KEY, PRIMARY and PARTITION are not reserved, so after DROP they keep their meaning when a name (or KEY) follows and name the column otherwise, as in PostgreSQL's ALTER TABLE t DROP key. After RENAME, INDEX, KEY and CONSTRAINT need a name and TO, so RENAME key TO k2 renames a column.
  • CHANGE joins TABLE and COLUMN in isReservedKeywordAsIdentifier() as a keyword that is always followed by a name, so CHANGE value v INT keeps parsing.
  • An omitted CREATE SCHEMA name still ends at AUTHORIZATION and at a MySQL database option, and a plain identifier is still the name (CREATE SCHEMA charset).
  • KeywordOrIdentifier() has no callers left and is removed.

Compatibility: parser result classes and their public methods are unchanged, and statements that parsed before give the same model and toString(). The generated parser loses the KeywordOrIdentifier() production, which had no other callers. JSqlParser keeps one set of reserved words for all dialects, so like ALTER TABLE t ADD COLUMN key INT, which it already takes, Dialect.MYSQL now also takes ALTER TABLE t DROP key and RENAME key TO k2, which MySQL rejects because KEY is reserved there; likewise DROP primary with Dialect.POSTGRESQL.

Not changed: the reserved words that isReservedKeywordAsIdentifier() admits only in some positions (OFFSET, ONLY, RETURNING, SET, BETWEEN) still fail after DROP, INDEX, KEY or TO; SIMILAR before TO is still lexed as SIMILAR TO; a schema named AUTHORIZATION, CHARACTER, COLLATE or ENCRYPTION still fails.

Reference: MySQL 8.4 ALTER TABLE, CREATE DATABASE, Keywords and Reserved Words; PostgreSQL 18 ALTER TABLE, CREATE SCHEMA, SQL Key Words.

Validation

  • KeywordsTest: every non-reserved keyword (445) as the column of DROP COLUMN, DROP COLUMN IF EXISTS, CHANGE COLUMN and AFTER; as the index of DROP INDEX and DROP KEY and the constraint of DROP CONSTRAINT [IF EXISTS]; as both names of RENAME COLUMN, RENAME INDEX, RENAME KEY and RENAME CONSTRAINT; as the catalog, schema and AUTHORIZATION names of CREATE SCHEMA; type names and value in these positions without COLUMN; and DROP key / DROP KEY k, DROP primary / DROP PRIMARY KEY, DROP partition / DROP PARTITION p1, p2, RENAME key TO k2 / RENAME KEY k TO k2, CREATE SCHEMA AUTHORIZATION link, CREATE SCHEMA DEFAULT CHARACTER SET utf8mb4 and CREATE SCHEMA charset, each checked against the model it should give. 1,751 of the 1,782 new cases fail on the base (2ba80d35c).
  • A survey of 1,101 words (the MySQL 8.4 and PostgreSQL 18 keyword lists and the parser's keyword tokens) in 144 DDL and DML name positions, with and without the dialect: the branch fixes 27,942 parse failures of the base and adds none.
  • MySQL 8.4.5 (PREPARE only) and PostgreSQL 18.6 accepted all 21 and 18 statements of this change I checked, and rejected only DROP key / RENAME key TO k2 (MySQL) and DROP primary (PostgreSQL), noted above.
  • Java 17 gradle check (Spotless, Checkstyle, PMD, JaCoCo verification) and checkGrammarAmbiguity: 18,620 tests, 0 failures, 25 skipped; no choice conflicts.

AI assistance: prepared with Claude Code.

…d CREATE SCHEMA

Signed-off-by: minleejae <mmj9808@gmail.com>
@manticore-projects
manticore-projects merged commit 5868b52 into JSQLParser:master Dec 2, 2026
10 checks passed
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