@@ -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;
@@ -5924,6 +5966,9 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
59245966 }
59255967
59265968 private boolean isConditionKeywordAhead(int offset) {
5969+ if (isDialectIdentifierKeyword(getToken(offset).kind)) {
5970+ return false;
5971+ }
59275972 switch (getToken(offset).kind) {
59285973 // Each suffix's start token:
59295974 case K_OVERLAPS: // OVERLAPS
@@ -12637,6 +12682,15 @@ String RelObjectName() :
1263712682 && getToken(1).kind <= MAX_NON_RESERVED_WORD })
1263812683 result = NonReservedWord()
1263912684
12685+ /* ── Keywords of other dialects that the selected dialect lacks ──── */
12686+ /* (before the guarded list, whose guard checks the position only) */
12687+ | LOOKAHEAD({ isDialectIdentifierKeyword(getToken(1).kind) })
12688+ ( tk=<K_CONNECT_BY_ROOT> | tk=<K_DISTINCTROW> | tk=<K_EXCLUDES> | tk=<K_FINAL>
12689+ | tk=<K_ILIKE> | tk=<K_INCLUDES> | tk=<K_MINUS> | tk=<K_NOCYCLE> | tk=<K_OUTPUT>
12690+ | tk=<K_PIVOT> | tk=<K_PREFERRING> | tk=<K_QUALIFY> | tk=<K_SEMI> | tk=<K_SQL_CACHE>
12691+ | tk=<K_SQL_CALC_FOUND_ROWS> | tk=<K_STRAIGHT> | tk=<K_TOP> | tk=<K_UNPIVOT>
12692+ | tk=<K_USE> | tk=<K_XOR> )
12693+
1264012694 /* ── Reserved keywords usable as identifiers (context-guarded) ─── */
1264112695 | LOOKAHEAD({ isReservedKeywordAsIdentifier() })
1264212696 ( tk=<K_ALL> | tk=<K_ANY> | tk=<K_BETWEEN> | tk=<K_CASEWHEN> | tk=<K_CONNECT>
@@ -13473,8 +13527,9 @@ void MySqlSelectModifier(PlainSelect select):
1347313527}
1347413528{
1347513529 ( LOOKAHEAD({ getToken(1).kind == K_HIGH_PRIORITY && isMySqlSelectModifierAhead() }) token=<K_HIGH_PRIORITY>
13476- | token=<K_STRAIGHT>
13477- | token=<K_SQL_CALC_FOUND_ROWS> | token=<K_SQL_CACHE>
13530+ | LOOKAHEAD({ (getToken(1).kind == K_STRAIGHT || getToken(1).kind == K_SQL_CALC_FOUND_ROWS
13531+ || getToken(1).kind == K_SQL_CACHE) && isMySqlSelectModifierAhead() })
13532+ ( token=<K_STRAIGHT> | token=<K_SQL_CALC_FOUND_ROWS> | token=<K_SQL_CACHE> )
1347813533 | LOOKAHEAD({ getToken(1).kind == K_SQL_NO_CACHE && isMySqlSelectModifierAhead() }) token=<K_SQL_NO_CACHE>
1347913534 | LOOKAHEAD({ getToken(1).kind == S_IDENTIFIER && isMySqlSelectModifierAhead() }) token=<S_IDENTIFIER> )
1348013535 {
@@ -13667,7 +13722,13 @@ PlainSelect PlainSelect() #PlainSelect:
1366713722{
1366813723 <K_SELECT>
1366913724
13670- [ LOOKAHEAD(2) <K_STRAIGHT> { plainSelect.setMySqlHintStraightJoin(true); } ]
13725+ // A choice rather than [ ... ], so that the dialect check also holds while scanning ahead
13726+ (
13727+ LOOKAHEAD({ getToken(1).kind == K_STRAIGHT && !isDialectIdentifierKeyword(K_STRAIGHT) })
13728+ <K_STRAIGHT> { plainSelect.setMySqlHintStraightJoin(true); }
13729+ |
13730+ {}
13731+ )
1367113732
1367213733 { plainSelect.setOracleHint(getOracleHint()); }
1367313734
@@ -13678,7 +13739,7 @@ PlainSelect PlainSelect() #PlainSelect:
1367813739 // Redshift allows TOP before DISTINCT
1367913740 // https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html
1368013741 // @Todo: reflect the order when de-parsing
13681- [ LOOKAHEAD(2) top = Top() { plainSelect.setTop(top); } ]
13742+ [ LOOKAHEAD(2, { !isDialectIdentifierKeyword(K_TOP) } ) top = Top() { plainSelect.setTop(top); } ]
1368213743
1368313744 ( LOOKAHEAD({ isMySqlSelectModifierAhead() }) MySqlSelectModifier(plainSelect) )*
1368413745
@@ -13691,6 +13752,7 @@ PlainSelect PlainSelect() #PlainSelect:
1369113752 [ LOOKAHEAD(2) "ON" "(" distinctOn=SelectItemsList() { plainSelect.getDistinct().setOnSelectItems(distinctOn); } ")" ]
1369213753 )
1369313754 |
13755+ LOOKAHEAD({ getToken(1).kind == K_DISTINCTROW && !isDialectIdentifierKeyword(K_DISTINCTROW) })
1369413756 <K_DISTINCTROW> { distinct = new Distinct(); distinct.setUseDistinctRow(true); plainSelect.setDistinct(distinct); }
1369513757 |
1369613758 <K_UNIQUE> { distinct = new Distinct(true); plainSelect.setDistinct(distinct); }
@@ -13715,7 +13777,7 @@ PlainSelect PlainSelect() #PlainSelect:
1371513777 )
1371613778 ]
1371713779
13718- [ LOOKAHEAD(2) top = Top() { plainSelect.setTop(top); } ]
13780+ [ LOOKAHEAD(2, { !isDialectIdentifierKeyword(K_TOP) } ) top = Top() { plainSelect.setTop(top); } ]
1371913781
1372013782 (
1372113783 // PostgreSQL allows SELECT with no target expressions.
@@ -15653,14 +15715,14 @@ OracleHierarchicalExpression OracleHierarchicalQueryClause():
1565315715 (
1565415716 (
1565515717 <K_START> <K_WITH> expr=XorExpression() {result.setStartExpression(expr);}
15656- <K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
15718+ <K_CONNECT> <K_BY> [ LOOKAHEAD(1, { !isDialectIdentifierKeyword(K_NOCYCLE) }) <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
1565715719 {
1565815720 result.setConnectExpression(expr);
1565915721 }
1566015722 )
1566115723 |
1566215724 (
15663- <K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
15725+ <K_CONNECT> <K_BY> [ LOOKAHEAD(1, { !isDialectIdentifierKeyword(K_NOCYCLE) }) <K_NOCYCLE> { result.setNoCycle(true); } ] expr=XorExpression()
1566415726 {
1566515727 result.setConnectExpression(expr);
1566615728 result.setConnectFirst(true);
@@ -16382,7 +16444,7 @@ Expression XorExpression():
1638216444{
1638316445 result=AndChain()
1638416446 (
16385- LOOKAHEAD(2)
16447+ LOOKAHEAD(2, { getToken(1).kind != K_XOR || !isDialectIdentifierKeyword(K_XOR) } )
1638616448 (
1638716449 <K_OR> right=AndChain() { result = new OrExpression(result, right); linkAST(result, first); }
1638816450 |
@@ -17618,7 +17680,9 @@ Expression PrimaryExpression() #PrimaryExpression:
1761817680
1761917681 | LOOKAHEAD(2, {!interrupted}) retval = NextValExpression()
1762017682
17621- | retval=ConnectByRootOperator()
17683+ | LOOKAHEAD({ getToken(1).kind == K_CONNECT_BY_ROOT
17684+ && !isDialectIdentifierKeyword(K_CONNECT_BY_ROOT) })
17685+ retval=ConnectByRootOperator()
1762217686
1762317687 | LOOKAHEAD({ isPriorOperatorAhead() }) retval=ConnectByPriorOperator()
1762417688
0 commit comments