dongjoon-hyun commented on code in PR #60285:
URL: https://github.com/apache/spark/pull/60285#discussion_r4542026862


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala:
##########
@@ -733,10 +733,18 @@ 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 
has a query context
+ * in every mode, unlike `Divide`, whose context is set only under ANSI. 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 {
+  override def initQueryContext(): Option[QueryContext] = Some(origin.context)

Review Comment:
   With this change, the division keeps its context in the mutable 
`queryContext` field, and `copyTagsFrom` overwrites that field in place on the 
instance that a rule returns. That instance can be shared with a temp view or a 
parent Dataset, so the error of one query can report the `CAST` context of 
another. If I read the code correctly, with ANSI on:
   
   ```scala
   sql("SELECT CAST(CAST(id - 2147483648 AS INT) AS INTERVAL MONTH) / -1 AS d 
FROM range(1)")
     .createOrReplaceTempView("v")
   sql("SELECT CAST(d AS INTERVAL YEAR TO MONTH) FROM v").collect()  // (1)
   sql("SELECT d FROM v").collect()                                   // (2)
   ```
   
   - `SessionCatalog.getTempViewPlan` reuses the stored analyzed plan, and 
`QueryExecution` clones only the plan nodes, not their expressions, so (1) gets 
the `DivideYMInterval` instance of the view.
   - In (1), `CollapseProject` puts that instance under the `Cast`, and 
`SimplifyCasts` returns it, so `transformDown` calls `copyTagsFrom(cast)` on it 
(`TreeNode.scala` L596).
   - Both queries fail, but (2) then reports the context of (1): its `== SQL 
==` block shows the text of (1), with the fragment `CAST(d AS INTERVAL YEAR TO 
MONTH)`.
   
   The same happens with a Dataset: `base.select($"d".cast("interval year to 
month"))` changes the context that a later `base.collect()` reports, even if 
`base` ran before.
   
   This predates the PR for ANSI `Add` and the other `SupportQueryContext` 
expressions, but the division used to read its own `origin`, which rules do not 
change, so it was not affected. Could we file a follow-up JIRA for the in-place 
update in `copyTagsFrom` and link it in the PR description?



##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionAnsiErrorsSuite.scala:
##########
@@ -115,6 +116,36 @@ class QueryExecutionAnsiErrorsSuite extends 
SharedSparkSession {
       context = ExpectedContext(fragment = "interval 1 day / 0", start = 7, 
stop = 24))
   }
 
+  test("SPARK-61352: an interval division error keeps its query context") {

Review Comment:
   The PR fixes the context that the division loses on executors, which applies 
to a `DataFrameQueryContext` from the Column API too, but this test and the new 
ones in `QueryExecutionErrorsSuite` and `IntervalExpressionsSuite` check only 
SQL text contexts. Could we add a DataFrame case in both whole-stage modes, 
e.g. `spark.range(1).select(expr("CAST(id + 1 AS INTERVAL DAY)") / 
$"id".cast("int"))`, checking `fragment = "div"` and `callSitePattern = 
getCurrentClassCallSitePattern` as the `lit(5) / lit(0)` test does at L69-74?



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