maropu commented on a change in pull request #32494:
URL: https://github.com/apache/spark/pull/32494#discussion_r631505644



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala
##########
@@ -111,6 +111,44 @@ object UnionEstimation {
       AttributeMap.empty[ColumnStat]
     }
 

Review comment:
       The `estimate` method is a bit too long, so how about splitting it into 
smaller functions?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/FilterEstimation.scala
##########
@@ -225,7 +225,7 @@ case class FilterEstimation(plan: Filter) extends Logging {
       attr: Attribute,
       isNull: Boolean,
       update: Boolean): Option[Double] = {
-    if (!colStatsMap.contains(attr) || !colStatsMap(attr).hasCountStats) {
+    if (!colStatsMap.contains(attr) || colStatsMap(attr).nullCount.isEmpty) {

Review comment:
       This fix seems fine, but is that the possible state where 
`distinctCount.isDefined` is false and `nullCount.isDefined` is true?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala
##########
@@ -111,6 +111,44 @@ object UnionEstimation {
       AttributeMap.empty[ColumnStat]
     }
 
+    val attrToComputeNullCount = 
union.children.map(_.output).transpose.zipWithIndex.filter {
+      case (attrs, _) => attrs.zipWithIndex.forall {
+        case (attr, childIndex) =>
+          val attrStats = union.children(childIndex).stats.attributeStats
+          attrStats.get(attr).isDefined && attrStats(attr).nullCount.isDefined
+      }
+    }
+
+    val newAttrStats = if (attrToComputeNullCount.nonEmpty) {
+      val outputAttrStats = new ArrayBuffer[(Attribute, ColumnStat)]()
+      attrToComputeNullCount.foreach {
+        case (attrs, outputIndex) =>
+          val colWithNullStatValues = 
attrs.zipWithIndex.foldLeft[Option[BigInt]](None) {
+            case (totalNullCount, (attr, childIndex)) =>
+              val colStat = 
union.children(childIndex).stats.attributeStats(attr)
+              if (totalNullCount.isDefined) {

Review comment:
       `totalNullCount.isDefined` can be false?

##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/UnionEstimationSuite.scala
##########
@@ -188,7 +188,58 @@ class UnionEstimationSuite extends StatsEstimationTestBase 
{
 
     val union = Union(Seq(child1, child2))
 
-    val expectedStats = logical.Statistics(sizeInBytes = 2 * 1024, rowCount = 
Some(4))
+    // Only null count is present in the attribute stats
+    val expectedStats = logical.Statistics(
+      sizeInBytes = 2 * 1024,
+      rowCount = Some(4),
+      attributeStats = AttributeMap(
+        Seq(attrInt -> ColumnStat(nullCount = Some(0)))))
+    assert(union.stats === expectedStats)
+  }
+
+  test("col stats estimation when null count stats are not present for one 
child") {
+    val sz = Some(BigInt(1024))
+    val attrInt = AttributeReference("cint", IntegerType)()
+    val columnInfo = AttributeMap(
+      Seq(
+        attrInt -> ColumnStat(
+          distinctCount = Some(2),
+          min = Some(1),
+          max = Some(2),
+          nullCount = Some(2),
+          avgLen = Some(4),
+          maxLen = Some(4))))
+
+    // No null count
+    val columnInfo1 = AttributeMap(
+      Seq(
+        AttributeReference("cint1", IntegerType)() -> ColumnStat(
+          distinctCount = Some(2),
+          min = Some(3),
+          max = Some(4),
+          avgLen = Some(8),
+          maxLen = Some(8))))
+
+    val child1 = StatsTestPlan(
+      outputList = columnInfo.keys.toSeq,
+      rowCount = 2,
+      attributeStats = columnInfo,
+      size = sz)
+
+    val child2 = StatsTestPlan(
+      outputList = columnInfo1.keys.toSeq,
+      rowCount = 2,
+      attributeStats = columnInfo1,
+      size = sz)
+
+    val union = Union(Seq(child1, child2))
+
+    // Null count should not present in the stats
+    val expectedStats = logical.Statistics(
+      sizeInBytes = 2 * 1024,
+      rowCount = Some(4),
+      attributeStats = AttributeMap(
+          Seq(attrInt -> ColumnStat(min = Some(1), max = Some(4), nullCount = 
None))))

Review comment:
       nit: two idents?




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to