spark-test commented on code in PR #60285:
URL: https://github.com/apache/spark/pull/60285#discussion_r4545852484
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala:
##########
@@ -514,6 +517,73 @@ 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).
+ val castContext = "== SQL (line 1, position 1) ==\ncast"
+ 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))),
Review Comment:
**Nit (P3):** This case list reaches four of the six interpreted `evalFunc`
branches that this PR switches to `getContextOrNull()`: a year-month interval
divided by an integral or a double, and a day-time interval divided by an
integral or a decimal. No case divides a year-month interval by a decimal
(`intervalExpressions.scala:776`) or a day-time interval by a float or double
(`intervalExpressions.scala:813`).
Reading the other tests, none would catch a regression in those two branches
either. The SPARK-60746 cases use these divisors but build the expressions
without an origin, so they cannot tell a lost context from a kept one. The
end-to-end suites divide only by integers, and their executor-built projections
run the shared generated code rather than `evalFunc`. The decimal and double
divisions in `interval.sql` are evaluated on the driver, by constant folding or
as a local relation, where `origin` is still intact. So reverting either call
to `origin.context` would leave every test passing, while interpreted
evaluation on an executor (the codegen fallback,
`spark.sql.codegen.factoryMode=NO_CODEGEN`, or a `CodegenFallback` parent)
would lose the context again. Results are not affected.
Two more cases close the gap. Both divisions overflow, as the SPARK-60746
test already expects for these divisors, so the loop checks each of them in all
four forms:
```suggestion
Seq(
withQuery("ym / -1")(DivideYMInterval(ymMin, Literal(-1))),
withQuery("ym / -1.0D")(DivideYMInterval(ymMin, Literal(-1.0d))),
withQuery("ym / -1BD")(DivideYMInterval(ymMin, Literal(Decimal(-1)))),
withQuery("ym / 0")(DivideYMInterval(ymMin, Literal(0))),
withQuery("dt / -1L")(DivideDTInterval(dtMin, Literal(-1L))),
withQuery("dt / -1BD")(DivideDTInterval(dtMin, Literal(Decimal(-1)))),
withQuery("dt / -1.0D")(DivideDTInterval(dtMin, Literal(-1.0d))),
withQuery("dt / 0")(DivideDTInterval(dtMin, Literal(0))),
```
**Verification:**
- **Regression:** Changing the year-month-by-decimal or the
day-time-by-fractional interpreted division branch back to origin.context, one
at a time, makes the new case for that branch fail, because a serialized copy
evaluated by the interpreted path then reports an empty context.
- **Behavior:** With the production code as in the PR, both new cases pass
in every form the test checks: as built, under a removed ANSI cast whose
context they take, under a removed LEGACY cast where they keep their own, and
rebuilt from a deserialized copy.
--
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]