datafusion-ballista icon indicating copy to clipboard operation
datafusion-ballista copied to clipboard

AQE replan can swap a broadcast exchange onto the probe side of a CollectLeft join, pinning the join stage to one task

Open NoahKusaba opened this issue 2 months ago • 1 comments

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

Found in query plans from TPC-H q8 at SF1000 in #2419 (see the q8 analysis, "Stage 5 runs the whole join in a single task").

AQE plans a broadcast from estimated statistics. If the broadcast side then measures larger, the replan's join_selection swaps the CollectLeft join. The broadcast ExchangeExec lands on the probe side but still reports one partition, so the join stage runs as a single task. q8 stage 5 (Ballista 54.0.0, from q08.md in #2419) ran 79.9M input rows through one task:

- task duration (ms): min=7429 p25=7429 median=7429 p75=7429 max=7429
- task input rows: min=79913700 p25=79913700 median=79913700 p75=79913700 max=79913700

SortShuffleWriterExec: partitioning=Hash([l_orderkey@0], 256)
  HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(s_suppkey@0, l_suppkey@1)], projection=[l_orderkey@2, l_extendedprice@4, l_discount@5, s_nationkey@1]
    CoalescePartitionsExec
      ShuffleReaderExec: upstream_stage: 3, partitioning: UnknownPartitioning(256)
    ShuffleReaderExec: upstream_stage: 2, broadcast: true, upstream_partition_count: 256

The probe side (second child) is a broadcast: true reader, which reports a single partition.

Expected: the probe-side reader is partitioned (partitioning: UnknownPartitioning(256)), not broadcast: true, so stage 5 runs 256 tasks.

Describe the solution you'd like

Add a hook to DataFusion's ExecutionPlan, which join_selection calls on the input it moves to the probe side during a CollectLeft swap:

fn as_probe_side(&self) -> Result<Option<Arc<dyn ExecutionPlan>>> { Ok(None) }

Ballista implements it on ExchangeExec, so no extra optimizer rule is needed:

fn as_probe_side(&self) -> Result<Option<Arc<dyn ExecutionPlan>>> {
    if !self.broadcast || self.input.properties().output_ordering().is_some() {
        return Ok(None);
    }
    Ok(Some(Arc::new(Self::new_with_details(
        self.input.clone(), None, self.plan_id, self.stage_id.clone(),
        self.shuffle_partitions.clone(), self.coalesce.clone(),
        self.range_repartition_routing.clone(), false, self.inactive_stage,
    ))))
}

The fix happens at the swap itself, with no rule ordering to get right. It has been prototyped against DataFusion main and passes the probe-side tests in Ballista. It needs an upstream DataFusion PR and a Ballista upgrade to a release that includes it.

Describe alternatives you've considered

  • Ballista-only rule. Run a rule after join_selection that replaces a broadcast ExchangeExec on a CollectLeft probe side with a partitioned ExchangeExec over the same stage. It works on the current DataFusion, so it could bridge until the hook lands. However, it repairs the plan after the swap and depends on where it sits in the rule order. (Hacky)
  • Remove ExchangeExec's broadcast flag. Derive broadcast from the exchange's side of the join, so a swap can't misplace it. Cleanest, but a larger refactor.

Additional context

  • Null-aware anti joins (#2198) still swap; DistributedExchangeRule keeps their probe side coalesced. Broadcasts over ordered inputs are left unchanged.
  • Related: #2434, #2024, #2121.

NoahKusaba avatar Sep 14 '26 23:09 NoahKusaba

take

NoahKusaba avatar Sep 15 '26 03:09 NoahKusaba