Commit e57198a
authored
feat(metric): Add output skewness metric to detect skewed plans easier (#21211)
## 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 #.
## Rationale for this change
### Output Skewness Metric
This introduces a metric to quantify execution skew across partitions,
inspired by recent work on intra-partition parallelism for Parquet scans
(#20529).
cc @Dandandan and @alamb (I think this is related to your ongoing work)
For example, a Parquet scan with 4 partitions:
```
partition 1: output_rows = 100
partition 2: output_rows = 0
partition 3: output_rows = 0
partition 4: output_rows = 0
```
represents a highly skewed workload, where most work is concentrated in
a single partition.
This metric normalizes skew into the range `[0%, 100%]`, where `0%`
indicates a perfectly balanced distribution and `100%` indicates maximal
skew. This makes it easy to detect skew via simple thresholds in
automated tooling.
### Demo
Run clickbench q41, the parquet exec has skew score 92%, from `EXPLAIN
ANALYZE VERBOSE` we can verify only 2 out of 14 partitions has output
rows from the parquet scan.
```sh
DataFusion CLI v52.3.0
> CREATE or replace EXTERNAL TABLE hits STORED AS PARQUET LOCATION '/Users/yongting/Code/datafusion/benchmarks/data/hits_partitioned';
0 row(s) fetched.
Elapsed 0.338 seconds.
-- Clickbench Q41
> explain analyze SELECT "WindowClientWidth", "WindowClientHeight", COUNT(*) AS PageViews FROM hits WHERE "CounterID" = 62 AND "EventDate" >= '2013-07-01' AND "EventDate" <= '2013-07-31' AND "IsRefresh" = 0 AND "DontCountHits" = 0 AND "URLHash" = 2868770270353813622 GROUP BY "WindowClientWidth", "WindowClientHeight" ORDER BY PageViews DESC LIMIT 10 OFFSET 10000;
...
DataSourceExec: ..., output_rows_skew=92.35%...] |
```
---
### Definition
Let `r_i` be the output rows of partition `i`:
```
# ranges from [1, partition_count], if perfectly balanced, it reaches partition_count for maximum effective parallelism
effective_parallelism = (sum(r_i))^2 / sum(r_i^2)
# convert effective parallelism to a skewness from 0%(perfectly balanced) to 100%(extreme skew)
output_rows_skew =
(1 - (effective_parallelism - 1) / (partition_count - 1)) * 100%
```
Examples:
- `[10, 10, 10, 10]` → skew = `0%`
- `[40, 0, 0, 0]` → skew = `100%`
---
### Motivation
I think there are several more further works to do to tackle the
skewness, so this metric can help. Even with intra-partition parallelism
(e.g., morsel-driven Parquet scan), skew can still propagate to
downstream operators:
```
partition 1: parquet_scan(output_rows=100) → FilterExec(90)
partition 2: parquet_scan(0) → FilterExec(0)
partition 3: parquet_scan(0) → FilterExec(0)
partition 4: parquet_scan(0) → FilterExec(0)
```
Downstream operators like `FilterExec` may remain underutilized,
suggesting the need for additional techniques such as internal
parallelism or repartitioning. This metric helps identify such cases and
guide further optimization.
---
### Why calculate on output_rows
It's easy to implement since `output_rows` are already tracked, the
downside is it requires some knowledge to interpret, for instance if we
see a large skewness value on parquet DataSource, we want to ensure a)
parquet has internal parallelism mechanism to evenly distribute the work
b) the downstream won't be affected by the produced skewness
## What changes are included in this PR?
Add a derived `output_rows_skew` to DataSourceExec with parquet source,
in the follow-ups I think it's also useful to other datasources, and
FilterExec (which might also introduce skewness during execution)
## 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)?
-->
sqllogictests
## Are there any user-facing changes?
No
<!--
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.
-->1 parent e883171 commit e57198a
File tree
7 files changed
+275
-32
lines changed- datafusion
- core/tests/sql
- datasource/src
- physical-expr-common/src/metrics
- sqllogictest/test_files
- docs/source/user-guide
7 files changed
+275
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
887 | 887 | | |
888 | 888 | | |
889 | 889 | | |
890 | | - | |
| 890 | + | |
| 891 | + | |
891 | 892 | | |
892 | 893 | | |
893 | 894 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
356 | 358 | | |
357 | 359 | | |
358 | 360 | | |
359 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
360 | 375 | | |
361 | 376 | | |
362 | 377 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
| |||
125 | 129 | | |
126 | 130 | | |
127 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
128 | 187 | | |
129 | 188 | | |
130 | 189 | | |
| |||
178 | 237 | | |
179 | 238 | | |
180 | 239 | | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
181 | 272 | | |
182 | 273 | | |
183 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
468 | 468 | | |
469 | 469 | | |
470 | 470 | | |
| 471 | + | |
| 472 | + | |
471 | 473 | | |
472 | 474 | | |
473 | 475 | | |
| |||
485 | 487 | | |
486 | 488 | | |
487 | 489 | | |
| 490 | + | |
488 | 491 | | |
489 | 492 | | |
490 | 493 | | |
| |||
493 | 496 | | |
494 | 497 | | |
495 | 498 | | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
496 | 504 | | |
497 | 505 | | |
498 | 506 | | |
| |||
544 | 552 | | |
545 | 553 | | |
546 | 554 | | |
547 | | - | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
548 | 558 | | |
549 | 559 | | |
550 | 560 | | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | 561 | | |
| 562 | + | |
568 | 563 | | |
569 | 564 | | |
570 | 565 | | |
571 | 566 | | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
572 | 592 | | |
573 | 593 | | |
574 | | - | |
| 594 | + | |
575 | 595 | | |
576 | | - | |
| 596 | + | |
577 | 597 | | |
578 | 598 | | |
579 | | - | |
580 | | - | |
581 | 599 | | |
582 | 600 | | |
583 | | - | |
584 | | - | |
| 601 | + | |
585 | 602 | | |
586 | 603 | | |
587 | 604 | | |
| |||
865 | 882 | | |
866 | 883 | | |
867 | 884 | | |
868 | | - | |
| 885 | + | |
| 886 | + | |
869 | 887 | | |
870 | 888 | | |
871 | 889 | | |
| |||
1232 | 1250 | | |
1233 | 1251 | | |
1234 | 1252 | | |
1235 | | - | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
1236 | 1268 | | |
1237 | 1269 | | |
1238 | 1270 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| |||
0 commit comments