gene-bordegaray opened a new pull request, #20331:
URL: https://github.com/apache/datafusion/pull/20331

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and 
enhancements and this helps us generate change logs for our releases. You can 
link an issue to this PR using the GitHub syntax. For example `Closes #123` 
indicates that this PR will close issue #123.
   -->
   
   Closes #20195 
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in 
the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your 
changes and offer better suggestions for fixes.  
   -->
   
   Dynamic filter pushdown was completely when `preserve_file_partitions` on 
due to a correctness bug.
   
   **The Problem**
   
   When preserve_file_partitions enabled, DataFusion treats file groups as 
pre-partitioned data. Existing dynamic filtering used hash-based routing which 
is incompatible with the value-based partitioning that file groups are kept in:
   
   Example:
   ```
   Table partitioned by col_a:
   - Partition 0: col_a = 'A'
   - Partition 1: col_a = 'B'
   - Partition 2: col_a = 'C'
   
   Dimension Table: values = ['A', 'B']
   
   SELECT * FROM large_table
   JOIN small_table ON large_table.col_a = small_table.col_a
   
   Hash routing doesn't work:
   - Hash routing: hash('A') % 3 might map to partition 1 (not partition 0)
   - File partitioning: 'A' data is in partition 0 (value-based)
   ```
   
   For this reason was diabled, this PR re-enables it via `PartitionIndex` 
routing for dynamic filters.
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is 
sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   **Partition-Indexed dynamic filtering**
   
   New routing mode that uses direct partition-to-partition mapping:
   ```
   Build partition 0 → filters Probe partition 0
   Build partition 1 → filters Probe partition 1
   Build partition 2 → filters Probe partition 2
   ```
   
   Example:
   ```
   HashJoinExec: mode=Partitioned, routing=PartitionIndex, on=[col_a = col_b]
       DataSourceExec: table_large, file_groups={3: [col_a=A], [col_a=B], 
[col_a=C]} predicate=DynamicFilter[
           {0: col_a IN ['A','B']},  -- Partition 0 filtered
           {1: col_a IN ['A','B']},  -- Partition 1 filtered
          {2: col_a IN ['A','B']}   -- Partition 2 filtered (no matches, pruned)
       ]
       DataSourceExec: table_small, values: [col_b='A', col_b='B']
   
   How it works:
   - Build partition 0 (col_b='A') creates filter for probe partition 0 
(col_a='A')
   - Build partition 1 (col_b='B') creates filter for probe partition 1 
(col_a='B')
   - Probe partition 2 (col_a='C') gets pruned (no matching build partition)
   ```
   
   **Alignment Detection**
   Detects compatible partitioning to enable safe optimization:
   - Both sides file-grouped (value-based partitioning) -> PartitionIndex
   - Both sides hash-repartitioned (hash-based partitioning) -> CashHash
   - Both has different partitioning -> Error, this shouldn't happen and can 
cause incorrect results
     match (left.repartitioned, right.repartitioned) {
   
   In the case there is a `RepartitionExec` in the path leading from the 
`DataSourceExec` to either the build or probe side of a Partitioned Hash Join 
-> Falls back to CaseHash.
   
   The reason is `RepartitionExec` uses hash(value) % N to distribute rows, 
breaking the value-based partition alignment. When hash-partitioned, partition 
0 no longer contains 'A' exclusively, breaking the partition index assumptions
   
   With hash partitioning, use:
   ```
     CASE hash(col_a) % 4
       WHEN 0 THEN filter_partition_0
       WHEN 1 THEN filter_partition_1
       ...
     END
   ```
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are 
they covered by existing tests)?
   -->
   sqlogictests: test_files/preserve_file_partitioning.slt
   Integration tests: 
datafusion/core/tests/physical_optimizer/filter_pushdown.rs
   Unit tests: in effected files
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api 
change` label.
   -->
   
   Yes a new error message can appear is partition hash joins are not aligned 
properly and the dynamic filtering display for partition index is a but 
different then CASE routing.


-- 
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