SEPURI-SAI-KRISHNA commented on code in PR #60175:
URL: https://github.com/apache/spark/pull/60175#discussion_r4511464624


##########
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:
   This narrowing is the existing behavior, not something this PR changes: on 
Spark 4.0.3 and on this PR, `300 PRECEDING AND 0.5 FOLLOWING` and `300.0 
PRECEDING AND CURRENT ROW` on a `TINYINT` key fail with `CAST_OVERFLOW` with 
ANSI mode on and return 5 instead of 6 rows for the last value with ANSI mode 
off (checked on both).
   
   A fractional offset on an integral key is also truncated by that cast (`0.5 
FOLLOWING` becomes `0 FOLLOWING`), so fixing it raises a separate semantic 
question beyond overflow, and I would rather keep this PR to integral offsets. 
I made the comment on the coercion tests explicit that such frames are coerced 
as before, and I can open a follow-up JIRA for non-integral offsets on integral 
keys if that sounds right.
   



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