Github user cloud-fan commented on a diff in the pull request: https://github.com/apache/spark/pull/16228#discussion_r100303485 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala --- @@ -0,0 +1,314 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable + +import org.apache.spark.internal.Logging +import org.apache.spark.sql.catalyst.CatalystConf +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, AttributeReference, Expression} +import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Join, Statistics} +import org.apache.spark.sql.catalyst.plans.logical.statsEstimation.EstimationUtils._ +import org.apache.spark.sql.types.DataType + + +object JoinEstimation extends Logging { + /** + * Estimate statistics after join. Return `None` if the join type is not supported, or we don't + * have enough statistics for estimation. + */ + def estimate(conf: CatalystConf, join: Join): Option[Statistics] = { + join.joinType match { + case Inner | Cross | LeftOuter | RightOuter | FullOuter => + InnerOuterEstimation(conf, join).doEstimate() + case LeftSemi | LeftAnti => + LeftSemiAntiEstimation(conf, join).doEstimate() + case _ => + logDebug(s"[CBO] Unsupported join type: ${join.joinType}") + None + } + } +} + +case class InnerOuterEstimation(conf: CatalystConf, join: Join) extends Logging { + + private val leftStats = join.left.stats(conf) + private val rightStats = join.right.stats(conf) + + /** + * Estimate output size and number of rows after a join operator, and update output column stats. + */ + def doEstimate(): Option[Statistics] = join match { + case _ if !rowCountsExist(conf, join.left, join.right) => + None + + case ExtractEquiJoinKeys(joinType, leftKeys, rightKeys, condition, left, right) => + // 1. Compute join selectivity + val joinKeyPairs = extractJoinKeys(leftKeys, rightKeys) + val selectivity = joinSelectivity(joinKeyPairs, leftStats, rightStats) + + // 2. Estimate the number of output rows + val leftRows = leftStats.rowCount.get + val rightRows = rightStats.rowCount.get + val innerRows = ceil(BigDecimal(leftRows * rightRows) * selectivity) + + // Make sure outputRows won't be too small based on join type. + val outputRows = joinType match { + case LeftOuter => + // All rows from left side should be in the result. + leftRows.max(innerRows) + case RightOuter => + // All rows from right side should be in the result. + rightRows.max(innerRows) + case FullOuter => + // Simulate full outer join as obtaining the number of elements in the union of two + // finite sets: A \cup B = A + B - A \cap B => A FOJ B = A + B - A IJ B. + // But the "inner join" part can be much larger than A \cap B, making the simulated + // result much smaller. To prevent this, we choose the larger one between the simulated + // part and the inner part. + (leftRows + rightRows - innerRows).max(innerRows) + case _ => + // Don't change for inner or cross join + innerRows + } + + // 3. Update statistics based on the output of join + val intersectedStats = if (selectivity == 0) { + AttributeMap[ColumnStat](Nil) + } else { + updateIntersectedStats(joinKeyPairs, leftStats, rightStats) + } + val inputAttrStats = AttributeMap( + leftStats.attributeStats.toSeq ++ rightStats.attributeStats.toSeq) + val attributesWithStat = join.output.filter(a => inputAttrStats.contains(a)) + val (fromLeft, fromRight) = attributesWithStat.partition(join.left.outputSet.contains(_)) + val outputStats: Map[Attribute, ColumnStat] = join.joinType match { + case LeftOuter => + // Don't update column stats for attributes from left side. + fromLeft.map(a => (a, inputAttrStats(a))).toMap ++ + updateAttrStats(outputRows, fromRight, inputAttrStats, intersectedStats) + case RightOuter => + // Don't update column stats for attributes from right side. + updateAttrStats(outputRows, fromLeft, inputAttrStats, intersectedStats) ++ + fromRight.map(a => (a, inputAttrStats(a))).toMap + case FullOuter => + // Don't update column stats for attributes from both sides. + attributesWithStat.map(a => (a, inputAttrStats(a))).toMap + case _ => + // Update column stats from both sides for inner or cross join. + updateAttrStats(outputRows, attributesWithStat, inputAttrStats, intersectedStats) + } + val outputAttrStats = AttributeMap(outputStats.toSeq) + + Some(Statistics( + sizeInBytes = getOutputSize(join.output, outputRows, outputAttrStats), + rowCount = Some(outputRows), + attributeStats = outputAttrStats, + isBroadcastable = false)) + + case _ => + // When there is no equi-join condition, we do estimation like cartesian product. + val inputAttrStats = AttributeMap( + leftStats.attributeStats.toSeq ++ rightStats.attributeStats.toSeq) + // Propagate the original column stats + val outputAttrStats = getOutputMap(inputAttrStats, join.output) + val outputRows = leftStats.rowCount.get * rightStats.rowCount.get + Some(Statistics( + sizeInBytes = getOutputSize(join.output, outputRows, outputAttrStats), + rowCount = Some(outputRows), + attributeStats = outputAttrStats, + isBroadcastable = false)) + } + + // scalastyle:off + /** + * The number of rows of A inner join B on A.k1 = B.k1 is estimated by this basic formula: + * T(A IJ B) = T(A) * T(B) / max(V(A.k1), V(B.k1)), where V is the number of distinct values of + * that column. The underlying assumption for this formula is: each value of the smaller domain + * is included in the larger domain. + * Generally, inner join with multiple join keys can also be estimated based on the above + * formula: + * T(A IJ B) = T(A) * T(B) / (max(V(A.k1), V(B.k1)) * max(V(A.k2), V(B.k2)) * ... * max(V(A.kn), V(B.kn))) + * However, the denominator can become very large and excessively reduce the result, so we use a + * conservative strategy to take only the largest max(V(A.ki), V(B.ki)) as the denominator. + */ + // scalastyle:on + def joinSelectivity( + joinKeyPairs: Seq[(AttributeReference, AttributeReference)], + leftStats: Statistics, + rightStats: Statistics): BigDecimal = { + + var ndvDenom: BigInt = -1 + var i = 0 + while(i < joinKeyPairs.length && ndvDenom != 0) { + val (leftKey, rightKey) = joinKeyPairs(i) + // Do estimation if we have enough statistics + if (columnStatsExist((leftStats, leftKey), (rightStats, rightKey))) { + // Check if the two sides are disjoint + val leftKeyStats = leftStats.attributeStats(leftKey) + val rightKeyStats = rightStats.attributeStats(rightKey) + val lRange = Range(leftKeyStats.min, leftKeyStats.max, leftKey.dataType) + val rRange = Range(rightKeyStats.min, rightKeyStats.max, rightKey.dataType) + if (Range.isIntersected(lRange, rRange)) { + // Get the largest ndv among pairs of join keys + val maxNdv = leftKeyStats.distinctCount.max(rightKeyStats.distinctCount) + if (maxNdv > ndvDenom) ndvDenom = maxNdv + } else { + // Set ndvDenom to zero to indicate that this join should have no output + ndvDenom = 0 + } + } + i += 1 + } + + if (ndvDenom < 0) { + // There isn't join keys or column stats for any of the join key pairs, we do estimation like + // cartesian product. + 1 + } else if (ndvDenom == 0) { + // One of the join key pairs is disjoint, thus the two sides of join is disjoint. + 0 + } else { + 1 / BigDecimal(ndvDenom) + } + } + + /** Update column stats for output attributes. */ --- End diff -- can you explain the heuristic used in this method in the doc?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org