asolimando opened a new pull request, #27630:
URL: https://github.com/apache/datafusion/pull/27630

   ## Which issue does this PR close?
   
   - Closes #27629.
   
   ## Rationale for this change
   
   A join with an input that is known to be empty (`Rows=Exact(0)`) reports 
`Rows=Inexact(0)`, but the join provably returns no rows. Consumers that treat 
only `Exact` values as facts cannot use the estimate:
   
   - `EnforceDistribution` adds a round-robin repartition above a child unless 
the child's row count is `Exact(n)` with `n <= batch_size`. An `Inexact` count 
is used only when 
`datafusion.optimizer.use_row_number_estimates_to_optimize_partitioning` is 
set, and it is `false` by default. So with the default config, plans 
repartition the output of a join that is known to be empty (see the plan change 
below).
   - Operators that skip `Exact(0)` inputs, such as the exchanges proposed in 
#27600, cannot skip these joins.
   
   #23936 and #24000 fixed the same loss of `Exact(0)` in filter selectivity 
and in `FilterExec`.
   
   **Root cause, on the reproducer from the issue** (`t` has 3 rows, `e` has 0 
rows, `SELECT * FROM t JOIN e ON t.a = e.a`):
   
   1. The scans report `Rows=Exact(3)` for `t` and `Rows=Exact(0)` for `e`.
   2. `estimate_join_statistics` calls `estimate_join_cardinality`, which calls 
`estimate_inner_join_cardinality`. That function sees `Exact(0)` on the right 
side and returns `Exact(0)`.
   3. `estimate_join_cardinality` takes the value with `get_value()` and stores 
`0` in `PartialJoinStatistics::num_rows`, which is a plain `usize`. At this 
point the precision is lost.
   4. `estimate_join_statistics` wraps every estimate as 
`Precision::Inexact(stats.num_rows)`, so the join reports `Rows=Inexact(0)`, 
and `Bytes=Absent`.
   
   ## What changes are included in this PR?
   
   - `estimate_join_statistics` now checks, before estimating, whether the join 
output is provably empty. If it is, it reports `num_rows = Exact(0)` and 
`total_byte_size = Exact(0)`. Column statistics are unchanged.
   - The check is in a new private function, `join_output_is_empty`. An empty 
input empties the output for these join types:
     - `Inner`, `LeftSemi`, `RightSemi`: either side empty
     - `Left`, `LeftAnti`, `LeftMark`: left side empty
     - `Right`, `RightAnti`, `RightMark`: right side empty
     - `Full`: both sides empty
   
   ## What is the testing strategy for this PR?
   
   - Unit test `test_join_with_empty_input_is_exactly_empty` in 
`joins/utils.rs`: all 10 join types, each with the left input empty, the right 
input empty, and both empty.
   - New `explain.slt` case with `show_statistics`: an inner join with an empty 
table reports `Rows=Exact(0), Bytes=Exact(0)`. Without the fix, this case fails 
with `Rows=Inexact(0), Bytes=Absent`.
   - One existing expected plan changes, in `join.slt.part` (included by 
`join_only.slt` and `join_disable_repartition_joins.slt`). The inner join of 
the two empty tables `t1` and `t5` is now `Exact(0)`, so `EnforceDistribution` 
no longer adds the `RepartitionExec: RoundRobinBatch(4)` above it, or the 
`CoalescePartitionsExec` that merges the 4 partitions back into 1 for the 
`CollectLeft` build side. The query result is the same.
   
   ## Are there any user-facing changes?
   
   Joins with an input known to be empty now report `Rows=Exact(0)` and 
`Bytes=Exact(0)` instead of `Rows=Inexact(0)`, and plans can change as a result 
(see above). There are no public API changes.
   
   ----
   
   Disclaimer: I used AI to assist in the code generation, I have manually 
reviewed the output and it matches my intention and understanding.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to