Skip to content

HashJoinExec with a fetch reads its whole probe side when its build-side hash map is empty #27726

Backlog

Description

@mbutrovich

Describe the bug

A hash join's build-side hash map is empty when the build side has no rows, or when nulls equal nothing (NullEqualsNothing) and every build row has a NULL key. In that case, for each probe batch, HashJoinStream takes its empty build side path. That path ignores the status push_batch returns and goes back to FetchProbeBatch. LimitedBatchCoalescer sets finished only in finish(), so the stream keeps reading the probe side after it has fetch rows. When the map has entries, the stream calls finish() on LimitReached and stops.

The output is right, since the coalescer drops rows past the limit. The join reads and joins the rest of the probe input for nothing. The join types that return rows on that path are RightAnti and RightMark. With the default enable_migration_hash_join = true, inner, left, right, and full joins take the batched path instead (RegularJoinKind::from_join_type).

To Reproduce

In datafusion-cli:

SET datafusion.execution.target_partitions = 1;
CREATE TABLE probe AS SELECT value AS k FROM generate_series(1, 100000);
CREATE TABLE null_keys AS SELECT CAST(NULL AS BIGINT) AS k;
CREATE TABLE other_keys AS SELECT CAST(-1 AS BIGINT) AS k;
EXPLAIN ANALYZE SELECT * FROM probe WHERE NOT EXISTS (SELECT 1 FROM null_keys WHERE null_keys.k = probe.k) LIMIT 10;
EXPLAIN ANALYZE SELECT * FROM probe WHERE NOT EXISTS (SELECT 1 FROM other_keys WHERE other_keys.k = probe.k) LIMIT 10;

Both plans are HashJoinExec: mode=CollectLeft, join_type=RightAnti, on=[(k@0, k@0)], fetch=10, and both return 10 rows. With null_keys as the build side, the join's probe metrics are input_batches=13, input_rows=100.0 K: it read the whole probe side. With other_keys, whose map has an entry, they are input_batches=1, input_rows=8.19 K.

Expected behavior

Once it has fetch rows, the join stops reading its probe side, as it does when the map has entries.

Additional context

#20492 stopped the join from reading the probe side when an empty build side means empty output. Here the output isn't empty.

Activity

  1. added
    bug
    on Nov 24, 2026
  2. added theissue type on Nov 24, 2026
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