-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
260 additions
and
5 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
121 changes: 121 additions & 0 deletions
121
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,121 @@ | ||
/* | ||
* 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, CometPlan, CometRowToColumnarExec, CometSinkPlaceHolder} | ||
import org.apache.spark.sql.execution.{ColumnarToRowExec, InputAdapter, RowToColumnarExec, SparkPlan, WholeStageCodegenExec} | ||
import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, Cost, CostEvaluator, QueryStageExec, 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 */ | ||
val DEFAULT_COMET_OPERATOR_COST = 0.8 | ||
|
||
/** Relative cost of a transition (C2R, R2C) */ | ||
val DEFAULT_TRANSITION_COST = 1.0 | ||
|
||
override def evaluateCost(plan: SparkPlan): Cost = { | ||
|
||
// 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) | ||
def computePlanCost(plan: SparkPlan): Double = { | ||
|
||
// get children even for leaf nodes at query stage edges | ||
def getChildren(plan: SparkPlan) = plan match { | ||
case a: AdaptiveSparkPlanExec => Seq(a.inputPlan) | ||
case qs: QueryStageExec => Seq(qs.plan) | ||
case p => p.children | ||
} | ||
|
||
val children = getChildren(plan) | ||
val childPlanCost = children.map(computePlanCost).sum | ||
val operatorCost = plan match { | ||
case _: AdaptiveSparkPlanExec => 0 | ||
case _: CometSinkPlaceHolder => 0 | ||
case _: InputAdapter => 0 | ||
case _: WholeStageCodegenExec => 0 | ||
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 | ||
} | ||
|
||
def isSparkNative(plan: SparkPlan): Boolean = plan match { | ||
case p: AdaptiveSparkPlanExec => isSparkNative(p.inputPlan) | ||
case p: QueryStageExec => isSparkNative(p.plan) | ||
case _: CometPlan => false | ||
case _ => true | ||
} | ||
|
||
def isCometNative(plan: SparkPlan): Boolean = plan match { | ||
case p: AdaptiveSparkPlanExec => isCometNative(p.inputPlan) | ||
case p: QueryStageExec => isCometNative(p.plan) | ||
case _: CometPlan => true | ||
case _ => false | ||
} | ||
|
||
def isTransition(plan1: SparkPlan, plan2: SparkPlan) = { | ||
(isSparkNative(plan1) && isCometNative(plan2)) || | ||
(isCometNative(plan1) && isSparkNative(plan2)) | ||
} | ||
|
||
val transitionCost = if (children.exists(ch => isTransition(plan, ch))) { | ||
DEFAULT_TRANSITION_COST | ||
} else { | ||
0 | ||
} | ||
|
||
|
||
val totalCost = operatorCost + transitionCost + childPlanCost | ||
|
||
logWarning(s"total cost is $totalCost ($operatorCost + $transitionCost + $childPlanCost) " + | ||
s"for ${plan.nodeName}") | ||
|
||
totalCost | ||
} | ||
|
||
// 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
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
67 changes: 67 additions & 0 deletions
67
spark/src/test/scala/org/apache/comet/CostBasedOptimizerSuite.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,67 @@ | ||
/* | ||
* 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.sql.CometTestBase | ||
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
||
class CostBasedOptimizerSuite extends CometTestBase with AdaptiveSparkPlanHelper { | ||
|
||
private val dataGen = DataGenerator.DEFAULT | ||
|
||
test("tbd") { | ||
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
SQLConf.COALESCE_PARTITIONS_ENABLED.key -> "false", | ||
CometConf.COMET_ENABLED.key -> "true", | ||
CometConf.COMET_EXEC_ENABLED.key -> "true", | ||
CometConf.COMET_EXEC_SHUFFLE_ENABLED.key -> "true", | ||
CometConf.COMET_CBO_ENABLED.key -> "true", | ||
CometConf.COMET_EXPLAIN_FALLBACK_ENABLED.key -> "true") { | ||
val table = "t1" | ||
withTable(table, "t2") { | ||
sql(s"create table t1(col string, a int, b float) using parquet") | ||
sql(s"create table t2(col string, a int, b float) using parquet") | ||
val tableSchema = spark.table(table).schema | ||
val rows = dataGen.generateRows( | ||
1000, | ||
tableSchema, | ||
Some(() => dataGen.generateString("tbd:", 6))) | ||
val data = spark.createDataFrame(spark.sparkContext.parallelize(rows), tableSchema) | ||
data.write | ||
.mode("append") | ||
.insertInto(table) | ||
data.write | ||
.mode("append") | ||
.insertInto("t2") | ||
val x = checkSparkAnswer/*AndOperator*/("select t1.col as x " + | ||
"from t1 join t2 on cast(t1.col as timestamp) = cast(t2.col as timestamp) " + | ||
"order by x") | ||
|
||
// TODO assert that we fell back for whole plan | ||
println(x._1) | ||
println(x._2) | ||
|
||
assert(!x._2.toString().contains("CometSortExec")) | ||
} | ||
} | ||
} | ||
|
||
} |