perf: avoid FFI import/export between native subtree and ShuffleWriter#4507
Open
mbutrovich wants to merge 16 commits into
Open
perf: avoid FFI import/export between native subtree and ShuffleWriter#4507mbutrovich wants to merge 16 commits into
mbutrovich wants to merge 16 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #3925. Replaces #3930.
Rationale for this change
When
CometShuffleExchangeExechas aCometNativeExecchild, the current code runs twoCometExecIterators per partition: one for the upstream subtree, one forScan("ShuffleWriterInput") -> ShuffleWriterconsuming the FFI'd batches back. The JVM doesn't read the data, so the round-trip is pure overhead.This PR collapses the two iterators into one
CometExecIteratorrooted atShuffleWriter(child = childNativeOp): no JVM intermediary, no Arrow FFI export/import, one fewercreatePlan/releasePlanpair per partition. The proto already supported this nested form.Why this replaces #3930
#3930 also removed the FFI but kept the two-iterator structure, passing batches between them through a JVM-side
u64-keyedMutex<HashMap<u64, RecordBatch>>. This PR achieves the same result without the global registry, the batch-handle protocol, or the second iterator: the child plan is just a child operator in the same DataFusion plan as the writer.What changes are included in this PR?
CometNativeExecexposesNativeExecContext(buildNativeContext()/executeColumnarWithContext(ctx)) so the shuffle path reusesdoExecuteColumnar's per-partition setup (broadcast alignment, plan-data injection, subqueries, encryption).CometNativeShuffleInputRDDis a thin scheduling-anchor RDD that resolves leaf partitions on the driver and carries per-partition leaf/shuffle-block iterators.NativeShuffleSpec(child native op, metric node, exec context) is carried byCometShuffleDependencyand only populated on native-child shuffles.CometShuffleExchangeExecdispatches: native-child shuffles useprepareNativeShuffleDependencyand the thin RDD; everything else uses the existing 5-argprepareShuffleDependencywith a synthesizedScan("ShuffleWriterInput")placeholder. Behavior unchanged forCometSparkToColumnarExec,CometCollectLimitExec,CometTakeOrderedAndProjectExec.CometNativeShuffleWriterrewritten to one code path overShuffleWriter(child = spec.childNativeOp).Removing the FFI boundary surfaces three issues the old deep-copy +
ScanExeccast was masking:width_bucket,date_trunc,collect_set, ...). NewSchemaAlignExecbetween the inlined child and the writer casts per column and widens nullability toactual.nullable || expected.nullable. Each distinct drift logs one process-deduped warning pointing at DataFusion / DataFusion-Spark functions whose Arrow return type drifts from Spark catalyst's declared type #4515. NewShuffleWriter.expected_output_schemaproto field.HashAggregateExec.resultExpressions(EXISTS / row-existence-only subqueries) where the JVM-declaredoutput=[]disagrees with the native aggregate's natural shape. Fixed inCometBaseAggregate.doConvert: when natural shape differs from declared shape, emit an explicitProjectionproto op above the HashAgg. NativeOpStruct::HashAggalways returns its natural shape.HashAggregate.result_exprs(field 3) andapply_result_projection(field 8) reserved.ShuffleWriterExec. With the writer now the root operator for any stage that ends in a shuffle, an upstreamDataFusionError::External(SparkError)(e.g. decimal overflow in ANSI mode) was being re-wrapped byexternal_shuffle'sArrowError::ExternalErroradapter, hiding the typed exception from the JNI bridge's single-level downcast. Drop the wrap;try_flattenalready handles the matchingDataFusionErrortypes directly.How are these changes tested?
Existing tests, plus:
CometAggregateSuiteregression test for the catalyst-pruned aggregate path. The original Spark SQL test that surfaced the aggregate bug (subquery/exists-subquery/exists-orderby-limit.sqlquery 19) is also covered by the SQL-tests harness CI.