-
Notifications
You must be signed in to change notification settings - Fork 260
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 all 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
166 changes: 166 additions & 0 deletions
166
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,166 @@ | ||
| /* | ||
| * 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 scala.jdk.CollectionConverters._ | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| 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 | ||
| import org.apache.comet.serde.{ExprOuterClass, OperatorOuterClass} | ||
|
|
||
| case class CometCostEstimate(acceleration: Double) | ||
|
|
||
| trait CometCostModel { | ||
|
|
||
| /** Estimate the relative cost of one operator */ | ||
| def estimateCost(plan: SparkPlan): CometCostEstimate | ||
| } | ||
|
|
||
| class DefaultCometCostModel extends CometCostModel with Logging { | ||
|
|
||
| // optimistic default of 2x acceleration | ||
| private val defaultAcceleration = 2.0 | ||
|
|
||
| override def estimateCost(plan: SparkPlan): CometCostEstimate = { | ||
|
|
||
| logTrace(s"estimateCost for $plan") | ||
|
|
||
| // Walk the entire plan tree and accumulate costs | ||
| var totalAcceleration = 0.0 | ||
| var operatorCount = 0 | ||
|
|
||
| def collectOperatorCosts(node: SparkPlan): Unit = { | ||
| val operatorCost = estimateOperatorCost(node) | ||
| logTrace( | ||
| s"Operator: ${node.getClass.getSimpleName}, " + | ||
| s"Cost: ${operatorCost.acceleration}") | ||
| 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 | ||
| } | ||
|
|
||
| logTrace( | ||
| s"Plan: ${plan.getClass.getSimpleName}, Total operators: $operatorCount, " + | ||
| s"Average acceleration: $averageAcceleration") | ||
|
|
||
| CometCostEstimate(averageAcceleration) | ||
| } | ||
|
|
||
| /** Estimate the cost of a single operator */ | ||
| private def estimateOperatorCost(plan: SparkPlan): CometCostEstimate = { | ||
| val result = plan match { | ||
| case op: CometProjectExec => | ||
| logTrace("CometProjectExec found - evaluating expressions") | ||
| // Cast nativeOp to Operator and extract projection expressions | ||
| val operator = op.nativeOp.asInstanceOf[OperatorOuterClass.Operator] | ||
| val projection = operator.getProjection | ||
| val expressions = projection.getProjectListList.asScala | ||
| logTrace(s"Found ${expressions.length} expressions in projection") | ||
|
|
||
| val costs = expressions.map { expr => | ||
| val cost = estimateCometExpressionCost(expr) | ||
| logTrace(s"Expression cost: $cost") | ||
| cost | ||
| } | ||
| val total = costs.sum | ||
| val average = total / expressions.length.toDouble | ||
| logTrace(s"CometProjectExec total cost: $total, average: $average") | ||
| CometCostEstimate(average) | ||
|
|
||
| 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 _: CometPlan => | ||
| logTrace(s"Generic CometPlan: ${plan.getClass.getSimpleName}") | ||
| CometCostEstimate(defaultAcceleration) | ||
| case _ => | ||
| logTrace(s"Non-Comet operator: ${plan.getClass.getSimpleName}") | ||
| // Spark operator | ||
| CometCostEstimate(1.0) | ||
| } | ||
|
|
||
| logTrace(s"${plan.getClass.getSimpleName} -> acceleration: ${result.acceleration}") | ||
| result | ||
| } | ||
|
|
||
| /** Estimate the cost of a Comet protobuf expression */ | ||
| private def estimateCometExpressionCost(expr: ExprOuterClass.Expr): Double = { | ||
| val result = expr.getExprStructCase match { | ||
| // Handle specialized expression types | ||
| case ExprOuterClass.Expr.ExprStructCase.SUBSTRING => 6.3 | ||
|
|
||
| // Handle generic scalar functions | ||
| case ExprOuterClass.Expr.ExprStructCase.SCALARFUNC => | ||
| val funcName = expr.getScalarFunc.getFunc | ||
| funcName match { | ||
| // String expression numbers from CometStringExpressionBenchmark | ||
| case "ascii" => 0.6 | ||
| case "octet_length" => 0.6 | ||
| case "lower" => 3.0 | ||
| case "upper" => 3.0 | ||
| case "char" => 0.6 | ||
| case "initcap" => 0.9 | ||
| case "trim" => 0.4 | ||
| case "concat_ws" => 0.5 | ||
| case "length" => 9.1 | ||
| case "repeat" => 0.4 | ||
| case "reverse" => 6.9 | ||
| case "instr" => 0.6 | ||
| case "replace" => 1.3 | ||
| case "string_space" => 0.8 | ||
| case "translate" => 0.8 | ||
| case _ => defaultAcceleration | ||
| } | ||
|
|
||
| case _ => | ||
| logTrace( | ||
| s"Expression: Unknown type ${expr.getExprStructCase} -> " + | ||
| s"$defaultAcceleration") | ||
| defaultAcceleration | ||
| } | ||
| result | ||
| } | ||
|
|
||
| } | ||
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.
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.