Skip to content

Commit

Permalink
current reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
kgyrtkirk committed May 28, 2024
1 parent 2b698d7 commit 57d060a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 141 deletions.
59 changes: 3 additions & 56 deletions sql/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,75 +33,22 @@
# part of the calcite-core-<version>.jar under "codegen" directory.

data: {
default: tdd("../default_config.fmpp")

# Data declarations for this parser.
#
# Default declarations are in default_config.fmpp; if you do not include a
# declaration ('imports' or 'nonReservedKeywords', for example) in this file,
# FMPP will use the declaration from default_config.fmpp.
parser: {
# Generated parser implementation package and class name.
package: "org.apache.druid.sql.calcite.parser",
class: "DruidSqlParserImpl",

# List of additional classes and packages to import.
# Example. "org.apache.calcite.sql.*", "java.util.List".
imports: [
"java.util.List"
"org.apache.calcite.sql.SqlNode"
"org.apache.calcite.sql.SqlInsert"
"org.apache.calcite.sql.SqlNodeList"
"org.apache.calcite.sql.SqlBasicCall"
"org.apache.druid.java.util.common.granularity.Granularity"
"org.apache.druid.java.util.common.granularity.GranularityType"
"org.apache.druid.java.util.common.granularity.Granularities"
"org.apache.druid.sql.calcite.parser.DruidSqlInsert"
"org.apache.druid.sql.calcite.parser.DruidSqlParserUtils"
"org.apache.druid.sql.calcite.external.ExtendOperator"
"org.apache.druid.sql.calcite.external.ParameterizeOperator"
"org.apache.druid.sql.calcite.parser.ExternalDestinationSqlIdentifier"
"java.util.HashMap"
]

# List of new keywords. Example: "DATABASES", "TABLES". If the keyword is not a reserved
# keyword add it to 'nonReservedKeywords' section.
keywords: [
"CLUSTERED"
"OVERWRITE"
"PARTITIONED"
"EXTERN"
]

nonReservedKeywordsToAdd: [
"OVERWRITE"
"EXTERN"
]

# List of methods for parsing custom SQL statements.
# Return type of method implementation should be 'SqlNode'.
# Example: SqlShowDatabases(), SqlShowTables().
statementParserMethods: [
"DruidSqlInsertEof()"
"DruidSqlExplain()"
"DruidSqlReplaceEof()"
]

# List of methods for parsing custom data types.
# Return type of method implementation should be "SqlTypeNameSpec".
# Example: SqlParseTimeStampZ().
dataTypeParserMethods: [
"DruidType()"
]
package: "org.apache.calcite.sql.parser.impl",
class: "SqlParserImpl",

# List of files in @includes directory that have parser method
# implementations for parsing custom SQL statements, literals or types
# given as part of "statementParserMethods", "literalParserMethods" or
# "dataTypeParserMethods".
implementationFiles: [
"common.ftl"
"explain.ftl"
"replace.ftl"
"parserImpls.ftl"
]
}
}
Expand Down
94 changes: 9 additions & 85 deletions sql/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -1697,21 +1697,16 @@ SqlNode CreateSetSemanticsTableIfNeeded(
/**
* Parses an INSERT statement.
*/
// Using fully qualified name for Pair class, since Calcite also has a same class name being used in the Parser.jj
SqlNode SqlInsert() :
{
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
final SqlNodeList keywordList;
final SqlIdentifier destination;
SqlNode tableRef = null;
final SqlIdentifier tableName;
SqlNode tableRef;
SqlNode source;
final SqlNodeList columnList;
final Span s;
final Pair<SqlNodeList, SqlNodeList> p;
SqlInsert sqlInsert;
SqlGranularityLiteral partitionedBy = null;
SqlNodeList clusteredBy = null;
SqlIdentifier exportFileFormat = null;
}
{
(
Expand All @@ -1723,15 +1718,9 @@ SqlNode SqlInsert() :
SqlInsertKeywords(keywords) {
keywordList = new SqlNodeList(keywords, s.addAll(keywords).pos());
}
<INTO>
(
LOOKAHEAD(2)
<EXTERN> <LPAREN> destination = ExternalDestination() <RPAREN>
|
destination = CompoundTableIdentifier()
( tableRef = TableHints(destination) | { tableRef = destination; } )
[ LOOKAHEAD(5) tableRef = ExtendTable(tableRef) ]
)
<INTO> tableName = CompoundTableIdentifier()
( tableRef = TableHints(tableName) | { tableRef = tableName; } )
[ LOOKAHEAD(5) tableRef = ExtendTable(tableRef) ]
(
LOOKAHEAD(2)
p = ParenthesizedCompoundIdentifierList() {
Expand All @@ -1746,47 +1735,12 @@ SqlNode SqlInsert() :
}
| { columnList = null; }
)
[
<AS> exportFileFormat = FileFormat()
]
(
<OVERWRITE>
{
throw org.apache.druid.sql.calcite.parser.DruidSqlParserUtils.problemParsing(
"An OVERWRITE clause is not allowed with INSERT statements. Use REPLACE statements if overwriting existing segments is required or remove the OVERWRITE clause."
);
}
|
source = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY)
)
// PARTITIONED BY is necessary, but is kept optional in the grammar. It is asserted that it is not missing in the
// IngestHandler#validate() so that we can return a custom error message.
[
<PARTITIONED> <BY>
partitionedBy = PartitionGranularity()
]
[
clusteredBy = ClusteredBy()
]
{
sqlInsert = new SqlInsert(s.end(source), keywordList, destination, source, columnList);
return DruidSqlInsert.create(sqlInsert, partitionedBy, clusteredBy, exportFileFormat);
}
}

SqlNode DruidSqlInsertEof() :
{
SqlNode sqlInsert;
}
{
sqlInsert = SqlInsert()
<EOF>
{
return sqlInsert;
source = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY) {
return new SqlInsert(s.end(source), keywordList, tableRef, source,
columnList);
}
}


/*
* Abstract production:
*
Expand Down Expand Up @@ -2044,17 +1998,6 @@ SqlLiteral JoinType() :
}
}

/*
* Druid note: this file is copied from core/src/main/codegen/templates/Parser.jj in Calcite 1.35.0, with changes to
* to add two elements of Druid syntax to the FROM clause:
*
* id [ (<args>) ]
*
* And
*
* TABLE(<fn>(<args>)) (<schema>)
*
*/
/**
* Parses the FROM clause for a SELECT.
*
Expand Down Expand Up @@ -2228,27 +2171,18 @@ SqlNode TableRef3(ExprContext exprContext, boolean lateral) :
{
final SqlIdentifier tableName;
SqlNode tableRef;
List<SqlNode> paramList;
final SqlIdentifier alias;
final Span s;
SqlNodeList args;
final SqlNodeList columnAliasList;
SqlUnnestOperator unnestOp = SqlStdOperatorTable.UNNEST;
SqlNodeList extendList = null;
}
{
(
LOOKAHEAD(2)
tableName = CompoundTableIdentifier()
( tableRef = TableHints(tableName) | { tableRef = tableName; } )
// BEGIN: Druid-specific code
[
paramList = FunctionParameterList(ExprContext.ACCEPT_NONCURSOR)
{
tableRef = ParameterizeOperator.PARAM.createCall(tableRef, paramList);
}
]
// END: Druid-specific code
[ tableRef = ExtendTable(tableRef) ]
tableRef = Over(tableRef)
[ tableRef = Snapshot(tableRef) ]
[ tableRef = MatchRecognize(tableRef) ]
Expand All @@ -2273,16 +2207,6 @@ SqlNode TableRef3(ExprContext exprContext, boolean lateral) :
|
[ <LATERAL> { lateral = true; } ]
tableRef = TableFunctionCall()
// BEGIN: Druid-specific code
[
[ <EXTEND> ]
extendList = ExtendList()
{
tableRef = ExtendOperator.EXTEND.createCall(
Span.of(tableRef, extendList).pos(), tableRef, extendList);
}
]
// END: Druid-specific code
tableRef = addLateral(tableRef, lateral)
|
tableRef = ExtendedTableRef()
Expand Down

0 comments on commit 57d060a

Please sign in to comment.