Skip to content

SortMergeJoinExec panics instead of returning an error when its output coalescer rejects a batch #27728

Backlog

Description

@mbutrovich

Describe the bug

MaterializingSortMergeJoinStream calls .expect("Failed to push output batch") on BatchCoalescer::push_batch in on_children_exhausted and in stage_filtered_output. push_batch returns an error when it cannot concatenate the batches it holds, so the join panics where it should return that error. Both functions return Result, so ? would propagate it.

To Reproduce

A filtered left join whose output coalescer combines two staged batches whose Dictionary(Int8, Utf8) columns hold 200 values between them. Each build batch holds 101 values, and the filter keeps half of each batch's pairs, so neither stage fills an output batch by itself. Added to datafusion/physical-plan/src/joins/sort_merge_join/tests.rs:

#[tokio::test]
async fn coalescer_error_panics() -> Result<()> {
    use arrow::datatypes::Int8Type;
    let left_schema = Arc::new(Schema::new(vec![
        Field::new("lk", DataType::Int32, false),
        Field::new("lx", DataType::Int32, false),
    ]));
    let right_schema = Arc::new(Schema::new(vec![
        Field::new("rk", DataType::Int32, false),
        Field::new("rx", DataType::Int32, false),
        Field::new(
            "p",
            DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Utf8)),
            false,
        ),
    ]));
    let left = RecordBatch::try_new(
        Arc::clone(&left_schema),
        vec![
            Arc::new(Int32Array::from((0..200).collect::<Vec<_>>())),
            Arc::new(Int32Array::from(vec![0; 200])),
        ],
    )?;
    // Each key has one row that fails `lx <= rx` and one that passes
    let right_batch = |base: i32, tag: &str| -> Result<RecordBatch> {
        let (mut k, mut x, mut p) = (vec![], vec![], vec![]);
        for i in 0..100 {
            k.extend([base + i, base + i]);
            x.extend([-1, 0]);
            p.extend([format!("{tag}-fails"), format!("{tag}{i}")]);
        }
        Ok(RecordBatch::try_new(
            Arc::clone(&right_schema),
            vec![
                Arc::new(Int32Array::from(k)),
                Arc::new(Int32Array::from(x)),
                Arc::new(p.iter().map(String::as_str).collect::<DictionaryArray<Int8Type>>()),
            ],
        )?)
    };
    let right = vec![right_batch(0, "a")?, right_batch(100, "b")?];
    let left_exec = TestMemoryExec::try_new_exec(&[vec![left]], Arc::clone(&left_schema), None)?;
    let right_exec = TestMemoryExec::try_new_exec(&[right], Arc::clone(&right_schema), None)?;
    let on: JoinOn = vec![(
        Arc::new(Column::new_with_schema("lk", &left_schema)?) as _,
        Arc::new(Column::new_with_schema("rk", &right_schema)?) as _,
    )];
    let filter = JoinFilter::new(
        Arc::new(BinaryExpr::new(
            Arc::new(Column::new("lx", 0)),
            Operator::LtEq,
            Arc::new(Column::new("rx", 1)),
        )),
        vec![
            ColumnIndex { index: 1, side: JoinSide::Left },
            ColumnIndex { index: 1, side: JoinSide::Right },
        ],
        Arc::new(Schema::new(vec![
            Field::new("lx", DataType::Int32, false),
            Field::new("rx", DataType::Int32, false),
        ])),
    );
    let join = SortMergeJoinExec::try_new(
        left_exec,
        right_exec,
        on,
        Some(filter),
        Left,
        vec![SortOptions::default()],
        NullEquality::NullEqualsNothing,
    )?;
    let ctx = Arc::new(
        TaskContext::default().with_session_config(SessionConfig::new().with_batch_size(200)),
    );
    let _ = common::collect(join.execute(0, ctx)?).await;
    Ok(())
}

It panics:

panicked at datafusion/physical-plan/src/joins/sort_merge_join/materializing_stream.rs:988:22:
Failed to push output batch: ComputeError("Failed to concatenate column 'p': Dictionary key bigger than the key type")

Expected behavior

The join returns the error. HashJoinExec returns the same overflow as an error: the same tables joined with a hash join in datafusion-cli fail with Execution error: Dictionary key type Int8 cannot represent 200 distinct values.

Activity

  1. added
    bug
    on Nov 24, 2026
  2. added theissue type on Nov 24, 2026
  3. mbutrovich commented on Nov 24, 2026

    @mbutrovich
    ContributorAuthor

    The same stream has three more expect calls on its coalescer, with the message Failed to push batch to BatchCoalescer: in push_batch_with_null_metadata, push_batch_with_filter_metadata, and push_batch_without_metadata. Property-based tests reached the last one:

    panicked at datafusion/physical-plan/src/joins/sort_merge_join/materializing_stream.rs:580:14:
    Failed to push batch to BatchCoalescer: ComputeError("Failed to concatenate column 'p1': Dictionary key bigger than the key type")
    

    A fix for this issue should cover all five.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions