Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -217,7 +217,7 @@ object Union {
}

/**
* Logical plan for unioning two plans, without a distinct. This is UNION ALL in SQL.
* Logical plan for unioning multiple plans, without a distinct. This is UNION ALL in SQL.
*
* @param byName Whether resolves columns in the children by column names.
* @param allowMissingCol Allows missing columns in children query plans. If it is true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ object BasicStatsPlanVisitor extends LogicalPlanVisitor[Statistics] {

override def visitScriptTransform(p: ScriptTransformation): Statistics = default(p)

override def visitUnion(p: Union): Statistics = fallback(p)
override def visitUnion(p: Union): Statistics = {
val stats = p.children.map(_.stats)
val rowCount = if (stats.exists(_.rowCount.isEmpty)) {
None
} else {
Some(stats.map(_.rowCount.get).sum)
}
Statistics(sizeInBytes = stats.map(_.sizeInBytes).sum, rowCount = rowCount)
}
Comment on lines +82 to +90

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same logic, just add row count:

override def visitUnion(p: Union): Statistics = {
Statistics(sizeInBytes = p.children.map(_.stats.sizeInBytes).sum)
}


override def visitWindow(p: Window): Statistics = fallback(p)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ class BasicStatsEstimationSuite extends PlanTest with StatsEstimationTestBase {
expectedStatsCboOff = Statistics(sizeInBytes = 120))
}

test("SPARK-34031: Union operator missing rowCount when enable CBO") {
val union = Union(plan :: plan :: plan :: Nil)
val childrenSize = union.children.size
val sizeInBytes = plan.size.get * childrenSize
val rowCount = Some(plan.rowCount * childrenSize)
checkStats(union,
expectedStatsCboOn = Statistics(sizeInBytes = sizeInBytes, rowCount = rowCount),
expectedStatsCboOff = Statistics(sizeInBytes = sizeInBytes))
}

/** Check estimated stats when cbo is turned on/off. */
private def checkStats(
plan: LogicalPlan,
Expand Down