You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/03/27 09:50:32 UTC

[GitHub] [spark] AngersZhuuuu commented on a change in pull request #31978: [SPARK-34841][SQL] Push ANSI interval binary expressions into into (if/else) branches

AngersZhuuuu commented on a change in pull request #31978:
URL: https://github.com/apache/spark/pull/31978#discussion_r602697895



##########
File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PushFoldableIntoBranchesSuite.scala
##########
@@ -259,6 +261,79 @@ class PushFoldableIntoBranchesSuite
       CaseWhen(Seq((a, FalseLiteral), (c, FalseLiteral)), None))
   }
 
+  test("SPARK-33848: Push down dateTimeExpression with binary expression through If/CaseWhen") {
+    val d = Date.valueOf("2021-01-01")
+    // If
+    assertEquivalent(AddMonths(Literal(d),
+      If(a, Literal(1), Literal(2))),
+      If(a, Literal(Date.valueOf("2021-02-01")), Literal(Date.valueOf("2021-03-01"))))
+    assertEquivalent(DateAdd(Literal(d),
+      If(a, Literal(1), Literal(2))),
+      If(a, Literal(Date.valueOf("2021-01-02")), Literal(Date.valueOf("2021-01-03"))))
+    assertEquivalent(DateAddInterval(Literal(d),
+      If(a, Literal(new CalendarInterval(1, 1, 0)),
+        Literal(new CalendarInterval(1, 2, 0)))),
+      If(a, Literal(Date.valueOf("2021-02-02")), Literal(Date.valueOf("2021-02-03"))))
+    assertEquivalent(DateAddYMInterval(Literal(d),
+      If(a, Literal.create(Period.ofMonths(1), YearMonthIntervalType),
+        Literal.create(Period.ofMonths(2), YearMonthIntervalType))),
+      If(a, Literal(Date.valueOf("2021-02-01")), Literal(Date.valueOf("2021-03-01"))))
+    assertEquivalent(DateDiff(Literal(d),
+      If(a, Literal(Date.valueOf("2021-02-01")), Literal(Date.valueOf("2021-03-01")))),
+      If(a, Literal(-31), Literal(-59)))
+    assertEquivalent(DateSub(Literal(d),
+      If(a, Literal(1), Literal(2))),
+      If(a, Literal(Date.valueOf("2020-12-31")), Literal(Date.valueOf("2020-12-30"))))
+    assertEquivalent(TimestampAddYMInterval(Literal(d.getTime, TimestampType),
+      If(a, Literal.create(Period.ofMonths(1), YearMonthIntervalType),
+        Literal.create(Period.ofMonths(2), YearMonthIntervalType))),
+      If(a, Literal.create(Timestamp.valueOf("1970-02-19 07:04:48"), TimestampType),

Review comment:
       > hmm, the result should be `2021-01-01` + 1 month = `2021-02-01`.
   
   Fix it

##########
File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PushFoldableIntoBranchesSuite.scala
##########
@@ -259,6 +261,79 @@ class PushFoldableIntoBranchesSuite
       CaseWhen(Seq((a, FalseLiteral), (c, FalseLiteral)), None))
   }
 
+  test("SPARK-33848: Push down dateTimeExpression with binary expression through If/CaseWhen") {
+    val d = Date.valueOf("2021-01-01")
+    // If
+    assertEquivalent(AddMonths(Literal(d),
+      If(a, Literal(1), Literal(2))),
+      If(a, Literal(Date.valueOf("2021-02-01")), Literal(Date.valueOf("2021-03-01"))))
+    assertEquivalent(DateAdd(Literal(d),
+      If(a, Literal(1), Literal(2))),
+      If(a, Literal(Date.valueOf("2021-01-02")), Literal(Date.valueOf("2021-01-03"))))
+    assertEquivalent(DateAddInterval(Literal(d),
+      If(a, Literal(new CalendarInterval(1, 1, 0)),
+        Literal(new CalendarInterval(1, 2, 0)))),
+      If(a, Literal(Date.valueOf("2021-02-02")), Literal(Date.valueOf("2021-02-03"))))
+    assertEquivalent(DateAddYMInterval(Literal(d),
+      If(a, Literal.create(Period.ofMonths(1), YearMonthIntervalType),
+        Literal.create(Period.ofMonths(2), YearMonthIntervalType))),
+      If(a, Literal(Date.valueOf("2021-02-01")), Literal(Date.valueOf("2021-03-01"))))
+    assertEquivalent(DateDiff(Literal(d),
+      If(a, Literal(Date.valueOf("2021-02-01")), Literal(Date.valueOf("2021-03-01")))),
+      If(a, Literal(-31), Literal(-59)))
+    assertEquivalent(DateSub(Literal(d),
+      If(a, Literal(1), Literal(2))),
+      If(a, Literal(Date.valueOf("2020-12-31")), Literal(Date.valueOf("2020-12-30"))))
+    assertEquivalent(TimestampAddYMInterval(Literal(d.getTime, TimestampType),

Review comment:
       > `getTime()` returns milliseconds but `TimestampType` has microsecond resolution. Please, construct correct timestamp expression, here.
   
   Changed




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org