Skip to content

Parse keywords as names by dialect and grammar position - #2973

Open
minleejae wants to merge 17 commits into
JSQLParser:masterfrom
minleejae:refactor/name-positions-20261203
Open

minleejae wants to merge 17 commits into
JSQLParser:masterfrom
minleejae:refactor/name-positions-20261203

Conversation

@minleejae

@minleejae minleejae commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

RelObjectName() inferred whether a keyword could be used as a name from the preceding token, and several helpers duplicated its keyword lists. This PR centralizes those rules in NameKeywords and adds dedicated grammar productions for object names, qualified name parts, aliases, and role names.

What changes

  • MySQL uses MySQL 8.4’s reserved-word rules for object names and recognizes keywords immediately before a dot as name parts, as in ALTER TABLE and.t ....
  • PostgreSQL uses PostgreSQL 18’s keyword categories to decide which words are valid in each position. For example, SELECT 1 AS from is valid because a select-item alias follows the ColLabel rule.
  • Both dialects accept keywords after a dot, as in SELECT t.where FROM t, and recognize implicit aliases when the next token clearly ends the alias.

Parsing changes are limited to MySQL, MariaDB, and PostgreSQL. MySQL and PostgreSQL now treat SEL as an identifier. In MySQL mode, SIMILAR TO now produces a SimilarToExpression instead of a LikeExpression.

Validation

  • The keyword survey found 650 fixes across MySQL and PostgreSQL, with zero regressions.
  • gradle check and checkGrammarAmbiguity completed with 19,268 tests, 0 failures, 25 skipped, and no grammar choice conflicts.
  • Server checks confirmed the newly accepted statements, with three existing MySQL discrepancies involving JOIN row and DROP COLUMN IF EXISTS.

Prepared with Claude Code.

Whether a keyword token is a name was decided from lists kept in several
places: RelObjectName() lists the reserved keywords it reads as names, and
isIdentifierishNameToken(), isAliasAhead() and
isReservedKeywordSafeByFollower() repeat that list; the keywords of other
dialects are listed in RelObjectName() and in DialectKeywordNames; and ten
helpers repeat the check for the non-reserved range.

NameKeywords now holds these sets, with the dialect lists that were nested in
the parser, and the helpers use it. The grammar still spells the alternatives
of RelObjectName() out, so NameKeywordsTest parses every reserved keyword as a
name with no dialect, MySQL and PostgreSQL, and checks that it is one exactly
when a set holds it.

Signed-off-by: minleejae <mmj9808@gmail.com>
The places that read the name of a table, column, index, constraint, schema,
role and the like call ObjectName(), and so does RelObjectNames() for the first
part of a qualified name. ObjectName() reads what RelObjectName() reads; the
position now has a name of its own, which the next commit uses.

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

With Dialect.MYSQL or Dialect.POSTGRESQL, ObjectName() reads the reserved
keywords of this parser that the dialect does not reserve as names wherever it
stands (NameKeywords.isObjectName()), as in CHANGE c offset INT or REFERENCES
value (b). The previous-token rule isNameAfter() is no longer needed. Column
references in expressions keep reading RelObjectName(), as a keyword such as
BETWEEN or INTERVAL may start syntax there.

MySQL and MariaDB have no ONLY prefix of a relation. There ONLY is now the
prefix only before a name, or before "(" in LOCK and TRUNCATE, and otherwise
the name of the relation, as in TRUNCATE TABLE only, ALTER TABLE only.t or
CREATE INDEX i ON only (a). With MySQL, UPDATE ONLY t and DELETE FROM ONLY t
read only as the table and t as its alias, as MySQL does.

Signed-off-by: minleejae <mmj9808@gmail.com>
…here MySQL and PostgreSQL do

NamePart() reads the parts of a qualified name after ".". With MySQL and
PostgreSQL it takes every keyword (ReservedKeyword()), as both read only a name
after a dot, as in SELECT t.where FROM t. SelectItemAlias() takes every keyword
after AS with PostgreSQL, as in SELECT 1 AS from; the alias of a table is a
ColId there and keeps its rule. AliasName() reads the aliases of tables and
other sources the way ObjectName() reads names. NameKeywordsTest checks that
ReservedKeyword() lists every reserved keyword.

Signed-off-by: minleejae <mmj9808@gmail.com>
@minleejae
minleejae force-pushed the refactor/name-positions-20261203 branch from 1c78aad to 47e9449 Compare December 3, 2026 05:14
@minleejae minleejae changed the title refactor: keep the keyword sets for names in one class and name the positions that read names fix: read object names, name parts and aliases by position, with the keywords MySQL and PostgreSQL take there Dec 3, 2026
…pers after it

Five lookahead helpers tested by hand for an identifier, quoted or not, or a
non-reserved keyword: isFunctionNameToken(), which goes,
isInsertTableSourceAhead(), isRowPatternPrimaryAhead(), isDuckDBLambdaAhead()
and RelationOptions.isOnlyAhead(). They call
NameKeywords.isIdentifierOrNonReserved() now. isIdentifierishNameToken()
becomes isNameToken() over NameKeywords.isName(), and
isDialectIdentifierKeyword() becomes isDialectName(), as in NameKeywords. The
comment of RelObjectNameExt() names the places that read it today. No change
in behaviour.

Signed-off-by: minleejae <mmj9808@gmail.com>
ParserKeywordsUtils found the keywords of the grammar with a pattern that
missed a declaration such as <K_IGNORE : "IGNORE">, so 23 keywords were left
out, among them the reserved DEFAULT, IGNORE and SEMI. keywords.rst lists them
now, NameKeywordsTest checks them as names, and ReservedKeyword() lists them
too.

Signed-off-by: minleejae <mmj9808@gmail.com>
…ve its object names from it

The parser held PostgreSQL 18's keywords by category to check cursor, role,
conversion and utility option names, and NameKeywords listed by hand the
reserved names that PostgreSQL takes as names of objects, which is the same
column identifier (ColId) rule. NameKeywords now holds the table, the checks
call isPostgreSqlColId(), isPostgreSqlReserved() and the like, and
isObjectName() takes a reserved name as an object name with PostgreSQL when
the table makes it a ColId. That adds IGNORE and NEXTVAL, which PostgreSQL
does not know as keywords; the positions tried read them as names already.
Moving the table out of CCJSqlParser.class takes 424 entries off its constant
pool.

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

NameKeywords listed by hand the reserved names that MySQL takes as names of
objects. It now holds MySQL 8.4's reserved keywords as
INFORMATION_SCHEMA.KEYWORDS lists them, and QUALIFY and TABLESAMPLE, which that
table lists as non-reserved but MySQL rejects as names; isObjectName() takes a
reserved name as an object name with MySQL when neither holds it. That adds
NEXTVAL, which MySQL does not know as a keyword.

Signed-off-by: minleejae <mmj9808@gmail.com>
MySQL's lexer does not look a word up as a keyword when "." and an
identifier character follow it directly (sql_lex.cc, MY_LEX_IDENT), so any
word starts a qualified name there: ALTER TABLE and.t ..., RENAME TABLE
from.t TO from.u, SELECT all.a FROM t. The parser read the part after the
dot that way already, with NamePart(), but the first part still went
through ObjectName() and its keyword table.

With Dialect.MYSQL the token manager now turns a reserved keyword into an
identifier when "." and an identifier character follow it without
whitespace, as MySQL's lexer does. With a space before the dot, and with
other dialects, the keyword stays a keyword.

Signed-off-by: minleejae <mmj9808@gmail.com>
The lexer reads SEL as SELECT (Teradata's short form), so sel failed as the
name of a table, a column or an alias in every dialect. Neither MySQL nor
PostgreSQL knows SEL as a keyword.

With Dialect.MYSQL and Dialect.POSTGRESQL the token manager turns SEL into
an identifier. Without a dialect, and with the other dialects, SEL a FROM t
stays a query; with these two it now fails, as it does on their servers.

Signed-off-by: minleejae <mmj9808@gmail.com>
…operator with PostgreSQL

MySQL 8.0 removed SQL_CACHE (sql_yacc.yy keeps it as OBSOLETE_TOKEN_784), so
MySQL 8.4 takes sql_cache as the name of a table or a column. The parser
read it as a select modifier with every dialect but PostgreSQL, and nowhere
else as a name.

With Dialect.MYSQL, SQL_CACHE is now a keyword of other dialects
(NameKeywords.isDialectName()), a name wherever RelObjectName() reads one.
It stays a modifier before a select item, as SQL_NO_CACHE does, so that
SELECT SQL_CACHE a FROM t keeps its reading.

Where the dialect has no such modifier, SQL_CACHE with MySQL and
SQL_NO_CACHE with PostgreSQL, a comparison, IN, IS, LIKE or BETWEEN after
the word continues an expression that the word starts, rather than
following a modifier: SELECT sql_no_cache IN (1, 2) FROM t with
PostgreSQL. With MySQL, SQL_NO_CACHE and the other modifiers keep failing
there, as on the server.

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

PostgreSQL's RoleSpec is a NonReservedWord (gram.y), a ColId or a type and
function name keyword, so it takes cross, inner, is, join, like and the
others of that kind as role names: ALTER TABLE t OWNER TO cross, GRANT
SELECT ON t TO inner, CREATE SCHEMA s AUTHORIZATION natural. The parser read
role names through ObjectName(), a ColId, which leaves these out.

RoleName() now reads the role after OWNER TO, AUTHORIZATION, OWNED BY, the
TO of CREATE POLICY and the FOR of USER MAPPING, the name of CREATE and
ALTER ROLE, and the user or role of an account name, where GRANT and
REASSIGN OWNED read their grantees. With PostgreSQL it also takes the
reserved keywords of this parser that are type and function name keywords
(NameKeywords.isTypeFuncName(), TypeFuncNameKeyword()); otherwise it reads
what ObjectName() reads.

Signed-off-by: minleejae <mmj9808@gmail.com>
…ias where the alias ends

MySQL takes offset, only, returning, settings, glob, similar and the like as
aliases without AS (SELECT a offset FROM t, SELECT a FROM t only), but the
parser read them as the start of a clause or an operator of another dialect
(OFFSET, RETURNING, ClickHouse's PREWHERE and SETTINGS, GLOB, MATCH_ANY,
OVERLAPS, SIMILAR TO, GLOBAL IN) and failed.

With MySQL and PostgreSQL, isBareAliasAhead() now takes a keyword that the
dialect reads as a name as an alias when the token after it ends the alias:
the end of the statement, a comma, ")", or a keyword that starts the next
clause, a set operation or a join. MySQL reads a word that it does not
reserve as a name, other than MEMBER, which starts MEMBER OF after an
expression; PostgreSQL a column identifier (ColId). The clause or operator
needs more than that, so OFFSET 5, GLOB 'x*' and PREWHERE a = 1 keep their
readings; ISNULL and NOTNULL, which need nothing more, stay operators.

Signed-off-by: minleejae <mmj9808@gmail.com>
…nother dialect starts

Four DDL positions decided from one token that a word starts an option or a
clause, although MySQL or PostgreSQL takes the word as a name there:

- CREATE SCHEMA encryption, CREATE SCHEMA character (PostgreSQL) and ALTER
  DATABASE encryption CHARACTER SET utf8mb4: with MySQL and PostgreSQL
  ENCRYPTION, and with PostgreSQL CHARACTER, which MySQL reserves, now start
  a MySQL database option only before "=" or a string, and before SET;
  with MySQL, CREATE SCHEMA authorization names the schema before the end
  or an option, as MySQL has no AUTHORIZATION there.
- DROP INDEX concurrently ON t: with MySQL, CONCURRENTLY before ON is the
  index.
- GENERATED ALWAYS AS (identity + 1): IDENTITY as the fifth token started
  an identity column even after "(".
- ON DUPLICATE KEY UPDATE nothing = 1: with MySQL, NOTHING before "=" or
  "." is a column.

Each change applies with Dialect.MYSQL or Dialect.POSTGRESQL, where the
server accepts the statement; the options and clauses keep their readings.

Signed-off-by: minleejae <mmj9808@gmail.com>
The lexer reads "similar TO" as one token, SIMILAR TO, so ALTER TABLE t
RENAME COLUMN similar TO c failed, although MySQL has no SIMILAR TO and takes
similar as a name.

With Dialect.MYSQL the token manager splits that token into the word similar
and TO, as MySQL's lexer reads them. a SIMILAR TO 'x', which MySQL does not
accept, then reads as a SimilarToExpression rather than a LikeExpression.

Signed-off-by: minleejae <mmj9808@gmail.com>
…s in expressions

Three expression positions took a keyword for the start of its construct
although MySQL or PostgreSQL reads a name there:

- ROW started a row constructor after an operator even without "(", so
  t.id = row.id failed. With MySQL and PostgreSQL it now needs "(".
- "(" VALUES started a VALUES list, so SELECT (values) FROM t failed. With
  PostgreSQL, where values is a column name, the list needs "(" after
  VALUES.
- PostgreSQL's func_name takes its type and function name keywords, so
  like('a') calls a function. With PostgreSQL, InternalFunction() now
  reads such a keyword before "(" as the function name, with the tokens of
  TypeFuncNameKeyword() and NameKeywords.isTypeFuncName(), as RoleName()
  does; LATERAL VIEW OUTER and ", OUTER" keep reading OUTER as the keyword,
  now with an explicit lookahead.

Signed-off-by: minleejae <mmj9808@gmail.com>
@minleejae minleejae changed the title fix: read object names, name parts and aliases by position, with the keywords MySQL and PostgreSQL take there fix: read object names, name parts, aliases and role names by position, with the keywords MySQL and PostgreSQL take there Dec 3, 2026
@minleejae minleejae changed the title fix: read object names, name parts, aliases and role names by position, with the keywords MySQL and PostgreSQL take there Parse keywords as names by dialect and grammar position Dec 3, 2026
Codacy's PMD rule FieldDeclarationsShouldBeAtStartOfClass reported the six
keyword sets of NameKeywords, which follow its private constructor. The
constructor now follows them. No change in behaviour.

Signed-off-by: minleejae <mmj9808@gmail.com>
@minleejae
minleejae force-pushed the refactor/name-positions-20261203 branch from ac3454c to 07562e6 Compare December 3, 2026 12:46

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