flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] asfgit closed pull request #6188: [FLINK-6846] [table] Add timestampAdd tableApi
Date Fri, 03 Aug 2018 09:44:16 GMT
asfgit closed pull request #6188: [FLINK-6846] [table] Add timestampAdd tableApi
URL: https://github.com/apache/flink/pull/6188
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/flink-libraries/flink-table/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java
b/flink-libraries/flink-table/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java
index fe09d181430..a5e3fbaf90b 100644
--- a/flink-libraries/flink-table/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java
+++ b/flink-libraries/flink-table/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java
@@ -90,6 +90,11 @@ private DateTimeUtils() {}
 	 */
 	public static final long MILLIS_PER_DAY = 86400000; // = 24 * 60 * 60 * 1000
 
+	/**
+	 * The number of milliseconds in a week.
+	 */
+	public static final long MILLIS_PER_WEEK = 604800000L; // = 7 * 24 * 60 * 60 * 1000
+
 	/**
 	 * Calendar set to the epoch (1970-01-01 00:00:00 UTC). Useful for
 	 * initializing other values. Calendars are not immutable, so be careful not
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala
index 91d72ce50ab..129337cabfd 100644
--- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala
@@ -614,6 +614,20 @@ trait ImplicitExpressionOperations {
     */
   def years = year
 
+  /**
+  * Creates an interval of the given number of quarters.
+  *
+  * @return interval of months
+  */
+  def quarter = toMonthInterval(expr, 3)
+
+  /**
+  * Creates an interval of the given number of quarters.
+  *
+  * @return interval of months
+  */
+  def quarters = quarter
+
   /**
     * Creates an interval of the given number of months.
     *
@@ -628,6 +642,20 @@ trait ImplicitExpressionOperations {
     */
   def months = month
 
+  /**
+    * Creates an interval of the given number of weeks.
+    *
+    * @return interval of milliseconds
+    */
+  def week = toMilliInterval(expr, MILLIS_PER_WEEK)
+
+  /**
+    * Creates an interval of the given number of weeks.
+    *
+    * @return interval of milliseconds
+    */
+  def weeks = week
+
   /**
     * Creates an interval of the given number of days.
     *
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/ExpressionParser.scala
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/ExpressionParser.scala
index faf6268e447..e3e657ae27e 100644
--- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/ExpressionParser.scala
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/ExpressionParser.scala
@@ -17,7 +17,7 @@
  */
 package org.apache.flink.table.expressions
 
-import org.apache.calcite.avatica.util.DateTimeUtils.{MILLIS_PER_DAY, MILLIS_PER_HOUR, MILLIS_PER_MINUTE,
MILLIS_PER_SECOND}
+import org.apache.calcite.avatica.util.DateTimeUtils.{MILLIS_PER_WEEK,MILLIS_PER_DAY, MILLIS_PER_HOUR,
MILLIS_PER_MINUTE, MILLIS_PER_SECOND}
 import org.apache.flink.api.common.typeinfo.{SqlTimeTypeInfo, TypeInformation}
 import org.apache.flink.table.api._
 import org.apache.flink.table.expressions.ExpressionUtils.{toMilliInterval, toMonthInterval}
@@ -60,8 +60,12 @@ object ExpressionParser extends JavaTokenParsers with PackratParsers {
   lazy val LOG: Keyword = Keyword("log")
   lazy val YEARS: Keyword = Keyword("years")
   lazy val YEAR: Keyword = Keyword("year")
+  lazy val QUARTERS: Keyword = Keyword("quarters")
+  lazy val QUARTER: Keyword = Keyword("quarter")
   lazy val MONTHS: Keyword = Keyword("months")
   lazy val MONTH: Keyword = Keyword("month")
+  lazy val WEEKS: Keyword = Keyword("weeks")
+  lazy val WEEK: Keyword = Keyword("week")
   lazy val DAYS: Keyword = Keyword("days")
   lazy val DAY: Keyword = Keyword("day")
   lazy val HOURS: Keyword = Keyword("hours")
@@ -273,13 +277,17 @@ object ExpressionParser extends JavaTokenParsers with PackratParsers
{
     composite <~ "." ~ TO_TIME ~ opt("()") ^^ { e => Cast(e, SqlTimeTypeInfo.TIME)
}
 
   lazy val suffixTimeInterval : PackratParser[Expression] =
-    composite ~ "." ~ (YEARS | MONTHS | DAYS | HOURS | MINUTES | SECONDS | MILLIS |
-      YEAR | MONTH | DAY | HOUR | MINUTE | SECOND | MILLI) ^^ {
+    composite ~ "." ~ (YEARS | QUARTERS | MONTHS | WEEKS | DAYS |  HOURS | MINUTES |
+      SECONDS | MILLIS | YEAR | QUARTER | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND | MILLI)
^^ {
 
     case expr ~ _ ~ (YEARS.key | YEAR.key) => toMonthInterval(expr, 12)
 
+    case expr ~ _ ~ (QUARTERS.key | QUARTER.key) => toMonthInterval(expr, 3)
+
     case expr ~ _ ~ (MONTHS.key | MONTH.key) => toMonthInterval(expr, 1)
 
+    case expr ~ _ ~ (WEEKS.key | WEEKS.key) => toMilliInterval(expr, MILLIS_PER_WEEK)
+
     case expr ~ _ ~ (DAYS.key | DAY.key) => toMilliInterval(expr, MILLIS_PER_DAY)
 
     case expr ~ _ ~ (HOURS.key | HOUR.key) => toMilliInterval(expr, MILLIS_PER_HOUR)
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/symbols.scala
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/symbols.scala
index ec127e25d20..02399bed4de 100644
--- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/symbols.scala
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/symbols.scala
@@ -86,6 +86,7 @@ object TimeIntervalUnit extends TableSymbols {
   val YEAR_TO_MONTH = Value(TimeUnitRange.YEAR_TO_MONTH)
   val MONTH = Value(TimeUnitRange.MONTH)
   val QUARTER = Value(TimeUnitRange.QUARTER)
+  val WEEK = Value(TimeUnitRange.WEEK)
   val DAY = Value(TimeUnitRange.DAY)
   val DAY_TO_HOUR = Value(TimeUnitRange.DAY_TO_HOUR)
   val DAY_TO_MINUTE = Value(TimeUnitRange.DAY_TO_MINUTE)
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/time.scala
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/time.scala
index 5dff7742c07..aff3d6785ca 100644
--- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/time.scala
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/time.scala
@@ -49,6 +49,7 @@ case class Extract(timeIntervalUnit: Expression, temporal: Expression) extends
E
       case SymbolExpression(TimeIntervalUnit.YEAR)
            | SymbolExpression(TimeIntervalUnit.MONTH)
            | SymbolExpression(TimeIntervalUnit.QUARTER)
+           | SymbolExpression(TimeIntervalUnit.WEEK)
            | SymbolExpression(TimeIntervalUnit.DAY)
         if temporal.resultType == SqlTimeTypeInfo.DATE
           || temporal.resultType == SqlTimeTypeInfo.TIMESTAMP
diff --git a/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala
b/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala
index 453de19a23c..21f429b934f 100644
--- a/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala
+++ b/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala
@@ -1442,12 +1442,24 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
       "EXTRACT(YEAR FROM f16)",
       "1996")
 
+    testAllApis(
+      'f16.extract(TimeIntervalUnit.QUARTER),
+      "f16.extract(QUARTER)",
+      "EXTRACT(QUARTER FROM f16)",
+      "4")
+
     testAllApis(
       'f16.extract(TimeIntervalUnit.MONTH),
       "extract(f16, MONTH)",
       "EXTRACT(MONTH FROM f16)",
       "11")
 
+    testAllApis(
+      'f16.extract(TimeIntervalUnit.WEEK),
+      "extract(f16, WEEK)",
+      "EXTRACT(WEEK FROM f16)",
+      "45")
+
     testAllApis(
       'f16.extract(TimeIntervalUnit.DAY),
       "f16.extract(DAY)",
@@ -1460,12 +1472,24 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
       "EXTRACT(YEAR FROM f18)",
       "1996")
 
+    testAllApis(
+      'f18.extract(TimeIntervalUnit.QUARTER),
+      "f18.extract(QUARTER)",
+      "EXTRACT(QUARTER FROM f18)",
+      "4")
+
     testAllApis(
       'f18.extract(TimeIntervalUnit.MONTH),
       "f18.extract(MONTH)",
       "EXTRACT(MONTH FROM f18)",
       "11")
 
+    testAllApis(
+      'f18.extract(TimeIntervalUnit.WEEK),
+      "f18.extract(WEEK)",
+      "EXTRACT(WEEK FROM f18)",
+      "45")
+
     testAllApis(
       'f18.extract(TimeIntervalUnit.DAY),
       "f18.extract(DAY)",
@@ -1538,6 +1562,12 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
       "EXTRACT(MONTH FROM f20)",
       "1")
 
+    testAllApis(
+      'f20.extract(TimeIntervalUnit.QUARTER),
+      "f20.extract(QUARTER)",
+      "EXTRACT(QUARTER FROM f20)",
+      "1")
+
     testAllApis(
       'f20.extract(TimeIntervalUnit.YEAR),
       "f20.extract(YEAR)",
@@ -1894,32 +1924,47 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
   @Test
   def testQuarter(): Unit = {
     testAllApis(
-      "1997-01-27".toDate.quarter(),
-      "'1997-01-27'.toDate.quarter()",
+      "1997-01-27".toDate.extract(TimeIntervalUnit.QUARTER),
+      "'1997-01-27'.toDate.extract(QUARTER)",
       "QUARTER(DATE '1997-01-27')",
       "1")
 
     testAllApis(
-      "1997-04-27".toDate.quarter(),
-      "'1997-04-27'.toDate.quarter()",
+      "1997-04-27".toDate.extract(TimeIntervalUnit.QUARTER),
+      "'1997-04-27'.toDate.extract(QUARTER)",
       "QUARTER(DATE '1997-04-27')",
       "2")
 
     testAllApis(
-      "1997-12-31".toDate.quarter(),
-      "'1997-12-31'.toDate.quarter()",
+      "1997-12-31".toDate.extract(TimeIntervalUnit.QUARTER),
+      "'1997-12-31'.toDate.extract(QUARTER)",
       "QUARTER(DATE '1997-12-31')",
       "4")
   }
 
   @Test
   def testTimestampAdd(): Unit = {
-    val data = Seq(
-      (1, "TIMESTAMP '2017-11-29 22:58:58.998'"),
-      (3, "TIMESTAMP '2017-11-29 22:58:58.998'"),
-      (-1, "TIMESTAMP '2017-11-29 22:58:58.998'"),
-      (-61, "TIMESTAMP '2017-11-29 22:58:58.998'"),
-      (-1000, "TIMESTAMP '2017-11-29 22:58:58.998'")
+    val data = Seq((
+        1,
+        "TIMESTAMP '2017-11-29 22:58:58.998'",
+        "2017-11-29 22:58:58.998".toTimestamp,
+        "'2017-11-29 22:58:58.998'.toTimestamp"), (
+        3,
+        "TIMESTAMP '2017-11-29 22:58:58.998'",
+        "2017-11-29 22:58:58.998".toTimestamp,
+        "'2017-11-29 22:58:58.998'.toTimestamp"), (
+        -1,
+        "TIMESTAMP '2017-11-29 22:58:58.998'",
+        "2017-11-29 22:58:58.998".toTimestamp,
+        "'2017-11-29 22:58:58.998'.toTimestamp"), (
+        -61,
+        "TIMESTAMP '2017-11-29 22:58:58.998'",
+        "2017-11-29 22:58:58.998".toTimestamp,
+        "'2017-11-29 22:58:58.998'.toTimestamp"),(
+        -1000,
+        "TIMESTAMP '2017-11-29 22:58:58.998'",
+        "2017-11-29 22:58:58.998".toTimestamp,
+        "'2017-11-29 22:58:58.998'.toTimestamp")
     )
 
     val YEAR = Seq(
@@ -1998,27 +2043,53 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
       "SQL_TSI_SECOND" -> SECOND
     )
 
-    for ((interval, result) <- intervalMapResults) {
-      testSqlApi(
-        s"TIMESTAMPADD($interval, ${data.head._1}, ${data.head._2})", result.head)
-      testSqlApi(
-        s"TIMESTAMPADD($interval, ${data(1)._1}, ${data(1)._2})", result(1))
-      testSqlApi(
-        s"TIMESTAMPADD($interval, ${data(2)._1}, ${data(2)._2})", result(2))
-      testSqlApi(
-        s"TIMESTAMPADD($interval, ${data(3)._1}, ${data(3)._2})", result(3))
-      testSqlApi(
-        s"TIMESTAMPADD($interval, ${data(4)._1}, ${data(4)._2})", result(4))
+    def intervalCount(interval: String, count: Int): (Expression, String) = interval match
{
+      case "YEAR" => (count.years, s"${count}.years")
+      case "SQL_TSI_YEAR" => (count.years, s"${count}.years")
+      case "MONTH" => (count.months, s"${count}.months")
+      case "SQL_TSI_MONTH" => (count.months, s"${count}.months")
+      case "WEEK" => (count.weeks, s"${count}.weeks")
+      case "SQL_TSI_WEEK" => (count.weeks, s"${count}.weeks")
+      case "DAY" => (count.days, s"${count}.days")
+      case "SQL_TSI_DAY" => (count.days, s"${count}.days")
+      case "HOUR" => (count.hours, s"${count}.hours")
+      case "SQL_TSI_HOUR" => (count.hours, s"${count}.hours")
+      case "MINUTE" => (count.minutes, s"${count}.minutes")
+      case "SQL_TSI_MINUTE" => (count.minutes, s"${count}.minutes")
+      case "SECOND" => (count.seconds, s"${count}.seconds")
+      case "SQL_TSI_SECOND" => (count.seconds, s"${count}.seconds")
     }
 
-    testSqlApi("TIMESTAMPADD(HOUR, CAST(NULL AS INTEGER), TIMESTAMP '2016-02-24 12:42:25')",
"null")
+    for ((interval, result) <- intervalMapResults) {
+      if (!interval.contains("QUARTER")) {
+        for (i <- 0 to 4) {
+          val timeInterval = intervalCount(interval, data(i)._1)
+          testAllApis(
+            data(i)._3 + timeInterval._1,
+            s"${timeInterval._2} + ${data(i)._4}",
+            s"TIMESTAMPADD($interval, ${data(i)._1}, ${data(i)._2})",
+            result(i))
+        }
+      }
+    }
 
-    testSqlApi("TIMESTAMPADD(HOUR, -200, CAST(NULL AS TIMESTAMP))", "null")
+    testSqlApi(
+      "TIMESTAMPADD(HOUR, CAST(NULL AS INTEGER), TIMESTAMP '2016-02-24 12:42:25')",
+      "null")
 
-    testSqlApi("TIMESTAMPADD(DAY, 1, DATE '2016-06-15')", "2016-06-16")
+    testSqlApi(
+      "TIMESTAMPADD(HOUR, -200, CAST(NULL AS TIMESTAMP))",
+      "null")
 
-    testSqlApi("TIMESTAMPADD(MONTH, 3, CAST(NULL AS TIMESTAMP))", "null")
+    testAllApis(
+      "2016-06-15".toDate + 1.day,
+      "'2016-06-15'.toDate + 1.day",
+      "TIMESTAMPADD(DAY, 1, DATE '2016-06-15')",
+      "2016-06-16")
 
+    testSqlApi(
+      "TIMESTAMPADD(MONTH, 3, CAST(NULL AS TIMESTAMP))",
+      "null")
   }
 
   // ----------------------------------------------------------------------------------------------


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message