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

   ## 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 #17635.
   
   ## Rationale for this change
   
   This PR introduces a Perfect Hash Join optimization by using an array-based 
direct mapping(`ArrayMap`) instead of a HashMap.
   The array-based approach significantly outperforms the standard Hash Join 
when the build-side keys are **_dense_** (i.e., the ratio of `count / (max - 
min)` is high) or when the key range `(max - min)` is sufficiently **small**.
   
   <!--
    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.  
   -->
   
   The following results from the hj.rs benchmark suite.  The benchmark was 
executed with the optimization enabled by setting 
`DATAFUSION_EXECUTION_PERFECT_HASH_JOIN_MIN_KEY_DENSITY=0.8`
   ```
   ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
   ┃ Query        ┃      base ┃ perfect_hj ┃        Change ┃
   ┡━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
   │ QQuery 1     │   5.15 ms │    4.57 ms │ +1.13x faster │
   │ QQuery 2     │ 132.34 ms │   90.67 ms │ +1.46x faster │
   │ QQuery 3     │ 117.28 ms │   68.26 ms │ +1.72x faster │
   │ QQuery 4     │ 142.72 ms │   94.52 ms │ +1.51x faster │
   │ QQuery 5     │ 177.38 ms │  145.15 ms │ +1.22x faster │
   │ QQuery 6     │  77.69 ms │   79.75 ms │     no change │
   └──────────────┴───────────┴────────────┴───────────────┘
   ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
   ┃ Benchmark Summary         ┃          ┃
   ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
   │ Total Time (base)         │ 652.57ms │
   │ Total Time (perfect_hj)   │ 482.92ms │
   │ Average Time (base)       │ 108.76ms │
   │ Average Time (perfect_hj) │  80.49ms │
   │ Queries Faster            │        5 │
   │ Queries Slower            │        0 │
   │ Queries with No Change    │        1 │
   │ Queries with Failure      │        0 │
   └───────────────────────────┴──────────┘
   ```
   
   
   The following results from tpch-sf10
   ```
   ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
   ┃ Query        ┃       base ┃ perfect_hj ┃        Change ┃
   ┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
   │ QQuery 1     │  739.66 ms │  743.84 ms │     no change │
   │ QQuery 2     │  315.94 ms │  317.53 ms │     no change │
   │ QQuery 3     │  655.79 ms │  669.24 ms │     no change │
   │ QQuery 4     │  215.48 ms │  218.79 ms │     no change │
   │ QQuery 5     │ 1131.42 ms │ 1146.03 ms │     no change │
   │ QQuery 6     │  202.32 ms │  190.83 ms │ +1.06x faster │
   │ QQuery 7     │ 1734.06 ms │ 1710.50 ms │     no change │
   │ QQuery 8     │ 1185.05 ms │ 1173.90 ms │     no change │
   │ QQuery 9     │ 2036.76 ms │ 1994.30 ms │     no change │
   │ QQuery 10    │  907.32 ms │  893.20 ms │     no change │
   │ QQuery 11    │  306.63 ms │  275.46 ms │ +1.11x faster │
   │ QQuery 12    │  404.00 ms │  381.95 ms │ +1.06x faster │
   │ QQuery 13    │  531.67 ms │  498.58 ms │ +1.07x faster │
   │ QQuery 14    │  317.63 ms │  303.04 ms │     no change │
   │ QQuery 15    │  602.24 ms │  572.18 ms │     no change │
   │ QQuery 16    │  200.00 ms │  201.68 ms │     no change │
   │ QQuery 17    │ 1848.67 ms │ 1790.60 ms │     no change │
   │ QQuery 18    │ 2130.63 ms │ 2179.84 ms │     no change │
   │ QQuery 19    │  501.81 ms │  529.85 ms │  1.06x slower │
   │ QQuery 20    │  637.91 ms │  661.72 ms │     no change │
   │ QQuery 21    │ 1882.43 ms │ 1917.10 ms │     no change │
   │ QQuery 22    │  130.68 ms │  141.76 ms │  1.08x slower │
   └──────────────┴────────────┴────────────┴───────────────┘
   ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
   ┃ Benchmark Summary         ┃            ┃
   ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
   │ Total Time (base)         │ 18618.10ms │
   │ Total Time (perfect_hj)   │ 18511.93ms │
   │ Average Time (base)       │   846.28ms │
   │ Average Time (perfect_hj) │   841.45ms │
   │ Queries Faster            │          4 │
   │ Queries Slower            │          2 │
   │ Queries with No Change    │         16 │
   │ Queries with Failure      │          0 │
   └───────────────────────────┴────────────┘
   ```
   
   ## What changes are included in this PR?
   
   - During the `collect_left_input` (build) phase, we now conditionally use an 
`ArrayMap` instead of a standard `JoinHashMapType`. This optimization is 
triggered only when the following conditions are met:
     - The join is on a single integer join key.
     - The key distribution is sufficiently dense or the key range (max - min) 
is small enough to justify an array-based allocation.
   -  The `ArrayMap` works by storing the minimum key as an offset and using a 
Vec to directly map a key `k` to its build-side index via `data[k- offset]`.
   
   
   
   
   <!--
   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.
   -->
   
   ## 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)?
   -->
   
   ## Are there any user-facing changes?
   
   Yes, this PR introduces two new session configuration parameters to control 
the behavior of the Perfect Hash Join optimization:
   
   - `perfect_hash_join_small_build_threshold`: This parameter defines the 
maximum key range (max_key - min_key) for the build side to be considered 
"small." If the key range is below this threshold, the array-based join will be 
triggered regardless of key density.
   - `perfect_hash_join_min_key_density`: This parameter sets the minimum 
density (row_count / key_range) required to enable the perfect hash join 
optimization for larger key ranges
   <!--
   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.
   -->
   


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