spark-test commented on code in PR #60616:
URL: https://github.com/apache/spark/pull/60616#discussion_r4608414107
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/JoinEstimationSuite.scala:
##########
@@ -679,6 +679,52 @@ class JoinEstimationSuite extends StatsEstimationTestBase {
}
}
+ test("SPARK-60830: CHAR and VARCHAR join keys under read-side checks use
their stats") {
+ val stat = ColumnStat(distinctCount = Some(10), nullCount = Some(0),
avgLen = Some(1),
+ maxLen = Some(1))
+ val nullStat = ColumnStat(distinctCount = Some(0), nullCount = Some(10),
avgLen = Some(5),
+ maxLen = Some(5))
+ def checkedSide(key: AttributeReference, keyStat: ColumnStat): LogicalPlan
= Project(
+ Seq(Alias(readSideCheck(key), key.name)()),
+ StatsTestPlan(Seq(key), 10, AttributeMap(Seq(key -> keyStat))))
+ Seq(charAttr _, varcharAttr _).foreach { column =>
+ def joinRows(keyStat: ColumnStat): Option[BigInt] = {
+ val (left, right) = (checkedSide(column("l", 5), keyStat),
+ checkedSide(column("r", 5), keyStat))
+ Join(left, right, Inner, Some(EqualTo(left.output.head,
right.output.head)),
+ JoinHint.NONE).stats.rowCount
+ }
+ // 10 x 10 rows with 10 distinct values on each side.
+ assert(joinRows(stat).contains(BigInt(10)))
+ // Keys without non-null values match no rows.
+ assert(joinRows(nullStat).contains(BigInt(0)))
+
+ // Under first-class CHAR/VARCHAR types a key compared with a STRING key
is cast to it.
+ val left = checkedSide(column("l", 5), stat)
+ val s = AttributeReference("s", StringType)()
+ val right = StatsTestPlan(Seq(s), 10, AttributeMap(Seq(s -> stat)))
+ assert(Join(left, right, Inner, Some(EqualTo(Cast(left.output.head,
StringType), s)),
Review Comment:
**Nit (P3):** Every cast join key in these cases is the left key, and the
unique side is the left `grouped` plan; the end-to-end join in
`StatisticsCollectionSuite` also has the CHAR/VARCHAR table on the left of `ON
c = s`. `JoinEstimation` strips right keys in separate code
(`stripCastToPlainString(rk)` at `JoinEstimation.scala:360`, and
`rightKeys.map(stripCastToPlainString)` at `JoinEstimation.scala:64-65`), so
reverting either right-side strip would keep every new test green even though,
under either first-class conf, a join such as `strings JOIN t ON s = c` would
then fall back to the cartesian estimate or lose its uniqueness bound.
Could you mirror these cases with the CHAR/VARCHAR key on the right: the
STRING side on the left joined with `Cast(right.output.head, StringType)`, and
a right side grouped on that key against a left side without column stats?
**Verification:**
- **Behavior:** For CHAR and VARCHAR keys under the read-side check, a join
of two 10-row sides with the STRING key on the left and the cast key on the
right is estimated at 10 rows from the distinct counts, and with the right side
grouped on that key against a 1000-row left side without column statistics it
is estimated at 1000 rows.
- **Regression:** Each new case fails when the right-side key strip or the
right-side uniqueness strip in JoinEstimation is reverted while the left-side
strips stay.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala:
##########
@@ -354,7 +356,9 @@ case class JoinEstimation(join: Join) extends Logging {
private def extractJoinKeysWithColStats(
leftKeys: Seq[Expression],
rightKeys: Seq[Expression]): Seq[(AttributeReference,
AttributeReference)] = {
- leftKeys.zip(rightKeys).collect {
+ leftKeys.zip(rightKeys).map { case (lk, rk) =>
Review Comment:
**Nit (P3):** The PR description and the migration guide defer equi-joins
between CHAR or VARCHAR keys of different types to SPARK-61871, but nothing in
the code says so: a key under a cast to `VARCHAR(10)`, or under the comparison
padding of CHAR keys of different lengths, is still not an `AttributeReference`
after `stripCastToPlainString`, and the `collect` below drops the pair. With
the TODO(SPARK-60830) removed from `getAliasStats`, a reader would take this
coverage as final. How about a TODO next to the extraction:
```suggestion
// TODO(SPARK-61871): keys of different CHAR or VARCHAR types, which a
cast to a CHAR or
// VARCHAR type or the comparison padding of CHAR keys of different
lengths wraps, are not
// matched to their column statistics, so such joins keep their current
estimate.
leftKeys.zip(rightKeys).map { case (lk, rk) =>
```
**Verification:**
- **Inspection:** The join-key extraction in JoinEstimation carries a TODO
that names SPARK-61871 and the deferred keys of different CHAR or VARCHAR
types, including CHAR keys of different lengths, and no code behavior changes.
--
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]