Skip to content

Commit 67a7a29

Browse files
committed
Optimize maxRows and maxRowsPerPartition for join and union
1 parent a8a4a77 commit 67a7a29

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -580,39 +580,37 @@ case class Union(
580580
allowMissingCol: Boolean = false) extends UnionBase {
581581
assert(!allowMissingCol || byName, "`allowMissingCol` can be true only if `byName` is true.")
582582

583-
override def maxRows: Option[Long] = {
584-
var sum = BigInt(0)
585-
children.foreach { child =>
586-
if (child.maxRows.isDefined) {
587-
sum += child.maxRows.get
588-
if (!sum.isValidLong) {
589-
return None
583+
override lazy val maxRows: Option[Long] = {
584+
val sum = children.foldLeft(Option(BigInt(0))) {
585+
case (Some(acc), child) =>
586+
child.maxRows match {
587+
case Some(n) =>
588+
val newSum = acc + n
589+
if (newSum.isValidLong) Some(newSum) else None
590+
case None => None
590591
}
591-
} else {
592-
return None
593-
}
592+
case (None, _) => None
594593
}
595-
Some(sum.toLong)
594+
sum.map(_.toLong)
596595
}
597596

598597
final override val nodePatterns: Seq[TreePattern] = Seq(UNION)
599598

600599
/**
601600
* Note the definition has assumption about how union is implemented physically.
602601
*/
603-
override def maxRowsPerPartition: Option[Long] = {
604-
var sum = BigInt(0)
605-
children.foreach { child =>
606-
if (child.maxRowsPerPartition.isDefined) {
607-
sum += child.maxRowsPerPartition.get
608-
if (!sum.isValidLong) {
609-
return None
602+
override lazy val maxRowsPerPartition: Option[Long] = {
603+
val sum = children.foldLeft(Option(BigInt(0))) {
604+
case (Some(acc), child) =>
605+
child.maxRowsPerPartition match {
606+
case Some(n) =>
607+
val newSum = acc + n
608+
if (newSum.isValidLong) Some(newSum) else None
609+
case None => None
610610
}
611-
} else {
612-
return None
613-
}
611+
case (None, _) => None
614612
}
615-
Some(sum.toLong)
613+
sum.map(_.toLong)
616614
}
617615

618616
private def duplicatesResolvedPerBranch: Boolean =
@@ -666,7 +664,7 @@ case class Join(
666664
hint: JoinHint)
667665
extends BinaryNode with PredicateHelper {
668666

669-
override def maxRows: Option[Long] = {
667+
override lazy val maxRows: Option[Long] = {
670668
joinType match {
671669
case Inner | Cross | FullOuter | LeftOuter | RightOuter | LeftSingle
672670
if left.maxRows.isDefined && right.maxRows.isDefined =>

0 commit comments

Comments
 (0)