2010YOUY01 opened a new pull request, #21211:
URL: https://github.com/apache/datafusion/pull/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 
(https://github.com/apache/datafusion/issues/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 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
   
   Even with intra-partition parallelism (e.g., morsel-driven Parquet scan), 
skew can still propagate to downstream operators:
   ```
   partition 1: parquet_scan(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.
   -->
   


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