vinodkc commented on code in PR #58195:
URL: https://github.com/apache/spark/pull/58195#discussion_r4620515572
##########
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:
Fixed (0ec456adf00). The session column is now matched by expression ID
rather than by attribute equality: restoreProj is a single projection over
join2(internalRow, mergedSession), keeping each non-session column at its own
ordinal and taking the session column from a BoundReference at the ordinal
appended after the row's columns, where the merged session sits, so a
subquery-qualified session attribute can no longer leave the merged session
unbound. Verified the regression empirically before fixing: the one-DISTINCT
query above returned three one-event sessions with a count of 1 each, and a
pandas grouped-aggregate UDF over the same shape returned three one-row groups.
New tests cover the subquery shape for a plain STRING key and a mixed-spelling
UTF8_LCASE key (CollationSuite), and for the pandas UDF
(test_pandas_udf_grouped_agg.py, shared mixin so it also runs in the Connect
parity suite).
One note on the binding: the merged session is read through a BoundReference
at a fixed appended ordinal rather than through an attribute with a fresh
expression ID, because the iterator is constructed inside mapPartitions on
executors, where NamedExpression.newExprId starts from 0 and could collide with
a driver-generated expression ID in inputSchema, silently binding the merged
session back to the row's own session column.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/UpdatingSessionsIterator.scala:
##########
@@ -184,29 +201,26 @@ class UpdatingSessionsIterator(
throw SparkException.internalError("The iterator must be sorted by key and
session start!")
}
- private val join = new JoinedRow
private val join2 = new JoinedRow
- private val valueProj = GenerateUnsafeProjection.generate(valuesExpressions,
inputSchema)
+ private val nonSessionProj =
+ GenerateUnsafeProjection.generate(nonSessionExpressions, inputSchema)
private val restoreProj = GenerateUnsafeProjection.generate(inputSchema,
- groupingWithoutSession.map(_.toAttribute) ++
Seq(sessionExpression.toAttribute) ++
- valuesExpressions.map(_.toAttribute))
-
- private def generateGroupingKey(): InternalRow = {
- val newRow = new
SpecificInternalRow(Seq(sessionExpression.toAttribute).toStructType)
- newRow.update(0, currentSession)
- join(currentKeys, newRow)
- }
+ nonSessionExpressions ++ Seq(sessionExpression.toAttribute))
private def closeCurrentSession(keyChanged: Boolean): Unit = {
returnRows = rowsForCurrentSession
rowsForCurrentSession = null
- val groupingKey = generateGroupingKey().copy()
+ // The merged session every row of this session is rewritten to.
Everything else in
+ // each row is projected from the row itself, so rows of a session that
mixes the
+ // spellings of a collated key keep their own spelling (see
nonSessionExpressions).
+ val mergedSession = new
SpecificInternalRow(Seq(sessionExpression.toAttribute).toStructType)
+ mergedSession.update(0, currentSession)
val currentRowsIter = returnRows.generateIterator().map { internalRow =>
- val valueRow = valueProj(internalRow)
- restoreProj(join2(groupingKey, valueRow)).copy()
+ val rowWithoutSession = nonSessionProj(internalRow)
Review Comment:
Done in the same commit (0ec456adf00) -- nonSessionProj and the
rowWithoutSession intermediate row are gone: restoreProj is one projection over
join2(internalRow, mergedSession) that keeps each non-session column at its own
ordinal and takes the session column, chosen by expression ID, from the
appended merged session, so each emitted row is written once, on the default
binary-stable path as well.
--
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]