Skip to content

Commit

Permalink
[CALCITE-6021] Add CURRENT_DATETIME function (enabled in BigQuery lib…
Browse files Browse the repository at this point in the history
…rary)
  • Loading branch information
tanclary committed Oct 10, 2023
1 parent a67d089 commit a5f3926
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
56 changes: 34 additions & 22 deletions babel/src/test/resources/sql/big-query.iq
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,25 @@ SELECT current_date() AS the_date, t.current_date FROM t;
#
# Returns DATETIME

!if (false) {
SELECT CURRENT_DATETIME() as now;
+----------------------------+
| now |
+----------------------------+
| 2016-05-19T10:38:47.046465 |
+----------------------------+
SELECT CURRENT_DATETIME() > DATETIME '2008-12-25 15:30:00' as now;
+------+
| now |
+------+
| true |
+------+
(1 row)

!ok

SELECT CURRENT_DATETIME as now;
+----------------------------+
| now |
+----------------------------+
| 2016-05-19T10:38:47.046465 |
+----------------------------+
SELECT CURRENT_DATETIME > DATETIME '2008-12-25 15:30:00' as now;
+------+
| now |
+------+
| true |
+------+
(1 row)

!ok
!}

# When a column named current_datetime is present, the column name and
# the function call without parentheses are ambiguous. To ensure the
Expand All @@ -173,16 +175,26 @@ SELECT CURRENT_DATETIME as now;
# select the function in the now column and the table column in the
# current_datetime column.

!if (false) {
WITH t AS (SELECT 'column value' AS `current_datetime`)
SELECT current_datetime() as now, t.current_datetime FROM t;
+----------------------------+------------------+
| now | current_datetime |
+----------------------------+------------------+
| 2016-05-19T10:38:47.046465 | column value |
+----------------------------+------------------+
SELECT current_datetime() > DATETIME '2008-12-25 15:30:00' as now, t.current_datetime FROM t;
+------+------------------+
| now | current_datetime |
+------+------------------+
| true | column value |
+------+------------------+
(1 row)

!ok

SELECT CURRENT_DATETIME('UTC') > DATETIME '2008-12-25 15:30:00';
+--------+
| EXPR$0 |
+--------+
| true |
+--------+
(1 row)

!ok
!}

#####################################################################
# CURRENT_TIME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.COTH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CSC;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CSCH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CURRENT_DATETIME;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.DATE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.DATEADD;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.DATETIME;
Expand Down Expand Up @@ -996,6 +997,7 @@ Builder populate3() {
map.put(CURRENT_TIME, systemFunctionImplementor);
map.put(CURRENT_TIMESTAMP, systemFunctionImplementor);
map.put(CURRENT_DATE, systemFunctionImplementor);
map.put(CURRENT_DATETIME, systemFunctionImplementor);
map.put(LOCALTIME, systemFunctionImplementor);
map.put(LOCALTIMESTAMP, systemFunctionImplementor);

Expand Down Expand Up @@ -3476,6 +3478,12 @@ private static class SystemFunctionImplementor
return Expressions.call(BuiltInMethod.CURRENT_TIME.method, root);
} else if (op == CURRENT_DATE) {
return Expressions.call(BuiltInMethod.CURRENT_DATE.method, root);
} else if (op == CURRENT_DATETIME) {
if (call.getOperands().size() == 0) {
return Expressions.call(BuiltInMethod.CURRENT_DATETIME.method, root);
} else {
return Expressions.call(BuiltInMethod.CURRENT_DATETIME2.method, argValueList.get(0), root);
}
} else if (op == LOCALTIMESTAMP) {
return Expressions.call(BuiltInMethod.LOCAL_TIMESTAMP.method, root);
} else if (op == LOCALTIME) {
Expand Down
17 changes: 16 additions & 1 deletion core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4653,7 +4653,22 @@ public static int currentDate(DataContext root) {
return date;
}

/** SQL {@code LOCAL_TIMESTAMP} function. */
/** SQL {@code CURRENT_DATETIME} function. */
@NonDeterministic
public static Long currentDatetime(DataContext root) {
final long timestamp = DataContext.Variable.CURRENT_TIMESTAMP.get(root);
return datetime(timestamp);
}

/** SQL {@code CURRENT_DATETIME} function with a specified timezone. */
@NonDeterministic
public static @Nullable Long currentDatetime(@Nullable String timezone, DataContext root) {
if (timezone == null) { return null; }
final long timestamp = DataContext.Variable.UTC_TIMESTAMP.get(root);
return datetime(timestamp, timezone);
}

/** SQL {@code LOCAL_TIMESTAMP} function. */
@NonDeterministic
public static long localTimestamp(DataContext root) {
return DataContext.Variable.LOCAL_TIMESTAMP.get(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
@LibraryOperator(libraries = {BIG_QUERY})
public static final SqlFunction CURRENT_DATETIME =
SqlBasicFunction.create("CURRENT_DATETIME",
ReturnTypes.TIMESTAMP.andThen(SqlTypeTransforms.TO_NULLABLE),
ReturnTypes.TIMESTAMP_NULLABLE,
OperandTypes.NILADIC.or(OperandTypes.STRING),
SqlFunctionCategory.TIMEDATE)
.withSyntax(SqlSyntax.FUNCTION_ID);
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ public enum BuiltInMethod {
CURRENT_TIMESTAMP(SqlFunctions.class, "currentTimestamp", DataContext.class),
CURRENT_TIME(SqlFunctions.class, "currentTime", DataContext.class),
CURRENT_DATE(SqlFunctions.class, "currentDate", DataContext.class),
CURRENT_DATETIME(SqlFunctions.class, "currentDatetime", DataContext.class),
CURRENT_DATETIME2(SqlFunctions.class, "currentDatetime", String.class,
DataContext.class),

LOCAL_TIMESTAMP(SqlFunctions.class, "localTimestamp", DataContext.class),
LOCAL_TIME(SqlFunctions.class, "localTime", DataContext.class),
TIME_ZONE(SqlFunctions.class, "timeZone", DataContext.class),
Expand Down

0 comments on commit a5f3926

Please sign in to comment.