LuciferYang commented on code in PR #60616:
URL: https://github.com/apache/spark/pull/60616#discussion_r4625206963


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/FilterEstimationSuite.scala:
##########
@@ -999,6 +1000,50 @@ class FilterEstimationSuite extends 
StatsEstimationTestBase {
       expectedRowCount = 9)
   }
 
+  test("SPARK-60830: predicates on a CHAR/VARCHAR column under a read-side 
check") {

Review Comment:
   Added. A new `firstClassRead` test helper builds the read under either conf: 
the check under standard semantics, `readSidePadding` for CHAR and the bare 
column for VARCHAR under `preserveCharVarcharTypeInfo`. The filter and project 
tests now run over all four shapes, and the join test runs the cast keys over 
them.
   



##########
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:
   Added the mirrored cases: the STRING key on the left joined with the cast 
key on the right, and the grouped side on the right against a side without 
column stats. Passing the raw right keys to either the key extraction or the 
uniqueness check now fails the test (estimates 100 and 10000).
   



##########
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:
   Added a `TODO(SPARK-61871)` next to the key extraction.
   



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