andygrove opened a new issue, #7790:
URL: https://github.com/apache/datafusion-comet/issues/7790

   ## What is the problem?
   
   `executePlan` hands each output batch to the JVM as one `ArrowArray` and one 
`ArrowSchema` per column. A plan's output schema rarely changes between 
batches, but native builds each column's format string, name, metadata and, for 
a nested type, its children for every batch. The JVM then allocates a struct 
for it, parses it back into a `Field`, and makes a JNI call to release it.
   
   #1115 raised this cost for batches going from the JVM to native, and #4572 
removed it there by moving that direction to the Arrow C Stream Interface. 
Nothing has changed for batches going from native to the JVM.
   
   ## What is known
   
   #7405 sends the schema only when it changes, by recording each output 
column's type and field metadata in native and setting a flag bit in the 
returned row count when a schema is sent. It was closed unmerged to cut down 
the number of open PRs. The branch is `cache-native-output-schema` on 
`andygrove/datafusion-comet`, and it includes a `CometPlanOutputBenchmark` that 
can be reused. The results below come from that benchmark and are the only 
measurements so far.
   
   Measured with the benchmark, the per-batch cost is about 0.5 µs of CPU per 
flat column and 1.2 to 1.4 µs per `struct<bigint,string>` column, on top of 
exporting and importing the arrays themselves.
   
   | Case (1025 batches of 8192 rows, `local[1]`) | Wall time, schema every 
batch → cached | CPU per batch |
   | --- | --- | --- |
   | 100 output columns from one column read, `bigint` | 1.3x to 1.4x faster | 
17% lower |
   | 100 output columns from one column read, `string` | 1.3x faster | 12% 
lower |
   | 100 output columns from one column read, `struct<bigint,string>` | 1.3x 
faster | 18% lower |
   | 1 or 10 output columns | within noise | within noise |
   | 100 distinct `bigint` columns read from Parquet | under 1% | under 1% |
   
   These ran on an Apple M3 Max and on a Linux AMD Ryzen 9 7950X3D. The gain 
only shows where the plan does almost nothing per output column, so the schema 
work is a large share of the cost. When the plan decodes 100 different columns, 
the saving is lost in the decode.
   
   So the saving is real but small in absolute terms, and it is not known 
whether any real query reaches the case where it matters. That is why #7405 was 
not pushed to a merge.
   
   ## What needs research
   
   1. **Does it show up end to end?** Profile TPC-H and TPC-DS at a realistic 
scale, and at least one wide-table workload with hundreds of output columns, 
and measure how much CPU is spent building the schema in native and importing 
it in the JVM per batch. If it stays under about 1% of the task's CPU, the 
extra protocol (a flag bit in the row count, a schema record in `NativeUtil`) 
is probably not worth keeping.
   2. **Shuffle reads and small batches.** The cost is per batch, so it matters 
more when batches are small. Check queries that produce many small output 
batches, such as the output of a shuffle read or a highly selective filter.
   3. **Is an Arrow C Stream a better design?** #7405 chose to drop only the 
repeated schema because, when measured with arrow-rs 59.3 and Arrow Java 18.3, 
`ArrowArrayStreamReader` was slower than the current path for single-column and 
nested outputs. It builds a temporary `StructVector` for every batch through 
`Data.importIntoVectorSchemaRoot`. A stream would also turn native errors into 
`IOException` text, the problem #6243 worked around for input, and it would 
reuse one root for every batch, which changes who owns the batches 
`CometExecIterator` returns. The arrow-java 20.0.0 upgrade (#7664) may change 
the first measurement, so it is worth repeating.
   
   If the answer to the first question is that it matters, the work to finish 
is small:
   
   - Rebase `cache-native-output-schema` onto `main`. #6883 has merged since, 
and the branch already merged it and passes `None` for the schema cache in 
`LocalQueryIterator`'s path, but check it again.
   - Two review asks from the last round are not done. The test that checks the 
schema is sent once per task should use a batch size of 20, since with the 
current size each task gets only two batches and the test passes even if native 
drops the cache after one reuse. The sentence in `ffi.md` about freeing the 
schema structs should say they stay until `NativeUtil.close()`.
   


-- 
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]

Reply via email to