-
Notifications
You must be signed in to change notification settings - Fork 265
feat: Proof-of-concept of cost-based optimization [WIP / Experimental] #2906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
andygrove
wants to merge
20
commits into
apache:main
Choose a base branch
from
andygrove:cost-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fe8cfdd
Add trait
andygrove f05a629
format
andygrove 6662c96
configs
andygrove 7f5996d
integrate with Spark AQE
andygrove fde7f35
walk plan
andygrove 3f964c6
Save
andygrove 95551f1
more
andygrove e3efad5
add TODO
andygrove 6417e54
fix
andygrove 7227f25
format
andygrove 927edc5
test
andygrove bc2ced7
test
andygrove 8adc080
test
andygrove 162ee47
test
andygrove 63eeebc
test
andygrove 14ff5a5
test
andygrove 304e9f7
remove AQE integration
andygrove a7acf9a
use configs
andygrove f60d72a
remove debug logging
andygrove 54e1a33
remove nonsense test
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
98 changes: 98 additions & 0 deletions
98
spark/src/main/scala/org/apache/comet/cost/CometCostEvaluator.scala
This file contains hidden or 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,98 @@ | ||
| /* | ||
| * 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.cost | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.execution.adaptive.{Cost, CostEvaluator} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| import org.apache.comet.CometConf | ||
|
|
||
| /** | ||
| * Simple Cost implementation for Comet cost evaluator. | ||
| */ | ||
| case class CometCost(value: Double) extends Cost { | ||
| override def compare(that: Cost): Int = that match { | ||
| case CometCost(thatValue) => java.lang.Double.compare(value, thatValue) | ||
| case _ => 0 // If we can't compare, assume equal | ||
| } | ||
|
|
||
| override def toString: String = s"CometCost($value)" | ||
| } | ||
|
|
||
| /** | ||
| * Comet implementation of Spark's CostEvaluator for adaptive query execution. | ||
| * | ||
| * This evaluator uses the configured CometCostModel to estimate costs for query plans, allowing | ||
| * Spark's adaptive query execution to make informed decisions about whether to use Comet or Spark | ||
| * operators based on estimated performance. | ||
| */ | ||
| class CometCostEvaluator extends CostEvaluator with Logging { | ||
|
|
||
| @transient private lazy val costModel: CometCostModel = { | ||
| val conf = SQLConf.get | ||
| val costModelClass = CometConf.COMET_COST_MODEL_CLASS.get(conf) | ||
|
|
||
| try { | ||
| // scalastyle:off classforname | ||
| val clazz = Class.forName(costModelClass) | ||
| // scalastyle:on classforname | ||
| val constructor = clazz.getConstructor() | ||
| constructor.newInstance().asInstanceOf[CometCostModel] | ||
| } catch { | ||
| case e: Exception => | ||
| logWarning( | ||
| s"Failed to instantiate cost model class '$costModelClass', " + | ||
| s"falling back to DefaultCometCostModel. Error: ${e.getMessage}") | ||
| new DefaultCometCostModel() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates the cost of executing the given SparkPlan. | ||
| * | ||
| * This method uses the configured CometCostModel to estimate the acceleration factor for the | ||
| * plan, then converts it to a Cost object that Spark's adaptive query execution can use for | ||
| * decision making. | ||
| * | ||
| * @param plan | ||
| * The SparkPlan to evaluate | ||
| * @return | ||
| * A Cost representing the estimated execution cost | ||
| */ | ||
| override def evaluateCost(plan: SparkPlan): Cost = { | ||
| val estimate = costModel.estimateCost(plan) | ||
|
|
||
| // Convert acceleration factor to cost | ||
| // Lower cost means better performance, so we use the inverse of acceleration factor | ||
| // For example: | ||
| // - 2.0x acceleration -> cost = 0.5 (half the cost) | ||
| // - 0.8x acceleration -> cost = 1.25 (25% more cost) | ||
| val costValue = 1.0 / estimate.acceleration | ||
|
|
||
| logDebug( | ||
| s"Cost evaluation for ${plan.getClass.getSimpleName}: " + | ||
| s"acceleration=${estimate.acceleration}, cost=$costValue") | ||
|
|
||
| // Create Cost object with the calculated value | ||
| CometCost(costValue) | ||
| } | ||
| } |
103 changes: 103 additions & 0 deletions
103
spark/src/main/scala/org/apache/comet/cost/CometCostModel.scala
This file contains hidden or 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,103 @@ | ||
| /* | ||
| * 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.cost | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{BinaryArithmetic, Expression} | ||
| import org.apache.spark.sql.comet.{CometColumnarToRowExec, CometPlan, CometProjectExec} | ||
| import org.apache.spark.sql.comet.execution.shuffle.{CometColumnarShuffle, CometNativeShuffle, CometShuffleExchangeExec} | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
|
|
||
| import org.apache.comet.DataTypeSupport | ||
|
|
||
| case class CometCostEstimate(acceleration: Double) | ||
|
|
||
| trait CometCostModel { | ||
|
|
||
| /** Estimate the relative cost of one operator */ | ||
| def estimateCost(plan: SparkPlan): CometCostEstimate | ||
| } | ||
|
|
||
| class DefaultCometCostModel extends CometCostModel { | ||
|
|
||
| // optimistic default of 2x acceleration | ||
| private val defaultAcceleration = 2.0 | ||
|
|
||
| override def estimateCost(plan: SparkPlan): CometCostEstimate = { | ||
| // Walk the entire plan tree and accumulate costs | ||
| var totalAcceleration = 0.0 | ||
| var operatorCount = 0 | ||
|
|
||
| def collectOperatorCosts(node: SparkPlan): Unit = { | ||
| val operatorCost = estimateOperatorCost(node) | ||
| totalAcceleration += operatorCost.acceleration | ||
| operatorCount += 1 | ||
|
|
||
| // Recursively process children | ||
| node.children.foreach(collectOperatorCosts) | ||
| } | ||
|
|
||
| collectOperatorCosts(plan) | ||
|
|
||
| // Calculate average acceleration across all operators | ||
| // This is crude but gives us a starting point | ||
| val averageAcceleration = if (operatorCount > 0) { | ||
| totalAcceleration / operatorCount.toDouble | ||
| } else { | ||
| 1.0 // No acceleration if no operators | ||
| } | ||
|
|
||
| CometCostEstimate(averageAcceleration) | ||
| } | ||
|
|
||
| /** Estimate the cost of a single operator */ | ||
| private def estimateOperatorCost(plan: SparkPlan): CometCostEstimate = { | ||
| plan match { | ||
| case op: CometShuffleExchangeExec => | ||
| op.shuffleType match { | ||
| case CometNativeShuffle => CometCostEstimate(1.5) | ||
| case CometColumnarShuffle => | ||
| if (DataTypeSupport.hasComplexTypes(op.schema)) { | ||
| CometCostEstimate(0.8) | ||
| } else { | ||
| CometCostEstimate(1.1) | ||
| } | ||
| } | ||
| case _: CometColumnarToRowExec => | ||
| CometCostEstimate(1.0) | ||
| case op: CometProjectExec => | ||
| val total: Double = op.expressions.map(estimateExpressionCost).sum | ||
| CometCostEstimate(total / op.expressions.length.toDouble) | ||
| case _: CometPlan => | ||
| CometCostEstimate(defaultAcceleration) | ||
| case _ => | ||
| // Spark operator | ||
| CometCostEstimate(1.0) | ||
| } | ||
| } | ||
|
|
||
| /** Estimate the cost of an expression */ | ||
| private def estimateExpressionCost(expr: Expression): Double = { | ||
| expr match { | ||
| case _: BinaryArithmetic => | ||
| 2.0 | ||
| case _ => defaultAcceleration | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we could remove usage of vars and let the function return the totalAcceleration and operator count itself ?
Something like :
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how much of this code will still exist by the time the proof-of-concept is working and ready for detailed code review, so I'll hold off from making these changes now.
I am really looking for high-level feedback on the general approach at the moment.