jayshrivastava commented on PR #26593:
URL: https://github.com/apache/datafusion/pull/26593#issuecomment-7290374966

   @stuhood @mbutrovich thanks for the comments! And sorry for the delay.
   
   ### Intro 
   The partition skipping approach in 
https://github.com/apache/datafusion/issues/27600 suggests we distribute ranges 
like so (the "duplicated approach")
   ```
   Both: Range(4, splits=[10,20,30])
                    p0          p1          p2          p3
   Task A stats:   active      active      Exact(0)    Exact(0)
   Task B stats:   Exact(0)    Exact(0)    active      active
   ```
   But I'm interested in distributing them like this (the "split approach")
   ```
   Original: Range(4, splits=[10,20,30])
   
                       split across workers
                                |
                +---------------+---------------+
                |                               |
   Worker 0: Range(2, splits=[10])       Worker 1: Range(2, splits=[30]) 
   ```
   In this case, I need to preserve the split point 20 somehow. By using stats 
or partition bounds
   
   ### Body
   We can compare the two options in terms of performance and complexity
   
   a) Performance
   
   The performance of the former and latter **are close but nonzero** with the 
implementation of empty partition skipping (I benchmarked that 
[here](https://github.com/jayshrivastava/datafusion/commit/6e7978bc514df547e21b27b6f95278057a49a6a2#diff-caf0983cbac0190a5340566847d57f01719f2847a43ca0f6b9fc1a7975b3219f)
 building on Stu's bench: 
https://github.com/paradedb/datafusion/blob/732c2171387105f567d66a27ccc573ddc2cbe2c8)
   
   | Pipeline ending | 16 partitioners per worker, skip off | 16 partitioners 
per worker, skip on | 1,600 partitioners per worker, skip off | 1,600 
partitioners per worker, skip on |
   |---|---:|---:|---:|---:|
   | Gather into one stream | 1.103 | 1.317 | 27.198 | 2.029 |
   | Plan ends in repartition into Hash(16) | 3.626 | 3.850 | 127.047 | 4.694 |
   | Plan ends in repartition into Range(1,600) | 50.529 | 48.647 | 3,446.024 | 
51.889 |
   
   For example `48.647` vs `51.889` is relatively small. The overhead is still 
nonzero though. From the 
[report](https://github.com/jayshrivastava/datafusion/blob/6e7978bc514df547e21b27b6f95278057a49a6a2/docs/range-partitioning-empty-partition-benchmark.md):
   > The benchmark measured input-plan construction/destruction at 0.739 ms 
versus 0.062 ms; it did not measure distributed serialization, network setup, 
or peak memory. These are remaining costs to evaluate, not evidence of a 
distributed disaster.
   
   You have to store statistics for more partitions and loop over them to 
decide if you need to spawn tasks. In the single node benchmark, we add 
milliseconds to latency. Could be more in the distributed scenario but I 
haven't measured it.
   
   b) Complexity
   
   The approach in the bench sort of "distills" duplicated partitions them into 
"split" ones by using statistics to skip empty partitions. We would need to 
implement that "distillation" in DFD in some shape or form for routing a Range 
: Range repartition. For example, we would need determine which workers a 
shuffle client should request to get their desired ranges. That requires 
reconstructing the non-duplicated partitioning across workers to know the 
minimal number of connections to make.
   
   Imo the simpler approach is to skip the projection/transform entirely by 
storing split partitions only. Since the middle split points are dropped, we 
would probably store the original `RangePartitioning` in a separate struct, 
similarly to ballista, and reference it as needed. This way, you avoid the 
dependency on stats entirely and avoid CPU cycles as well.
   
   Taking the split approach avoids avoid having to reason about cases where 
`num_partitions > target_partitions` or `num_partitions > num_cores` due to 
empty partitions. The mental model of distributed datafusion is just 
`num_workers = total_partitions / num_cores_per_worker`. It aligns with the 
principle of a partition being a unit of parallelism in single node DF as well.
   
   Lastly, `Hash` partitions are distributed via a splitting approach rather 
than duplicating + inserting empty partitions. Ideally, we don't have one 
approach for `Hash` and a different one for `Range`. 
   
   ### Summary
   
   Having stats for empty partitions 
(https://github.com/apache/datafusion/issues/27600) is very useful. I would be 
in favor of merging that. Skipping task spawning for empty partitions based on 
stats seems useful as well. 
   
   However, this would not be my preferred approach to distributing range 
partitions in DFD if we decide not to merge this PR (which is totally 
acceptable, especially given all the alternatives and discussion here!). 
   
   I would be interested in @avantgardnerio's input as well, both from 
ballista's perspective and from 
https://github.com/apache/datafusion/issues/23089. The latter discusses how 
partition bounds can be useful for partition pruning and optimizing window 
functions. It also importantly notes how partition bounds != runtime stats 
because
   - `max_value = Exact(19)` means the **observed max** is 19. This has a 
different lifetime and semantic meeting than a split point in a partition.
   - split points are **tuples of expressions**. `PartitionStatistics` are 
stored **per column**, so you cannot cover the set of all possible split points 
for a table using statistics
   
   I would say the main paths forward for Range shuffles in DFD are
   ```
   1 -> We decide to store partition bounds (which != min/max stats)
      1a -> We use the approach in this PR
      1b -> We investigate an alternative approach, such as in 
https://github.com/apache/datafusion/issues/230892
   2 -> We don't store partition bounds
   ```
   If the argument is not strong enough for partition bounds today, we can go 
with (2). DFD has a path forward! We can defer this PR / discussion until later.
   
   


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