gruuya opened a new pull request, #25292:
URL: https://github.com/apache/datafusion/pull/25292
## 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 #25291.
## 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.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
Avoid scanning redundant files/row-groups/pages in the probe side of hash
joins, based on the values dictated by the build side.
## 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.
-->
- 2 new configs that control the thresholds for the dynamic discreet pruning
to take place
- wiring up `PushdownStrategy::Map`/`HashTableLookupExpr` to carry the build
side values from a hash join
- extend `build_predicate_expression` to build the associated pruning
expression from `HashTableLookupExpr`
## What is the testing strategy for this PR?
<!--
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
Briefly describe how this PR is tested, and point to the specific tests you
added. For example: 'This new feature is covered by the `sqllogictest` cases
added in `foo.slt`'.
If this PR does not add tests, explain why. For example, if the change is
already covered by existing tests, please mention it.
You should also check the `codecov` bot reply on this PR to confirm the
changed code is exercised.
-->
Unit tests added covering all changes.
Also tested manually that the problem from the issue is resolved now
```sql
> select version();
+--------------------------------------------+
| version() |
+--------------------------------------------+
| Apache DataFusion 55.0.0, aarch64 on macos |
+--------------------------------------------+
1 row(s) fetched.
Elapsed 0.009 seconds.
> copy (select i as k, random() as v from generate_series(0, 1999999) t(i))
to '/tmp/fact.parquet'
stored as parquet options ('format.max_row_group_size' '1000');
+---------+
| count |
+---------+
| 2000000 |
+---------+
1 row(s) fetched.
Elapsed 0.091 seconds.
> create external table fact stored as parquet location '/tmp/fact.parquet';
0 row(s) fetched.
Elapsed 0.010 seconds.
> create table dim as
select i as k from generate_series(0, 1999999) t(i) where i % 10000 < 200;
0 row(s) fetched.
Elapsed 0.010 seconds.
> set datafusion.optimizer.hash_join_dynamic_pruning_max_distinct_values =
0; -- disabled
0 row(s) fetched.
Elapsed 0.001 seconds.
> select count(*), sum(v) from fact join dim on fact.k = dim.k;
select count(*), sum(v) from fact join dim on fact.k = dim.k;
select count(*), sum(v) from fact join dim on fact.k = dim.k;
select count(*), sum(v) from fact join dim on fact.k = dim.k;
+----------+--------------------+
| count(*) | sum(fact.v) |
+----------+--------------------+
| 40000 | 19873.614509561437 |
+----------+--------------------+
1 row(s) fetched.
Elapsed 0.116 seconds.
+----------+-------------------+
| count(*) | sum(fact.v) |
+----------+-------------------+
| 40000 | 19873.61450956144 |
+----------+-------------------+
1 row(s) fetched.
Elapsed 0.096 seconds.
+----------+-------------------+
| count(*) | sum(fact.v) |
+----------+-------------------+
| 40000 | 19873.61450956144 |
+----------+-------------------+
1 row(s) fetched.
Elapsed 0.096 seconds.
+----------+-------------------+
| count(*) | sum(fact.v) |
+----------+-------------------+
| 40000 | 19873.61450956144 |
+----------+-------------------+
1 row(s) fetched.
Elapsed 0.093 seconds.
> explain analyze select count(*), sum(v) from fact join dim on fact.k =
dim.k
...
| | DataSourceExec: file_groups={12 groups:
[[tmp/fact.parquet:0..1929814], [tmp/fact.parquet:1929814..3859628],
[tmp/fact.parquet:3859628..5789442], [tmp/fact.parquet:5789442..7719256],
[tmp/fact.parquet:7719256..9649070], ...]}, projection=[k, v],
output_ordering=[k@0 ASC NULLS LAST], file_type=parquet,
predicate=DynamicFilter [ k@0 >= 0 AND k@0 <= 1990199 AND hash_lookup ],
dynamic_rg_pruning=eligible, pruning_predicate=k_null_count@1 != row_count@2
AND k_max@0 >= 0 AND k_null_count@1 != row_count@2 AND k_min@3 <= 1990199,
required_guarantees=[], metrics=[output_rows=1.99 M, elapsed_compute=551.82µs,
output_bytes=248.9 MB, output_batches=1.99 K, files_ranges_pruned_statistics=12
total → 12 matched, row_groups_pruned_statistics=2.00 K total → 1.99 K matched,
row_groups_pruned_bloom_filter=1.99 K total → 1.99 K matched,
page_index_pages_pruned=1.99 K total → 1.99 K matched,
page_index_rows_pruned=1.99 M total → 1.99 M matched, limit_pruned_r
ow_groups=0 total → 0 matched, batches_split=0, bytes_processed=22.1 MB,
bytes_scanned=21.3 MB, file_open_errors=0, file_scan_errors=0, files_opened=12,
files_processed=12, num_predicate_creation_errors=0,
predicate_evaluation_errors=0, pushdown_rows_matched=0, pushdown_rows_pruned=0,
row_groups_pruned_dynamic_filter=0, predicate_cache_inner_records=0,
predicate_cache_records=0, bloom_filter_eval_time=75.36µs,
metadata_load_time=1.25ms, page_index_eval_time=10.25ms,
row_pushdown_eval_time=36ns, statistics_eval_time=1.05ms,
time_elapsed_opening=17.55ms, time_elapsed_processing=106.88ms,
time_elapsed_scanning_total=1.17s, time_elapsed_scanning_until_data=14.07ms,
output_rows_skew=1.64%, scan_efficiency_ratio=96.46% (22.34 M/23.16 M)] |
...
> set datafusion.optimizer.hash_join_dynamic_pruning_max_distinct_values =
100000; -- enabled, default
0 row(s) fetched.
Elapsed 0.000 seconds
> select count(*), sum(v) from fact join dim on fact.k = dim.k;
select count(*), sum(v) from fact join dim on fact.k = dim.k;
select count(*), sum(v) from fact join dim on fact.k = dim.k;
select count(*), sum(v) from fact join dim on fact.k = dim.k;
+----------+--------------------+
| count(*) | sum(fact.v) |
+----------+--------------------+
| 40000 | 19873.614509561437 |
+----------+--------------------+
1 row(s) fetched.
Elapsed 0.026 seconds.
+----------+-------------------+
| count(*) | sum(fact.v) |
+----------+-------------------+
| 40000 | 19873.61450956144 |
+----------+-------------------+
1 row(s) fetched.
Elapsed 0.019 seconds.
+----------+-------------------+
| count(*) | sum(fact.v) |
+----------+-------------------+
| 40000 | 19873.61450956144 |
+----------+-------------------+
1 row(s) fetched.
Elapsed 0.020 seconds.
+----------+-------------------+
| count(*) | sum(fact.v) |
+----------+-------------------+
| 40000 | 19873.61450956144 |
+----------+-------------------+
1 row(s) fetched.
Elapsed 0.014 seconds.
> explain analyze select count(*), sum(v) from fact join dim on fact.k =
dim.k;
...
| | DataSourceExec: file_groups={12 groups:
[[tmp/fact.parquet:0..1929814], [tmp/fact.parquet:1929814..3859628],
[tmp/fact.parquet:3859628..5789442], [tmp/fact.parquet:5789442..7719256],
[tmp/fact.parquet:7719256..9649070], ...]}, projection=[k, v],
output_ordering=[k@0 ASC NULLS LAST], file_type=parquet,
predicate=DynamicFilter [ k@0 >= 0 AND k@0 <= 1990199 AND hash_lookup ],
dynamic_rg_pruning=eligible, pruning_predicate=k_null_count@1 != row_count@2
AND k_max@0 >= 0 AND k_null_count@1 != row_count@2 AND k_min@3 <= 1990199 AND
k_null_count@1 != row_count@2 AND IN_SET_INTERSECTS(k_min@3, k_max@0, 40000
values), required_guarantees=[], metrics=[output_rows=200.0 K,
elapsed_compute=76.72µs, output_bytes=25.0 MB, output_batches=200,
files_ranges_pruned_statistics=12 total → 12 matched,
row_groups_pruned_statistics=2.00 K total → 200 matched,
row_groups_pruned_bloom_filter=200 total → 200 matched,
page_index_pages_pruned=200 total → 200 matched, p
age_index_rows_pruned=200.0 K total → 200.0 K matched,
limit_pruned_row_groups=0 total → 0 matched, batches_split=0,
bytes_processed=22.1 MB, bytes_scanned=2.1 MB, file_open_errors=0,
file_scan_errors=0, files_opened=12, files_processed=12,
num_predicate_creation_errors=0, predicate_evaluation_errors=0,
pushdown_rows_matched=0, pushdown_rows_pruned=0,
row_groups_pruned_dynamic_filter=0, predicate_cache_inner_records=0,
predicate_cache_records=0, bloom_filter_eval_time=64.73µs,
metadata_load_time=2.78ms, page_index_eval_time=1.83ms,
row_pushdown_eval_time=36ns, statistics_eval_time=773.07µs,
time_elapsed_opening=77.42ms, time_elapsed_processing=91.60ms,
time_elapsed_scanning_total=189.39ms, time_elapsed_scanning_until_data=11.29ms,
output_rows_skew=1.63%, scan_efficiency_ratio=9.69% (2.24 M/23.16 M)] |
...
```
Note that the scanned rows are shrunk 10x (`output_rows=1.99 M` vs
`output_rows=200.0 K`), and consequently the execution time is improved 5x
(`0.096` vs `0.019` seconds).
Anecdotally, i'm also seeing 10x improvements in some other query shapes
(based on TPC-DS data), so it would be good to benchmark this some more.
## 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, the two new configs mirroring the in-list ones, as well as the
construction API for `HashTableLookupExpr`, which now accepts an optional
values arg too.
--
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]