Skip to content

Commit 47e9449

Browse files
committed
fix: read every keyword after a dot, and after AS in a select item, where 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>
1 parent 27cdde8 commit 47e9449

3 files changed

Lines changed: 123 additions & 32 deletions

File tree

‎src/main/java/net/sf/jsqlparser/parser/NameKeywords.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
/**
1313
* The keyword sets that decide which keywords the parser reads as names: in
14-
* {@code RelObjectName()}, and with MySQL and PostgreSQL in {@code ObjectName()}.
14+
* {@code RelObjectName()}, and with MySQL and PostgreSQL in {@code ObjectName()} and
15+
* {@code AliasName()}.
1516
*
1617
* <p>
1718
* JavaCC needs the tokens of a choice spelled out, so the grammar lists the reserved keywords and

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 110 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4668,6 +4668,12 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
46684668
return NameKeywords.isDialectName(getAsString(Feature.dialect), kind);
46694669
}
46704670

4671+
/** MySQL and PostgreSQL read every keyword after "." as a name. */
4672+
private boolean isEveryKeywordNameDialect() {
4673+
String dialect = getAsString(Feature.dialect);
4674+
return Dialect.MYSQL.name().equals(dialect) || Dialect.POSTGRESQL.name().equals(dialect);
4675+
}
4676+
46714677
/**
46724678
* Whether the selected dialect reads the next keyword as the name of a schema object wherever
46734679
* ObjectName() stands, although this parser reserves it, as NameKeywords.isObjectName()
@@ -11572,7 +11578,7 @@ ReturningOutputAlias ReturningOutputAliasDefinition():
1157211578
{
1157311579
refType = ReturningReferenceKind()
1157411580
<K_AS>
11575-
aliasName = RelObjectName()
11581+
aliasName = AliasName()
1157611582
{
1157711583
return new ReturningOutputAlias(refType, aliasName);
1157811584
}
@@ -12022,7 +12028,7 @@ Insert Insert():
1202212028
[ LOOKAHEAD({ isAliasAhead() && !isColumnMatchingAhead() && !isInsertTableSourceAhead()
1202312029
&& !(getToken(1).kind == K_OVERRIDING
1202412030
&& (getToken(2).kind == K_SYSTEM || getToken(2).kind == K_USER)) })
12025-
[<K_AS> { useAs = true; } ] name=RelObjectName() { table.setAlias(new Alias(name,useAs)); }]
12031+
[<K_AS> { useAs = true; } ] name=AliasName() { table.setAlias(new Alias(name,useAs)); }]
1202612032

1202712033
[ LOOKAHEAD(2) columns=InsertColumnList() ]
1202812034

@@ -12598,7 +12604,7 @@ ObjectNames RelObjectNames() : {
1259812604
( ( delimiter = "." | delimiter = ":" ) { delimiters.add(delimiter.image); } )
1259912605
)
1260012606

12601-
token = RelObjectNameExt() { data.add(token); }
12607+
token = NamePart() { data.add(token); }
1260212608
) *
1260312609

1260412610
{ return new ObjectNames(data, delimiters); }
@@ -12623,7 +12629,7 @@ ObjectNames ColumnIdentifier() : {
1262312629
( delimiter = "." { delimiters.add(delimiter.image); } )
1262412630
)
1262512631

12626-
token = RelObjectNameExt() { data.add(token); }
12632+
token = NamePart() { data.add(token); }
1262712633
) *
1262812634

1262912635
{ return new ObjectNames(data, delimiters); }
@@ -12675,8 +12681,10 @@ Column Column() #Column :
1267512681
* parameters (after ':'), index columns, constraint names, and similar
1267612682
* restricted contexts. Those call sites add the two tokens inline.
1267712683
*
12678-
* Names of schema objects are read through ObjectName(), where the dialect decides which other
12679-
* keywords are names.
12684+
* Names of schema objects, parts of qualified names and aliases are read through ObjectName(),
12685+
* NamePart(), AliasName() and SelectItemAlias(), where the dialect decides which other keywords
12686+
* are names. The other words that are read as names, such as option names or field names, call
12687+
* RelObjectName() directly.
1268012688
*/
1268112689
String RelObjectName() :
1268212690
{ Token tk = null; String result = null; }
@@ -12727,6 +12735,35 @@ Token ReservedName() :
1272712735
{ return tk; }
1272812736
}
1272912737

12738+
/*
12739+
* Every keyword that this parser reserves, for the positions where a dialect takes any keyword as
12740+
* a name. NameKeywordsTest checks that the list is complete.
12741+
*/
12742+
Token ReservedKeyword() :
12743+
{ Token tk; }
12744+
{
12745+
(
12746+
tk=<K_ALL> | tk=<K_AND> | tk=<K_ANY> | tk=<K_AS> | tk=<K_BETWEEN> | tk=<K_BOTH>
12747+
| tk=<K_CASEWHEN> | tk=<K_CHECK> | tk=<K_CONNECT> | tk=<K_CONNECT_BY_ROOT>
12748+
| tk=<K_CONSTRAINT> | tk=<K_CREATE> | tk=<K_CROSS> | tk=<K_DISTINCT> | tk=<K_DISTINCTROW>
12749+
| tk=<K_ELSE> | tk=<K_EXCEPT> | tk=<K_EXCLUDES> | tk=<K_FALSE> | tk=<K_FETCH> | tk=<K_FINAL>
12750+
| tk=<K_FOR> | tk=<K_FOREIGN> | tk=<K_FROM> | tk=<K_FULL> | tk=<K_GLOBAL> | tk=<K_GROUP>
12751+
| tk=<K_GROUPING> | tk=<K_HAVING> | tk=<K_IF> | tk=<K_IIF> | tk=<K_ILIKE> | tk=<K_IN>
12752+
| tk=<K_INCLUDES> | tk=<K_INNER> | tk=<K_INTERSECT> | tk=<K_INTERVAL> | tk=<K_INTO>
12753+
| tk=<K_IS> | tk=<K_JOIN> | tk=<K_LATERAL> | tk=<K_LEFT> | tk=<K_LIKE> | tk=<K_LIMIT>
12754+
| tk=<K_MINUS> | tk=<K_NATURAL> | tk=<K_NOCYCLE> | tk=<K_NOT> | tk=<K_NULL> | tk=<K_OFFSET>
12755+
| tk=<K_ON> | tk=<K_ONLY> | tk=<K_OPTIMIZE> | tk=<K_OR> | tk=<K_ORDER> | tk=<K_OUTER>
12756+
| tk=<K_OUTPUT> | tk=<K_PIVOT> | tk=<K_PREFERRING> | tk=<K_PROCEDURE> | tk=<K_PUBLIC>
12757+
| tk=<K_QUALIFY> | tk=<K_RETURNING> | tk=<K_RIGHT> | tk=<K_SELECT> | tk=<K_SET>
12758+
| tk=<K_SOME> | tk=<K_SQL_CACHE> | tk=<K_SQL_CALC_FOUND_ROWS> | tk=<K_START>
12759+
| tk=<K_STRAIGHT> | tk=<K_TABLES> | tk=<K_TABLESAMPLE> | tk=<K_TOP> | tk=<K_TRAILING>
12760+
| tk=<K_TRUE> | tk=<K_UNION> | tk=<K_UNIQUE> | tk=<K_UNPIVOT> | tk=<K_USE> | tk=<K_USING>
12761+
| tk=<K_VALUE> | tk=<K_VALUES> | tk=<K_WHEN> | tk=<K_WHERE> | tk=<K_WINDOW> | tk=<K_WITH>
12762+
| tk=<K_XOR>
12763+
)
12764+
{ return tk; }
12765+
}
12766+
1273012767
/*
1273112768
* Extended version of RelObjectName that additionally accepts FROM and SELECT
1273212769
* as unquoted identifiers.
@@ -12767,6 +12804,54 @@ String ObjectName() :
1276712804
{ return name; }
1276812805
}
1276912806

12807+
/*
12808+
* A part of a qualified name after ".", such as the column of t.c or the table of s.t. MySQL and
12809+
* PostgreSQL (attr_name) take every keyword there, as only a name can follow a "."; otherwise it
12810+
* reads what RelObjectNameExt() reads.
12811+
*/
12812+
String NamePart() :
12813+
{ String name; Token tk; }
12814+
{
12815+
(
12816+
LOOKAHEAD(1, { isEveryKeywordNameDialect() }) tk=ReservedKeyword() { name = tk.image; }
12817+
|
12818+
name = RelObjectNameExt()
12819+
)
12820+
{ return name; }
12821+
}
12822+
12823+
/*
12824+
* The alias of a select item, a table or another source, after AS or without it. With MySQL and
12825+
* PostgreSQL the keywords that the dialect does not reserve are aliases, as they are names of
12826+
* objects. Whether an alias starts without AS is decided before, by isAliasAhead().
12827+
*/
12828+
String AliasName() :
12829+
{ String name; Token tk; }
12830+
{
12831+
(
12832+
LOOKAHEAD({ isDialectObjectName() }) tk=ReservedName() { name = tk.image; }
12833+
|
12834+
name = RelObjectName()
12835+
)
12836+
{ return name; }
12837+
}
12838+
12839+
/*
12840+
* The alias of a select item. PostgreSQL takes every keyword after AS there (ColLabel), as in
12841+
* SELECT 1 AS from, though not as the alias of a table; otherwise it is an Alias().
12842+
*/
12843+
Alias SelectItemAlias() :
12844+
{ Token tk; Alias alias; }
12845+
{
12846+
(
12847+
LOOKAHEAD(2, { isPostgreSqlDialect() }) <K_AS> tk=ReservedKeyword()
12848+
{ alias = new Alias(tk.image, true); }
12849+
|
12850+
alias = Alias()
12851+
)
12852+
{ return alias; }
12853+
}
12854+
1277012855
Table Table() #TableName :
1277112856
{
1277212857
//String serverName = null, databaseName = null, schemaName = null, tableName = null;
@@ -13535,20 +13620,20 @@ LateralView LateralView() #LateralView:
1353513620
<K_LATERAL> <K_VIEW> [ <K_OUTER> { useOuter=true; } ]
1353613621
generatorFunction = Function()
1353713622
[ LOOKAHEAD(2)
13538-
tableName=RelObjectName()
13623+
tableName=AliasName()
1353913624
{
1354013625
tableAlias = new Alias(tableName, false);
1354113626
}
1354213627
]
13543-
<K_AS> columnName = RelObjectName() { columnAlias = new Alias(columnName, true); }
13628+
<K_AS> columnName = AliasName() { columnAlias = new Alias(columnName, true); }
1354413629

1354513630
// Spark SQL supports multiple Alias Columns: https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-lateral-view.html
1354613631
// we simulate this by setting the alias name to null and then just adding the columns
1354713632
[
1354813633
LOOKAHEAD(2) "," { columnAlias.setName(null); columnAlias.addAliasColumns( columnName); }
13549-
columnName = RelObjectName() { columnAlias.addAliasColumns( columnName); }
13634+
columnName = AliasName() { columnAlias.addAliasColumns( columnName); }
1355013635
(
13551-
LOOKAHEAD(2) "," columnName = RelObjectName() { columnAlias.addAliasColumns( columnName); }
13636+
LOOKAHEAD(2) "," columnName = AliasName() { columnAlias.addAliasColumns( columnName); }
1355213637
)*
1355313638
]
1355413639
{
@@ -14150,7 +14235,7 @@ WithItem<?> WithItem() #WithItem:
1415014235
// ClickHouse style expression alias: WITH <expression> AS <identifier>
1415114236
expr = WithItemExpression()
1415214237
<K_AS>
14153-
name = RelObjectName()
14238+
name = AliasName()
1415414239
{
1415514240
withItem = new WithItem().withExpression(expr).withAlias(new Alias(name, false));
1415614241
}
@@ -14331,7 +14416,7 @@ SelectItem<?> SelectItem() #SelectItem:
1433114416
}
1433214417
{
1433314418
expression = SelectItemExpression()
14334-
[ LOOKAHEAD({ isAliasAhead() }) alias=Alias() ]
14419+
[ LOOKAHEAD({ isAliasAhead() }) alias=SelectItemAlias() ]
1433514420
{
1433614421
SelectItem<?> selectItem = new SelectItem(expression, alias);
1433714422
linkAST(selectItem,jjtThis);
@@ -14602,7 +14687,7 @@ Alias Alias():
1460214687
// SELECT fun(x) AS (a,b,c)
1460314688
// SELECT fun(x) AS T(a,b,c)
1460414689
<K_AS>
14605-
[ LOOKAHEAD(2) name=RelObjectName() ]
14690+
[ LOOKAHEAD(2) name=AliasName() ]
1460614691
{ alias = new Alias(name, true ); }
1460714692

1460814693
aliasColumns = AliasColumns() { alias.setAliasColumns(aliasColumns); }
@@ -14616,7 +14701,7 @@ Alias Alias():
1461614701
// SELECT fun(x) T(a,b,c)
1461714702

1461814703
[<K_AS> { useAs = true; } ]
14619-
( name=RelObjectName() | token=<S_CHAR_LITERAL> { name=token.image; } )
14704+
( name=AliasName() | token=<S_CHAR_LITERAL> { name=token.image; } )
1462014705
{ alias = new Alias(name,useAs); }
1462114706

1462214707
[ LOOKAHEAD(2) aliasColumns = AliasColumns() { alias.setAliasColumns(aliasColumns); } ]
@@ -15619,7 +15704,7 @@ Join JoinerExpression() #JoinerExpression:
1561915704
( "," tableColumn=Column() { columns.add(tableColumn); } ) *
1562015705
")" { join.setUsingColumns(columns); }
1562115706
// PostgreSQL join_using_alias: a qualified join only, AS is required
15622-
[ LOOKAHEAD(2) <K_AS> usingAlias=RelObjectName() {
15707+
[ LOOKAHEAD(2) <K_AS> usingAlias=AliasName() {
1562315708
requireSyntax(!join.isNatural() && !join.isCross()
1562415709
&& !join.isSimple() && hasUnqualifiedColumns(columns),
1562515710
"An alias of the USING columns requires a qualified join"
@@ -17578,7 +17663,7 @@ Expression NavigationStep(Expression base):
1757817663
LOOKAHEAD({ Dialect.SQLSERVER.name().equals(getAsString(Feature.dialect)) && isFunctionAhead() })
1757917664
method=Function() { step = new MethodCallExpression(base, method); }
1758017665
|
17581-
nm=RelObjectNameExt() { step = new RowGetExpression(base, nm); }
17666+
nm=NamePart() { step = new RowGetExpression(base, nm); }
1758217667
)
1758317668
|
1758417669
step = ArrayExpression(base)
@@ -20915,7 +21000,7 @@ String IndexKeyAttributeName():
2091521000
{
2091621001
name=ObjectName()
2091721002
( LOOKAHEAD(".", { Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect)) })
20918-
"." part=RelObjectNameExt() { name += "." + part; }
21003+
"." part=NamePart() { name += "." + part; }
2091921004
)*
2092021005
{ return name; }
2092121006
}
@@ -20944,7 +21029,7 @@ String PostgreSqlStorageParameterName():
2094421029
{ String name; String part; }
2094521030
{
2094621031
name=RelObjectName()
20947-
( LOOKAHEAD(1, ".") "." part=RelObjectNameExt() { name += "." + part; } )*
21032+
( LOOKAHEAD(1, ".") "." part=NamePart() { name += "." + part; } )*
2094821033
{ return name; }
2094921034
}
2095021035

@@ -24106,7 +24191,7 @@ XmlTypeModifier XmlTypeModifier():
2410624191
token=<S_IDENTIFIER> { kind = XmlTypeModifier.Kind.valueOf(token.image.toUpperCase(Locale.ROOT)); }
2410724192
]
2410824193
name=ObjectName() { names.add(name); }
24109-
[ "." name=RelObjectNameExt() { names.add(name); } ]
24194+
[ "." name=NamePart() { names.add(name); } ]
2411024195
")"
2411124196
{ return new XmlTypeModifier(kind, names); }
2411224197
}
@@ -24288,7 +24373,7 @@ HandlerStatement HandlerStatement():
2428824373
table=Table() <K_OPEN>
2428924374
[
2429024375
[ <K_AS> { useAs = true; } ]
24291-
name=RelObjectName() { table.setAlias(new Alias(name, useAs)); }
24376+
name=AliasName() { table.setAlias(new Alias(name, useAs)); }
2429224377
]
2429324378
|
2429424379
name=ObjectName() { table = linkToLastToken(createTable(Arrays.asList(name))); }
@@ -28741,7 +28826,7 @@ void AccessKeyword(String expected):
2874128826

2874228827
String AccessQualifiedName():
2874328828
{ String name; String part; }
28744-
{ name=ObjectName() ( "." part=RelObjectNameExt() { name += "." + part; } )* { return name; } }
28829+
{ name=ObjectName() ( "." part=NamePart() { name += "." + part; } )* { return name; } }
2874528830

2874628831
List<String> AccessNames():
2874728832
{ List<String> names = new ArrayList<String>(); String name; }
@@ -29169,7 +29254,7 @@ TransitionRelation PostgreSqlTransitionRelation():
2916929254
{ TransitionRelation result = new TransitionRelation(); Token token; String name; }
2917029255
{
2917129256
( token=<S_IDENTIFIER> ) { result.setImage(accessEnum(TransitionRelation.Image.class, token.image)); }
29172-
<K_TABLE> [ <K_AS> { result.setUseAs(true); } ] name=RelObjectName() { result.setName(name); return result; }
29257+
<K_TABLE> [ <K_AS> { result.setUseAs(true); } ] name=AliasName() { result.setName(name); return result; }
2917329258
}
2917429259

2917529260
/** PostgreSQL ALTER TRIGGER name ON table RENAME TO or [NO] DEPENDS ON EXTENSION. */
@@ -30024,7 +30109,7 @@ String TypeDdlName():
3002430109
{ String name; String part; }
3002530110
{
3002630111
name=ObjectName()
30027-
( LOOKAHEAD(2) "." part=RelObjectNameExt() { name += "." + part; } )*
30112+
( LOOKAHEAD(2) "." part=NamePart() { name += "." + part; } )*
3002830113
{ return name; }
3002930114
}
3003030115

@@ -31166,7 +31251,7 @@ String IdentifierChain():
3116631251
}
3116731252
{
3116831253
identifierChain=RelObjectNameExt()
31169-
( LOOKAHEAD(2) "." part=RelObjectNameExt() { identifierChain += "." + part; } )*
31254+
( LOOKAHEAD(2) "." part=NamePart() { identifierChain += "." + part; } )*
3117031255

3117131256
{
3117231257
return identifierChain;
@@ -31178,7 +31263,7 @@ String IdentifierChain2(String identifierChain):
3117831263
String part;
3117931264
}
3118031265
{
31181-
( LOOKAHEAD(2) "." part=RelObjectNameExt() { identifierChain += "." + part; } )*
31266+
( LOOKAHEAD(2) "." part=NamePart() { identifierChain += "." + part; } )*
3118231267
{
3118331268
return identifierChain;
3118431269
}

‎src/test/java/net/sf/jsqlparser/parser/NameKeywordsTest.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import org.junit.jupiter.params.provider.MethodSource;
2424

2525
/**
26-
* Checks that the keyword alternatives of {@code RelObjectName()} in the grammar and the sets of
27-
* {@link NameKeywords} agree, in both directions: a reserved keyword is read as a name exactly when
28-
* one of the sets holds it.
26+
* Checks that the keyword alternatives in the grammar and the sets of {@link NameKeywords} agree,
27+
* in both directions: a reserved keyword is read as a name exactly when one of the sets holds it,
28+
* and {@code ReservedKeyword()}, which MySQL and PostgreSQL take after "." and PostgreSQL after the
29+
* AS of a select item, lists every reserved keyword.
2930
*
3031
* <p>
3132
* The statements put the keyword where {@code isReservedKeywordAsIdentifier()} always finds a name
32-
* position (after COLUMN, after "(" and after "."), so that only the token alternatives decide.
33+
* position (after COLUMN, after "(", after "." and after AS), so that only the token alternatives
34+
* decide.
3335
*/
3436
class NameKeywordsTest {
3537

@@ -57,10 +59,13 @@ void testReservedKeywordIsANameExactlyWhenASetHoldsIt(String keyword, Dialect di
5759
"after COLUMN");
5860
assertEquals(name, parses("INSERT INTO t (" + keyword + ") VALUES (1)", dialect),
5961
"in a column list");
60-
// RelObjectNameExt() takes FROM and SELECT as well
62+
// RelObjectNameExt() takes FROM and SELECT as well, and MySQL and PostgreSQL every keyword
6163
assertEquals(name || kind == CCJSqlParserConstants.K_FROM
62-
|| kind == CCJSqlParserConstants.K_SELECT,
64+
|| kind == CCJSqlParserConstants.K_SELECT || dialect != null,
6365
parses("SELECT t." + keyword + " FROM t", dialect), "after a dot");
66+
// PostgreSQL takes every keyword after the AS of a select item, not of a table
67+
assertEquals(name || dialect == Dialect.POSTGRESQL,
68+
parses("SELECT a AS " + keyword + " FROM t", dialect), "after AS");
6469
}
6570

6671
private static boolean parses(String sql, Dialect dialect) {

0 commit comments

Comments
 (0)