vinodkc commented on code in PR #58195:
URL: https://github.com/apache/spark/pull/58195#discussion_r4634913196


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/UnsafeRowUtils.scala:
##########
@@ -237,4 +237,35 @@ object UnsafeRowUtils {
       !st.supportsBinaryEquality
     case _ => false
   }
+
+  /**
+   * Returns an equality checker for grouping keys (UnsafeRows) of the given 
data types.
+   * If every type is binary-stable, equality is a plain binary comparison. 
Otherwise (e.g.
+   * a STRING with a non-binary collation), values that are semantically equal 
can have
+   * different binary representations, so the checker falls back to an 
ordering-based
+   * comparison that applies to all fields.
+   *
+   * `checkBytesFirst` controls whether that fallback compares the key bytes 
before the
+   * ordering, and is only observable for non-binary-stable keys: 
byte-identical rows
+   * compare equal under every collation and SQL ordering, so a byte-first 
check answers
+   * same-spelling keys with one byte comparison, while every check whose 
bytes differ
+   * pays a failed byte comparison before the same ordering comparison. Both 
forms group
+   * keys the same way, so the choice only moves cost between checks of 
identically
+   * spelled keys and all other checks; each call site's comment says why it 
passes the
+   * value it does.
+   */
+  def rowEqualityChecker(
+      dataTypes: Seq[DataType],
+      checkBytesFirst: Boolean): (UnsafeRow, UnsafeRow) => Boolean = {
+    if (dataTypes.forall(isBinaryStable)) {
+      (key1, key2) => key1.equals(key2)
+    } else {
+      val ordering = InterpretedOrdering.forSchema(dataTypes.toIndexedSeq)

Review Comment:
   Fixed (107ca58cd4c). rowEqualityChecker now builds the fallback for keys 
holding a CALENDAR INTERVAL with RowOrdering.createNaturalAscendingOrdering 
over the key types instead of InterpretedOrdering.forSchema, keeps the byte 
pre-check exactly as checkBytesFirst selects it and the InterpretedOrdering for 
every other key, and the Scaladoc states that keys holding a CALENDAR INTERVAL 
use the generated comparator because the interpreted ordering cannot compare 
intervals. Verified the regression empirically before fixing: the new SQL test 
failed on the unfixed checker with [DATATYPE_CANNOT_ORDER] Type 
PhysicalCalendarIntervalType does not support ordered operations, at its first 
query (the plain aggregate), before the one-DISTINCT and struct queries. New 
tests: session window aggregation groups collated keys holding a calendar 
interval (CollationSuite) covers the plain and one-DISTINCT plans over a 
UTF8_LCASE string and a make_interval value with rows that differ only in the 
interval, 
 the interval as the first grouping column, and the interval nested in a struct 
beside the collated string, for both checkBytesFirst-relevant iterators; a 
helper unit test in UnsafeRowUtilsSuite covers the checker for such a key in 
both checkBytesFirst forms: byte-identical keys and keys differing only in the 
string case report equal, keys whose intervals differ report unequal.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/window/WindowGroupLimitExec.scala:
##########
@@ -205,12 +206,34 @@ class GroupedLimitIterator(
 
   val grouping = UnsafeProjection.create(partitionSpec, output)
 
+  // A non-binary-stable key (e.g. a collated STRING) makes the checker fall 
back to an
+  // ordering-based comparison over all fields. Float and double keys arrive 
already
+  // normalized by NormalizeFloatingNumbers (-0.0 becomes 0.0, NaNs are 
canonical), so
+  // both branches group them the same way WindowExec does. The bytes are 
checked first,
+  // so a key spelled like its group's first row costs one byte comparison and 
only keys
+  // whose bytes differ pay the ordering comparison.
+  private val groupKeyEqualityCheck: (UnsafeRow, UnsafeRow) => Boolean =
+    UnsafeRowUtils.rowEqualityChecker(partitionSpec.map(_.dataType), 
checkBytesFirst = true)

Review Comment:
   Done (d595a084885). GroupedLimitIterator now builds one 
UnsafeRowKeyOperations over a StructType of the key types when some 
partition-key type is not binary-stable and 
UnsafeRowKeyOperations.supportsDataType admits every key type, and decides 
group membership with nextGroup.equals(currentGroup) || 
keyOperations.areEqual(nextGroup, currentGroup), the shape ObjectAggregationMap 
uses; binary-stable keys and any key type the key operations do not admit keep 
the shared checker, a single byte comparison, the remembered group-equality 
answer and every point that clears it are unchanged, and the comment above the 
check now says that this operator compares collated keys with the key equality 
to avoid the interpreted ordering work at each group boundary. New test 
WindowGroupLimitExec collation grouping with struct and array partition keys 
(CollationSuite): forwarded-row cases whose PARTITION BY key is a struct 
holding a UTF8_LCASE string with alternating spellings (with a second group 
diffe
 ring only in the non-collated struct field) and an array of collated strings 
with mixed spellings, asserting the Final node forwards one row per collation 
group for ROW_NUMBER with r <= 1 and comparing the answers against the same 
queries with the window group limit disabled; the existing direct-key and 
DOUBLE mixed-spec tests keep covering their shapes through the new branch.



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