This is an automated email from the ASF dual-hosted git repository.

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new c1435da  [CALCITE-4279] Allow BigQuery to parse and validate Niladic 
functions (Mr. Swett)
c1435da is described below

commit c1435da3b24274bb4d0a53a1b7c35a5ce166a5c1
Author: Justin Swett <[email protected]>
AuthorDate: Thu Oct 8 07:42:01 2020 -0700

    [CALCITE-4279] Allow BigQuery to parse and validate Niladic functions (Mr. 
Swett)
    
    * Add CURRENT_DATETIME to SqlStdOperatorTable
    * Removed current_datetime from reserved words
    
    close apache/calcite#2203
---
 .../calcite/sql/fun/SqlLibraryOperators.java       |  8 ++++++
 .../calcite/sql/validate/SqlConformanceEnum.java   |  1 +
 .../calcite/sql/test/SqlOperatorBaseTest.java      |  3 +++
 .../org/apache/calcite/test/SqlValidatorTest.java  | 31 ++++++++++++++++++++++
 site/_docs/reference.md                            |  2 ++
 5 files changed, 45 insertions(+)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index d578496..57d080e 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -212,6 +212,14 @@ public abstract class SqlLibraryOperators {
           ReturnTypes.DATE_NULLABLE, null, OperandTypes.STRING,
           SqlFunctionCategory.TIMEDATE);
 
+  /** The "CURRENT_DATETIME([timezone])" function. */
+  @LibraryOperator(libraries = {BIG_QUERY})
+  public static final SqlFunction CURRENT_DATETIME =
+      new SqlFunction("CURRENT_DATETIME", SqlKind.OTHER_FUNCTION,
+          ReturnTypes.TIMESTAMP.andThen(SqlTypeTransforms.TO_NULLABLE), null,
+          OperandTypes.or(OperandTypes.NILADIC, OperandTypes.STRING),
+          SqlFunctionCategory.TIMEDATE);
+
   /** The "DATE_FROM_UNIX_DATE(integer)" function; returns a DATE value
    * a given number of seconds after 1970-01-01. */
   @LibraryOperator(libraries = {BIG_QUERY})
diff --git 
a/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java 
b/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java
index 0bd37b9..30a41f1 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java
@@ -277,6 +277,7 @@ public enum SqlConformanceEnum implements SqlConformance {
     case BABEL:
     case LENIENT:
     case MYSQL_5:
+    case BIG_QUERY:
       return true;
     default:
       return false;
diff --git 
a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java 
b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
index c4f5612..ebf6e5c 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
@@ -5642,6 +5642,9 @@ public abstract class SqlOperatorBaseTest {
     // parser. In the regular parser, DATE is a reserved keyword.
     tester.checkNull("\"DATE\"(null)");
     tester.checkScalar("\"DATE\"('1985-12-06')", "1985-12-06", "DATE NOT 
NULL");
+    tester.checkType("CURRENT_DATETIME()", "TIMESTAMP(0) NOT NULL");
+    tester.checkType("CURRENT_DATETIME('America/Los_Angeles')", "TIMESTAMP(0) 
NOT NULL");
+    tester.checkType("CURRENT_DATETIME(CAST(NULL AS VARCHAR(20)))", 
"TIMESTAMP(0)");
   }
 
   @Test void testAbsFunc() {
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 8416c6c..70c78f4 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -56,6 +56,7 @@ import org.apache.calcite.sql.validate.SqlValidatorScope;
 import org.apache.calcite.sql.validate.SqlValidatorUtil;
 import org.apache.calcite.test.catalog.CountingFactory;
 import org.apache.calcite.testlib.annotations.LocaleEnUs;
+import org.apache.calcite.tools.ValidationException;
 import org.apache.calcite.util.Bug;
 import org.apache.calcite.util.ImmutableBitSet;
 
@@ -361,6 +362,12 @@ public class SqlValidatorTest extends SqlValidatorTestCase 
{
     sql("SELECT +'abc' from (values(true))").ok();
   }
 
+  @Test void testNiladicForBigQuery() {
+    sql("select current_time, current_time(), current_date, "
+        + "current_date(), current_timestamp, current_timestamp()")
+        .withConformance(SqlConformanceEnum.BIG_QUERY).ok();
+  }
+
   @Test void testEqualNotEqual() {
     expr("''=''").ok();
     expr("'abc'=n''").ok();
@@ -1492,6 +1499,30 @@ public class SqlValidatorTest extends 
SqlValidatorTestCase {
             + "Was expecting 2 arguments");
   }
 
+  @Test void testCurrentDatetime() throws SqlParseException, 
ValidationException {
+    final String currentDateTimeExpr = "select ^current_datetime^";
+    Sql shouldFail = sql(currentDateTimeExpr)
+        .withConformance(SqlConformanceEnum.BIG_QUERY);
+    final String expectedError = "query [select CURRENT_DATETIME]; exception "
+        + "[Column 'CURRENT_DATETIME' not found in any table]; class "
+        + "[class org.apache.calcite.sql.validate.SqlValidatorException]; pos 
[line 1 col 8 thru line 1 col 8]";
+    shouldFail.fails("Column 'CURRENT_DATETIME' not found in any table");
+
+    final SqlOperatorTable bigqueryTable =
+        SqlLibraryOperatorTableFactory.INSTANCE.getOperatorTable(
+            SqlLibrary.STANDARD,
+            SqlLibrary.BIG_QUERY);
+    sql("select current_datetime()")
+        .withConformance(SqlConformanceEnum.BIG_QUERY)
+        .withOperatorTable(bigqueryTable).ok();
+    sql("select CURRENT_DATETIME('America/Los_Angeles')")
+        .withConformance(SqlConformanceEnum.BIG_QUERY)
+        .withOperatorTable(bigqueryTable).ok();
+    sql("select CURRENT_DATETIME(CAST(NULL AS VARCHAR(20)))")
+        .withConformance(SqlConformanceEnum.BIG_QUERY)
+        .withOperatorTable(bigqueryTable).ok();
+  }
+
   @Test void testInvalidFunction() {
     wholeExpr("foo()")
         .fails("No match found for function signature FOO..");
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index 92a170d..1bea20a 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -1314,6 +1314,7 @@ Not implemented:
 | LOCALTIMESTAMP(precision) | Returns the current date and time in the session 
time zone in a value of datatype TIMESTAMP, with *precision* digits of precision
 | CURRENT_TIME              | Returns the current time in the session time 
zone, in a value of datatype TIMESTAMP WITH TIME ZONE
 | CURRENT_DATE              | Returns the current date in the session time 
zone, in a value of datatype DATE
+| CURRENT_DATE              | Returns the current date in the session time 
zone, in a value of datatype DATE
 | CURRENT_TIMESTAMP         | Returns the current date and time in the session 
time zone, in a value of datatype TIMESTAMP WITH TIME ZONE
 | EXTRACT(timeUnit FROM datetime) | Extracts and returns the value of a 
specified datetime field from a datetime value expression
 | FLOOR(datetime TO timeUnit) | Rounds *datetime* down to *timeUnit*
@@ -2433,6 +2434,7 @@ semantics.
 | m p | CONCAT(string [, string ]*)                  | Concatenates two or 
more strings
 | m | COMPRESS(string)                               | Compresses a string 
using zlib compression and returns the result as a binary string.
 | p | CONVERT_TIMEZONE(tz1, tz2, datetime)           | Converts the timezone 
of *datetime* from *tz1* to *tz2*
+| b | CURRENT_DATETIME([timezone])                   | Returns the currenttime 
as a DATETIME from *timzezone*
 | 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_FROM_UNIX_DATE(integer)                   | Returns the DATE that 
is *integer* days after 1970-01-01

Reply via email to