-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental cost-based optimizer to avoid performance regressions
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
spark/src/main/scala/org/apache/comet/CometCostEvaluator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.comet | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.comet.{CometExec, CometRowToColumnarExec} | ||
import org.apache.spark.sql.execution.{ColumnarToRowExec, RowToColumnarExec, SparkPlan} | ||
import org.apache.spark.sql.execution.adaptive.{Cost, CostEvaluator, SimpleCost} | ||
|
||
/** | ||
* The goal of this cost model is to avoid introducing performance regressions in query stages | ||
* during AQE. | ||
* | ||
* This evaluator will be called twice; once for the original Spark plan and once for the Comet | ||
* plan. Spark will choose the cheapest plan. | ||
*/ | ||
class CometCostEvaluator extends CostEvaluator with Logging { | ||
|
||
/** Baseline cost for Spark operator is 1.0 */ | ||
val DEFAULT_SPARK_OPERATOR_COST = 1.0 | ||
|
||
/** Relative cost of Comet operator. This is intentionally conservative for now */ | ||
val DEFAULT_COMET_OPERATOR_COST = 0.8 | ||
|
||
/** Transitions are expensive */ | ||
val DEFAULT_TRANSITION_COST = 2.0 | ||
|
||
override def evaluateCost(plan: SparkPlan): Cost = { | ||
|
||
def computePlanCost(plan: SparkPlan): Double = { | ||
// TODO this is a crude prototype where we just penalize transitions, but | ||
// this can evolve into a true cost model where we have real numbers for the relative | ||
// performance of Comet operators & expressions versus the Spark versions | ||
// | ||
// Some areas to explore | ||
// - can we use statistics from previous query stage(s)? | ||
// - transition after filter should be cheaper than transition before filter (such as when | ||
// reading from Parquet followed by filter. Comet will filter first then transition) | ||
val childPlanCost = plan.children.map(computePlanCost).sum | ||
val operatorCost = plan match { | ||
case RowToColumnarExec(_) => DEFAULT_TRANSITION_COST | ||
case ColumnarToRowExec(_) => DEFAULT_TRANSITION_COST | ||
case CometRowToColumnarExec(_) => DEFAULT_TRANSITION_COST | ||
case _: CometExec => DEFAULT_COMET_OPERATOR_COST | ||
case _ => DEFAULT_SPARK_OPERATOR_COST | ||
} | ||
operatorCost + childPlanCost | ||
} | ||
|
||
// TODO can we access statistics from previous query stages? | ||
val estimatedRowCount = 1000 | ||
val cost = (computePlanCost(plan) * estimatedRowCount).toLong | ||
|
||
logWarning(s"Computed cost of $cost for $plan") | ||
|
||
SimpleCost(cost) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters