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

[PR] [SPARK-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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

   <!--
   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.
   -->
   
   This patch adds one more case to the optimization rule `UnwrapCastInBinaryComparison`. The added case handles unwrapping cast in binary comparison between timestamp and timestamp_ntz types.
   
   ### 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.
   -->
   
   We encountered a case that a predicate on a timestamp_ntz column is not pushed down. The query plan looks like:
   
   ```
   == Analyzed Logical Plan ==
   batch: timestamp
   Project [batch#26466]
   +- Filter (batch#26466 >= cast(2023-12-21 10:00:00 as timestamp))
      +- SubqueryAlias spark_catalog.default.timestamp_view
         +- View (`spark_catalog`.`default`.`timestamp_view`, [batch#26466])
            +- Project [cast(batch#26467 as timestamp) AS batch#26466]
               +- Project [cast(batch#26463 as timestamp) AS batch#26467]
                  +- SubqueryAlias spark_catalog.default.table_timestamp
                     +- Relation spark_catalog.default.table_timestamp[batch#26463] parquet
   
   == Optimized Logical Plan ==
   Project [cast(batch#26463 as timestamp) AS batch#26466]
   +- Filter (isnotnull(batch#26463) AND (cast(batch#26463 as timestamp) >= 2023-12-21 10:00:00))
      +- Relation spark_catalog.default.table_timestamp[batch#26463] parquet
   ```
   
   The predicate compares a timestamp_ntz column with a literal value. As the column is wrapped in a cast expression to timestamp type, the literal (string) is wrapped with a cast to timestamp type. The literal with cast is foldable so it is evaluated to literal of timestamp earlier. So the predicate becomes `cast(batch#26463 as timestamp) >= 2023-12-21 10:00:00`. As the cast is in column side, it cannot be pushed down to data source/table.
   
   We have an optimization rule `UnwrapCastInBinaryComparison` that handles similar cases but it doesn't cover timestamp types.
   
   After this patch, the query plan looks like:
   
   ```
   == Analyzed Logical Plan ==                        
   batch: timestamp                                     
   Project [batch#26446]                
   +- Filter (batch#26446 >= cast(2023-12-21 10:00:00 as timestamp))                                                  
      +- SubqueryAlias spark_catalog.default.timestamp_view                                                           
         +- View (`spark_catalog`.`default`.`timestamp_view`, [batch#26446])                                          
            +- Project [cast(batch#26447 as timestamp) AS batch#26446]    
               +- Project [cast(batch#26443 as timestamp) AS batch#26447]                                             
                  +- SubqueryAlias spark_catalog.default.table_timestamp                                              
                     +- Relation spark_catalog.default.table_timestamp[batch#26443] parquet
   
   == Optimized Logical Plan ==
   Project [cast(batch#26443 as timestamp) AS batch#26446]
   +- Filter (isnotnull(batch#26443) AND (batch#26443 >= 2023-12-21 10:00:00))
      +- Relation spark_catalog.default.table_timestamp[batch#26443] parquet
   ```
   
   ### 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'.
   -->
   
   Yes. Casts between timestamp and timestamp_ntz in binary comparison will be unwrap if possible.
   
   ### 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.
   -->
   
   Added unit test.
   
   ### 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.
   -->
   
   No


-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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

   cc @gengliangwang and @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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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

   Ya, it's a nice point. I filed the following for that.
   - SPARK-46527: Verify and unify the code between `In/InSet` and binary comparison
   
   Merged to master for Apache Spark 4.0.0. 
   
   Thank you, @viirya , @cloud-fan , @HyukjinKwon .


-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp

Review Comment:
   `cast(ts_ntz_col as ts) compare ts_lit` means `convertTz(col_value, tz, UTC) compare lit_value`, and we are trying to reverse it to `col_value compare convertTz(lit_value, UTC, tz)`.
   
   We need to be careful here, as datetime with timezone is very tricky. My proposal is to do a round trip tests before rewriting it: if `convertTz(convertTz(lit_value, UTC, tz), tz, UTC) == lit_value`. This means we convert the comparison to `convertTz(col_value, tz, UTC) compare convertTz(convertTz(lit_value, UTC, tz), tz, UTC) == lit_value` first, and we strip the `convertTz(_, tz, UTC)` from both sides.



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), Literal(value, literalType))
+        if AnyTimestampType.acceptsType(fromExp.dataType) &&
+          AnyTimestampType.acceptsType(literalType) && value != null =>
+      val newExpr = be.withNewChildren(Seq(fromExp, Cast(Literal(value, literalType),
+        fromExp.dataType, timeZoneId, evalMode)))
+      Some(newExpr)

Review Comment:
   Please let me know if this makes sense (if I miss anything this could be wrong).



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), Literal(value, literalType))
+        if AnyTimestampType.acceptsType(fromExp.dataType) &&
+          AnyTimestampType.acceptsType(literalType) && value != null =>
+      val newExpr = be.withNewChildren(Seq(fromExp, Cast(Literal(value, literalType),
+        fromExp.dataType, timeZoneId, evalMode)))
+      Some(newExpr)

Review Comment:
   cc @sunchao 



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -41,7 +41,7 @@ import org.apache.spark.sql.types._
  *
  * Currently this only handles cases where:
  *   1). `fromType` (of `fromExp`) and `toType` are of numeric types (i.e., short, int, float,
- *     decimal, etc), boolean type or datetime type
+ *     decimal, etc), boolean type or datetime type, or timestamp types

Review Comment:
   datetime type includes timestamp



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala:
##########
@@ -3710,6 +3710,62 @@ class DataFrameSuite extends QueryTest
         parameters = Map("viewName" -> "AUTHORIZATION"))
     }
   }
+
+  test("SPARK-46502: Unwrap timestamp cast on timestamp_ntz column") {
+    def getQueryResult(ruleEnabled: Boolean): Seq[Row] = {
+      val ruleName = if (ruleEnabled) {
+        ""
+      } else {
+        "org.apache.spark.sql.catalyst.optimizer.UnwrapCastInBinaryComparison"
+      }
+
+      var result: Seq[Row] = Seq.empty
+
+      withSQLConf(SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> ruleName,
+        "spark.boson.enabled" -> "false") {

Review Comment:
   Oops, removed.



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #44480:
URL: https://github.com/apache/spark/pull/44480#discussion_r1436674275


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,21 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp
+    case be @ BinaryComparison(
+      c @ Cast(fromExp, _, timeZoneId, evalMode), Literal(value, literalType))
+        if AnyTimestampType.acceptsType(fromExp.dataType) &&
+          AnyTimestampType.acceptsType(literalType) && value != null =>
+      // datetime with timezone is tricky, do a round trip to check if the rewrite is okay.
+      val newCast = Cast(Literal(value, literalType), fromExp.dataType, timeZoneId, evalMode)
+      val roundTrip = Cast(newCast, literalType, timeZoneId, evalMode)

Review Comment:
   Ya, this looks safe.



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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

   > Apache Spark explicitly requires Timezone information for these kind of conversions.
   > 
   > https://github.com/apache/spark/blob/bb85bbcf16f7c837a47d0cf430bc94899709c254/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala#L269-L275
   > 
   > So, is the proposal to ignore the timezone for the foldable values?
   
   For the `Cast` in optimization stage, it must be resolved and the condition of resolved one for timestamp conversion is that timezone information is available:
   
   ```scala
   override lazy val resolved: Boolean =
       childrenResolved && checkInputDataTypes().isSuccess && (!needsTimeZone || timeZoneId.isDefined)
   ```
   
   


-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), Literal(value, literalType))
+        if AnyTimestampType.acceptsType(fromExp.dataType) &&
+          AnyTimestampType.acceptsType(literalType) && value != null =>
+      val newExpr = be.withNewChildren(Seq(fromExp, Cast(Literal(value, literalType),
+        fromExp.dataType, timeZoneId, evalMode)))
+      Some(newExpr)

Review Comment:
   LGTM.



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,21 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp
+    case be @ BinaryComparison(
+      c @ Cast(fromExp, _, timeZoneId, evalMode), Literal(value, literalType))
+        if AnyTimestampType.acceptsType(fromExp.dataType) &&
+          AnyTimestampType.acceptsType(literalType) && value != null =>
+      // datetime with timezone is tricky, do a round trip to check if the rewrite is okay.
+      val newCast = Cast(Literal(value, literalType), fromExp.dataType, timeZoneId, evalMode)
+      val roundTrip = Cast(newCast, literalType, timeZoneId, evalMode)
+      if (roundTrip.eval().asInstanceOf[Long] == value.asInstanceOf[Long]) {
+        val newExpr = be.withNewChildren(Seq(fromExp, newCast))
+        Some(newExpr)
+      } else {
+        None
+      }
+

Review Comment:
   Not related to this PR, but I just noticed that people keep supporting new data types in binary comparison, but not `In` and `InSet`. It will be better if we can somehow unify the code between `In/InSet` and binary comparison.
   
   cc @wangyum 



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #44480: [SPARK-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison
URL: https://github.com/apache/spark/pull/44480


-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp

Review Comment:
   Added round trip check before the rewriting it to make sure if it is safe.



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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

   Thank you @HyukjinKwon @dongjoon-hyun @cloud-fan @beliefer 


-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp

Review Comment:
   I use `Cast.eval` instead of `convertTz` directly because otherwise we need to handle timestamp -> timestamp_ntz and timestamp_ntz -> timestamp two cases here. Let me know if you prefer we use `convertTz` directly 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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp

Review Comment:
   Added round trip check before the rewriting 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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #44480:
URL: https://github.com/apache/spark/pull/44480#discussion_r1436673782


##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala:
##########
@@ -3710,6 +3710,62 @@ class DataFrameSuite extends QueryTest
         parameters = Map("viewName" -> "AUTHORIZATION"))
     }
   }
+
+  test("SPARK-46502: Unwrap timestamp cast on timestamp_ntz column") {
+    def getQueryResult(ruleEnabled: Boolean): Seq[Row] = {
+      val ruleName = if (ruleEnabled) {
+        ""
+      } else {
+        "org.apache.spark.sql.catalyst.optimizer.UnwrapCastInBinaryComparison"
+      }
+
+      var result: Seq[Row] = Seq.empty
+
+      withSQLConf(SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> ruleName,
+        "spark.boson.enabled" -> "false") {

Review Comment:
   Please remove this, @viirya . 😄 



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), Literal(value, literalType))
+        if AnyTimestampType.acceptsType(fromExp.dataType) &&
+          AnyTimestampType.acceptsType(literalType) && value != null =>
+      val newExpr = be.withNewChildren(Seq(fromExp, Cast(Literal(value, literalType),
+        fromExp.dataType, timeZoneId, evalMode)))
+      Some(newExpr)

Review Comment:
   Please let me know if this makes sense (if I miss anything).



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -144,6 +144,15 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
           value != null =>
       Some(unwrapTimestampToDate(be, fromExp, ts, timeZoneId, evalMode))
 
+    // Timestamp/Timestamp_NTZ -> Timestamp_NTZ/Timestamp
+    case be @ BinaryComparison(
+      Cast(fromExp, _, timeZoneId, evalMode), Literal(value, literalType))
+        if AnyTimestampType.acceptsType(fromExp.dataType) &&
+          AnyTimestampType.acceptsType(literalType) && value != null =>
+      val newExpr = be.withNewChildren(Seq(fromExp, Cast(Literal(value, literalType),
+        fromExp.dataType, timeZoneId, evalMode)))
+      Some(newExpr)

Review Comment:
   I think this should be correct, but please let me know if this makes sense (if I miss anything).



-- 
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-46502][SQL] Support timestamp types in UnwrapCastInBinaryComparison [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -41,7 +41,7 @@ import org.apache.spark.sql.types._
  *
  * Currently this only handles cases where:
  *   1). `fromType` (of `fromExp`) and `toType` are of numeric types (i.e., short, int, float,
- *     decimal, etc), boolean type or datetime type
+ *     decimal, etc), boolean type or datetime type, or timestamp types

Review Comment:
   Okay, removed 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