Skip to content

Commit

Permalink
Add BQ data types to babel and server
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasbanghart authored and wnob committed Nov 22, 2022
1 parent b7eebb1 commit 7a84f33
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
24 changes: 24 additions & 0 deletions babel/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ data: {
"org.apache.calcite.sql.babel.SqlBabelCreateTable",
"org.apache.calcite.sql.babel.TableCollectionType",
"org.apache.calcite.sql.ddl.SqlDdlNodes",
# TODO(CALCITE-5346) BQ specific
"org.apache.calcite.sql.SqlAlienSystemTypeNameSpec",
]

# List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
# not a reserved keyword, add it to the 'nonReservedKeywords' section.
keywords: [
# TODO(CALCITE-5346) BQ specific
"STRING"
"BYTES"
"FLOAT64"
"BOOL"
"INT64"

"IF"
"SEMI"
"VOLATILE"
Expand All @@ -44,6 +53,13 @@ data: {
# List of non-reserved keywords to add;
# items in this list become non-reserved
nonReservedKeywordsToAdd: [
# TODO(CALCITE-5346) BQ specific
"STRING"
"BYTES"
"FLOAT64"
"BOOL"
"INT64"

# not in core, added in babel
"IF"
"SEMI"
Expand Down Expand Up @@ -557,6 +573,14 @@ data: {
"NullSafeEqual"
]

# List of methods for parsing custom data types.
# Return type of method implementation should be "SqlTypeNameSpec".
# Example: SqlParseTimeStampZ().
dataTypeParserMethods: [
# TODO(CALCITE-5346) BQ specific
"BigQuerySqlTypeNames()"
]

# 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
Expand Down
48 changes: 48 additions & 0 deletions babel/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,51 @@ void NullSafeEqual(List<Object> list, ExprContext exprContext, Span s) :
}
AddExpression2b(list, ExprContext.ACCEPT_SUB_QUERY)
}

/**
* A sql type name extended basic data type, it has a counterpart basic
* sql type name but always represents as a special alias compared with the standard name.
* We'll want to replace this with a more generic solution at some point but this
* satisfies our immediate need. This fix-up should not be merged upstream.
*
* TODO(CALCITE-5346)
*/
SqlTypeNameSpec BigQuerySqlTypeNames() :
{
final SqlTypeName typeName;
final String typeAlias;
int precision = -1;
}
{
(
<BOOL> {
typeName = SqlTypeName.BOOLEAN;
typeAlias = token.image;
}
|
<BYTES> {
typeName = SqlTypeName.VARBINARY;
typeAlias = token.image;
precision = Integer.MAX_VALUE;
}
|
<FLOAT64> {
typeName = SqlTypeName.DOUBLE;
typeAlias = token.image;
}
|
<INT64> {
typeName = SqlTypeName.BIGINT;
typeAlias = token.image;
}
|
<STRING> {
typeName = SqlTypeName.VARCHAR;
typeAlias = token.image;
precision = Integer.MAX_VALUE;
}
)
{
return new SqlAlienSystemTypeNameSpec(typeAlias, typeName, precision, getPos());
}
}
9 changes: 9 additions & 0 deletions core/src/main/codegen/default_config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parser: {
# List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
# not a reserved keyword, add it to the 'nonReservedKeywords' section.
keywords: [
# TODO(CALCITE-5346) BQ specific
"STRING"
"BYTES"
"FLOAT64"
Expand All @@ -35,6 +36,13 @@ parser: {

# List of keywords from "keywords" section that are not reserved.
nonReservedKeywords: [
# TODO(CALCITE-5346) BQ specific
"STRING"
"BYTES"
"FLOAT64"
"BOOL"
"INT64"

"A"
"ABSENT"
"ABSOLUTE"
Expand Down Expand Up @@ -392,6 +400,7 @@ parser: {
# Return type of method implementation should be "SqlTypeNameSpec".
# Example: SqlParseTimeStampZ().
dataTypeParserMethods: [
# TODO(CALCITE-5346) BQ specific
"BigQuerySqlTypeNames()"
]

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* We'll want to replace this with a more generic solution at some point but this
* satisfies our immediate need. This fix-up should not be merged upstream.
*
* For tracking https://issues.apache.org/jira/browse/CALCITE-5346
* TODO(CALCITE-5346)
*/
SqlTypeNameSpec BigQuerySqlTypeNames() :
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.fun.SqlLibrary;
import org.apache.calcite.sql.fun.SqlLibraryOperatorTableFactory;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.validate.SqlNameMatchers;
Expand Down
17 changes: 17 additions & 0 deletions server/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ data: {
"org.apache.calcite.sql.SqlCreate"
"org.apache.calcite.sql.SqlDrop"
"org.apache.calcite.sql.ddl.SqlDdlNodes"
# TODO(CALCITE-5346) BQ specific
"org.apache.calcite.sql.SqlAlienSystemTypeNameSpec"
]

# List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
# not a reserved keyword, add it to the 'nonReservedKeywords' section.
keywords: [
# TODO(CALCITE-5346) BQ specific
"STRING"
"BYTES"
"FLOAT64"
"BOOL"
"INT64"

"IF"
"MATERIALIZED"
"STORED"
Expand Down Expand Up @@ -82,6 +91,14 @@ data: {
"SqlDropFunction"
]

# List of methods for parsing custom data types.
# Return type of method implementation should be "SqlTypeNameSpec".
# Example: SqlParseTimeStampZ().
dataTypeParserMethods: [
# TODO(CALCITE-5346) BQ specific
"BigQuerySqlTypeNames()"
]

# 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
Expand Down
48 changes: 48 additions & 0 deletions server/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,51 @@ SqlDrop SqlDropFunction(Span s, boolean replace) :
return SqlDdlNodes.dropFunction(s.end(this), ifExists, id);
}
}

/**
* A sql type name extended basic data type, it has a counterpart basic
* sql type name but always represents as a special alias compared with the standard name.
* We'll want to replace this with a more generic solution at some point but this
* satisfies our immediate need. This fix-up should not be merged upstream.
*
* TODO(CALCITE-5346)
*/
SqlTypeNameSpec BigQuerySqlTypeNames() :
{
final SqlTypeName typeName;
final String typeAlias;
int precision = -1;
}
{
(
<BOOL> {
typeName = SqlTypeName.BOOLEAN;
typeAlias = token.image;
}
|
<BYTES> {
typeName = SqlTypeName.VARBINARY;
typeAlias = token.image;
precision = Integer.MAX_VALUE;
}
|
<FLOAT64> {
typeName = SqlTypeName.DOUBLE;
typeAlias = token.image;
}
|
<INT64> {
typeName = SqlTypeName.BIGINT;
typeAlias = token.image;
}
|
<STRING> {
typeName = SqlTypeName.VARCHAR;
typeAlias = token.image;
precision = Integer.MAX_VALUE;
}
)
{
return new SqlAlienSystemTypeNameSpec(typeAlias, typeName, precision, getPos());
}
}

0 comments on commit 7a84f33

Please sign in to comment.