Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
d9edd0e
refactor: keep the keyword sets that decide names in one class
minleejae Dec 3, 2026
276628b
refactor: read the names of schema objects through ObjectName()
minleejae Dec 3, 2026
27cdde8
fix: read keywords that MySQL or PostgreSQL does not reserve as names…
minleejae Dec 3, 2026
47e9449
fix: read every keyword after a dot, and after AS in a select item, w…
minleejae Dec 3, 2026
36a8555
refactor: decide plain names in NameKeywords and name the dialect hel…
minleejae Dec 3, 2026
da04406
fix: read keyword tokens declared with a space before the colon
minleejae Dec 3, 2026
7dedc91
refactor: keep PostgreSQL 18's keyword table in NameKeywords and deri…
minleejae Dec 3, 2026
f849dad
refactor: derive the object names of MySQL from MySQL 8.4's reserved …
minleejae Dec 3, 2026
9268c16
fix: read a reserved keyword right before a dot as a name with MySQL
minleejae Dec 3, 2026
00773ae
fix: read sel as a name with MySQL and PostgreSQL
minleejae Dec 3, 2026
20126eb
fix: read sql_cache as a name with MySQL, and sql_no_cache before an …
minleejae Dec 3, 2026
1a7bf41
fix: read the type and function name keywords of PostgreSQL as role n…
minleejae Dec 3, 2026
2d9e96e
fix: read a keyword that MySQL or PostgreSQL takes as a name as an al…
minleejae Dec 3, 2026
e9b15a3
fix: read names that MySQL and PostgreSQL accept where an option of a…
minleejae Dec 3, 2026
c94e2dd
fix: read similar before TO as a name with MySQL
minleejae Dec 3, 2026
06a4b76
fix: read row, values and the type and function name keywords as name…
minleejae Dec 3, 2026
07562e6
style: declare the keyword sets of NameKeywords before its constructor
minleejae Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
449 changes: 449 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/NameKeywords.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ParserKeywordsUtils {
* compound regex tokens like {@code <K_DATETIMELITERAL: ...>} won't match.
*/
private static final Pattern SIMPLE_TOKEN_PATTERN =
Pattern.compile("<K_\\w+:\\s*\"(\\w+)\"\\s*>", Pattern.MULTILINE);
Pattern.compile("<K_\\w+\\s*:\\s*\"(\\w+)\"\\s*>", Pattern.MULTILINE);

private ParserKeywordsUtils() {
// utility class
Expand Down
1,498 changes: 812 additions & 686 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/site/sphinx/keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ The following Keywords are **reserved** in JSQLParser-|JSQLPARSER_VERSION| and m
+---------------------------+
| CROSS |
+---------------------------+
| DEFAULT |
+---------------------------+
| DISTINCT |
+---------------------------+
| DISTINCTROW |
Expand Down Expand Up @@ -69,6 +71,8 @@ The following Keywords are **reserved** in JSQLParser-|JSQLPARSER_VERSION| and m
+---------------------------+
| IF |
+---------------------------+
| IGNORE |
+---------------------------+
| IIF |
+---------------------------+
| ILIKE |
Expand Down Expand Up @@ -137,6 +141,8 @@ The following Keywords are **reserved** in JSQLParser-|JSQLPARSER_VERSION| and m
+---------------------------+
| RIGHT |
+---------------------------+
| SEMI |
+---------------------------+
| SET |
+---------------------------+
| SOME |
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/net/sf/jsqlparser/parser/NameKeywordsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect;
import net.sf.jsqlparser.statement.UnsupportedStatement;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Checks that the keyword alternatives in the grammar and the sets of {@link NameKeywords} agree,
* in both directions: a reserved keyword is read as a name exactly when one of the sets holds it,
* and {@code ReservedKeyword()}, which MySQL and PostgreSQL take after "." and PostgreSQL after the
* AS of a select item, lists every reserved keyword. With MySQL every keyword right before "." is a
* name as well, and with PostgreSQL the names of roles take its type and function name keywords.
*
* <p>
* The statements put the keyword where {@code isReservedKeywordAsIdentifier()} always finds a name
* position (after COLUMN, after "(", after "." and after AS), so that only the token alternatives
* decide.
*/
class NameKeywordsTest {

private static final File GRAMMAR =
new File("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");

static Stream<Arguments> reservedKeywords() throws IOException {
List<Arguments> arguments = new ArrayList<>();
for (String keyword : ParserKeywordsUtils.getReservedKeywords(GRAMMAR)) {
for (Dialect dialect : new Dialect[] {null, Dialect.MYSQL, Dialect.POSTGRESQL}) {
arguments.add(Arguments.of(keyword, dialect));
}
}
return arguments.stream();
}

@ParameterizedTest(name = "{0} with {1}")
@MethodSource("reservedKeywords")
void testReservedKeywordIsANameExactlyWhenASetHoldsIt(String keyword, Dialect dialect) {
int kind = CCJSqlParserUtil.newParser(keyword).getNextToken().kind;
boolean name = NameKeywords.isName(dialect == null ? null : dialect.name(), kind);

assertEquals(name, parses("ALTER TABLE t ADD COLUMN " + keyword + " INT", dialect),
"after COLUMN");
assertEquals(name, parses("INSERT INTO t (" + keyword + ") VALUES (1)", dialect),
"in a column list");
// RelObjectNameExt() takes FROM and SELECT as well, and MySQL and PostgreSQL every keyword
assertEquals(name || kind == CCJSqlParserConstants.K_FROM
|| kind == CCJSqlParserConstants.K_SELECT || dialect != null,
parses("SELECT t." + keyword + " FROM t", dialect), "after a dot");
// PostgreSQL takes every keyword after the AS of a select item, not of a table
assertEquals(name || dialect == Dialect.POSTGRESQL,
parses("SELECT a AS " + keyword + " FROM t", dialect), "after AS");
}

static Stream<String> reservedKeywordWords() throws IOException {
return ParserKeywordsUtils.getReservedKeywords(GRAMMAR).stream();
}

@ParameterizedTest(name = "{0}")
@MethodSource("reservedKeywordWords")
void testMySqlReadsAKeywordRightBeforeADotAsAName(String keyword) {
int kind = CCJSqlParserUtil.newParser(keyword).getNextToken().kind;

assertTrue(parses("ALTER TABLE " + keyword + ".t ADD COLUMN c INT", Dialect.MYSQL),
"before a dot");
assertTrue(parses("SELECT " + keyword + ".a FROM t", Dialect.MYSQL), "in an expression");
// with a space before the dot MySQL looks the word up as a keyword
assertEquals(NameKeywords.isName(Dialect.MYSQL.name(), kind),
parses("ALTER TABLE " + keyword + " .t ADD COLUMN c INT", Dialect.MYSQL),
"before a space");
}

@ParameterizedTest(name = "{0}")
@MethodSource("reservedKeywordWords")
void testPostgreSqlReadsAReservedKeywordAsARoleNameExactlyWhenASetHoldsIt(String keyword) {
Token token = CCJSqlParserUtil.newParser(keyword).getNextToken();
String dialect = Dialect.POSTGRESQL.name();
boolean parses = parses("ALTER TABLE t OWNER TO " + keyword, Dialect.POSTGRESQL);

if (NameKeywords.isTypeFuncName(dialect, token)) {
assertTrue(parses);
} else if (!NameKeywords.isReservedName(token.kind)
&& !NameKeywords.isAliasCheckedByFollower(token.kind)) {
// the reserved names and ARRAY depend on their follower here, the others on the sets
assertEquals(NameKeywords.isDialectName(dialect, token.kind), parses);
}
}

private static boolean parses(String sql, Dialect dialect) {
try {
CCJSqlParser parser = CCJSqlParserUtil.newParser(sql);
if (dialect != null) {
parser.withDialect(dialect);
}
return !(parser.Statement() instanceof UnsupportedStatement)
&& parser.getToken(1).kind == CCJSqlParserConstants.EOF;
} catch (ParseException | RuntimeException e) {
return false;
}
}
}
Loading
Loading