@@ -1095,7 +1095,7 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
10951095 if (op == K_AND || op == OP_DOUBLEAND) prec = 4;
10961096 // MySQL puts XOR between AND and OR; other dialects keep one
10971097 // left-associative level for OR and XOR.
1098- else if (op == K_XOR)
1098+ else if (op == K_XOR && !isDialectIdentifierKeyword(op) )
10991099 prec = Dialect.MYSQL.name().equals(getAsString(Feature.dialect)) ? 3 : 2;
11001100 else if (op == K_OR) prec = 2;
11011101 else if (ternary) prec = 2;
@@ -1425,7 +1425,7 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
14251425 * alternatives accepted there (base identifiers, DATA_TYPE, non-reserved keywords
14261426 * and the reserved-keyword-as-identifier list).
14271427 */
1428- private static boolean isIdentifierishNameToken(Token t) {
1428+ private boolean isIdentifierishNameToken(Token t) {
14291429 switch (t.kind) {
14301430 case S_IDENTIFIER: case S_QUOTED_IDENTIFIER: case DATA_TYPE:
14311431 case K_DATETIMELITERAL: case K_DATE_LITERAL:
@@ -1438,7 +1438,8 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
14381438 case K_TOP: case K_VALUE: case K_VALUES:
14391439 return true;
14401440 default:
1441- return t.kind >= MIN_NON_RESERVED_WORD && t.kind <= MAX_NON_RESERVED_WORD;
1441+ return t.kind >= MIN_NON_RESERVED_WORD && t.kind <= MAX_NON_RESERVED_WORD
1442+ || isDialectIdentifierKeyword(t.kind);
14421443 }
14431444 }
14441445
@@ -2393,7 +2394,7 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
23932394 /** SEMI/ANTI stay contextual identifiers unless they prefix a JOIN. */
23942395 private boolean isSemiAntiJoinAhead() {
23952396 int kind = getToken(1).kind;
2396- if (kind != K_SEMI && kind != K_ANTI) {
2397+ if (kind != K_SEMI && kind != K_ANTI || isDialectIdentifierKeyword(kind) ) {
23972398 return false;
23982399 }
23992400 return getToken(2).kind == K_JOIN;
@@ -2511,6 +2512,9 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
25112512 }
25122513 Token next = getToken(2);
25132514 int following = getToken(3).kind;
2515+ if (isDialectIdentifierKeyword(next.kind)) {
2516+ return false;
2517+ }
25142518 switch (next.kind) {
25152519 case CLOSING_BRACKET:
25162520 case K_FROM: case K_WHERE: case K_HAVING: case K_WINDOW: case K_INTO:
@@ -2697,6 +2701,11 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
26972701 return false;
26982702 }
26992703
2704+ // A keyword of another dialect is a plain name in a dialect that lacks it
2705+ if (isDialectIdentifierKeyword(kind)) {
2706+ return true;
2707+ }
2708+
27002709 // For reserved keywords in alias position, skip the structural-
27012710 // keyword whitelist and go directly to follower disambiguation.
27022711 // Only check keywords that are actually in RelObjectName's token
@@ -3762,7 +3771,7 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
37623771 }
37633772
37643773 /** Whether the token can name a cursor: PostgreSQL takes any column identifier (ColId). */
3765- private static boolean isCursorNameToken(Token token) {
3774+ private boolean isCursorNameToken(Token token) {
37663775 if (!isIdentifierishNameToken(token)) {
37673776 return false;
37683777 }
@@ -4631,6 +4640,39 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
46314640 return Dialect.MYSQL.name().equals(dialect) || Dialect.MARIADB.name().equals(dialect);
46324641 }
46334642
4643+ /**
4644+ * Whether the keyword belongs only to clauses of other dialects and the selected dialect does
4645+ * not know it as a keyword at all, so it is a plain identifier there. MySQL 8.4 and
4646+ * PostgreSQL 18 have no PIVOT, MINUS, OUTPUT or SEMI JOIN, MySQL no ILIKE, and PostgreSQL
4647+ * neither XOR, QUALIFY nor MySQL's select modifiers. Without a dialect they stay reserved.
4648+ */
4649+ private boolean isDialectIdentifierKeyword(int kind) {
4650+ String dialect = getAsString(Feature.dialect);
4651+ if (Dialect.MYSQL.name().equals(dialect)) {
4652+ switch (kind) {
4653+ case K_CONNECT_BY_ROOT: case K_EXCLUDES: case K_FINAL: case K_ILIKE:
4654+ case K_INCLUDES: case K_MINUS: case K_NOCYCLE: case K_OUTPUT: case K_PIVOT:
4655+ case K_PREFERRING: case K_SEMI: case K_TOP: case K_UNPIVOT:
4656+ return true;
4657+ default:
4658+ return false;
4659+ }
4660+ }
4661+ if (Dialect.POSTGRESQL.name().equals(dialect)) {
4662+ switch (kind) {
4663+ case K_CONNECT_BY_ROOT: case K_DISTINCTROW: case K_EXCLUDES: case K_FINAL:
4664+ case K_INCLUDES: case K_MINUS: case K_NOCYCLE: case K_OUTPUT: case K_PIVOT:
4665+ case K_PREFERRING: case K_QUALIFY: case K_SEMI: case K_SQL_CACHE:
4666+ case K_SQL_CALC_FOUND_ROWS: case K_STRAIGHT: case K_TOP: case K_UNPIVOT:
4667+ case K_USE: case K_XOR:
4668+ return true;
4669+ default:
4670+ return false;
4671+ }
4672+ }
4673+ return false;
4674+ }
4675+
46344676 /** MySQL's EXPLAIN, PREPARE, EXECUTE, DROP PREPARE and DO: MySQL, MariaDB or no dialect. */
46354677 private boolean isMySqlUtilityDialect() {
46364678 return getAsString(Feature.dialect) == null || isMySqlDialect();
@@ -5173,7 +5215,7 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
51735215 // These existing modifiers remain available without a dialect preset.
51745216 if (token.kind == K_STRAIGHT || token.kind == K_SQL_CACHE
51755217 || token.kind == K_SQL_CALC_FOUND_ROWS) {
5176- return true ;
5218+ return !isDialectIdentifierKeyword(token.kind) ;
51775219 }
51785220 // Keep qualified names, aliases and standalone identifier expressions intact.
51795221 int next = getToken(2).kind;
@@ -5898,6 +5940,9 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
58985940 }
58995941
59005942 private boolean isConditionKeywordAhead(int offset) {
5943+ if (isDialectIdentifierKeyword(getToken(offset).kind)) {
5944+ return false;
5945+ }
59015946 switch (getToken(offset).kind) {
59025947 // Each suffix's start token:
59035948 case K_OVERLAPS: // OVERLAPS
@@ -12597,6 +12642,15 @@ String RelObjectName() :
1259712642 && getToken(1).kind <= MAX_NON_RESERVED_WORD })
1259812643 result = NonReservedWord()
1259912644
12645+ /* ── Keywords of other dialects that the selected dialect lacks ──── */
12646+ /* (before the guarded list, whose guard checks the position only) */
12647+ | LOOKAHEAD({ isDialectIdentifierKeyword(getToken(1).kind) })
12648+ ( tk=<K_CONNECT_BY_ROOT> | tk=<K_DISTINCTROW> | tk=<K_EXCLUDES> | tk=<K_FINAL>
12649+ | tk=<K_ILIKE> | tk=<K_INCLUDES> | tk=<K_MINUS> | tk=<K_NOCYCLE> | tk=<K_OUTPUT>
12650+ | tk=<K_PIVOT> | tk=<K_PREFERRING> | tk=<K_QUALIFY> | tk=<K_SEMI> | tk=<K_SQL_CACHE>
12651+ | tk=<K_SQL_CALC_FOUND_ROWS> | tk=<K_STRAIGHT> | tk=<K_TOP> | tk=<K_UNPIVOT>
12652+ | tk=<K_USE> | tk=<K_XOR> )
12653+
1260012654 /* ── Reserved keywords usable as identifiers (context-guarded) ─── */
1260112655 | LOOKAHEAD({ isReservedKeywordAsIdentifier() })
1260212656 ( tk=<K_ALL> | tk=<K_ANY> | tk=<K_BETWEEN> | tk=<K_CASEWHEN> | tk=<K_CONNECT>
@@ -13433,8 +13487,9 @@ void MySqlSelectModifier(PlainSelect select):
1343313487}
1343413488{
1343513489 ( LOOKAHEAD({ getToken(1).kind == K_HIGH_PRIORITY && isMySqlSelectModifierAhead() }) token=<K_HIGH_PRIORITY>
13436- | token=<K_STRAIGHT>
13437- | token=<K_SQL_CALC_FOUND_ROWS> | token=<K_SQL_CACHE>
13490+ | LOOKAHEAD({ (getToken(1).kind == K_STRAIGHT || getToken(1).kind == K_SQL_CALC_FOUND_ROWS
13491+ || getToken(1).kind == K_SQL_CACHE) && isMySqlSelectModifierAhead() })
13492+ ( token=<K_STRAIGHT> | token=<K_SQL_CALC_FOUND_ROWS> | token=<K_SQL_CACHE> )
1343813493 | LOOKAHEAD({ getToken(1).kind == K_SQL_NO_CACHE && isMySqlSelectModifierAhead() }) token=<K_SQL_NO_CACHE>
1343913494 | LOOKAHEAD({ getToken(1).kind == S_IDENTIFIER && isMySqlSelectModifierAhead() }) token=<S_IDENTIFIER> )
1344013495 {
@@ -13627,7 +13682,13 @@ PlainSelect PlainSelect() #PlainSelect:
1362713682{
1362813683 <K_SELECT>
1362913684
13630- [ LOOKAHEAD(2) <K_STRAIGHT> { plainSelect.setMySqlHintStraightJoin(true); } ]
13685+ // A choice rather than [ ... ], so that the dialect check also holds while scanning ahead
13686+ (
13687+ LOOKAHEAD({ getToken(1).kind == K_STRAIGHT && !isDialectIdentifierKeyword(K_STRAIGHT) })
13688+ <K_STRAIGHT> { plainSelect.setMySqlHintStraightJoin(true); }
13689+ |
13690+ {}
13691+ )
1363113692
1363213693 { plainSelect.setOracleHint(getOracleHint()); }
1363313694
@@ -13638,7 +13699,7 @@ PlainSelect PlainSelect() #PlainSelect:
1363813699 // Redshift allows TOP before DISTINCT
1363913700 // https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html
1364013701 // @Todo: reflect the order when de-parsing
13641- [ LOOKAHEAD(2) top = Top() { plainSelect.setTop(top); } ]
13702+ [ LOOKAHEAD(2, { !isDialectIdentifierKeyword(K_TOP) } ) top = Top() { plainSelect.setTop(top); } ]
1364213703
1364313704 ( LOOKAHEAD({ isMySqlSelectModifierAhead() }) MySqlSelectModifier(plainSelect) )*
1364413705
@@ -13651,6 +13712,7 @@ PlainSelect PlainSelect() #PlainSelect:
1365113712 [ LOOKAHEAD(2) "ON" "(" distinctOn=SelectItemsList() { plainSelect.getDistinct().setOnSelectItems(distinctOn); } ")" ]
1365213713 )
1365313714 |
13715+ LOOKAHEAD({ getToken(1).kind == K_DISTINCTROW && !isDialectIdentifierKeyword(K_DISTINCTROW) })
1365413716 <K_DISTINCTROW> { distinct = new Distinct(); distinct.setUseDistinctRow(true); plainSelect.setDistinct(distinct); }
1365513717 |
1365613718 <K_UNIQUE> { distinct = new Distinct(true); plainSelect.setDistinct(distinct); }
@@ -13675,7 +13737,7 @@ PlainSelect PlainSelect() #PlainSelect:
1367513737 )
1367613738 ]
1367713739
13678- [ LOOKAHEAD(2) top = Top() { plainSelect.setTop(top); } ]
13740+ [ LOOKAHEAD(2, { !isDialectIdentifierKeyword(K_TOP) } ) top = Top() { plainSelect.setTop(top); } ]
1367913741
1368013742 (
1368113743 // PostgreSQL allows SELECT with no target expressions.
@@ -15613,14 +15675,14 @@ OracleHierarchicalExpression OracleHierarchicalQueryClause():
1561315675 (
1561415676 (
1561515677 <K_START> <K_WITH> expr=XorExpression() {result.setStartExpression(expr);}
15616- <K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
15678+ <K_CONNECT> <K_BY> [ LOOKAHEAD(1, { !isDialectIdentifierKeyword(K_NOCYCLE) }) <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
1561715679 {
1561815680 result.setConnectExpression(expr);
1561915681 }
1562015682 )
1562115683 |
1562215684 (
15623- <K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
15685+ <K_CONNECT> <K_BY> [ LOOKAHEAD(1, { !isDialectIdentifierKeyword(K_NOCYCLE) }) <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
1562415686 {
1562515687 result.setConnectExpression(expr);
1562615688 result.setConnectFirst(true);
@@ -16342,7 +16404,7 @@ Expression XorExpression():
1634216404{
1634316405 result=AndChain()
1634416406 (
16345- LOOKAHEAD(2)
16407+ LOOKAHEAD(2, { getToken(1).kind != K_XOR || !isDialectIdentifierKeyword(K_XOR) } )
1634616408 (
1634716409 <K_OR> right=AndChain() { result = new OrExpression(result, right); linkAST(result, first); }
1634816410 |
@@ -17578,7 +17640,9 @@ Expression PrimaryExpression() #PrimaryExpression:
1757817640
1757917641 | LOOKAHEAD(2, {!interrupted}) retval = NextValExpression()
1758017642
17581- | retval=ConnectByRootOperator()
17643+ | LOOKAHEAD({ getToken(1).kind == K_CONNECT_BY_ROOT
17644+ && !isDialectIdentifierKeyword(K_CONNECT_BY_ROOT) })
17645+ retval=ConnectByRootOperator()
1758217646
1758317647 | LOOKAHEAD({ isPriorOperatorAhead() }) retval=ConnectByPriorOperator()
1758417648
0 commit comments