metanil commented on code in PR #59760:
URL: https://github.com/apache/spark/pull/59760#discussion_r4545810825
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/physical/partitioning.scala:
##########
@@ -2586,24 +2624,57 @@ case class KeyedShuffleSpec(
// `createPartitioning` replaces a transform's argument with the other
child's cluster key,
// which drops whatever surrounds the key, e.g. the `+ 1` of
`bucket(4, b + 1)` or the `.a`
// of `bucket(4, s.a)`. Such a spec's own child would then be laid out
differently from the
- // children shuffled onto it.
+ // children shuffled onto it. Whether Spark can evaluate it is
`canEvaluatePartitioning`.
case t: TransformExpression => argumentsAreColumns(t)
case _ => false
} &&
// Shuffling another child onto these keys evaluates the partition
expressions per row to
// decide where each row goes, and reduced keys are not what those
expressions compute.
partitioning.expressionsDescribeKeys
+ /** Whether Spark can evaluate each transform of this layout, see
`canEvaluate`. */
+ override lazy val canEvaluatePartitioning: Boolean =
partitioning.expressions.forall(canEvaluate)
+
override def createPartitioning(clustering: Seq[Expression]): Partitioning =
{
assert(clustering.size == distribution.clustering.size,
"Required distributions of join legs should be the same size.")
-
val newExpressions = partitioning.expressions.zip(keyPositions).map {
case (te: TransformExpression, positionSet) =>
// `canCreatePartitioning` admits only a template over bare columns,
see there.
rebuiltOver(te, clustering(positionSet.head))
case (_, positionSet) => clustering(positionSet.head)
}
+ // The child shuffled onto this layout is placed by evaluating what is
built here, which would
+ // fail on an executor. A caller has to ask `canEvaluatePartitioning`
first, which answers for
+ // this layout's own keys, so a caller that does not is a bug.
+ assert(canEvaluatePartitioning,
+ s"Cannot shuffle a child onto a layout Spark cannot evaluate:
${partitioning.expressions}")
+ // A join's keys share their types, apart from nullability and field
names, so that answers
+ // for what is built too. A Pandas or Arrow cogroup's need not
(SPARK-60782). Its other key can
+ // be one the layout's function does not take, e.g. a `BIGINT` against a
function declaring
+ // `INT`, or a string of another collation. Or one the function takes, but
stored otherwise than
+ // the layout's column, e.g. an `INT` widened to a `BIGINT` column's
declared type: every row is
+ // then placed, but the cogroup pairs its groups by comparing the two
sides' key rows, so they
+ // do not pair, or the cogroup fails. Falling back to a hash shuffle would
pair them wrongly
+ // too. So such a child fails here, while planning, with an error that
names the key's type.
+ // This covers transform positions only. An identity position is rebuilt
over the other key
+ // whatever its type, and a layout Spark cannot evaluate takes the hash
shuffle. Both behave as
+ // before this check, and are left to SPARK-60782.
+ partitioning.expressions.zip(keyPositions).zip(newExpressions).foreach {
+ case ((te: TransformExpression, positionSet), t: TransformExpression)
+ if !canEvaluate(t) || !positionSet.forall { p =>
+ TransformExpression.keysStoredAlike(clustering(p).dataType,
te.references.head.dataType)
+ } =>
+ // `name()` and `inputTypes()` are connector code, which must not
replace this error.
+ val functionClass = t.function.getClass.getName
+ val name = TransformExpression.orElse(t.function.name(), functionClass)
+ throw QueryExecutionErrors.storagePartitionJoinKeyTypeMismatchError(
+ t.numBucketsOpt.fold(name)(n => s"$name($n)"),
+ te.references.head.dataType,
+ TransformExpression.orElse(t.function.inputTypes().toSeq, Nil),
+ t.children.head)
Review Comment:
Good catch, done: the error now names the first key at the column's
positions that fails `keysStoredAlike`, and keeps `t.children.head` for the
not-evaluable case. Your `groupby("c", "c")` against `(x INT, y BIGINT)` shape
is pinned in `EnsureRequirementsSuite` ("the key type error names the cogroup
key that does not match"); it now names `y` (`BIGINT`).
--
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]