spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [spark] xianyinxin commented on a change in pull request #24983: [SPARK-27714][SQL][CBO] Support Genetic Algorithm based join reorder
Date Wed, 07 Aug 2019 03:06:51 GMT
xianyinxin commented on a change in pull request #24983: [SPARK-27714][SQL][CBO] Support Genetic
Algorithm based join reorder
URL: https://github.com/apache/spark/pull/24983#discussion_r311352726
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CostBasedJoinReorder.scala
 ##########
 @@ -470,3 +397,427 @@ object JoinReorderDPFilters extends PredicateHelper {
  * extended with the set of connected/unconnected plans.
  */
 case class JoinGraphInfo (starJoins: Set[Int], nonStarJoins: Set[Int])
+
+/**
+ * Reorder the joins using a genetic algorithm. The algorithm treat the reorder problem
+ * to a traveling salesmen problem, and use genetic algorithm give an optimized solution.
+ *
+ * The implementation refs the geqo in postgresql, which is contibuted by Darrell Whitley:
+ * https://www.postgresql.org/docs/9.1/geqo-pg-intro.html
+ *
+ * For more info about genetic algorithm and the edge recombination crossover, pls see:
+ * "A Genetic Algorithm Tutorial, Darrell Whitley"
+ * https://link.springer.com/article/10.1007/BF00175354
+ * and "Scheduling Problems and Traveling Salesmen: The Genetic Edge Recombination Operator,
+ * Darrell Whitley et al." https://dl.acm.org/citation.cfm?id=657238
+ * respectively.
+ */
+object JoinReorderGA extends PredicateHelper with Logging {
+
+  def search(
+      conf: SQLConf,
+      items: Seq[LogicalPlan],
+      conditions: Set[Expression],
+      output: Seq[Attribute]): Option[LogicalPlan] = {
+
+    val startTime = System.nanoTime()
+
+    val itemsWithIndex = items.zipWithIndex.map {
+      case (plan, id) => id -> JoinPlan(Set(id), plan, Set.empty, Cost(0, 0))
+    }.toMap
+
+    val topOutputSet = AttributeSet(output)
+
+    val pop = Population(conf, itemsWithIndex, conditions, topOutputSet).evolve
+
+    val durationInMs = (System.nanoTime() - startTime) / (1000 * 1000)
+    logInfo(s"Join reordering finished. Duration: $durationInMs ms, number of items: " +
+        s"${items.length}, number of plans in memo: ${ pop.chromos.size}")
+
+    assert(pop.chromos.head.basicPlans.size == items.length)
+    pop.chromos.head.integratedPlan match {
+      case Some(joinPlan) => joinPlan.plan match {
+        case p @ Project(projectList, _: Join) if projectList != output =>
+          assert(topOutputSet == p.outputSet)
+          // Keep the same order of final output attributes.
+          Some(p.copy(projectList = output))
+        case finalPlan if !sameOutput(finalPlan, output) =>
+          Some(Project(output, finalPlan))
+        case finalPlan =>
+          Some(finalPlan)
+      }
+      case _ => None
+    }
+  }
+}
+
+/**
+ * A pair of parent individuals can breed a child with certain crossover process.
+ * With crossover, child can inherit gene from its parents, and these gene snippets
+ * finally compose a new [[Chromosome]].
+ */
+@DeveloperApi
+trait Crossover {
+
+  /**
+   * Generate a new [[Chromosome]] from the given parent [[Chromosome]]s,
+   * with this crossover algorithm.
+   */
+  def newChromo(father: Chromosome, mother: Chromosome) : Chromosome
+}
+
+case class EdgeTable(table: Map[JoinPlan, Seq[JoinPlan]])
+
+/**
+ * This class implements the Genetic Edge Recombination algorithm.
+ * For more information about the Genetic Edge Recombination,
+ * see "Scheduling Problems and Traveling Salesmen: The Genetic Edge
+ * Recombination Operator" by Darrell Whitley et al.
+ * https://dl.acm.org/citation.cfm?id=657238
+ */
+object EdgeRecombination extends Crossover {
+
+  def genEdgeTable(father: Chromosome, mother: Chromosome) : EdgeTable = {
+    val fatherTable = father.basicPlans.map(g => g -> findNeighbours(father.basicPlans,
g)).toMap
+    val motherTable = mother.basicPlans.map(g => g -> findNeighbours(mother.basicPlans,
g)).toMap
+    EdgeTable(
+      fatherTable.map(entry => entry._1 -> (entry._2 ++ motherTable(entry._1))))
+  }
+
+  def findNeighbours(genes: Seq[JoinPlan], g: JoinPlan) : Seq[JoinPlan] = {
+    val genesIndexed = genes.toIndexedSeq
+    val index = genesIndexed.indexOf(g)
+    val length = genes.size
+    if (index > 0 && index < length - 1) {
+      Seq(genesIndexed(index - 1), genesIndexed(index + 1))
+    } else if (index == 0) {
+      Seq(genesIndexed(1), genesIndexed(length - 1))
+    } else if (index == length - 1) {
+      Seq(genesIndexed(0), genesIndexed(length - 2))
+    } else {
+      Seq()
+    }
+  }
+
+  override def newChromo(father: Chromosome, mother: Chromosome): Chromosome = {
+    var newGenes: Seq[JoinPlan] = Seq()
+    // 1. Generate the edge table.
+    var table = genEdgeTable(father, mother).table
+    // 2. Choose a start point randomly from the heads of father/mother.
+    var current =
+      if (util.Random.nextInt(2) == 0) father.basicPlans.head else mother.basicPlans.head
+    newGenes :+= current
+
+    var stop = false
+    while (!stop) {
+      // 3. Filter out the chosen point from the edge table.
+      table = table.map(
+        entry => entry._1 -> entry._2.filter(g => if (g == current) false else true)
 
 Review comment:
   Fixed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

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


Mime
View raw message