You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2026/10/14 15:52:43 UTC
[PR] perf: build an AQE cache that ends in a Spark operator straight into Arrow [datafusion-comet]
andygrove opened a new pull request, #6991:
URL: https://github.com/apache/datafusion-comet/pull/6991
## Which issue does this PR close?
Closes #6952.
## Rationale for this change
From Spark 4.0, when the cache serializer accepts columnar input, as Comet's does, Spark marks a cached `AdaptiveSparkPlanExec` columnar and AQE plans its final stage for columnar output. When that stage ends in a row operator, Spark puts a `RowToColumnarExec` on top. It fills Spark's own `OnHeapColumnVector`s, and Comet's serializer then copies those into Arrow. That's two copies where the row path, which Spark uses without AQE and on 3.x, makes one, and it happens whether or not Comet runs any part of the query.
## What changes are included in this PR?
- A new post-columnar rule, `CometCacheBuildRule`, replaces a `RowToColumnarExec` at the root of the plan with `CometSparkToColumnarExec`, which writes Arrow straight from the rows. The cache serializer then stores those batches as they are.
- Only a transition at the root is replaced. That is where Spark puts the one it adds for a stage's columnar output, and no other stage that Spark plans for columnar output has a row operator at its root: every other query is planned for row output, an exchange stage is rooted at its exchange, and Comet's dynamic pruning subquery is rooted at a broadcast exchange.
- The rule has the same guards as `CometCacheColumnarRule`. It runs when Comet is loaded and `spark.comet.exec.inMemoryCache.enabled` is on, and not in plan-only mode, so it also covers a session with Comet exec turned off. It also requires two more things. Comet's serializer has to be the installed `spark.sql.cache.serializer`, so that another serializer still gets the batches Spark planned for it. And the schema has to be one `CometSparkToColumnarExec` can convert. Otherwise Spark's transition stays.
- It runs after `EliminateRedundantTransitions` and before `CometCacheColumnarRule`.
## How are these changes tested?
Two new tests in `CometInMemoryCacheSuite`:
- "AQE builds a cache whose final stage ends in a Spark operator straight into Arrow" caches an AQE query whose final stage ends in a Spark `Project` over a native scan and shuffle. It checks that the cached final plan has `CometSparkToColumnarExec` at the root, with output batches, and no `RowToColumnarExec`. It also covers the cases that keep Spark's transition: Comet's cache turned off, plan-only mode, and an `array<int>` column, which `CometSparkToColumnarExec` does not convert. In every case the relation is stored in Comet's format and the rows match Spark's. The test runs on Spark 4.0+, because 3.x builds such a cache through rows.
- "CometCacheBuildRule converts a root transition only for Comet's cache serializer" applies the rule directly to a transition it builds. The transition is converted at the root, left alone below it, and left alone under a conf that names Spark's default serializer. The serializer is a static conf, and this suite pins it to Comet's, so a direct test is the only way to cover that guard.
Six mutants each fail one of the tests: not registering the rule, dropping the cache, plan-only, serializer or schema guard, and matching transitions below the root.
`CometInMemoryCacheSuite`, `CometInMemoryCachePruningSuite`, the three Kryo suites and `CachedBatchRowIteratorSuite` pass locally on Spark 3.4, 3.5, 4.0, 4.1 and 4.2. Scalafix and the strict-warnings build pass on 3.5.
I measured the cache build of #6952's query with a probe that isn't part of this PR. The query reads 5M rows of bigint, int, double and string columns and runs `repartition(8, $"a")` followed by a projection. Every arm ran in the same JVM, interleaved, nine builds each, on an Apple M3 Max with JDK 17, Spark 4.1 and a debug native library. The `main` arm turns `spark.comet.exec.inMemoryCache.enabled` off while the cache is built. That skips this rule and leaves the rest of the build the same.
| Final stage | `main` | This PR | Without AQE (one copy) |
| --- | ---: | ---: | ---: |
| Spark operators only (Comet exec and scan off) | 1018 ms | 868 ms | 864 ms |
| A Spark `Project` over a native scan and shuffle | 2808 ms | 2690 ms | |
With AQE on, a cache built by Spark operators now costs what the row path does.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: build an AQE cache that ends in a Spark operator straight into Arrow [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #6991:
URL: https://github.com/apache/datafusion-comet/pull/6991#issuecomment-6425251149
Thanks for the reviews @viirya!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: build an AQE cache that ends in a Spark operator straight into Arrow [datafusion-comet]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #6991:
URL: https://github.com/apache/datafusion-comet/pull/6991#discussion_r4304233291
##########
spark/src/main/scala/org/apache/comet/rules/CometCacheBuildRule.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 scala.collection.mutable.ListBuffer
+
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.comet.CometSparkToColumnarExec
+import org.apache.spark.sql.comet.execution.arrow.ArrowCachedBatchSerializer
+import org.apache.spark.sql.execution.{RowToColumnarExec, SparkPlan}
+import org.apache.spark.sql.internal.StaticSQLConf
+
+import org.apache.comet.CometConf.COMET_EXEC_IN_MEMORY_CACHE_ENABLED
+import org.apache.comet.CometSparkSessionExtensions.isCometLoaded
+
+/**
+ * Builds a cached adaptive plan that ends in a Spark row operator straight into Arrow.
+ *
+ * From Spark 4.0, when the cache serializer accepts columnar input, as Comet's does, Spark marks
+ * a cached `AdaptiveSparkPlanExec` columnar and AQE plans its final stage for columnar output. A
+ * final stage that ends in a row operator then gets a `RowToColumnarExec` on top, which fills
+ * Spark's own column vectors, and Comet's serializer copies those into Arrow before it writes
+ * them. `CometSparkToColumnarExec` writes Arrow straight from the rows, so the serializer stores
+ * its batches as they are, one copy instead of two.
+ *
+ * That `RowToColumnarExec` is the root of the plan these rules are handed. Spark plans no other
+ * stage for columnar output with a row operator at its root: every other query is planned for row
+ * output, an exchange stage is rooted at its exchange, and Comet's dynamic pruning subquery is
+ * rooted at a broadcast exchange. A transition below the root is left alone.
+ *
+ * Runs only when Comet's cache is enabled and its serializer is installed, so another serializer
+ * gets the batches Spark planned for it, and for schemas `CometSparkToColumnarExec` can convert.
+ *
+ * @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 CometCacheBuildRule(preview: Boolean = false) extends Rule[SparkPlan] {
+ override def apply(plan: SparkPlan): SparkPlan = plan match {
+ case transition @ RowToColumnarExec(child) if appliesTo(transition) =>
+ val conversion = CometSparkToColumnarExec(child)
Review Comment:
Spark's `RowToColumnarExec` and Comet's own row path (`convertInternalRowToCachedBatch`) both size cached batches by `spark.sql.inMemoryColumnarStorage.batchSize`. `CometSparkToColumnarExec` sizes them by `spark.comet.batchSize` instead. After this change the same `df.cache()` gets cut into different batches depending on whether AQE planned it. On this path a user's `inMemoryColumnarStorage.batchSize` is also silently ignored.
Batch size sets how fine-grained the cache's min/max pruning stats are, and how well each batch compresses. That's why `CometInMemoryCacheBenchmark` pins it to 10000 in every conf. Could the rule pass `conf.columnBatchSize` through to the conversion? One way is an optional batch-size parameter on `CometSparkToColumnarExec`. Then all three ways of building the cache would agree.
##########
spark/src/main/scala/org/apache/comet/rules/CometCacheBuildRule.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 scala.collection.mutable.ListBuffer
+
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.comet.CometSparkToColumnarExec
+import org.apache.spark.sql.comet.execution.arrow.ArrowCachedBatchSerializer
+import org.apache.spark.sql.execution.{RowToColumnarExec, SparkPlan}
+import org.apache.spark.sql.internal.StaticSQLConf
+
+import org.apache.comet.CometConf.COMET_EXEC_IN_MEMORY_CACHE_ENABLED
+import org.apache.comet.CometSparkSessionExtensions.isCometLoaded
+
+/**
+ * Builds a cached adaptive plan that ends in a Spark row operator straight into Arrow.
+ *
+ * From Spark 4.0, when the cache serializer accepts columnar input, as Comet's does, Spark marks
+ * a cached `AdaptiveSparkPlanExec` columnar and AQE plans its final stage for columnar output. A
+ * final stage that ends in a row operator then gets a `RowToColumnarExec` on top, which fills
+ * Spark's own column vectors, and Comet's serializer copies those into Arrow before it writes
+ * them. `CometSparkToColumnarExec` writes Arrow straight from the rows, so the serializer stores
+ * its batches as they are, one copy instead of two.
+ *
+ * That `RowToColumnarExec` is the root of the plan these rules are handed. Spark plans no other
+ * stage for columnar output with a row operator at its root: every other query is planned for row
+ * output, an exchange stage is rooted at its exchange, and Comet's dynamic pruning subquery is
+ * rooted at a broadcast exchange. A transition below the root is left alone.
+ *
+ * Runs only when Comet's cache is enabled and its serializer is installed, so another serializer
+ * gets the batches Spark planned for it, and for schemas `CometSparkToColumnarExec` can convert.
+ *
+ * @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 CometCacheBuildRule(preview: Boolean = false) extends Rule[SparkPlan] {
+ override def apply(plan: SparkPlan): SparkPlan = plan match {
+ case transition @ RowToColumnarExec(child) if appliesTo(transition) =>
+ val conversion = CometSparkToColumnarExec(child)
+ transition.logicalLink.foreach(conversion.setLogicalLink)
+ conversion
+ case _ => plan
+ }
+
+ private def appliesTo(transition: RowToColumnarExec): Boolean =
+ isCometLoaded(conf) &&
+ COMET_EXEC_IN_MEMORY_CACHE_ENABLED.get(conf) &&
+ (preview || !CometRule.planOnlyApplies(conf, transition)) &&
Review Comment:
I don't think the `preview` branch can ever match. `CometRule.buildPreview` inserts transitions with `ApplyColumnarRulesAndInsertTransitions(Seq.empty, outputsColumnar = false)`, and with `outputsColumnar = false` Spark never puts a `RowToColumnarExec` at the root. So the preview plan never reaches the `case` in `apply`.
Could we drop the `preview` parameter and its `@param` scaladoc, and keep only the `planOnlyApplies` guard? `preview` does matter in `CometCacheColumnarRule`, which transforms the whole tree. Keeping a copy here that has no effect suggests otherwise to the next reader.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: build an AQE cache that ends in a Spark operator straight into Arrow [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #6991:
URL: https://github.com/apache/datafusion-comet/pull/6991#discussion_r4377149805
##########
spark/src/main/scala/org/apache/comet/rules/CometCacheBuildRule.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 scala.collection.mutable.ListBuffer
+
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.comet.CometSparkToColumnarExec
+import org.apache.spark.sql.comet.execution.arrow.ArrowCachedBatchSerializer
+import org.apache.spark.sql.execution.{RowToColumnarExec, SparkPlan}
+import org.apache.spark.sql.internal.StaticSQLConf
+
+import org.apache.comet.CometConf.COMET_EXEC_IN_MEMORY_CACHE_ENABLED
+import org.apache.comet.CometSparkSessionExtensions.isCometLoaded
+
+/**
+ * Builds a cached adaptive plan that ends in a Spark row operator straight into Arrow.
+ *
+ * From Spark 4.0, when the cache serializer accepts columnar input, as Comet's does, Spark marks
+ * a cached `AdaptiveSparkPlanExec` columnar and AQE plans its final stage for columnar output. A
+ * final stage that ends in a row operator then gets a `RowToColumnarExec` on top, which fills
+ * Spark's own column vectors, and Comet's serializer copies those into Arrow before it writes
+ * them. `CometSparkToColumnarExec` writes Arrow straight from the rows, so the serializer stores
+ * its batches as they are, one copy instead of two.
+ *
+ * That `RowToColumnarExec` is the root of the plan these rules are handed. Spark plans no other
+ * stage for columnar output with a row operator at its root: every other query is planned for row
+ * output, an exchange stage is rooted at its exchange, and Comet's dynamic pruning subquery is
+ * rooted at a broadcast exchange. A transition below the root is left alone.
+ *
+ * Runs only when Comet's cache is enabled and its serializer is installed, so another serializer
+ * gets the batches Spark planned for it, and for schemas `CometSparkToColumnarExec` can convert.
+ *
+ * @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 CometCacheBuildRule(preview: Boolean = false) extends Rule[SparkPlan] {
+ override def apply(plan: SparkPlan): SparkPlan = plan match {
+ case transition @ RowToColumnarExec(child) if appliesTo(transition) =>
+ val conversion = CometSparkToColumnarExec(child)
Review Comment:
Good catch. The rule now passes `conf.columnBatchSize` to the conversion, through a new optional `batchSize` on `CometSparkToColumnarExec`. It still falls back to `spark.comet.batchSize` when unset. So Spark's transition, the serializer's row path and this path all cut at `spark.sql.inMemoryColumnarStorage.batchSize`. The AQE test sets it to 100 and checks that 1000 rows come out as at least 10 batches, and the direct rule test checks that it follows `columnBatchSize` and not `spark.comet.batchSize`.
##########
spark/src/main/scala/org/apache/comet/rules/CometCacheBuildRule.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 scala.collection.mutable.ListBuffer
+
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.comet.CometSparkToColumnarExec
+import org.apache.spark.sql.comet.execution.arrow.ArrowCachedBatchSerializer
+import org.apache.spark.sql.execution.{RowToColumnarExec, SparkPlan}
+import org.apache.spark.sql.internal.StaticSQLConf
+
+import org.apache.comet.CometConf.COMET_EXEC_IN_MEMORY_CACHE_ENABLED
+import org.apache.comet.CometSparkSessionExtensions.isCometLoaded
+
+/**
+ * Builds a cached adaptive plan that ends in a Spark row operator straight into Arrow.
+ *
+ * From Spark 4.0, when the cache serializer accepts columnar input, as Comet's does, Spark marks
+ * a cached `AdaptiveSparkPlanExec` columnar and AQE plans its final stage for columnar output. A
+ * final stage that ends in a row operator then gets a `RowToColumnarExec` on top, which fills
+ * Spark's own column vectors, and Comet's serializer copies those into Arrow before it writes
+ * them. `CometSparkToColumnarExec` writes Arrow straight from the rows, so the serializer stores
+ * its batches as they are, one copy instead of two.
+ *
+ * That `RowToColumnarExec` is the root of the plan these rules are handed. Spark plans no other
+ * stage for columnar output with a row operator at its root: every other query is planned for row
+ * output, an exchange stage is rooted at its exchange, and Comet's dynamic pruning subquery is
+ * rooted at a broadcast exchange. A transition below the root is left alone.
+ *
+ * Runs only when Comet's cache is enabled and its serializer is installed, so another serializer
+ * gets the batches Spark planned for it, and for schemas `CometSparkToColumnarExec` can convert.
+ *
+ * @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 CometCacheBuildRule(preview: Boolean = false) extends Rule[SparkPlan] {
+ override def apply(plan: SparkPlan): SparkPlan = plan match {
+ case transition @ RowToColumnarExec(child) if appliesTo(transition) =>
+ val conversion = CometSparkToColumnarExec(child)
+ transition.logicalLink.foreach(conversion.setLogicalLink)
+ conversion
+ case _ => plan
+ }
+
+ private def appliesTo(transition: RowToColumnarExec): Boolean =
+ isCometLoaded(conf) &&
+ COMET_EXEC_IN_MEMORY_CACHE_ENABLED.get(conf) &&
+ (preview || !CometRule.planOnlyApplies(conf, transition)) &&
Review Comment:
You're right. `buildPreview` inserts transitions with `outputsColumnar = false`, so no `RowToColumnarExec` can reach the root there. I dropped the `preview` parameter and its `@param`, and the rule keeps only the `planOnlyApplies` guard. The class doc now says why the preview needs no handling.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: build an AQE cache that ends in a Spark operator straight into Arrow [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #6991:
URL: https://github.com/apache/datafusion-comet/pull/6991
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: build an AQE cache that ends in a Spark operator straight into Arrow [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #6991:
URL: https://github.com/apache/datafusion-comet/pull/6991#issuecomment-6415908238
Added the build side to the doc string of `spark.comet.exec.inMemoryCache.enabled`: on Spark 4.0+ the setting also decides how an adaptive cached plan is built, so a final stage that ends in a Spark row operator is written straight into Arrow. I added the same note to the config table in `in-memory-cache.md`. All three are in 0ecb5f1c60.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org