Skip to content

fix: read keywords of other dialects as names with Dialect.MYSQL and Dialect.POSTGRESQL - #2969

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

manticore-projects merged 1 commit into
JSQLParser:masterfrom
minleejae:fix/dialect-foreign-keywords-20261202

Conversation

@minleejae

@minleejae minleejae commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

JSqlParser reserves some words for clauses of other dialects in every dialect, so with Dialect.MYSQL or Dialect.POSTGRESQL a column, table, constraint or alias named final, output, pivot, minus or semi was a parse error, although neither MySQL 8.4 nor PostgreSQL 18 knows these words as keywords: CREATE TABLE t (id INT, final INT), ALTER TABLE t ADD COLUMN output INT, SELECT a FROM t pivot or, with PostgreSQL, SELECT xor, use FROM t.

With these two dialects, the keywords that the dialect does not have are now plain names:

  • Dialect.MYSQL: CONNECT_BY_ROOT, EXCLUDES, FINAL, ILIKE, INCLUDES, MINUS, NOCYCLE, OUTPUT, PIVOT, PREFERRING, SEMI, TOP, UNPIVOT.
  • Dialect.POSTGRESQL: the same words except ILIKE, which PostgreSQL reserves, plus QUALIFY, USE, XOR and MySQL's DISTINCTROW, SQL_CACHE, SQL_CALC_FOUND_ROWS and STRAIGHT_JOIN.

isDialectIdentifierKeyword() holds the two lists. RelObjectName() takes these keywords in an alternative of their own, which comes before the guarded reserved keywords because isReservedKeywordAsIdentifier() checks the position only. isIdentifierishNameToken() and isAliasAhead() follow the same check. Where one of these words would start a clause, an operator or a modifier, that reading is skipped with the dialect: SEMI JOIN, XOR, ILIKE, INCLUDES, EXCLUDES, CONNECT_BY_ROOT, CONNECT BY NOCYCLE, TOP, MySQL's select modifiers and DISTINCTROW, and a trailing select-list comma before MINUS. Two spots needed a different shape, because JavaCC does not evaluate a semantic check inside [ ... ] or ( ... )* while scanning ahead: the STRAIGHT_JOIN hint after SELECT becomes a choice with an empty alternative, and the STRAIGHT_JOIN, SQL_CALC_FOUND_ROWS and SQL_CACHE alternatives of MySqlSelectModifier() carry isMySqlSelectModifierAhead() themselves.

Compatibility: without a dialect and with every other dialect, nothing changes, and these words keep their clauses (SELECT * FROM t FINAL, ... MINUS SELECT ..., a XOR b, LEFT SEMI JOIN, SELECT TOP 10). MySQL keeps XOR, DISTINCTROW, STRAIGHT_JOIN and SQL_CALC_FOUND_ROWS. Parser result classes and public methods are unchanged.

Not changed: SQL_CACHE stays a select modifier with Dialect.MYSQL, which MariaDB and MySQL 5.7 still have. QUALIFY and TABLESAMPLE are non-reserved MySQL keywords with a clause of their own, so the existing position checks still decide them. SEL is lexed as SELECT, and with the dialect MINUS still joins two queries where no name can stand, such as after a WHERE condition.

Reference: MySQL 8.4 Keywords and Reserved Words, PostgreSQL 18 SQL Key Words.

Validation

  • KeywordsTest: each listed keyword with its dialect as table, column, constraint and index name in CREATE TABLE, ALTER TABLE ADD/DROP/RENAME COLUMN and CREATE INDEX, and as column, alias and table alias in SELECT, INSERT and UPDATE; without a dialect FINAL, MINUS, XOR, LEFT SEMI JOIN and TOP keep their clauses, MySQL keeps XOR and DISTINCTROW, and PostgreSQL reads a xor as an alias. 31 of the 33 new cases fail on the base (5868b524b); the other two are top, already a name there.
  • 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 2,038 parse failures of the base, all with the two dialects, and adds none; results without a dialect are identical.
  • MySQL 8.4.5 (PREPARE only) accepted all 40 statements I checked with the MySQL words, and PostgreSQL 18.6 all 77 with the PostgreSQL words (CREATE TABLE, SELECT with aliases, ADD COLUMN, RENAME COLUMN); the only errors were semantic, such as a missing table.
  • Java 17 gradle check (Spotless, Checkstyle, PMD, JaCoCo verification) and checkGrammarAmbiguity: 18,686 tests, 0 failures, 25 skipped; no choice conflicts.

AI assistance: prepared with Claude Code.

…Dialect.POSTGRESQL

Signed-off-by: minleejae <mmj9808@gmail.com>
@minleejae
minleejae force-pushed the fix/dialect-foreign-keywords-20261202 branch from 96f1eb7 to 4d69ad3 Compare December 2, 2026 07:59
@manticore-projects
manticore-projects merged commit 78b33b1 into JSQLParser:master Dec 2, 2026
8 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