spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wzhfy <...@git.apache.org>
Subject [GitHub] spark pull request #16228: [SPARK-17076] [SQL] Cardinality estimation for jo...
Date Tue, 14 Feb 2017 01:03:33 GMT
Github user wzhfy commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16228#discussion_r100938944
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala
---
    @@ -0,0 +1,316 @@
    +/*
    + * 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 =>
    +          // T(A FOJ B) = T(A LOJ B) + T(A ROJ B) - T(A IJ B)
    +          leftRows.max(innerRows) + rightRows.max(innerRows) - 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) {
    --- End diff --
    
    yea good point, thanks


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message