rich7420 commented on code in PR #7405:
URL: https://github.com/apache/datafusion-comet/pull/7405#discussion_r4705896504
##########
spark/src/test/scala/org/apache/comet/vector/NativeUtilSuite.scala:
##########
@@ -312,6 +316,212 @@ 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))
+ try {
+ val values = batches.map(batch => (0 until
batch.numRows()).map(batch.column(0).getInt))
+ assert(values == Seq(Seq(1, 2), Seq(3), Seq(4, 5, 6)))
+ assert(NativeUtil.planOutputSchemaImports.sum() - imports == 1)
+ // The batches without a schema were passed the same empty schema
structs.
+ assert(schemaAddrs(1) == schemaAddrs(2))
+ } finally {
+ batches.foreach(_.close())
+ }
+ // Only the kept schema structs remain, until close() frees them.
+ nativeUtil.close()
+ assert(allocator.getAllocatedMemory == 0)
+ }
+ }
+
+ test(
+ "getNextPlanBatch reads a batch sent without a schema with the latest
schema it was sent") {
+ withIsolatedStructAllocator { (nativeUtil, allocator, _) =>
+ def nextBatch(vector: FieldVector, rows: Int, withSchema: Boolean):
ColumnarBatch =
+ Using.resource(vector) { _ =>
+ nativeUtil
+ .getNextPlanBatch(
+ 1,
+ (arrays, schemas) => {
+ val schema = if (withSchema) ArrowSchema.wrap(schemas(0)) else
null
+ Data.exportVector(allocator, vector, null,
ArrowArray.wrap(arrays(0)), schema)
+ if (withSchema) rows.toLong |
NativeUtil.OUTPUT_SCHEMA_EXPORTED else rows.toLong
+ })
+ .get
+ }
+ def ints(values: Seq[Int]): IntVector = {
+ val vector = new IntVector("value", allocator)
+ vector.allocateNew(values.length)
+ values.indices.foreach(i => vector.setSafe(i, values(i)))
+ vector.setValueCount(values.length)
+ vector
+ }
+ def longs(values: Seq[Long]): BigIntVector = {
+ val vector = new BigIntVector("value", allocator)
+ vector.allocateNew(values.length)
+ values.indices.foreach(i => vector.setSafe(i, values(i)))
+ vector.setValueCount(values.length)
+ vector
+ }
+
+ // The schema changes from int to bigint, and the batch after it carries
none. It has to
+ // read as longs, which the first schema's fields would not give.
+ val batches = Seq(
+ nextBatch(ints(Seq(1, 2)), 2, withSchema = true),
+ nextBatch(longs(Seq(Int.MaxValue.toLong + 1, 4L)), 2, withSchema =
true),
+ nextBatch(longs(Seq(5L, 6L, 7L)), 3, withSchema = false))
+ try {
+ val types = batches.map(_.column(0).dataType())
+ assert(types == Seq(IntegerType, LongType, LongType))
+ assert((0 until 3).map(batches(2).column(0).getLong) == Seq(5L, 6L,
7L))
+ assert((0 until 2).map(batches(1).column(0).getLong) ==
Seq(Int.MaxValue.toLong + 1, 4L))
+ } finally {
+ batches.foreach(_.close())
+ }
+ nativeUtil.close()
+ assert(allocator.getAllocatedMemory == 0)
+ }
+ }
+
+ test("getNextPlanBatch releases the structs native filled before failing") {
+ withIsolatedStructAllocator { (nativeUtil, allocator, _) =>
+ Using.resource(new IntVector("value", allocator)) { vector =>
+ vector.allocateNew(1)
+ vector.setSafe(0, 42)
+ vector.setValueCount(1)
+ val expected = new IOException("native failed after exporting one
column")
+ val actual = intercept[IOException] {
+ nativeUtil.getNextPlanBatch(
+ 2,
+ (arrays, schemas) => {
+ Data.exportVector(
+ allocator,
+ vector,
+ null,
+ ArrowArray.wrap(arrays(0)),
+ ArrowSchema.wrap(schemas(0)))
+ throw expected
+ })
+ }
+ assert(actual eq expected)
+ }
+ assert(allocator.getAllocatedMemory == 0)
+ }
+ }
+
+ test("getNextPlanBatch keeps its schema structs past the end of the output
for close()") {
+ withIsolatedStructAllocator { (nativeUtil, allocator, _) =>
+ assert(nativeUtil.getNextPlanBatch(2, (_, _) => -1L).isEmpty)
+ assert(allocator.getAllocatedMemory > 0)
+ nativeUtil.close()
+ assert(allocator.getAllocatedMemory == 0)
+ }
+ }
+
+ test("getNextPlanBatch rejects a batch without a schema before any schema
was sent") {
+ withIsolatedStructAllocator { (nativeUtil, allocator, _) =>
+ Using.resource(new IntVector("value", allocator)) { vector =>
+ vector.allocateNew(1)
+ vector.setSafe(0, 42)
+ vector.setValueCount(1)
+ val failure = intercept[IllegalStateException] {
+ nativeUtil.getNextPlanBatch(
+ 1,
+ (arrays, _) => {
+ Data.exportVector(allocator, vector, null,
ArrowArray.wrap(arrays(0)))
+ 1L
+ })
+ }
+ assert(failure.getMessage.contains("without a schema"))
+ }
+ nativeUtil.close()
+ assert(allocator.getAllocatedMemory == 0)
+ }
+ }
+
+ test("a native plan sends its output schema with its first batch only") {
+ withParquetTable((0 until 1000).map(i => (i.toLong, i.toString, (i,
s"v$i"))), "t") {
+ Seq(true, false).foreach { cacheOutputSchema =>
+ withSQLConf(
+ CometConf.COMET_BATCH_SIZE.key -> "100",
Review Comment:
Could we use batch size `20`? Each task gets only two batches here, so this
still passes for me if native drops the cache after one reuse.
##########
docs/source/contributor-guide/ffi.md:
##########
@@ -217,29 +217,42 @@ When the JVM needs results from native execution:
### Transfer Process
-`CometExecIterator.hasNext` fetches each batch through
`NativeUtil.getNextBatch`, with one JNI call per batch:
+`CometExecIterator.hasNext` fetches each batch through
`NativeUtil.getNextPlanBatch`, with one JNI call per batch:
-1. `NativeUtil.getNextBatch` allocates one empty `ArrowArray`/`ArrowSchema`
pair per output column from
- `CometArrowAllocator` and passes their memory addresses to
- `Native.executePlan(stage, partition, plan, arrayAddrs, schemaAddrs)`.
+1. `getNextPlanBatch` allocates one empty `ArrowArray` per output column from
`CometArrowAllocator`, and passes
+ their memory addresses to `Native.executePlan(stage, partition, plan,
arrayAddrs, schemaAddrs)`, together with
+ those of one empty `ArrowSchema` per column, which it keeps across batches.
2. `executePlan` (in `jni_api.rs`) polls the native plan for its next
`RecordBatch`, and `prepare_output` exports
- each column into its pair with `move_to_spark` (in `execution/utils.rs`).
`move_to_spark` zeroes the column's
- offsets (see [Array Offsets](#array-offsets)), then writes an
`FFI_ArrowArray` over the result, and an
- `FFI_ArrowSchema` built from its data type and field metadata, into the
JVM-allocated structs. `executePlan`
- returns the batch's row count, or `-1` at the end of the output. With
`spark.comet.debug.enabled` set,
+ each column into its `ArrowArray` with `move_array_to_spark` (in
`execution/utils.rs`), which zeroes the
+ column's offsets (see [Array Offsets](#array-offsets)) and writes an
`FFI_ArrowArray` over the result. When the
+ batch carries its schema, `move_to_spark` also writes an `FFI_ArrowSchema`
built from the column's data type and
+ field metadata into its `ArrowSchema`. `executePlan` returns the batch's
row count, with `OUTPUT_SCHEMA_EXPORTED`
+ (bit 32) set when the schema went too, or `-1` at the end of the output.
With `spark.comet.debug.enabled` set,
`prepare_output` first runs `validate_full` on every column.
-3. At the end of the output, `NativeUtil` releases the unused structs.
Otherwise `NativeUtil.importVector` imports
- each column with `ArrowImporter.importVector`, wraps it with
`CometVector.getVector`, and returns the vectors as
- a `ColumnarBatch`.
+3. At the end of the output, `NativeUtil` releases the unused structs.
Otherwise `NativeUtil` imports each column
Review Comment:
This reads as if the schema structs are freed here too, but they stay until
`NativeUtil.close()`. Could the sentence say that?
--
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]