andygrove commented on code in PR #7405:
URL: https://github.com/apache/datafusion-comet/pull/7405#discussion_r4542227238
##########
spark/src/main/scala/org/apache/comet/vector/NativeUtil.scala:
##########
@@ -326,13 +443,36 @@ class NativeUtil extends AutoCloseable {
}
override def close(): Unit = {
- // closing the dictionary provider also closes the dictionary arrays
- dictionaryProvider.close()
+ val schemas = planOutputSchemas
+ planOutputSchemas = null
+ try {
+ if (schemas != null) releaseArrowStructs(Array.empty, schemas)
+ } finally {
+ // closing the dictionary provider also closes the dictionary arrays
+ dictionaryProvider.close()
+ }
}
}
object NativeUtil {
+ /**
+ * Set in what `Native.executePlan` returns when native filled the schema
structs as well as the
+ * arrays. The row count is in the low 32 bits. Matches
`OUTPUT_SCHEMA_EXPORTED` in
+ * `jni_api.rs`.
+ */
+ val OUTPUT_SCHEMA_EXPORTED: Long = 1L << 32
+
+ /**
+ * How many times [[NativeUtil.getNextPlanBatch]] has imported an output
schema in this JVM, so
+ * that tests and benchmarks can check that a plan sends it only when it
changes.
+ */
+ private[apache] val planOutputSchemaImports = new LongAdder()
+
+ /** Whether `field` or a field nested in it is dictionary-encoded. */
+ private def hasDictionary(field: Field): Boolean =
+ field.getDictionary != null ||
field.getChildren.asScala.exists(hasDictionary)
Review Comment:
Moved to `CometVectorUtils.hasDictionary`, and the Python runner uses it
too. Its private copy is gone.
##########
spark/src/test/scala/org/apache/comet/vector/NativeUtilSuite.scala:
##########
@@ -312,6 +316,163 @@ class NativeUtilSuite extends CometTestBase {
}
}
+ test("getNextPlanBatch imports batches sent without a schema with the fields
sent last") {
+ withIsolatedStructAllocator { (nativeUtil, allocator, _) =>
+ val schemaAddrs = mutable.ArrayBuffer.empty[Seq[Long]]
+ def nextBatch(values: Seq[Int], withSchema: Boolean): ColumnarBatch =
+ Using.resource(new IntVector("value", allocator)) { vector =>
+ vector.allocateNew(values.length)
+ values.indices.foreach(i => vector.setSafe(i, values(i)))
+ vector.setValueCount(values.length)
+ nativeUtil
+ .getNextPlanBatch(
+ 1,
+ (arrays, schemas) => {
+ schemaAddrs += schemas.toSeq
+ val schema = if (withSchema) ArrowSchema.wrap(schemas(0)) else
null
+ Data.exportVector(allocator, vector, null,
ArrowArray.wrap(arrays(0)), schema)
+ val rows = values.length.toLong
+ if (withSchema) rows | NativeUtil.OUTPUT_SCHEMA_EXPORTED else
rows
+ })
+ .get
+ }
+
+ val imports = NativeUtil.planOutputSchemaImports.sum()
+ val batches = Seq(
+ nextBatch(Seq(1, 2), withSchema = true),
+ nextBatch(Seq(3), withSchema = false),
+ nextBatch(Seq(4, 5, 6), withSchema = false))
Review Comment:
Added `getNextPlanBatch reads a batch sent without a schema with the latest
schema it was sent`. It sends an `IntVector` with its schema, then a
`BigIntVector` with its schema, then one more `BigIntVector` without one, and
checks that the batches read back as int, long and long with the right values.
When I made `getNextPlanBatch` keep only the first schema's fields, this test
failed and the others still passed.
##########
spark/src/test/scala/org/apache/spark/sql/benchmark/CometPlanOutputBenchmark.scala:
##########
@@ -0,0 +1,213 @@
+/*
+ * 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 java.io.File
+import java.lang.management.ManagementFactory
+
+import scala.collection.mutable
+
+import org.apache.spark.benchmark.Benchmark
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.comet.CometNativeExec
+import org.apache.spark.sql.execution.{QueryExecution, SQLExecution}
+import org.apache.spark.sql.internal.SQLConf
+
+import org.apache.comet.CometConf
+import org.apache.comet.vector.NativeUtil
+
+/**
+ * Benchmark to measure what it costs to move the output batches of a native
plan to the JVM, with
+ * the Arrow schema of the output sent with every batch or only when it changes
+ * (`spark.comet.exec.cacheOutputSchema.enabled`).
+ *
+ * Most cases read one column of a Parquet table and project it `n` times, so
the native plan does
+ * almost nothing for each of its `n` output columns beyond exporting it. The
last case reads `n`
+ * distinct columns, so the work of the scan grows with `n` as well. The JVM
only counts the rows
+ * of the batches it receives, as a JVM operator that passes batches through
would, so these
+ * results bound the effect on a query from above.
+ *
+ * To run this benchmark:
+ * {{{
+ * SPARK_GENERATE_BENCHMARK_FILES=1 make \
+ * benchmark-org.apache.spark.sql.benchmark.CometPlanOutputBenchmark
+ * }}}
+ * Results will be written to
"spark/benchmarks/CometPlanOutputBenchmark-**results.txt".
+ */
+object CometPlanOutputBenchmark extends CometBenchmarkBase {
+
+ private val narrowRows = 8 * 1024 * 1024
+ private val wideRows = 1024 * 1024
+ private val wideColumns = 100
+
+ override def runCometBenchmark(mainArgs: Array[String]): Unit = {
+ // Keep the plan a scan and a projection under the transition to rows.
+ spark.conf.set(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key, "false")
+ withTempPath { dir =>
+ withTempTable("narrow", "wide") {
+ writeTable(
+ dir,
+ "narrow",
+ spark.sql(
+ "SELECT id AS i, CAST(id AS STRING) AS s, " +
+ s"named_struct('a', id, 'b', CAST(id AS STRING)) AS st FROM
range($narrowRows)"))
+ writeTable(
+ dir,
+ "wide",
+ spark.sql(
+ (0 until wideColumns)
+ .map(c => s"id + $c AS w$c")
+ .mkString("SELECT ", ", ", s" FROM range($wideRows)")))
+
+ for {
+ (column, typeName) <- Seq(
+ "i" -> "bigint",
+ "s" -> "string",
+ "st" -> "struct<bigint,string>")
+ n <- Seq(1, 10, 100)
+ } {
+ val query =
+ (0 until n).map(c => s"$column AS c$c").mkString("SELECT ", ", ",
" FROM narrow")
+ runCases(s"$n $typeName output columns, 1 column read", narrowRows,
query)
Review Comment:
Fixed. The row counts are `Long` values and `tasks` is `.toLong`.
`test-compile -Pspark-3.5 -Pstrict-warnings` passes now.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]