xiangfu0 opened a new pull request, #20389:
URL: https://github.com/apache/pinot/pull/20389

   ## Summary
   
   A query-wide `serverReturnFinalResult=true` can make a storage leaf emit an 
integer while the next MSE stage expects a distinct set. This PR makes the 
aggregate plan control the leaf output type. With the v2 physical optimizer, 
the option also enables scalar exchange for grouped exact distinct counts when 
routing proves the counts are additive.
   
   ## Minimal repro
   
   Use an offline table `partitioned_counts(num INT, name STRING)` with two 
hash partitions on `num`, one per leaf worker. The three rows are `(4, 'e')`, 
`(4, 'e')` and `(5, 'e')`.
   
   ```sql
   SET useMultistageEngine=true;
   SET usePhysicalOptimizer=true;
   SET serverReturnFinalResult=true;
   
   SELECT name, COUNT(DISTINCT num),
          COUNT(DISTINCT num) FILTER (WHERE num > 4)
   FROM partitioned_counts
   GROUP BY name;
   ```
   
   Before the fix, the Java MSE reducer can fail with `Integer cannot be cast 
to class java.util.Set`. The expected row is `('e', 2, 1)`. The first leaf 
deduplicates the two 4s, the second counts the 5, and the filter runs at the 
leaf. The three-row fixture is in `QueryHints.json`. To run it from the 
repository root, use the focused query suite.
   
   ```sh
   ./mvnw -pl pinot-query-runtime -am -Dtest=ResourceBasedQueriesTest \
     -Dpinot.fileFilter=QueryHints -Dsurefire.failIfNoSpecifiedTests=false test
   ```
   
   ## How the planner proves the sum is exact
   
   1. The v2 leaf assignment builds a hash-distribution descriptor from 
partition metadata and the segments actually routed for this query. All 
selected segments for each partition must map to one server and fit the 
supported partition-to-worker assignment.
   2. Before the exchange on the group keys, the planner checks that the 
aggregate input is a leaf and that every aggregate call in the group is an 
exact, single-argument `COUNT(DISTINCT x)` or `DISTINCTCOUNT(x)` over a scalar. 
Each argument must exactly match a hash key on that input. An explicit 
`aggOptions(is_leaf_return_final_result=...)` hint takes precedence. The legacy 
MSE planner continues to use that hint for scalar distinct counts.
   3. For each group, every occurrence of a distinct value then reaches one 
leaf. That leaf deduplicates it across its segments, so the per-leaf sets are 
disjoint even when the group spans workers. The leaves send `INT` counts and 
the reducer uses `SUMINT` to accumulate a `BIGINT`.
   
   Without that proof, the automatic path keeps the distinct sets and merges 
them. On the same rows, `COUNT(DISTINCT name)` grouped by `LENGTH(name)` must 
return 1. Both workers see `'e'`, so summing their local counts would return 2. 
The broker strips the two storage final-result options from request metadata, 
and the worker also ignores copies from older brokers. This preserves each 
stage's planned result type during rolling upgrades.
   
   ## Validation
   
   - Ran the planner option tests, the `QueryHints.json` execution tests 
against H2, and the dispatcher tests. 365 passed and 14 were skipped.
   - Fed two `INT` leaf counts of 1,500,000,000 into the planner-generated 
reducer and verified a `LONG` result of 3,000,000,000.
   - Spotless, license and Checkstyle checks passed on the three affected 
modules.
   


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