dylanhz created FLINK-41656:
-------------------------------

             Summary: Avoid per-record accumulator conversions in keyed batch 
sort aggregation
                 Key: FLINK-41656
                 URL: https://issues.apache.org/jira/browse/FLINK-41656
             Project: Flink
          Issue Type: Improvement
          Components: Table SQL / Planner
            Reporter: dylanhz


h3. Problem

In a keyed batch {{SortAggregate}}, Flink stores the current group's 
accumulator in its internal row representation. For {{APPROX_COUNT_DISTINCT}} 
(HLL) and variance aggregates such as {{VAR_POP}} (Welford), each call to 
{{accumulate}} converts the accumulator to a Java object and converts it back 
after the update. In a two-phase plan, the global aggregate also performs this 
round trip on its target accumulator for each incoming partial.

Rows for the same key are contiguous. Both implementations create a fresh 
accumulator for each group and can update it directly until the key changes. 
The repeated conversions therefore add work to every update without affecting 
the result.

h3. Proposed change

Keep the current group's HLL and Welford accumulators in their Java 
representation in keyed batch sort aggregation. In a two-phase plan, keep the 
existing internal row format for partial results exchanged between the local 
and global operators. Apply the optimization automatically for these two 
implementations.

h3. Compatibility

Other UDAFs must retain their existing per-record conversion behavior. For 
example, consider a UDAF with a {{BigDecimal}} accumulator field declared as 
{{DECIMAL(10, 2)}}. If each of three input rows adds {{0.004}}, converting the 
field after every update rounds it to {{0.00}} each time. Deferring conversion 
until the group ends would instead round {{0.012}} to {{0.01}}, changing the 
SQL result.

Cover one-phase and two-phase aggregation for HLL and Welford, and retain a 
regression test that runs HLL alongside this DECIMAL UDAF in the same operator.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to