perf: send a native plan's output schema to the JVM only when it changes #7405
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #7405"
general
weight: 0.35
rich7420
Which issue does this PR close?
No issue is open for this. #1115 raised the cost of sending the schema with every batch for batches going from the JVM to native, and #4572 removed it there by moving that direction to the Arrow C Stream Interface. This PR removes it for batches going from native to the JVM.
Rationale for this change
executePlanhands each output batch to the JVM as oneArrowArrayand oneArrowSchemaper column. A plan's output schema rarely changes between batches, but native exported it with every batch, building each column's format string, name, metadata and, for a nested type, its children. The JVM then allocated a struct for it, parsed it back into aField, and made a JNI call to release it. Measured with the benchmark in this PR, that costs about 0.5 µs of CPU per flat column and 1.2 to 1.4 µs perstruct<bigint,string>column in every batch, on top of exporting and importing the arrays themselves.Exporting the output as an Arrow C Stream, the way #4572 handles input, would also send the schema once. But Arrow Java's
ArrowArrayStreamReaderloads every batch throughData.importIntoVectorSchemaRoot, which builds a temporaryStructVectorand then loads its buffers into a reused root. In a standalone measurement of both paths (arrow-rs 59.3, Arrow Java 18.3), that made the stream slower than the current path for single-column and nested outputs. A stream would also turn native errors intoIOExceptiontext, the problem #6243 worked around for input, and it would reuse one root for every batch, which changes who owns the batchesCometExecIteratorreturns. KeepingexecutePlanand dropping only the repeated schema leaves errors, metrics and batch ownership as they are.What changes are included in this PR?
jni_api.rs): anOutputSchemaCacheheld by theExecutionContextrecords each output column's data type and field metadata asexecutePlanlast exported them. These two are whatmove_to_sparkbuilds theFFI_ArrowSchemafrom. While a batch matches them,prepare_outputwrites only the arrays, with the newmove_array_to_spark, which still applieszero_offsets, and doesn't read the schema addresses. Otherwise it writes the schemas too, records the new ones, and setsOUTPUT_SCHEMA_EXPORTED(bit 32) in the row count it returns. A batch with a dictionary anywhere in its types always carries its schema and clears the record, because the JVM gives each dictionary it imports a new id and keeps the values under that id.CometExecIteratorfetches batches throughNativeUtil.getNextPlanBatch, which allocates theArrowSchemastructs once and passes the same, still empty, structs with every batch. When native sets the flag, it imports the batch asgetNextBatchdoes, which consumes the structs, and keeps the batch's fields unless one of them has a dictionary. Otherwise it imports the arrays with those fields through the newArrowImporter.importVector(array, field, provider), which the schema overload now delegates to. Both imports share one loop and its cleanup.close()frees the schema structs that are left. Decoded shuffle blocks still carry a schema each, throughgetNextBatch.spark.comet.exec.cacheOutputSchema.enabled, on by default, makes native send the schema with every batch when it is off. Only native reads it; the JVM follows the flag. It is there for the benchmark and as a fallback.ffi.mdandmemory_management.mddescribe the protocol.CometPlanOutputBenchmark, which compares the two settings. Each case reads one column of a Parquet table and projects it 1, 10 or 100 times, so the native plan does almost nothing per output column beyond exporting it, and the JVM only counts the rows of each batch. One more case reads 100 distinct columns. Before timing, the benchmark checks that both settings return every row, and that the plan sent its schema with every batch with the cache off and once per task with it on. Wall time leaves planning out. The benchmark also reports each setting's process CPU time per batch: for a plan with no JVM input the native plan runs on a Tokio worker while the task thread imports the batches, so wall time hides the saving until the JVM side is the slower one, but a busy executor has no idle core to hide it on.How are these changes tested?
New
NativeUtilSuitetests check that:close();New Rust tests cover the cache's sequence of exports (the first batch writes the schema, a matching one doesn't, a new type writes it again, a dictionary writes it every time, and no addresses write nothing), a disabled cache writing the schema every time,
move_array_to_sparkread back through a schema exported for an earlier array, the column comparison (type and metadata count, name and nullability don't), andhas_dictionaryon nested types.Locally,
NativeUtilSuite,CometExecIteratorLifecycleSuite,CometNativeSuite,SparkErrorConverterSuite,CometArrowStreamSuite,CometNativeShuffleSuiteandCometExecSuitepass (367 tests), and so doescargo clippy --all-targets --workspace -- -D warnings.CometPlanOutputBenchmarkon macOS (Apple M3 Max, JDK 17.0.10,local[1], an optimized native build without LTO). Wall time is the best of each case's runs, with the cache off, on, and off again to show drift. CPU is the median process CPU time per batch over seven alternating samples. Each case has 1025 batches of 8192 rows, except the last, which has 129.| Output columns | Off | On | Off (repeat) | CPU per batch, off | CPU per batch, on |
| --- | ---: | ---: | ---: | ---: | ---: |
| 100 ×
bigint, 1 column read | 218 ms | 172 ms | 215 ms | 287 µs | 239 µs || 100 ×
string, 1 column read | 257 ms | 196 ms | 248 ms | 385 µs | 337 µs || 100 ×
struct<bigint,string>, 1 column read | 623 ms | 494 ms | 621 ms | 808 µs | 667 µs || 100 distinct
bigint, 100 columns read | 474 ms | 475 ms | 473 ms | 4165 µs | 4137 µs |Earlier runs gave 1.20x to 1.28x for the first three rows. With 1 or 10 output columns the two settings are within run-to-run noise on this machine: the expected saving is 0.5 to 14 µs on batches that cost 55 to 245 µs of CPU. In the last row the saving, 29 µs per batch, is under 1% of decoding 100 columns.
To run it:
It runs on
local[1]unlessCOMET_BENCHMARK_MASTERsays otherwise.<details>
<summary>Full benchmark output</summary>
</details>