Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ object AggregateEstimation {
(res, expr) => {
val columnStat = childStats.attributeStats(expr.asInstanceOf[Attribute])
val distinctCount = columnStat.distinctCount.get
val distinctValue: BigInt = if (distinctCount == 0 && columnStat.nullCount.get > 0) {
1
val distinctValue: BigInt = if (columnStat.nullCount.get > 0) {
distinctCount + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, actually, do we need to count null as distinct value? It's not counted as a distinct value in SQL (F.countDistinct or count(DISTINCT col)) and Pandas (unique() by default) at least.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into the current impl, seems we ignore null as distinct values:

val numNonNulls = if (col.nullable) Count(col) else Count(one)

ColumnStat(distinctCount = Some(0), min = None, max = None, nullCount = Some(rowCount),
avgLen = Some(dataType.defaultSize), maxLen = Some(dataType.defaultSize))

} else {
distinctCount
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class AggregateEstimationSuite extends StatsEstimationTestBase with PlanTest {
attr("key31") -> ColumnStat(distinctCount = Some(0), min = None, max = None,
nullCount = Some(0), avgLen = Some(4), maxLen = Some(4)),
attr("key32") -> ColumnStat(distinctCount = Some(0), min = None, max = None,
nullCount = Some(4), avgLen = Some(4), maxLen = Some(4))
nullCount = Some(4), avgLen = Some(4), maxLen = Some(4)),
attr("key33") -> ColumnStat(distinctCount = Some(2), min = None, max = None,
nullCount = Some(2), avgLen = Some(4), maxLen = Some(4))
))

private val nameToAttr: Map[String, Attribute] = columnInfo.map(kv => kv._1.name -> kv._1)
Expand Down Expand Up @@ -126,6 +128,15 @@ class AggregateEstimationSuite extends StatsEstimationTestBase with PlanTest {
expectedOutputRowCount = nameToColInfo("key22")._2.distinctCount.get)
}

test("group-by column with null value") {
checkAggStats(
tableColumns = Seq("key21", "key33"),
tableRowCount = 6,
groupByColumns = Seq("key21", "key33"),
expectedOutputRowCount = nameToColInfo("key21")._2.distinctCount.get *
(nameToColInfo("key33")._2.distinctCount.get + 1))
}

test("non-cbo estimation") {
val attributes = Seq("key12").map(nameToAttr)
val child = StatsTestPlan(
Expand Down