spark-test commented on code in PR #60175:
URL: https://github.com/apache/spark/pull/60175#discussion_r4504948501
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/window/WindowEvaluatorFactoryBase.scala:
##########
@@ -116,6 +116,20 @@ trait WindowEvaluatorFactoryBase {
val ordering = RowOrdering.create(orderSpec, childOutput)
RangeBoundOrdering(ordering, IdentityProjection, IdentityProjection)
+ case (RangeFrame, offset: Expression) if orderSpec.size == 1 &&
+ orderSpec.head.dataType.isInstanceOf[IntegralType] &&
+ offset.dataType.isInstanceOf[IntegralType] && offset.eval() != null
=>
Review Comment:
**Blocking (P1):** This guard sends an integral offset that evaluates to
NULL to the `Add`-based branch below. Because the coercion no longer casts an
integral offset of a wider type to the ORDER BY type, that branch can now
receive an INT key with a BIGINT offset, a pair its type match has no case for.
For example, `SELECT a, count(*) OVER (ORDER BY a RANGE BETWEEN CAST(NULL AS
BIGINT) PRECEDING AND CURRENT ROW) FROM VALUES (1), (2) AS t(a)` reaches that
match with `(IntegerType, LongType)` and fails every task with
`scala.MatchError`. On the base branch the offset was cast to INT, and the
query returned rows with a NULL bound. The same applies in descending order, to
a NULL `FOLLOWING` bound, to typed NULL parameters and Connect boundaries, and
to every frame implementation, since they all build their bounds here. One fix
is to send NULL integral offsets of any width to `IntegralRangeBoundOrdering`
as well, with a NULL-offset mode in which every bound is NULL: a NULL value
compa
res equal to it, and a non-NULL value sorts after it with NULLS FIRST and
before it with NULLS LAST, as `Add(value, NULL)` behaves today.
See **Shared repair plan 1** in the review body.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionHelper.scala:
##########
@@ -871,17 +871,37 @@ abstract class TypeCoercionHelper {
Seq(order),
SpecifiedWindowFrame(RangeFrame, lower, upper)
) =>
+ val boundaryType = integralBoundaryType(order.dataType, lower, upper)
+ .getOrElse(order.dataType)
s.copy(
frameSpecification = SpecifiedWindowFrame(
RangeFrame,
- createBoundaryCast(lower, order.dataType),
- createBoundaryCast(upper, order.dataType)
+ createBoundaryCast(lower, boundaryType),
+ createBoundaryCast(upper, boundaryType)
)
)
case other => other
}
+ /**
+ * For an integral order type and integral offsets, returns the widest of
these types. The
+ * offsets are not narrowed to the order type, where they might not fit: a
range frame ordered
+ * by an integral type compares the values with their bounds exactly.
+ */
+ private def integralBoundaryType(
+ orderType: DataType,
+ lower: Expression,
+ upper: Expression): Option[DataType] = {
+ val offsets = Seq(lower,
upper).filterNot(_.isInstanceOf[SpecialFrameBoundary])
+ if (orderType.isInstanceOf[IntegralType] &&
+ offsets.forall(o => o.resolved &&
o.dataType.isInstanceOf[IntegralType])) {
Review Comment:
**Non-blocking (P2):** This condition exempts every frame that has a
non-integral offset, so such frames keep the base branch's cast of every offset
to the ORDER BY type. With TINYINT values 0, 10, ..., 50, `count(*) OVER (ORDER
BY b RANGE BETWEEN 300 PRECEDING AND 0.5 FOLLOWING)` casts -300 to TINYINT:
with ANSI mode off it wraps to -44, so the frame of 50 is [6, 50] and holds 5
rows instead of 6, and with ANSI mode on the query fails with `CAST_OVERFLOW`.
`300.0 PRECEDING AND CURRENT ROW` behaves the same way, while the all-integral
`300 PRECEDING AND CURRENT ROW` is now exact. The new rule tests pin this
narrowing for a DECIMAL plus INT frame without saying why. A local fix here:
for a frame on an integral key that has a non-integral offset, keep casting to
the key type when every offset value fits it after the existing truncation
toward zero (NULL counts as fitting), and otherwise cast every offset to
BIGINT, which the new frame validation accepts and `IntegralRangeBoundOrderi
ng` compares exactly. Such frames stay unresolved until the coercion runs, so
their generated names do not change. Offsets beyond the BIGINT range are out of
reach of a BIGINT comparison.
**Verification:**
- **Regression:** Frames on TINYINT, SMALLINT and INT keys that pair a
non-fitting integral offset with a non-integral one, such as 300 PRECEDING AND
0.5 FOLLOWING on a TINYINT key, and frames whose only offset is a non-fitting
non-integral value, such as 300.0 PRECEDING, return the rows their definition
specifies with ANSI mode on and off instead of wrapping or failing with
CAST_OVERFLOW.
- **Compatibility:** Frames whose non-integral offsets fit the key type,
such as 1 PRECEDING AND 0.5 FOLLOWING on an INT key, keep the analyzed plan,
generated column name and rows they have in this PR.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionHelper.scala:
##########
@@ -871,17 +871,37 @@ abstract class TypeCoercionHelper {
Seq(order),
SpecifiedWindowFrame(RangeFrame, lower, upper)
) =>
+ val boundaryType = integralBoundaryType(order.dataType, lower, upper)
+ .getOrElse(order.dataType)
s.copy(
frameSpecification = SpecifiedWindowFrame(
RangeFrame,
- createBoundaryCast(lower, order.dataType),
- createBoundaryCast(upper, order.dataType)
+ createBoundaryCast(lower, boundaryType),
+ createBoundaryCast(upper, boundaryType)
)
)
case other => other
}
+ /**
+ * For an integral order type and integral offsets, returns the widest of
these types. The
+ * offsets are not narrowed to the order type, where they might not fit: a
range frame ordered
+ * by an integral type compares the values with their bounds exactly.
+ */
+ private def integralBoundaryType(
+ orderType: DataType,
+ lower: Expression,
+ upper: Expression): Option[DataType] = {
+ val offsets = Seq(lower,
upper).filterNot(_.isInstanceOf[SpecialFrameBoundary])
+ if (orderType.isInstanceOf[IntegralType] &&
+ offsets.forall(o => o.resolved &&
o.dataType.isInstanceOf[IntegralType])) {
+ Some((orderType +: offsets.map(_.dataType)).maxBy(_.defaultSize))
Review Comment:
**Blocking (P1):** Because each integral offset now keeps its own width,
equal offsets written with different widths fold to literals of different
types, such as `Literal(-1, INT)` and `Literal(-1L, BIGINT)`, and
`windowFrameExpressionFactoryPairs` keys physical frames by those typed
literals. So `SELECT count(*) OVER (ORDER BY i RANGE BETWEEN 1 PRECEDING AND
CURRENT ROW), sum(i) OVER (ORDER BY i RANGE BETWEEN CAST(1 AS BIGINT) PRECEDING
AND CURRENT ROW) FROM t` on an INT column builds two frames in one
`WindowExec`, where the base branch cast both offsets to INT and built one. The
same split happens when a SQL frame is combined with
`Window.orderBy(i).rangeBetween(-1, 0)`, whose offsets are BIGINT literals.
Each extra frame repeats the partition iteration (re-reading spilled pages when
the partition spilled), keeps its own frame buffer, and repeats the bound
comparisons and the aggregation work; Arrow Python window UDFs also get extra
bound columns. The results are the same. Sinc
e the as-written names need these offsets to stay uncast, the split is best
removed in window execution: key RANGE frames on a single integral ORDER BY
expression by offset value, for example by replacing each integral literal
offset in the frame key with the BIGINT literal of the same value.
`IntegralRangeBoundOrdering` uses only the offset's long value, so the merged
expressions get the same bounds.
See **Shared repair plan 1** in the review body.
--
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]