spark-test commented on code in PR #58195:
URL: https://github.com/apache/spark/pull/58195#discussion_r4623034493


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/UpdatingSessionsIterator.scala:
##########
@@ -51,13 +52,29 @@ class UpdatingSessionsIterator(
   private[this] val groupingWithoutSessionProjection: UnsafeProjection =
     UnsafeProjection.create(groupingWithoutSession, inputSchema)
 
-  private val valuesExpressions: Seq[Attribute] = 
inputSchema.diff(groupingExpressions)
+  // A non-binary-stable grouping key (e.g. a collated STRING) makes the 
checker fall back
+  // to an ordering-based comparison, so differently spelled collation-equal 
keys land in
+  // the same group; the bytes are checked first, so a key spelled like the 
session's
+  // first row costs one byte comparison and only keys whose bytes differ pay 
the ordering
+  // comparison. processedKeys below still hashes keys by bytes: once the 
group boundaries
+  // compare under the collation, a collation group cannot recur in correctly 
sorted
+  // input, so only a byte-identical key can hit the set.
+  private[this] val groupKeyEqualityCheck: (UnsafeRow, UnsafeRow) => Boolean =
+    UnsafeRowUtils.rowEqualityChecker(
+      groupingWithoutSession.map(_.dataType), checkBytesFirst = true)
+
+  // Every input column except the session column, in input order: each row of 
a closed
+  // session keeps its own values for these columns, and only its session 
column is
+  // replaced with the merged session. The rows of a session can spell a 
collated grouping
+  // key differently, and the DISTINCT-aggregate and pandas grouped-aggregate 
plans that
+  // consume this iterator read the grouping columns from the rows it emits.
+  private val nonSessionExpressions: Seq[Attribute] = 
inputSchema.diff(Seq(sessionExpression))

Review Comment:
   Thanks, the session column is now matched by expression ID, and the subquery 
shape is covered for both the one-DISTINCT and the pandas paths. On the note 
about the binding: an expression ID also records the JVM that created it, and 
ExprId equality compares that too (namedExpressions.scala, lines 51-56), so an 
ID created on an executor would not collide with one created on the driver. 
Reading the merged session through the appended bound ordinal is simpler either 
way, so nothing needs to change. This review raises one follow-up: the comment 
above the rewrite lists only the one-DISTINCT and pandas plans as consumers of 
this iterator and leaves out the Python UDAF aggregation plan.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4608864460","thread_id":"inline:4608864460","verdict_sha256":"cb46a826d260b518058cdeb485f83e6d3df5d63529a71bcaf216521e10daf3c5"}
 -->



##########
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. It 
pays off where
+   * most checks see keys spelled alike, such as rows compared against the 
first row of
+   * the current group; where that trade-off has not been measured for a site, 
pass
+   * `false` and compare collated keys with the ordering alone.

Review Comment:
   Thanks, the Scaladoc now states the trade-off and leaves each choice's 
reason to its call site. This review raises one follow-up: the reason the 
pandas window evaluator's comment gives for checking bytes first also describes 
WindowExec's identical partition loop, which skips the pre-check.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4608864469","thread_id":"inline:4608864469","verdict_sha256":"cb46a826d260b518058cdeb485f83e6d3df5d63529a71bcaf216521e10daf3c5"}
 -->



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