amaliujia commented on a change in pull request #1588: [CALCITE-3381] Unparse
to correct BigQuery interval syntax: "INTERVAL…
URL: https://github.com/apache/calcite/pull/1588#discussion_r350482904
##########
File path:
core/src/main/java/org/apache/calcite/sql/dialect/BigQuerySqlDialect.java
##########
@@ -154,6 +158,55 @@ public BigQuerySqlDialect(SqlDialect.Context context) {
}
}
+ /** BigQuery interval syntax: INTERVAL int64 time_unit. */
+ @Override public void unparseSqlIntervalLiteral(
+ SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int
rightPrec) {
+ SqlIntervalLiteral.IntervalValue interval =
+ (SqlIntervalLiteral.IntervalValue) literal.getValue();
+ writer.keyword("INTERVAL");
+ if (interval.getSign() == -1) {
+ writer.print("-");
+ }
+ Long intervalValueInLong;
+ try {
+ intervalValueInLong = Long.parseLong(literal.getValue().toString());
+ } catch (NumberFormatException e) {
+ throw new RuntimeException("Only INT64 is supported as the interval
value for BigQuery.");
+ }
+ writer.literal(intervalValueInLong.toString());
+ unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
+ RelDataTypeSystem.DEFAULT);
+ }
+
+ @Override public void unparseSqlIntervalQualifier(
+ SqlWriter writer, SqlIntervalQualifier qualifier, RelDataTypeSystem
typeSystem) {
+ final String start = validate(qualifier.timeUnitRange.startUnit).name();
+ if (qualifier.timeUnitRange.endUnit == null) {
+ writer.keyword(start);
+ } else {
+ throw new RuntimeException("Range time unit is not supported for
BigQuery.");
+ }
+ }
+
+ private TimeUnit validate(TimeUnit timeUnit) {
+ switch (timeUnit) {
+ case MICROSECOND:
+ case MILLISECOND:
+ case SECOND:
+ case MINUTE:
+ case HOUR:
+ case DAY:
+ case WEEK:
+ case MONTH:
+ case QUARTER:
+ case YEAR:
+ case ISOYEAR:
+ return timeUnit;
+ default:
+ throw new RuntimeException("Time unit " + timeUnit + " is not supported
for BigQuery.");
Review comment:
Tried to have a test to this exception, but it seems like those
not-supported time unit cannot fit into `INTERVAL val timeunit` syntax. For
example, Parser will reject `INTERVAL '10' NANOSECOND`.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services