You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "wankunde (via GitHub)" <gi...@apache.org> on 2023/11/23 15:25:22 UTC

[PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

wankunde opened a new pull request, #43982:
URL: https://github.com/apache/spark/pull/43982

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on PR #43982:
URL: https://github.com/apache/spark/pull/43982#issuecomment-1831263113

   cc @cloud-fan 


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wankunde (via GitHub)" <gi...@apache.org>.
wankunde commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405683975


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))

Review Comment:
   Updated, thanks



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))
+      case EqualNullSafe(_, _) =>

Review Comment:
   Updated, thanks



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wankunde (via GitHub)" <gi...@apache.org>.
wankunde commented on PR #43982:
URL: https://github.com/apache/spark/pull/43982#issuecomment-1856058427

   @wangyum @cloud-fan 
   
   I'm sorry, it seems  the expression `val isStartOfDay = EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]`  is incorrect.
   For example, the input ts is `2023-01-01 00:00:00`,  floorDate will be `2022-12-31 16:00:00`, and `Cast(floorDate, ts.dataType, tz, evalMode)` will be `2022-12-31 08:00:00`, I don't know why...


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405415267


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))

Review Comment:
   ```suggestion
           if (isStartOfDay) EqualTo(fromExp, floorDate) else FalseLiteral
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405415267


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))

Review Comment:
   We can further optimize it to `FalseLiteral` if the timestamp is not the start of day.
   ```suggestion
           if (isStartOfDay) EqualTo(fromExp, floorDate) else FalseLiteral
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1421702123


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val dateAddOne = DateAdd(floorDate, Literal(1, IntegerType))
+    val isStartOfDay =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        if (isStartOfDay) {
+          GreaterThanOrEqual(fromExp, floorDate)
+        } else {
+          GreaterThanOrEqual(fromExp, dateAddOne)
+        }
+      case _: EqualTo =>
+        if (isStartOfDay) {
+          EqualTo(fromExp, floorDate)
+        } else if (!fromExp.nullable) {
+          FalseLiteral
+        } else {
+          And(EqualTo(fromExp, floorDate), EqualTo(fromExp, dateAddOne))

Review Comment:
   BTW let's keep the high code review quality in the community. This is the core logic of this optimization and it's even highlighted in the PR description. I'm surprised we missed this case.



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "gatorsmile (via GitHub)" <gi...@apache.org>.
gatorsmile commented on PR #43982:
URL: https://github.com/apache/spark/pull/43982#issuecomment-1848887593

   @wangyum  Please be more careful in the code review.


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405654405


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))

Review Comment:
   How about?
   ```scala
           if (isStartOfDay) {
             EqualTo(fromExp, floorDate)
           } else if (!fromExp.nullable) {
             FalseLiteral
           } else {
             And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))
           }
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wankunde (via GitHub)" <gi...@apache.org>.
wankunde commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405537575


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))

Review Comment:
   Thanks @wangyum for your review.
   
   If `isStartOfDay` is false, the optimizer will change this expression to `FalseLiteral` which results in incorrect result if `fromExp` is null.
   So I use two EqualTo expressions to support nullable input expressions. During the execution phase, if `isStartOfDay` is true, the two EqualTo expressions can be reuse and no additional calculations are added. 



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1421701688


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val dateAddOne = DateAdd(floorDate, Literal(1, IntegerType))
+    val isStartOfDay =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        if (isStartOfDay) {
+          GreaterThanOrEqual(fromExp, floorDate)
+        } else {
+          GreaterThanOrEqual(fromExp, dateAddOne)
+        }
+      case _: EqualTo =>
+        if (isStartOfDay) {
+          EqualTo(fromExp, floorDate)
+        } else if (!fromExp.nullable) {
+          FalseLiteral
+        } else {
+          And(EqualTo(fromExp, floorDate), EqualTo(fromExp, dateAddOne))

Review Comment:
   actually there is already a `falseIfNotNull` method in this class.



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1413014878


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -138,6 +138,11 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
         if AnyTimestampType.acceptsType(fromExp.dataType) && value != null =>
       Some(unwrapDateToTimestamp(be, fromExp, date, timeZoneId, evalMode))
 
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), ts @ Literal(value, _))

Review Comment:
   Hm, don't we need to case-match `fromExp`s type for `DateType` here?



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum closed pull request #43982: [SPARK-46069][SQL] Support unwrap timestamp type to date type
URL: https://github.com/apache/spark/pull/43982


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1413403259


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(

Review Comment:
   nit: `Timestamp` is one word



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wankunde (via GitHub)" <gi...@apache.org>.
wankunde commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1414761339


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(

Review Comment:
   Sorry, fix it in the followup PR : https://github.com/apache/spark/pull/44134/files#diff-56271650e1a2efb907de03cfd8300018b021dd7ae338f13c6d6e6448cd2bac8aL144



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405654843


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))
+      case EqualNullSafe(_, _) =>

Review Comment:
   How about?
   ```scala
           if (isStartOfDay) EqualNullSafe(fromExp, floorDate) else FalseLiteral
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1413016701


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -138,6 +138,11 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
         if AnyTimestampType.acceptsType(fromExp.dataType) && value != null =>
       Some(unwrapDateToTimestamp(be, fromExp, date, timeZoneId, evalMode))
 
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), ts @ Literal(value, _))

Review Comment:
   e.g., `fromExp: DateType`



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #43982:
URL: https://github.com/apache/spark/pull/43982#issuecomment-1824992712

   cc @sunchao FYI


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wankunde (via GitHub)" <gi...@apache.org>.
wankunde commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405537995


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))
+      case EqualNullSafe(_, _) =>

Review Comment:
   The same with the EqualTo branch, the two equalTo expressions can support nullable input expressions.



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1421717944


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparisonSuite.scala:
##########
@@ -403,11 +405,40 @@ class UnwrapCastInBinaryComparisonSuite extends PlanTest with ExpressionEvalHelp
       castDate(f5) > nullLit || castDate(f6) > nullLit)
   }
 
+  test("SPARK-46069: Support unwrap timestamp type to date type") {
+    val tsLit = Literal.create(ts1, TimestampType)

Review Comment:
   > 3. isStartOfDay: return true if ts == floorDate
   
   For such an important concept, we don't have unit tests for it? The optimized comparison is different depending on this condition!



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on PR #43982:
URL: https://github.com/apache/spark/pull/43982#issuecomment-1837365342

   Merged to master.


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wankunde (via GitHub)" <gi...@apache.org>.
wankunde commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1413135870


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -138,6 +138,11 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
         if AnyTimestampType.acceptsType(fromExp.dataType) && value != null =>
       Some(unwrapDateToTimestamp(be, fromExp, date, timeZoneId, evalMode))
 
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), ts @ Literal(value, _))

Review Comment:
   Thanks for your review, add a followup for this data type checking: https://github.com/apache/spark/pull/44134



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405415194


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }

Review Comment:
   ```suggestion
       val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
       val isStartOfDay =
         EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]
       val ceilDate = if (isStartOfDay) floorDate else DateAdd(floorDate, Literal(1, IntegerType))
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1427091458


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val dateAddOne = DateAdd(floorDate, Literal(1, IntegerType))
+    val isStartOfDay =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]

Review Comment:
   how did you print it? Do you use Spark Cast (cast to string) to print it?



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wankunde (via GitHub)" <gi...@apache.org>.
wankunde commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1427645442


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val dateAddOne = DateAdd(floorDate, Literal(1, IntegerType))
+    val isStartOfDay =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]

Review Comment:
   Add a followup for this PR:  https://github.com/apache/spark/pull/44366



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on PR #43982:
URL: https://github.com/apache/spark/pull/43982#issuecomment-1906350570

   > @wangyum Please be more careful in the code review.
   
   Sorry. Will be careful in future code review.


-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1421700534


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val dateAddOne = DateAdd(floorDate, Literal(1, IntegerType))
+    val isStartOfDay =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        if (isStartOfDay) {
+          GreaterThanOrEqual(fromExp, floorDate)
+        } else {
+          GreaterThanOrEqual(fromExp, dateAddOne)
+        }
+      case _: EqualTo =>
+        if (isStartOfDay) {
+          EqualTo(fromExp, floorDate)
+        } else if (!fromExp.nullable) {
+          FalseLiteral
+        } else {
+          And(EqualTo(fromExp, floorDate), EqualTo(fromExp, dateAddOne))

Review Comment:
   This is very confusing. How can the `fromExp` be equal to two different values at the same time? This expression is either false or null.
   
   Do you mean `if(isnull(e), null, false)`? Then `And(IsNull(e), Literal(null, BooleanType))` should be more efficient.



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "wangyum (via GitHub)" <gi...@apache.org>.
wangyum commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1405415554


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,40 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val equalToFloorDateExpr =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode))
+    val equalToFloorDate = equalToFloorDateExpr.eval(EmptyRow).asInstanceOf[Boolean]
+    val ceilDate =
+      if (equalToFloorDate) {
+        floorDate
+      } else {
+        DateAdd(floorDate, Literal(1, IntegerType))
+      }
+
+    exp match {
+      case _: GreaterThan =>
+        GreaterThan(fromExp, floorDate)
+      case _: GreaterThanOrEqual =>
+        GreaterThanOrEqual(fromExp, ceilDate)
+      case _: EqualTo =>
+        And(EqualTo(fromExp, floorDate), EqualTo(fromExp, ceilDate))
+      case EqualNullSafe(_, _) =>

Review Comment:
   It seems the left side should not be nullable.
   ```suggestion
         case EqualNullSafe(left, _) if !left.nullable =>
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1413014878


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -138,6 +138,11 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
         if AnyTimestampType.acceptsType(fromExp.dataType) && value != null =>
       Some(unwrapDateToTimestamp(be, fromExp, date, timeZoneId, evalMode))
 
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), ts @ Literal(value, _))

Review Comment:
   Hm, don't we need to case-match `fromExp`s type here?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -138,6 +138,11 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
         if AnyTimestampType.acceptsType(fromExp.dataType) && value != null =>
       Some(unwrapDateToTimestamp(be, fromExp, date, timeZoneId, evalMode))
 
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), ts @ Literal(value, _))

Review Comment:
   Hm, don't we need to case-match `fromExp`s type for `DateType` here?



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1413015199


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -138,6 +138,11 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
         if AnyTimestampType.acceptsType(fromExp.dataType) && value != null =>
       Some(unwrapDateToTimestamp(be, fromExp, date, timeZoneId, evalMode))
 
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), ts @ Literal(value, _))

Review Comment:
   Shouldn't we also check the type from the cast too to be safer?



-- 
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: reviews-unsubscribe@spark.apache.org

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


Re: [PR] [SPARK-46069][SQL] Support unwrap timestamp type to date type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43982:
URL: https://github.com/apache/spark/pull/43982#discussion_r1427090570


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -329,6 +334,48 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
     }
   }
 
+  private def unwrapTimeStampToDate(
+      exp: BinaryComparison,
+      fromExp: Expression,
+      ts: Literal,
+      tz: Option[String],
+      evalMode: EvalMode.Value): Expression = {
+    val floorDate = Cast(ts, fromExp.dataType, tz, evalMode)
+    val dateAddOne = DateAdd(floorDate, Literal(1, IntegerType))
+    val isStartOfDay =
+      EqualTo(ts, Cast(floorDate, ts.dataType, tz, evalMode)).eval(EmptyRow).asInstanceOf[Boolean]

Review Comment:
   @wankunde  do you mean the cast here does not produce a timestamp with the time part to be 0 (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: reviews-unsubscribe@spark.apache.org

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