Github user ioana-delaney commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17546#discussion_r110314588
  
    --- Diff: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/StarJoinCostBasedReorderSuite.scala
 ---
    @@ -0,0 +1,428 @@
    +/*
    + * 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.optimizer
    +
    +import org.apache.spark.sql.catalyst.dsl.expressions._
    +import org.apache.spark.sql.catalyst.dsl.plans._
    +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap}
    +import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest}
    +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, 
LogicalPlan}
    +import org.apache.spark.sql.catalyst.rules.RuleExecutor
    +import 
org.apache.spark.sql.catalyst.statsEstimation.{StatsEstimationTestBase, 
StatsTestPlan}
    +import org.apache.spark.sql.internal.SQLConf
    +import org.apache.spark.sql.internal.SQLConf._
    +
    +
    +class StarJoinCostBasedReorderSuite extends PlanTest with 
StatsEstimationTestBase {
    +
    +  override val conf = new SQLConf().copy(
    +    CASE_SENSITIVE -> true,
    +    CBO_ENABLED -> true,
    +    JOIN_REORDER_ENABLED -> true,
    +    STARSCHEMA_DETECTION -> true,
    +    JOIN_REORDER_DP_STAR_FILTER -> true)
    +
    +  object Optimize extends RuleExecutor[LogicalPlan] {
    +    val batches =
    +      Batch("Operator Optimizations", FixedPoint(100),
    +        CombineFilters,
    +        PushDownPredicate,
    +        ReorderJoin(conf),
    +        PushPredicateThroughJoin,
    +        ColumnPruning,
    +        CollapseProject) ::
    +        Batch("Join Reorder", Once,
    +          CostBasedJoinReorder(conf)) :: Nil
    +  }
    +
    +  private val columnInfo: AttributeMap[ColumnStat] = AttributeMap(Seq(
    +    // F1 (fact table)
    +    attr("f1_fk1") -> ColumnStat(distinctCount = 100, min = Some(1), max = 
Some(100),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("f1_fk2") -> ColumnStat(distinctCount = 100, min = Some(1), max = 
Some(100),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("f1_fk3") -> ColumnStat(distinctCount = 100, min = Some(1), max = 
Some(100),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("f1_c1") -> ColumnStat(distinctCount = 100, min = Some(1), max = 
Some(100),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("f1_c2") -> ColumnStat(distinctCount = 100, min = Some(1), max = 
Some(100),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +
    +    // D1 (dimension)
    +    attr("d1_pk") -> ColumnStat(distinctCount = 100, min = Some(1), max = 
Some(100),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("d1_c2") -> ColumnStat(distinctCount = 50, min = Some(1), max = 
Some(50),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("d1_c3") -> ColumnStat(distinctCount = 50, min = Some(1), max = 
Some(50),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +
    +    // D2 (dimension)
    +    attr("d2_pk") -> ColumnStat(distinctCount = 20, min = Some(1), max = 
Some(20),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("d2_c2") -> ColumnStat(distinctCount = 10, min = Some(1), max = 
Some(10),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("d2_c3") -> ColumnStat(distinctCount = 10, min = Some(1), max = 
Some(10),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +
    +    // D3 (dimension)
    +    attr("d3_pk") -> ColumnStat(distinctCount = 10, min = Some(1), max = 
Some(10),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("d3_c2") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +    attr("d3_c3") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 0, avgLen = 4, maxLen = 4),
    +
    +    // T1 (regular table i.e. outside star)
    +    attr("t1_c1") -> ColumnStat(distinctCount = 20, min = Some(1), max = 
Some(20),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t1_c2") -> ColumnStat(distinctCount = 10, min = Some(1), max = 
Some(10),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t1_c3") -> ColumnStat(distinctCount = 10, min = Some(1), max = 
Some(10),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +
    +    // T2 (regular table)
    +    attr("t2_c1") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t2_c2") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t2_c3") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +
    +    // T3 (regular table)
    +    attr("t3_c1") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t3_c2") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t3_c3") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +
    +    // T4 (regular table)
    +    attr("t4_c1") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t4_c2") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t4_c3") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +
    +    // T5 (regular table)
    +    attr("t5_c1") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t5_c2") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t5_c3") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +
    +    // T6 (regular table)
    +    attr("t6_c1") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t6_c2") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4),
    +    attr("t6_c3") -> ColumnStat(distinctCount = 5, min = Some(1), max = 
Some(5),
    +      nullCount = 1, avgLen = 4, maxLen = 4)
    +
    +  ))
    +
    +  private val nameToAttr: Map[String, Attribute] = columnInfo.map(kv => 
kv._1.name -> kv._1)
    +  private val nameToColInfo: Map[String, (Attribute, ColumnStat)] =
    +    columnInfo.map(kv => kv._1.name -> kv)
    +
    +  private val f1 = StatsTestPlan(
    +    outputList = Seq("f1_fk1", "f1_fk2", "f1_fk3", "f1_c1", 
"f1_c2").map(nameToAttr),
    +    rowCount = 1000,
    +    size = Some(1000 * (8 + 4 * 5)),
    +    attributeStats = AttributeMap(Seq("f1_fk1", "f1_fk2", "f1_fk3", 
"f1_c1", "f1_c2")
    +      .map(nameToColInfo)))
    +
    +  // To control the layout of the join plans, keep the size for the 
non-fact tables constant
    +  // and vary the rowcount and the number of distinct values of the join 
columns.
    +  private val d1 = StatsTestPlan(
    +    outputList = Seq("d1_pk", "d1_c2", "d1_c3").map(nameToAttr),
    +    rowCount = 100,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("d1_pk", "d1_c2", 
"d1_c3").map(nameToColInfo)))
    +
    +  private val d2 = StatsTestPlan(
    +    outputList = Seq("d2_pk", "d2_c2", "d2_c3").map(nameToAttr),
    +    rowCount = 20,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("d2_pk", "d2_c2", 
"d2_c3").map(nameToColInfo)))
    +
    +  private val d3 = StatsTestPlan(
    +    outputList = Seq("d3_pk", "d3_c2", "d3_c3").map(nameToAttr),
    +    rowCount = 10,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("d3_pk", "d3_c2", 
"d3_c3").map(nameToColInfo)))
    +
    +  private val t1 = StatsTestPlan(
    +    outputList = Seq("t1_c1", "t1_c2", "t1_c3").map(nameToAttr),
    +    rowCount = 50,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("t1_c1", "t1_c2", 
"t1_c3").map(nameToColInfo)))
    +
    +  private val t2 = StatsTestPlan(
    +    outputList = Seq("t2_c1", "t2_c2", "t2_c3").map(nameToAttr),
    +    rowCount = 10,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("t2_c1", "t2_c2", 
"t2_c3").map(nameToColInfo)))
    +
    +  private val t3 = StatsTestPlan(
    +    outputList = Seq("t3_c1", "t3_c2", "t3_c3").map(nameToAttr),
    +    rowCount = 10,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("t3_c1", "t3_c2", 
"t3_c3").map(nameToColInfo)))
    +
    +  private val t4 = StatsTestPlan(
    +    outputList = Seq("t4_c1", "t4_c2", "t4_c3").map(nameToAttr),
    +    rowCount = 10,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("t4_c1", "t4_c2", 
"t4_c3").map(nameToColInfo)))
    +
    +  private val t5 = StatsTestPlan(
    +    outputList = Seq("t5_c1", "t5_c2", "t5_c3").map(nameToAttr),
    +    rowCount = 10,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("t5_c1", "t5_c2", 
"t5_c3").map(nameToColInfo)))
    +
    +  private val t6 = StatsTestPlan(
    +    outputList = Seq("t6_c1", "t6_c2", "t6_c3").map(nameToAttr),
    +    rowCount = 10,
    +    size = Some(3000),
    +    attributeStats = AttributeMap(Seq("t6_c1", "t6_c2", 
"t6_c3").map(nameToColInfo)))
    +
    +  test("Test 1: Star query with two dimensions and two regular tables") {
    +
    +    // d1     t1
    +    //   \   /
    +    //    f1
    +    //   /  \
    +    // d2    t2
    +    //
    +    // star: {f1, d1, d2}
    +    // non-star: {t1, t2}
    +    //
    +    // level 0: (t2 ), (d2 ), (f1 ), (d1 ), (t1 )
    +    // level 1: {f1 d1 }, {d2 f1 }
    +    // level 2: {d2 f1 d1 }
    +    // level 3: {t2 d1 d2 f1 }, {t1 d1 d2 f1 }
    +    // level 4: {f1 t1 t2 d1 d2 }
    +    //
    +    // Number of generated plans: 11 (vs. 20 w/o filter)
    +    val query =
    +      f1.join(t1).join(t2).join(d1).join(d2)
    +        .where((nameToAttr("f1_c1") === nameToAttr("t1_c1")) &&
    +          (nameToAttr("f1_c2") === nameToAttr("t2_c1")) &&
    +          (nameToAttr("f1_fk1") === nameToAttr("d1_pk")) &&
    +          (nameToAttr("f1_fk2") === nameToAttr("d2_pk")))
    +
    +    val expected =
    --- End diff --
    
    @wzhfy We have the results presented in SPARK-17791. I didn't run TPC-DS 
with CBO enabled, but it is on my to-do list.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to