dcoliversun commented on code in PR #5240:
URL: https://github.com/apache/incubator-gluten/pull/5240#discussion_r1549392276


##########
backends-velox/src/test/scala/org/apache/gluten/execution/TestOperator.scala:
##########
@@ -1236,4 +1238,52 @@ class TestOperator extends 
VeloxWholeStageTransformerSuite {
       }
     }
   }
+
+  test("Cast date to string") {
+    withTempPath {
+      path =>
+        Seq("2023-01-01", "2023-01-02", "2023-01-03")
+          .toDF("dateColumn")
+          .select(to_date($"dateColumn", "yyyy-MM-dd").as("dateColumn"))
+          .write
+          .parquet(path.getCanonicalPath)
+        
spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("view")
+        runQueryAndCompare("SELECT cast(dateColumn as string) from view") {
+          checkGlutenOperatorMatch[ProjectExecTransformer]
+        }
+    }
+  }
+
+  test("Cast date to timestamp") {
+    withTempPath {
+      path =>
+        Seq("2023-01-01", "2023-01-02", "2023-01-03")
+          .toDF("dateColumn")
+          .select(to_date($"dateColumn", "yyyy-MM-dd").as("dateColumn"))
+          .write
+          .parquet(path.getCanonicalPath)
+        
spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("view")
+        runQueryAndCompare("SELECT cast(dateColumn as timestamp) from view") {
+          checkGlutenOperatorMatch[ProjectExecTransformer]
+        }
+    }
+  }
+
+  test("cast date to timestamp with timezone") {
+    sql("SET spark.sql.session.timeZone = America/Los_Angeles")
+    val dfWithLA = sql("SELECT cast(date'2023-01-02 01:01:01' as timestamp) as 
ts")
+
+    sql("SET spark.sql.session.timeZone = Asia/Shanghai")
+    val dfWithSH = sql("SELECT cast(date'2023-01-02 01:01:01' as timestamp) as 
ts")
+
+    // They should be the same because date in spark represents the day
+    checkAnswer(dfWithLA, Row(Timestamp.valueOf("2023-01-02 00:00:00")))

Review Comment:
   I find the reason. With timestamp type, `df.show` is different from 
`df.collect`. `CheckAnswer` function use `collect` to do action. But I set 
expected answer as show result
   * vallina spark
   ```plain
   scala> spark.sql("SET spark.sql.session.timeZone = America/Los_Angeles")
   res0: org.apache.spark.sql.DataFrame = [key: string, value: string]
   
   scala> val dfWithLA = sql("SELECT cast(date'2023-01-02 01:01:01' as 
timestamp) as ts")
   dfWithLA: org.apache.spark.sql.DataFrame = [ts: timestamp]
   
   scala> dfWithLA.explain
   == Physical Plan ==
   *(1) Project [2023-01-02 00:00:00 AS ts#10]
   +- *(1) Scan OneRowRelation[]
   
   
   
   scala> dfWithLA.collect
   res2: Array[org.apache.spark.sql.Row] = Array([2023-01-02 08:00:00.0])       
   
   
   scala> dfWithLA.show
   +-------------------+
   |                 ts|
   +-------------------+
   |2023-01-02 00:00:00|
   +-------------------+
   
   
   scala> spark.sql("SET spark.sql.session.timeZone = Asia/Shanghai")
   res4: org.apache.spark.sql.DataFrame = [key: string, value: string]
   
   scala> val dfWithSH = sql("SELECT cast(date'2023-01-02 01:01:01' as 
timestamp) as ts")
   dfWithSH: org.apache.spark.sql.DataFrame = [ts: timestamp]
   
   scala> dfWithSH.explain
   == Physical Plan ==
   *(1) Project [2023-01-02 00:00:00 AS ts#29]
   +- *(1) Scan OneRowRelation[]
   
   
   
   scala> dfWithSH.collect
   res6: Array[org.apache.spark.sql.Row] = Array([2023-01-01 16:00:00.0])
   
   scala> dfWithSH.show
   +-------------------+
   |                 ts|
   +-------------------+
   |2023-01-02 00:00:00|
   +-------------------+
   ```
   * spark + gluten + velox
   ```plain
   scala> spark.sql("SET spark.sql.session.timeZone = America/Los_Angeles")
   res0: org.apache.spark.sql.DataFrame = [key: string, value: string]
   
   scala> val dfWithLA = sql("SELECT cast(date'2023-01-02 01:01:01' as 
timestamp) as ts")
   dfWithLA: org.apache.spark.sql.DataFrame = [ts: timestamp]
   
   scala> dfWithLA.explain
   == Physical Plan ==
   VeloxColumnarToRowExec
   +- ^(1) ProjectExecTransformer [2023-01-02 00:00:00 AS ts#10]
      +- ^(1) InputIteratorTransformer[fake_column#12]
         +- ^(1) InputAdapter
            +- ^(1) RowToVeloxColumnar
               +- *(1) Scan OneRowRelation[fake_column#12]
   
   
   
   scala> dfWithLA.collect
   res2: Array[org.apache.spark.sql.Row] = Array([2023-01-02 08:00:00.0])       
   
   
   scala> dfWithLA.show
   +-------------------+
   |                 ts|
   +-------------------+
   |2023-01-02 00:00:00|
   +-------------------+
   
   
   scala> spark.sql("SET spark.sql.session.timeZone = Asia/Shanghai")
   res4: org.apache.spark.sql.DataFrame = [key: string, value: string]
   
   scala> val dfWithSH = sql("SELECT cast(date'2023-01-02 01:01:01' as 
timestamp) as ts")
   dfWithSH: org.apache.spark.sql.DataFrame = [ts: timestamp]
   
   scala> dfWithSH.explain
   == Physical Plan ==
   VeloxColumnarToRowExec
   +- ^(3) ProjectExecTransformer [2023-01-02 00:00:00 AS ts#31]
      +- ^(3) InputIteratorTransformer[fake_column#33]
         +- ^(3) InputAdapter
            +- ^(3) RowToVeloxColumnar
               +- *(1) Scan OneRowRelation[fake_column#33]
   
   
   
   scala> dfWithSH.collect
   res6: Array[org.apache.spark.sql.Row] = Array([2023-01-01 16:00:00.0])
   
   scala> dfWithSH.show
   +-------------------+
   |                 ts|
   +-------------------+
   |2023-01-02 00:00:00|
   +-------------------+
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to