-
Notifications
You must be signed in to change notification settings - Fork 385
perf: fuse Comet cache vector reads into Spark codegen #5859
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
Open
peterxcli
wants to merge
23
commits into
apache:main
Choose a base branch
from
peterxcli:codex/cache-spark-consumer-benchmark
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.
+920
−95
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ec92ee5
perf: reduce Spark cache row conversion overhead
peterxcli 17dcdc6
chore: remove cache row reader benchmark results
peterxcli 8dc61ad
perf: feed cached Arrow columns into Spark codegen
peterxcli 091eb00
docs: illustrate Comet cache columnar rewrite
peterxcli eafdba5
fix: honor Comet disable switches for fused cache reads
peterxcli 49b7ec0
fix: align cache fusion with Spark codegen settings
peterxcli 03981e5
Merge branch 'main' into codex/cache-spark-consumer-benchmark
peterxcli ee0e24f
ci: retry after runner and dependency download failures
peterxcli 14b5d7b
Merge branch 'main' into codex/cache-spark-consumer-benchmark
peterxcli cea2caa
Merge upstream main into codex/cache-spark-consumer-benchmark
peterxcli eadfdff
Merge upstream main into codex/cache-spark-consumer-benchmark
peterxcli 64a25bc
Merge remote-tracking branch 'upstream/main' into HEAD
peterxcli dbfb182
Merge remote-tracking branch 'upstream/main' into HEAD
peterxcli 3aa6030
perf: split the generated cache row reader for wide projections
peterxcli 351503a
fix: do not fuse cache reads in plan-only mode
peterxcli d4ad3c4
test: check fused cache reads with Comet on and native execution off
peterxcli f6f245f
test: measure the fused cache reader in CometInMemoryCacheBenchmark
peterxcli 384ea00
docs: describe how Spark operators read Comet's cache format
peterxcli a150075
Merge remote-tracking branch 'apache/main' into HEAD
andygrove 4689053
Merge upstream main into codex/cache-spark-consumer-benchmark
peterxcli 869c5d2
fix: avoid numeric widening in cache benchmark
peterxcli 0da3690
ci: retry preflight after Maven Central download failure
peterxcli 73092ba
Merge remote-tracking branch 'upstream/main' into HEAD
peterxcli 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
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
100 changes: 100 additions & 0 deletions
100
spark/src/main/scala/org/apache/comet/rules/CometCacheColumnarRule.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,100 @@ | ||
| /* | ||
| * 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.rules | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.LeafExpression | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.comet.execution.arrow.ArrowCachedBatchSerializer | ||
| import org.apache.spark.sql.execution.{CodegenSupport, ColumnarToRowExec, ColumnarToRowTransition, SparkPlan, WholeStageCodegenExec} | ||
| import org.apache.spark.sql.execution.adaptive.QueryStageExec | ||
| import org.apache.spark.sql.execution.columnar.InMemoryTableScanExec | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| import org.apache.comet.CometConf.COMET_EXEC_IN_MEMORY_CACHE_ENABLED | ||
| import org.apache.comet.CometSparkSessionExtensions.isCometLoaded | ||
|
|
||
| /** | ||
| * Lets Spark's generated consumers read cached Arrow vectors without an intermediate UnsafeRow. | ||
| * | ||
| * Data flows upward. Spark's InputAdapter/whole-stage wrappers and an optional AQE cache stage | ||
| * are omitted: | ||
| * {{{ | ||
| * Before After | ||
| * +------------------------+ +------------------------+ | ||
| * | Spark codegen consumer | | Spark codegen consumer | | ||
| * +------------------------+ +------------------------+ | ||
| * ^ ^ | ||
| * | UnsafeRow | column values | ||
| * +------------------------+ +------------------------+ | ||
| * | InMemoryTableScanExec | | ColumnarToRowExec | | ||
| * | row iterator | | fused with consumer | | ||
| * +------------------------+ +------------------------+ | ||
| * ^ | ||
| * | ColumnarBatch | ||
| * +------------------------+ | ||
| * | InMemoryTableScanExec | | ||
| * | Arrow vectors | | ||
| * +------------------------+ | ||
| * }}} | ||
| * | ||
| * @param preview | ||
| * true in the plan-only preview, which shows the plan Comet would execute. Otherwise the rule | ||
| * leaves plans alone in plan-only mode, where Spark executes each query unchanged. | ||
| */ | ||
| case class CometCacheColumnarRule(preview: Boolean = false) extends Rule[SparkPlan] { | ||
| override def apply(plan: SparkPlan): SparkPlan = { | ||
| if (!isCometLoaded(conf) || !COMET_EXEC_IN_MEMORY_CACHE_ENABLED.get(conf)) return plan | ||
|
peterxcli marked this conversation as resolved.
|
||
| if (!preview && CometRule.planOnlyApplies(conf, plan)) return plan | ||
| if (!conf.wholeStageEnabled) return plan | ||
|
peterxcli marked this conversation as resolved.
|
||
| if (conf.getConf(SQLConf.CODEGEN_FACTORY_MODE).toString == "NO_CODEGEN") return plan | ||
|
|
||
| plan.transformUp { | ||
| case parent: CodegenSupport | ||
| if parent.supportCodegen && !parent.supportsColumnar && | ||
| !parent.isInstanceOf[ColumnarToRowTransition] && | ||
| !WholeStageCodegenExec.isTooManyFields(conf, parent.schema) && | ||
|
peterxcli marked this conversation as resolved.
|
||
| !parent.children.exists(p => WholeStageCodegenExec.isTooManyFields(conf, p.schema)) && | ||
| !parent.expressions.exists(_.exists { | ||
| case _: LeafExpression => false | ||
| case _: CodegenFallback => true | ||
| case _ => false | ||
| }) => | ||
| // Match the consuming edge rather than every scan: an existing columnar consumer (or a | ||
| // cache stage being materialized by AQE) must keep receiving batches. Spark inserts an | ||
| // InputAdapter around the scan later, while this transition fuses with the row consumer. | ||
| parent.withNewChildren(parent.children.map { | ||
| case child if isColumnarCometCache(child) => ColumnarToRowExec(child) | ||
| case child => child | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| private def isColumnarCometCache(plan: SparkPlan): Boolean = { | ||
| plan.supportsColumnar && (plan match { | ||
| case scan: InMemoryTableScanExec => | ||
| // The serializer delegates unsupported schemas to Spark, whose cache keeps its own reader. | ||
| scan.relation.cacheBuilder.serializer.isInstanceOf[ArrowCachedBatchSerializer] && | ||
| ArrowCachedBatchSerializer.supportsSchema(scan.relation.output) | ||
| case stage: QueryStageExec => isColumnarCometCache(stage.plan) | ||
| case _ => false | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.