Skip to content

perf: avoid FFI import/export between native subtree and ShuffleWriter#4507

Open
mbutrovich wants to merge 16 commits into
apache:mainfrom
mbutrovich:opt_native_shuffle
Open

perf: avoid FFI import/export between native subtree and ShuffleWriter#4507
mbutrovich wants to merge 16 commits into
apache:mainfrom
mbutrovich:opt_native_shuffle

Conversation

@mbutrovich
Copy link
Copy Markdown
Contributor

@mbutrovich mbutrovich commented May 29, 2026

Which issue does this PR close?

Closes #3925. Replaces #3930.

Rationale for this change

When CometShuffleExchangeExec has a CometNativeExec child, the current code runs two CometExecIterators per partition: one for the upstream subtree, one for Scan("ShuffleWriterInput") -> ShuffleWriter consuming 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 CometExecIterator rooted at ShuffleWriter(child = childNativeOp): no JVM intermediary, no Arrow FFI export/import, one fewer createPlan/releasePlan pair 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-keyed Mutex<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?

  • CometNativeExec exposes NativeExecContext (buildNativeContext() / executeColumnarWithContext(ctx)) so the shuffle path reuses doExecuteColumnar's per-partition setup (broadcast alignment, plan-data injection, subqueries, encryption).
  • CometNativeShuffleInputRDD is 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 by CometShuffleDependency and only populated on native-child shuffles.
  • CometShuffleExchangeExec dispatches: native-child shuffles use prepareNativeShuffleDependency and the thin RDD; everything else uses the existing 5-arg prepareShuffleDependency with a synthesized Scan("ShuffleWriterInput") placeholder. Behavior unchanged for CometSparkToColumnarExec, CometCollectLimitExec, CometTakeOrderedAndProjectExec.
  • CometNativeShuffleWriter rewritten to one code path over ShuffleWriter(child = spec.childNativeOp).

Removing the FFI boundary surfaces three issues the old deep-copy + ScanExec cast was masking:

  • DataFusion-vs-catalyst Arrow type drift (width_bucket, date_trunc, collect_set, ...). New SchemaAlignExec between the inlined child and the writer casts per column and widens nullability to actual.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. New ShuffleWriter.expected_output_schema proto field.
  • Catalyst-pruned HashAggregateExec.resultExpressions (EXISTS / row-existence-only subqueries) where the JVM-declared output=[] disagrees with the native aggregate's natural shape. Fixed in CometBaseAggregate.doConvert: when natural shape differs from declared shape, emit an explicit Projection proto op above the HashAgg. Native OpStruct::HashAgg always returns its natural shape. HashAggregate.result_exprs (field 3) and apply_result_projection (field 8) reserved.
  • Typed exception loss through ShuffleWriterExec. With the writer now the root operator for any stage that ends in a shuffle, an upstream DataFusionError::External(SparkError) (e.g. decimal overflow in ANSI mode) was being re-wrapped by external_shuffle's ArrowError::ExternalError adapter, hiding the typed exception from the JNI bridge's single-level downcast. Drop the wrap; try_flatten already handles the matching DataFusionError types directly.

How are these changes tested?

Existing tests, plus:

  • New CometAggregateSuite regression test for the catalyst-pruned aggregate path. The original Spark SQL test that surfaced the aggregate bug (subquery/exists-subquery/exists-orderby-limit.sql query 19) is also covered by the SQL-tests harness CI.

@mbutrovich mbutrovich marked this pull request as ready for review May 29, 2026 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid FFI import/export when passing batches between two native plans

1 participant