You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by ag...@apache.org on 2026/07/28 23:34:25 UTC
(datafusion-comet) branch main updated: test: add isolated and small-batch benchmarks for columnar-to-row conversion (#5113)
This is an automated email from the ASF dual-hosted git repository.
andygrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git
The following commit(s) were added to refs/heads/main by this push:
new cf81ab37a4 test: add isolated and small-batch benchmarks for columnar-to-row conversion (#5113)
cf81ab37a4 is described below
commit cf81ab37a45d2a423a4fef9692e444968e40eb2a
Author: Andy Grove <ag...@apache.org>
AuthorDate: Tue Jul 28 17:34:21 2026 -0600
test: add isolated and small-batch benchmarks for columnar-to-row conversion (#5113)
Add CometC2RIsolatedBench, which measures columnar-to-row conversion in
isolation on in-memory Arrow batches at batch sizes 8192, 512, and 32,
comparing the JVM rowIterator plus UnsafeProjection path against
NativeColumnarToRowConverter. The existing end-to-end benchmark is
dominated by the parquet scan and cannot observe conversion cost.
Also extend CometColumnarToRowBenchmark with two scenarios that the
existing groups do not cover: dictionary-encoded parquet input and a
Scala UDF consumer that defeats escape analysis, and disable ANSI mode
in its session so the data generation queries no longer fail with a
cast overflow on Spark 4.x.
Part of #5112
---
.../sql/benchmark/CometC2RIsolatedBench.scala | 106 +++++++++++++++++++++
.../benchmark/CometColumnarToRowBenchmark.scala | 73 ++++++++++++++
2 files changed, 179 insertions(+)
diff --git a/spark/src/test/scala/org/apache/spark/sql/benchmark/CometC2RIsolatedBench.scala b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometC2RIsolatedBench.scala
new file mode 100644
index 0000000000..ce2526406e
--- /dev/null
+++ b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometC2RIsolatedBench.scala
@@ -0,0 +1,106 @@
+/*
+ * 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.spark.sql.benchmark
+
+import org.apache.spark.benchmark.Benchmark
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.UnsafeProjection
+import org.apache.spark.sql.comet.execution.arrow.CometArrowConverters
+import org.apache.spark.sql.types._
+import org.apache.spark.sql.vectorized.ColumnarBatch
+import org.apache.spark.unsafe.types.UTF8String
+
+import org.apache.comet.NativeColumnarToRowConverter
+
+/**
+ * Isolated columnar-to-row microbenchmark that excludes the parquet scan entirely. Batches are
+ * built once in memory; each case converts them to rows and consumes the values so that the
+ * conversion itself is what gets measured.
+ */
+object CometC2RIsolatedBench {
+
+ private val totalRows = 1024 * 1024
+
+ private def makeBatches(schema: StructType, batchSize: Int): Array[ColumnarBatch] = {
+ val rows = (0 until totalRows).iterator.map { i =>
+ InternalRow(i.toLong, i, i.toDouble, UTF8String.fromString(s"value_$i"))
+ }
+ CometArrowConverters
+ .rowToArrowBatchIter(rows, schema, batchSize, "UTC", org.apache.comet.CometArrowAllocator)
+ .toArray
+ }
+
+ private def runForBatchSize(schema: StructType, batchSize: Int): Unit = {
+ val batches = makeBatches(schema, batchSize)
+
+ val benchmark =
+ new Benchmark(s"Isolated C2R (no scan), batchSize=$batchSize", totalRows.toLong)
+
+ benchmark.addCase("JVM rowIterator + UnsafeProjection") { _ =>
+ val proj = UnsafeProjection.create(schema.fields.map(_.dataType))
+ var sink = 0L
+ var b = 0
+ while (b < batches.length) {
+ val it = batches(b).rowIterator()
+ while (it.hasNext) {
+ val u = proj(it.next())
+ sink += u.getLong(0) + u.getUTF8String(3).numBytes()
+ }
+ b += 1
+ }
+ if (sink == Long.MinValue) println(sink)
+ }
+
+ benchmark.addCase("Native converter") { _ =>
+ val converter = new NativeColumnarToRowConverter(schema, batchSize)
+ var sink = 0L
+ try {
+ var b = 0
+ while (b < batches.length) {
+ val it = converter.convert(batches(b))
+ while (it.hasNext) {
+ val u = it.next()
+ sink += u.getLong(0) + u.getUTF8String(3).numBytes()
+ }
+ b += 1
+ }
+ } finally {
+ converter.close()
+ }
+ if (sink == Long.MinValue) println(sink)
+ }
+
+ benchmark.run()
+
+ batches.foreach(_.close())
+ }
+
+ def main(args: Array[String]): Unit = {
+ val schema = new StructType()
+ .add("a", LongType)
+ .add("b", IntegerType)
+ .add("c", DoubleType)
+ .add("d", StringType)
+
+ runForBatchSize(schema, 8192)
+ runForBatchSize(schema, 512)
+ runForBatchSize(schema, 32)
+ }
+}
diff --git a/spark/src/test/scala/org/apache/spark/sql/benchmark/CometColumnarToRowBenchmark.scala b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometColumnarToRowBenchmark.scala
index bcf5d73636..95eb44d1c9 100644
--- a/spark/src/test/scala/org/apache/spark/sql/benchmark/CometColumnarToRowBenchmark.scala
+++ b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometColumnarToRowBenchmark.scala
@@ -55,6 +55,7 @@ object CometColumnarToRowBenchmark extends CometBenchmarkBase {
.getOrCreate()
// Set default configs
+ sparkSession.conf.set(SQLConf.ANSI_ENABLED.key, "false")
sparkSession.conf.set(SQLConf.PARQUET_VECTORIZED_READER_ENABLED.key, "true")
sparkSession.conf.set(SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key, "true")
sparkSession.conf.set(CometConf.COMET_ENABLED.key, "false")
@@ -355,6 +356,70 @@ object CometColumnarToRowBenchmark extends CometBenchmarkBase {
}
}
+ /**
+ * Benchmark with dictionary-encoded parquet data (the session default disables dictionary
+ * encoding, but real-world parquet data is usually dictionary-encoded).
+ */
+ def dictionaryEncodedBenchmark(values: Int): Unit = {
+ val benchmark =
+ new Benchmark("Columnar to Row - Dictionary-encoded strings", values, output = output)
+
+ withTempPath { dir =>
+ withTempTable("parquetV1Table") {
+ val df = spark
+ .range(values)
+ .selectExpr(
+ "id",
+ "concat('val_', cast(id % 1000 as string)) as low_card_str",
+ "concat('city_', cast(id % 100 as string)) as city",
+ "concat('cat_', cast(id % 10 as string)) as category")
+
+ // Force dictionary encoding ON for this table only
+ df.write
+ .option("parquet.enable.dictionary", "true")
+ .option("compression", "snappy")
+ .parquet(dir.getCanonicalPath + "/parquetV1")
+ spark.read
+ .parquet(dir.getCanonicalPath + "/parquetV1")
+ .createOrReplaceTempView("parquetV1Table")
+
+ val query = "SELECT * FROM parquetV1Table"
+ addC2RBenchmarkCases(benchmark, query)
+ benchmark.run()
+ }
+ }
+ }
+
+ /**
+ * Benchmark where a JVM operator (Scala UDF projection + aggregate) consumes the rows. Unlike
+ * the noop() sink, this defeats escape analysis and exercises the WholeStageCodegen fusion
+ * asymmetry between the CodegenSupport JVM C2R and the iterator-based native C2R.
+ */
+ def jvmConsumerBenchmark(values: Int): Unit = {
+ val benchmark =
+ new Benchmark("Columnar to Row - JVM UDF consumer", values, output = output)
+
+ spark.udf.register("plus_one", (x: Long) => x + 1)
+
+ withTempPath { dir =>
+ withTempTable("parquetV1Table") {
+ val df = spark
+ .range(values)
+ .selectExpr(
+ "id as long_col",
+ "cast(id as int) as int_col",
+ "cast(id as double) as double_col",
+ "cast(id as string) as string_col")
+
+ prepareTable(dir, df)
+ val query =
+ "SELECT sum(plus_one(long_col)), min(string_col), sum(double_col) FROM parquetV1Table"
+ addC2RBenchmarkCases(benchmark, query)
+ benchmark.run()
+ }
+ }
+ }
+
override def runCometBenchmark(mainArgs: Array[String]): Unit = {
val numRows = 1024 * 1024 // 1M rows
@@ -389,5 +454,13 @@ object CometColumnarToRowBenchmark extends CometBenchmarkBase {
runBenchmark("Columnar to Row Conversion - Wide Rows") {
wideRowsBenchmark(numRows)
}
+
+ runBenchmark("Columnar to Row Conversion - Dictionary Encoded") {
+ dictionaryEncodedBenchmark(numRows)
+ }
+
+ runBenchmark("Columnar to Row Conversion - JVM UDF Consumer") {
+ jvmConsumerBenchmark(numRows)
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org