Skip to content

fix: read MySQL and PostgreSQL unreserved keywords as DDL names, and running/final/approximate before IN (...) - #2972

Open
minleejae wants to merge 3 commits into
JSQLParser:masterfrom
minleejae:fix/reserved-words-name-positions-20261202
Open

minleejae wants to merge 3 commits into
JSQLParser:masterfrom
minleejae:fix/reserved-words-name-positions-20261202

Conversation

@minleejae

Copy link
Copy Markdown
Contributor

Two kinds of names that MySQL 8.4 or PostgreSQL 18 accepts were still parse errors.

Keywords the dialect does not reserve, in DDL name positions. isReservedKeywordAsIdentifier() accepts a guarded reserved keyword as a name only after a few words such as TABLE, COLUMN, ( or ,, or when its follower rules it safe. After the other words that DDL always follows with a name, these keywords failed with Dialect.MYSQL and Dialect.POSTGRESQL, although the servers do not reserve them:

  • MySQL: OFFSET, ONLY, RETURNING, ANY, SOME and VALUE, as in ALTER TABLE t ADD COLUMN c INT AFTER offset, ALTER TABLE t DROP only, ALTER TABLE t RENAME COLUMN c TO returning, ALTER TABLE t ADD INDEX offset (a), CREATE TABLE t (a INT, KEY any (a)) and ALTER TABLE t ADD FOREIGN KEY (a) REFERENCES value (b).
  • PostgreSQL: SET and BETWEEN, as in ALTER TABLE t ADD set INT, ALTER TABLE t ALTER set TYPE BIGINT, ALTER INDEX set RENAME TO ix, ALTER TABLE t SET SCHEMA between and CREATE SEQUENCE set, and IF, GROUPING and VALUES as referenced tables, as in REFERENCES if (b).

DialectKeywordNames.isNameAfter() now reads these keywords as names right after such a word:

  • MySQL: ADD, ALTER, DROP, MODIFY, TO, INDEX, KEY, REFERENCES, DATABASE, SCHEMA, VIEW, TRIGGER, PARTITION and AFTER.
  • PostgreSQL: ADD, ALTER, DROP, TO, INDEX, PARTITION, SCHEMA, SEQUENCE, VIEW, TRIGGER, REFERENCES, AUTHORIZATION, INHERIT and ATTRIBUTE for SET and BETWEEN; REFERENCES for IF, GROUPING and VALUES.

isReservedKeywordAsIdentifier() consults it after the check for PostgreSQL's ONLY relation prefix. QUALIFY is left out: INFORMATION_SCHEMA.KEYWORDS lists it as non-reserved, but MySQL 8.4 rejected it as a column, table and index name in every form I tried.

A column named running, final or approximate before a keyword and a parenthesis. The checks for MATCH_RECOGNIZE's RUNNING | FINAL function(...) and for the APPROXIMATE prefix of a function call looked only at the third token, so WHERE running IN (1, 2), WHERE final LIKE ('a%'), CHECK (approximate IN (1, 2)) and PARTITION BY RANGE (running) (PARTITION ...) failed, running and approximate in every dialect. SELECT approximate(x) FROM t, a call of a function named approximate, failed as well. These prefixes are now read only before a function name (isFunctionNameToken()): an identifier or a non-reserved keyword. APPROXIMATE COUNT(DISTINCT a) and FINAL COUNT(*) / RUNNING LAST(a) in MEASURES parse as before.

The pull request has three commits: a refactoring that moves the keyword lists of #2969 into the nested class DialectKeywordNames, without changing behaviour, as AlterConstraintLookahead does to keep code out of CCJSqlParser.class; the DDL names; and the function prefixes.

Compatibility: parser result classes and public methods are unchanged. Without a dialect and with other dialects, the DDL names are unchanged. Statements that parsed before give the same model and toString(); among them LIMIT 10 OFFSET 5, OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY, START TRANSACTION READ ONLY, JSON_VALUE(... RETURNING ...), INSERT INTO t VALUE (1), a = ANY (SELECT ...), UPDATE t SET, a BETWEEN 1 AND 2, CREATE INDEX ON t (a), DROP INDEX IF EXISTS i, ALTER TYPE ty ADD VALUE 'x', ALTER TABLE ONLY t and TRUNCATE ONLY t.

Not changed: with MySQL, OFFSET, ONLY and RETURNING still fail as aliases (SELECT a offset FROM t offset), as the new name of CHANGE (CHANGE c offset INT), and only in INSERT INTO only (a), ALTER TABLE only.t and TRUNCATE TABLE only. There the MySQL dialect keeps the readings that PostgreSqlCursorTest, MySqlLockTablesTest and PostgreSQLModifyFormsTest pin: the standard OFFSET ... ROWS and PostgreSQL's ONLY prefix.

Reference: MySQL 8.4 Keywords and Reserved Words, ALTER TABLE; PostgreSQL 18 SQL Key Words, ALTER TABLE; MATCH_RECOGNIZE (RUNNING / FINAL).

Validation

  • KeywordsTest: the keywords in the listed positions with their dialects; running, final and approximate before IN (...), LIKE (...), = (...) and in CHECK (... IN ...), with and without a dialect; and the clauses, operators and function calls above, which keep their readings. 83 of the 84 new cases fail on the base (1038c30f8).
  • A survey of 1,150 words (the MySQL 8.4 and PostgreSQL 18 keyword lists, the parser's keyword tokens and the words it reads contextually) in 225 DDL and DML positions per dialect and without one, plus 11 forms of a name before a keyword and a parenthesis: the branch fixes 322 parse failures of the base (MySQL 203, PostgreSQL 98, no dialect 21) and adds none.
  • All 322 statements were accepted by the servers: MySQL 8.4.5 (PREPARE only) for 216, MySQL 8.0.46 for the 6 that PREPARE does not take (ALTER DATABASE, CREATE TRIGGER), and PostgreSQL 18.6 for 114. The only errors were semantic, such as a missing table or role.
  • Java 17 gradle check (Spotless, Checkstyle, PMD, JaCoCo verification) and checkGrammarAmbiguity: 18,788 tests, 0 failures, 25 skipped; no choice conflicts. The first two commits pass the same check on their own (18,704 and 18,779 tests).

AI assistance: prepared with Claude Code.

Signed-off-by: minleejae <mmj9808@gmail.com>
… after DDL keywords

Signed-off-by: minleejae <mmj9808@gmail.com>
…a function name

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

1 participant