uros-b commented on code in PR #57680:
URL: https://github.com/apache/spark/pull/57680#discussion_r3700469336


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionHelper.scala:
##########
@@ -302,6 +302,35 @@ abstract class TypeCoercionHelper {
         }
     }
 
+  /** Whether `dt` is on the LTZ/NTZ timestamp fractional-precision axis (a 
micro or nanos type). */
+  private def isTimestampFamily(dt: DataType): Boolean =
+    TimestampFamily.fractionalPrecision(dt).isDefined
+
+  /**
+   * Common operand type for [[SubtractTimestamps]] over two differing 
timestamp-family operands.
+   * The operands are widened to the larger of the two fractional-second 
precisions (the micro types
+   * count as 6, the nanos types carry their own precision `p` in [7, 9]) and 
unified in one
+   * time-zone family:
+   *   - a cross-family pair unifies in the no-time-zone (NTZ) family, 
mirroring the microsecond
+   *     precedent where TIMESTAMP - TIMESTAMP_NTZ coerces both operands to 
TIMESTAMP_NTZ;
+   *   - a same-family pair keeps that family, so a TIMESTAMP - 
TIMESTAMP_LTZ(p) style pair still
+   *     subtracts in the session time zone (DST-aware) exactly as a pure LTZ 
pair does.
+   * The subtraction reads only each operand's epochMicros and always yields a 
microsecond-grid
+   * DayTimeIntervalType, so widening the precision never changes the numeric 
result -- it only
+   * keeps the two operands the same concrete type. For a pure-micro 
cross-family pair this returns
+   * TimestampNTZType, identical to the pre-nanos behavior.
+   */
+  private def subtractTimestampsCommonType(dt1: DataType, dt2: DataType): 
DataType = {
+    val p = math.max(
+      TimestampFamily.fractionalPrecision(dt1).getOrElse(6),
+      TimestampFamily.fractionalPrecision(dt2).getOrElse(6))
+    if (TimestampFamily.isLtz(dt1) && TimestampFamily.isLtz(dt2)) {
+      if (p <= 6) TimestampType else TimestampLTZNanosType(p)
+    } else {
+      if (p <= 6) TimestampNTZType else TimestampNTZNanosType(p)

Review Comment:
   Nearby findWiderDateTimeType uses MicrosPrecision = 6; 
subtractTimestampsCommonType hardcodes 6. Prefer the same named constant for 
consistency.



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