KurtYoung commented on a change in pull request #15136:
URL: https://github.com/apache/flink/pull/15136#discussion_r594809220



##########
File path: 
flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/utils/ExpressionTestBase.scala
##########
@@ -103,11 +109,166 @@ abstract class ExpressionTestBase {
     relBuilder.scan(tableName)
 
     // reset test exprs
-    testExprs.clear()
+    validExprs.clear()
+    invalidSqlExprs.clear()
+    invalidTableApiExprs.clear()
   }
 
   @After
   def evaluateExprs(): Unit = {
+
+    // evaluate valid expressions
+    evaluateGivenExprs(validExprs)
+
+    // evaluate invalid expressions
+    invalidSqlExprs.foreach {
+      case (sqlExpr, keywords, clazz) => {
+        try {
+          val invalidExprs = mutable.ArrayBuffer[(String, RexNode, String)]()
+          addSqlTestExpr(sqlExpr, keywords, invalidExprs, clazz)
+          evaluateGivenExprs(invalidExprs)
+          fail(s"Expected a $clazz, but no exception is thrown.")
+        } catch {
+          case e if e.getClass == clazz =>
+            if (keywords != null) {
+              assertTrue(
+                s"The actual exception message \n${e.getMessage}\n" +
+                  s"doesn't contain expected keyword \n$keywords\n",
+                e.getMessage.contains(keywords))
+            }
+          case e: Throwable =>
+            e.printStackTrace()
+            fail(s"Expected throw ${clazz.getSimpleName}, but is $e.")
+        }
+      }
+    }
+
+    invalidTableApiExprs.foreach {
+      case (tableExpr, keywords, clazz) => {
+        try {
+          val invalidExprs = mutable.ArrayBuffer[(String, RexNode, String)]()
+          addTableApiTestExpr(tableExpr, keywords, invalidExprs, clazz)
+          evaluateGivenExprs(invalidExprs)
+          fail(s"Expected a $clazz, but no exception is thrown.")
+        } catch {
+          case e if e.getClass == clazz =>
+            if (keywords != null) {
+              assertTrue(
+                s"The actual exception message \n${e.getMessage}\n" +
+                  s"doesn't contain expected keyword \n$keywords\n",
+                e.getMessage.contains(keywords))
+            }
+          case e: Throwable =>
+            e.printStackTrace()
+            fail(s"Expected throw ${clazz.getSimpleName}, but is $e.")
+        }
+      }
+    }
+  }
+
+
+  def testAllApis(
+    expr: Expression,
+    sqlExpr: String,
+    expected: String): Unit = {

Review comment:
       and all the following methods

##########
File path: 
flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/utils/ExpressionTestBase.scala
##########
@@ -103,11 +109,166 @@ abstract class ExpressionTestBase {
     relBuilder.scan(tableName)
 
     // reset test exprs
-    testExprs.clear()
+    validExprs.clear()
+    invalidSqlExprs.clear()
+    invalidTableApiExprs.clear()
   }
 
   @After
   def evaluateExprs(): Unit = {
+
+    // evaluate valid expressions
+    evaluateGivenExprs(validExprs)
+
+    // evaluate invalid expressions
+    invalidSqlExprs.foreach {
+      case (sqlExpr, keywords, clazz) => {
+        try {
+          val invalidExprs = mutable.ArrayBuffer[(String, RexNode, String)]()
+          addSqlTestExpr(sqlExpr, keywords, invalidExprs, clazz)
+          evaluateGivenExprs(invalidExprs)
+          fail(s"Expected a $clazz, but no exception is thrown.")
+        } catch {
+          case e if e.getClass == clazz =>
+            if (keywords != null) {
+              assertTrue(
+                s"The actual exception message \n${e.getMessage}\n" +
+                  s"doesn't contain expected keyword \n$keywords\n",
+                e.getMessage.contains(keywords))
+            }
+          case e: Throwable =>
+            e.printStackTrace()
+            fail(s"Expected throw ${clazz.getSimpleName}, but is $e.")
+        }
+      }
+    }
+
+    invalidTableApiExprs.foreach {
+      case (tableExpr, keywords, clazz) => {
+        try {
+          val invalidExprs = mutable.ArrayBuffer[(String, RexNode, String)]()
+          addTableApiTestExpr(tableExpr, keywords, invalidExprs, clazz)
+          evaluateGivenExprs(invalidExprs)
+          fail(s"Expected a $clazz, but no exception is thrown.")
+        } catch {
+          case e if e.getClass == clazz =>
+            if (keywords != null) {
+              assertTrue(
+                s"The actual exception message \n${e.getMessage}\n" +
+                  s"doesn't contain expected keyword \n$keywords\n",
+                e.getMessage.contains(keywords))
+            }
+          case e: Throwable =>
+            e.printStackTrace()
+            fail(s"Expected throw ${clazz.getSimpleName}, but is $e.")
+        }
+      }
+    }
+  }
+
+
+  def testAllApis(
+    expr: Expression,
+    sqlExpr: String,
+    expected: String): Unit = {

Review comment:
       4 space indents here

##########
File path: 
flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/TemporalTypesTest.scala
##########
@@ -1122,6 +1135,214 @@ class TemporalTypesTest extends ExpressionTestBase {
 
   }
 
+  @Test
+  def testToTimestampLtzShanghai(): Unit = {
+    config.setLocalTimeZone(ZoneId.of("Asia/Shanghai"))
+
+    //INT -> TIMESTAMP_LTZ
+    testAllApis(
+      toTimestampLtz(100),
+      "TO_TIMESTAMP_LTZ(100)",
+      "1970-01-01 08:01:40.000")
+
+    //TINYINT -> TIMESTAMP_LTZ
+    testAllApis(
+      toTimestampLtz(100.cast(DataTypes.TINYINT())),
+      "TO_TIMESTAMP_LTZ(CAST(100 AS TINYINT))",
+      "1970-01-01 08:01:40.000")
+
+    //BIGINT -> TIMESTAMP_LTZ
+    testAllApis(
+      toTimestampLtz(100.cast(DataTypes.BIGINT())),
+      "TO_TIMESTAMP_LTZ(CAST(100 AS BIGINT))",
+      "1970-01-01 08:01:40.000")
+
+    //FLOAT -> TIMESTAMP_LTZ
+    testAllApis(
+      toTimestampLtz(100.01.cast(DataTypes.FLOAT())),
+      "TO_TIMESTAMP_LTZ(CAST(100.01 AS FLOAT))",
+      "1970-01-01 08:01:40.010")
+
+    //DOUBLE -> TIMESTAMP_LTZ
+    testAllApis(
+      toTimestampLtz(100.123.cast(DataTypes.DOUBLE())),
+      "TO_TIMESTAMP_LTZ(CAST(100.123 AS DOUBLE))",
+      "1970-01-01 08:01:40.123")
+
+    //DECIMAL -> TIMESTAMP_LTZ
+    testAllApis(
+      toTimestampLtz(100.cast(DataTypes.DECIMAL(38, 18))),
+      "TO_TIMESTAMP_LTZ(100, 0)",
+      "1970-01-01 08:01:40.000")
+    testAllApis(
+      toTimestampLtz(-100.cast(DataTypes.DECIMAL(38, 18))),
+      "TO_TIMESTAMP_LTZ(-100)",
+      "1970-01-01 07:58:20.000")
+
+    //keep scale
+    testAllApis(
+      toTimestampLtz(1234, 3),
+      "TO_TIMESTAMP_LTZ(1234, 3)",
+      "1970-01-01 08:00:01.234")
+    //drop scale
+    testAllApis(
+      toTimestampLtz(0.01, 3),
+      "TO_TIMESTAMP_LTZ(0.01, 3)",
+      "1970-01-01 08:00:00.000")
+  }
+
+  @Test
+  def testToTimestampLtzUTC(): Unit = {
+    config.setLocalTimeZone(ZoneId.of("UTC"))
+    testAllApis(
+      toTimestampLtz(100),
+      "TO_TIMESTAMP_LTZ(100)",
+      "1970-01-01 00:01:40.000")
+
+    testAllApis(
+      toTimestampLtz(100, 0),
+      "TO_TIMESTAMP_LTZ(100, 0)",
+      "1970-01-01 00:01:40.000")
+
+    testAllApis(
+      toTimestampLtz(1234, 3),
+      "TO_TIMESTAMP_LTZ(1234, 3)",
+      "1970-01-01 00:00:01.234")
+
+    testAllApis(
+      toTimestampLtz(-100),
+      "TO_TIMESTAMP_LTZ(-100)",
+      "1969-12-31 23:58:20.000")
+  }
+
+  @Test
+  def testBoundaryForToTimestampLtz(): Unit = {
+    config.setLocalTimeZone(ZoneId.of("UTC"))
+
+    // INT
+    testAllApis(
+      toTimestampLtz(JInt.MIN_VALUE.cast(DataTypes.INT())),
+      s"TO_TIMESTAMP_LTZ(CAST(${JInt.MIN_VALUE} AS INTEGER))",
+      "1901-12-13 20:45:52.000")
+    testAllApis(
+      toTimestampLtz(JInt.MAX_VALUE.cast(DataTypes.INT())),
+      s"TO_TIMESTAMP_LTZ(CAST(${JInt.MAX_VALUE} AS INTEGER))",
+      "2038-01-19 03:14:07.000")
+
+    // TINYINT
+    testAllApis(
+      toTimestampLtz(-127.cast(DataTypes.TINYINT())),

Review comment:
       should be `-128` and `127`?




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


Reply via email to