MaxGekk commented on code in PR #60285:
URL: https://github.com/apache/spark/pull/60285#discussion_r4535265062


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala:
##########
@@ -733,10 +733,17 @@ case class MultiplyDTInterval(
  * The division of an interval by a number. The interpreted path and the 
generated code both call
  * the `IntervalMathUtils.divide` overload for the interval type and the kind 
of the divisor, which
  * holds the checks and the error, so that the two paths cannot drift apart.
+ *
+ * The division raises on overflow and on a zero divisor in every mode, so it 
always keeps the
+ * query context. It keeps it as a `SupportQueryContext` field rather than 
reading `origin` when
+ * it evaluates: `origin` is not serialized, so a copy of the expression 
deserialized on an
+ * executor, where an interpreted or a non-whole-stage projection is built, 
has an empty one.
  */
-trait IntervalDivide extends BinaryExpression {
+trait IntervalDivide extends BinaryExpression with SupportQueryContext {

Review Comment:
   Agreed that it's the same kind of gap, but it's older and changes the error 
the multiplication raises, so I'd keep it out of this PR. I opened SPARK-61439 
for `MultiplyYMInterval` / `MultiplyDTInterval`; SPARK-61196 already covers 
add, subtract and negate. Both should take the context from a 
`SupportQueryContext` field, as here.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala:
##########
@@ -514,6 +517,67 @@ class IntervalExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     }
   }
 
+  test("SPARK-61352: an interval error keeps its query context") {
+    // The helpers evaluate a serialized copy of the expression, as an 
executor does. `TreeNode`
+    // does not serialize `origin`, so the copy must carry the context itself. 
Each expression is
+    // built inside `withOrigin` and evaluated outside it: evaluated inside, 
the copy would read
+    // the origin back from `CurrentOrigin` and the test could not fail.
+    def withQuery(query: String)(expr: => Expression): (Expression, String) = {
+      val o = Origin(
+        line = Some(1),
+        startPosition = Some(7),
+        startIndex = Some(7),
+        stopIndex = Some(7 + query.length - 1),
+        sqlText = Some(s"select $query"))
+      (withOrigin(o)(expr), s"== SQL (line 1, position 8) ==\nselect $query")
+    }
+    // A cast to the type of the expression, with a context of its own (ANSI) 
or none (LEGACY).
+    def sameTypeCast(expr: Expression, evalMode: EvalMode.Value): Expression = 
{
+      val o = Origin(
+        line = Some(1),
+        startPosition = Some(0),
+        startIndex = Some(0),
+        stopIndex = Some(3),
+        sqlText = Some("cast"))
+      withOrigin(o)(Cast(expr, expr.dataType, None, evalMode))
+    }
+    val ser = new JavaSerializer(new SparkConf()).newInstance()
+
+    val ymMin = Literal(Period.ofMonths(Int.MinValue))
+    val dtMin = Literal(Duration.of(Long.MinValue, ChronoUnit.MICROS))
+    Seq(
+      withQuery("ym / -1")(DivideYMInterval(ymMin, Literal(-1))),
+      withQuery("ym / -1.0D")(DivideYMInterval(ymMin, Literal(-1.0d))),
+      withQuery("ym / 0")(DivideYMInterval(ymMin, Literal(0))),
+      withQuery("dt / -1L")(DivideDTInterval(dtMin, Literal(-1L))),
+      withQuery("dt / -1BD")(DivideDTInterval(dtMin, Literal(Decimal(-1)))),
+      withQuery("dt / 0")(DivideDTInterval(dtMin, Literal(0))),
+      withQuery("make_ym_interval(2147483647, 0)")(
+        MakeYMInterval(Literal(Int.MaxValue), Literal(0))),
+      withQuery("make_dt_interval(2147483647)")(
+        MakeDTInterval(Literal(Int.MaxValue), Literal(0), Literal(0),
+          Literal(Decimal(0, Decimal.MAX_LONG_DIGITS, 6))))
+    ).foreach { case (expr, context) =>
+      // A rule that replaces a node calls `copyTagsFrom` on the replacement, 
as `SimplifyCasts`
+      // does when it replaces a cast to the same type with its child.
+      val replacingCasts = Seq(EvalMode.ANSI, EvalMode.LEGACY).map { evalMode 
=>

Review Comment:
   Done in b43ba62096e: each case is a factory, and every form gets an instance 
of its own. The ANSI cast form now expects the cast's context, and the non-ANSI 
one the expression's own. With `copyTagsFrom` copying `None` again, the test 
fails.



##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala:
##########
@@ -843,6 +843,53 @@ class QueryExecutionErrorsSuite
     }
   }
 
+  test("SPARK-61352: an interval error keeps its query context in non-ANSI 
mode") {
+    // The division and the `make_*_interval` functions raise in every mode. 
Without whole-stage
+    // codegen the projection is built on the executor from a deserialized 
copy of the expression.
+    // The expressions read a column, so that they are not constant-folded on 
the driver.
+    // `SimplifyCasts` replaces a cast to the type of the expression with the 
expression, which
+    // keeps its own context although the non-ANSI cast has none.
+    def expectedContext(query: String, fragment: String): ExpectedContext = {

Review Comment:
   Done in b43ba62096e: `IntervalErrorContextTestUtils` holds the two division 
queries and `expectedContext`, and both suites mix it in.



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