Skip to content

Commit

Permalink
[CALCITE-5508] Add constructor functions for DATE, TIME, TIMESTAMP, D…
Browse files Browse the repository at this point in the history
…ATETIME (enabled in BigQuery library)

New DATE functions such as DATE(int, int, int) needs to work
alongside the existing DATE(string) function; all are
validated usig the type operand checker, but after
validation DATE(string) is desugared to CAST(string AS DATE).

Following
  [CALCITE-5424] Customize handling of literals based on type system
which deferred validation of DATE, TIME, TIMESTAMP literals,
we now also allow DATETIME literal. (In BigQuery mode it is
mapped to a literal in Calcite's TIMESTAMP type.)

Also includes a fix for
  [CALCITE-5498] BigQuery TIMESTAMP() function short notation for
  timezone offsets isn’t supported in Java 8

In a few places I had to change test output. I removed "UTC"
from test output and added TODO comments; these changes
should reverted when
  [CALCITE-5446] Support TIMESTAMP WITH LOCAL TIME ZONE type in JDBC driver
is fixed. And I manually added the offset to some TIMESTAMP
WITH LOCAL TIME ZONE LITERALS (which were written as TIMESTAMP
because big-query.iq uses BigQuery's type aliasing); these
changes can be reverted when
  [CALCITE-5496] Support time zones when parsing TIMESTAMP WITH LOCAL TIME ZONE
is fixed.

Close apache#3023
  • Loading branch information
julianhyde committed Feb 10, 2023
1 parent 3f82118 commit 0b3f859
Show file tree
Hide file tree
Showing 21 changed files with 828 additions and 126 deletions.
3 changes: 1 addition & 2 deletions babel/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,8 @@ data: {

# List of methods for parsing builtin function calls.
# Return type of method implementation should be "SqlNode".
# Example: "DateFunctionCall()".
# Example: "DateaddFunctionCall()".
builtinFunctionCallMethods: [
"DateFunctionCall()"
"DateaddFunctionCall()"
]

Expand Down
20 changes: 0 additions & 20 deletions babel/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@ JoinType LeftSemiJoin() :
<LEFT> <SEMI> <JOIN> { return JoinType.LEFT_SEMI_JOIN; }
}

SqlNode DateFunctionCall() :
{
final SqlFunctionCategory funcType = SqlFunctionCategory.USER_DEFINED_FUNCTION;
final SqlIdentifier qualifiedName;
final Span s;
final SqlLiteral quantifier;
final List<? extends SqlNode> args;
}
{
<DATE> {
s = span();
qualifiedName = new SqlIdentifier(unquotedIdentifier(), getPos());
}
args = FunctionParameterList(ExprContext.ACCEPT_SUB_QUERY) {
quantifier = (SqlLiteral) args.get(0);
args.remove(0);
return createCall(qualifiedName, s.end(this), funcType, quantifier, args);
}
}

SqlNode DateaddFunctionCall() :
{
final Span s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.parser.babel.SqlBabelParserImpl;
import org.apache.calcite.sql.pretty.SqlPrettyWriter;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.tools.Planner;
Expand Down Expand Up @@ -112,6 +113,13 @@ public static void main(String[] args) throws Exception {
.with(CalciteConnectionProperty.CONFORMANCE,
SqlConformanceEnum.BABEL)
.with(CalciteConnectionProperty.LENIENT_OPERATOR_LOOKUP, true)
.with(
ConnectionFactories.addType("DATETIME", typeFactory ->
typeFactory.createSqlType(SqlTypeName.TIMESTAMP)))
.with(
ConnectionFactories.addType("TIMESTAMP", typeFactory ->
typeFactory.createSqlType(
SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE)))
.connect();
case "scott-postgresql":
return CalciteAssert.that()
Expand Down

0 comments on commit 0b3f859

Please sign in to comment.