julianhyde commented on code in PR #2973:
URL: https://github.com/apache/calcite/pull/2973#discussion_r1026775499


##########
site/_docs/reference.md:
##########
@@ -2596,7 +2596,16 @@ semantics.
 | p | CONVERT_TIMEZONE(tz1, tz2, datetime)           | Converts the timezone 
of *datetime* from *tz1* to *tz2*
 | b | CURRENT_DATETIME([timezone])                   | Returns the current 
time as a TIMESTAMP from *timezone*
 | m | DAYNAME(datetime)                              | Returns the name, in 
the connection's locale, of the weekday in *datetime*; for example, it returns 
'星期日' for both DATE '2020-02-10' and TIMESTAMP '2020-02-10 10:10:10'
-| b | DATE(string)                                   | Equivalent to 
`CAST(string AS DATE)`
+| b | DATE(integer, integer, integer)                | Returns a date object 
given year, month, and day
+| b | DATE(timestamp)                                | Extracts the UTC date 
from a timestamp
+| b | DATE(timestamp, string)                        | Extracts the date from 
a timestamp, in a given time zone
+| b | TIMESTAMP(string)                              | Equivalent to 
`CAST(string AS TIMESTAMP)`
+| b | TIMESTAMP(string, string)                      | Equivalent to 
`CAST(string AS TIMESTAMP)`, converted to a given time zone
+| b | TIMESTAMP(date)                                | Convert a UTC date to a 
timestamp (at midnight)
+| b | TIMESTAMP(date, string)                        | Convert a date to a 
timestamp (at midnight), in a given time zone
+| b | TIME(integer, integer, integer)                | Returns a time object 
given hour, minute, and second
+| b | TIME(timestamp)                                | Extracts the UTC time 
from a timestamp

Review Comment:
   A Calcite TIMESTAMP value does not have a UTC time. Even though you're 
implementing for BQ, you must use Calcite terminology here.



##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -149,6 +159,40 @@ public class SqlFunctions {
 
   private static final Pattern PATTERN_0_STAR_E = Pattern.compile("0*E");
 
+  // Date formatter for BigQuery's timestamp literals:
+  // 
https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#timestamp_literals
+  private static final DateTimeFormatter BIG_QUERY_TIMESTAMP_LITERAL_FORMATTER 
=
+      new DateTimeFormatterBuilder()
+          // Unlike ISO 8601, BQ only supports years between 1 - 9999,
+          // but can support single-digit month and day parts.
+          .appendValue(ChronoField.YEAR, 4)
+          .appendLiteral('-')
+          .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, SignStyle.NOT_NEGATIVE)
+          .appendLiteral('-')
+          .appendValue(ChronoField.DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
+          // Everything after the date is optional. Optional sections can be 
nested.
+          .optionalStart()
+          // BQ accepts either a literal 'T' or a space to separate the date 
from the time,
+          // so make the 'T' optional but pad with 1 space if it's omitted.
+          .padNext(1, ' ')
+          .optionalStart()
+          .appendLiteral('T')
+          .optionalEnd()
+          // Unlike ISO 8601, BQ can support single-digit hour, minute, and 
second parts.
+          .appendValue(ChronoField.HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE)
+          .appendLiteral(':')
+          .appendValue(ChronoField.MINUTE_OF_HOUR, 1, 2, 
SignStyle.NOT_NEGATIVE)
+          .appendLiteral(':')
+          .appendValue(ChronoField.SECOND_OF_MINUTE, 1, 2, 
SignStyle.NOT_NEGATIVE)
+          // ISO 8601 supports up to nanosecond precision, but BQ only up to 
microsecond.
+          .optionalStart()
+          .appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true)
+          .optionalEnd()
+          .optionalStart()
+          .parseLenient()
+          .appendOffsetId()
+          .toFormatter(Locale.ROOT);
+

Review Comment:
   is this the code that Will said was non-JDK8 compliant? 



##########
site/_docs/reference.md:
##########
@@ -2596,7 +2596,16 @@ semantics.
 | p | CONVERT_TIMEZONE(tz1, tz2, datetime)           | Converts the timezone 
of *datetime* from *tz1* to *tz2*
 | b | CURRENT_DATETIME([timezone])                   | Returns the current 
time as a TIMESTAMP from *timezone*
 | m | DAYNAME(datetime)                              | Returns the name, in 
the connection's locale, of the weekday in *datetime*; for example, it returns 
'星期日' for both DATE '2020-02-10' and TIMESTAMP '2020-02-10 10:10:10'
-| b | DATE(string)                                   | Equivalent to 
`CAST(string AS DATE)`
+| b | DATE(integer, integer, integer)                | Returns a date object 
given year, month, and day

Review Comment:
   this list should be alphabetically sorted



##########
core/src/main/java/org/apache/calcite/sql/fun/SqlDateFunction.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql.fun;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.sql.SqlCallBinding;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlOperandCountRange;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlOperatorBinding;
+import org.apache.calcite.sql.type.SqlOperandCountRanges;
+import org.apache.calcite.sql.type.SqlOperandTypeChecker;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+import java.util.Locale;
+
+/**
+ * The Google BigQuery {@code DATE} function returns a date object and can be 
invoked in 3 ways:

Review Comment:
   javadoc is HTML. you need <ul> and <p> tags to achieve formatting



##########
babel/src/main/codegen/includes/parserImpls.ftl:
##########
@@ -42,6 +42,46 @@ SqlNode DateFunctionCall() :
     }
 }
 
+SqlNode TimestampFunctionCall() :
+{
+    final SqlFunctionCategory funcType = 
SqlFunctionCategory.USER_DEFINED_FUNCTION;

Review Comment:
   not sure it should be user-defined function. a library function, maybe



##########
babel/src/test/resources/sql/big-query.iq:
##########
@@ -1051,29 +1051,98 @@ select unix_date(timestamp '2008-12-25') as d;
 
 !ok
 
-# DATE
-# 'date(x) is shorthand for 'cast(x as date)'
-select date('1970-01-01') as d;
+# DATE(year, month, day)
+select date(2022, 11, 15) as d;
 +------------+
 | d          |
 +------------+
-| 1970-01-01 |
+| 2022-11-15 |
 +------------+
 (1 row)
 
 !ok
 
-!if (false) {
-select date(cast(null as varchar(10))) as d;
-+---+
-| D |
-+---+
-|   |
-+---+
+# TIMESTAMP(string)

Review Comment:
   big-query.iq was created by pasting in doc from google's site. please 
continue the tradition.



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@calcite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to