[
https://issues.apache.org/jira/browse/FLINK-41097?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150285#comment-18150285
]
Benjamin Allévius commented on FLINK-41097:
-------------------------------------------
We hit this in production batch jobs on 2.3.0 with the default adaptive
settings: about 1M groups a
day got a NULL AVG, which SUM(x) / COUNT(x) over the same rows did not. Some
additional data points:
*It also affects 1.20.5 (LTS)*, so 1.20.5 could be added to Affects Version/s.
*Self-contained SQL client repro with default thresholds* (standalone cluster
from the binary
distribution, {{./bin/start-cluster.sh}}, then {{./bin/sql-client.sh -f
avg.sql}}):
{code:sql}
SET 'execution.runtime-mode' = 'batch';
SET 'sql-client.execution.result-mode' = 'tableau';
SET 'parallelism.default' = '1';
SET 'table.optimizer.agg-phase-strategy' = 'TWO_PHASE';
-- 2,000,000 rows, 1,000,000 groups: every group gets one non-NULL value (rows
0..999,999)
-- and one NULL value (rows 1,000,000..1,999,999).
CREATE TEMPORARY TABLE src (id BIGINT) WITH (
'connector' = 'datagen',
'number-of-rows' = '2000000',
'fields.id.kind' = 'sequence',
'fields.id.start' = '0',
'fields.id.end' = '1999999'
);
CREATE TEMPORARY VIEW t AS
SELECT id % 1000000 AS g, CASE WHEN id < 1000000 THEN CAST(1.0 AS DOUBLE) END
AS x FROM src;
-- Expected: wrong_groups = 0 (every group's AVG(x) is 1.0, like SUM(x) /
COUNT(x)).
SELECT COUNT(*) AS `groups`, SUM(CASE WHEN a IS NULL THEN 1 ELSE 0 END) AS
wrong_groups
FROM (SELECT g, AVG(x) AS a, SUM(x) / COUNT(x) AS s FROM t GROUP BY g);
{code}
On 2.3.0 this prints {{wrong_groups = 1000000}}; with
{{SET 'table.exec.local-hash-agg.adaptive.enabled' = 'false';}} it prints 0.
The local aggregate logs
"Local hash aggregation is suppressed" after the 500,000-row sample, and every
group whose NULL row
arrives after that comes back NULL.
With a file source holding the same 2M rows in different orders (parallelism 1
unless noted,
TWO_PHASE, default adaptive settings), the number of groups with a NULL AVG on
2.3.0:
||Input order||Groups||NULL AVG||
|non-NULL rows first, then NULL rows|1,000,000|1,000,000|
|NULL rows first, then non-NULL rows|1,000,000|500,000|
|shuffled, parallelism 2|1,000,000|500,699|
|each group's two rows adjacent|1,000,000|0 (sample is exactly 50% distinct,
not above 0.5)|
|non-NULL first, 400,000 groups (800k rows)|400,000|300,000|
|non-NULL first, 200,000 groups (400k rows, below the sample size)|200,000|0|
|ONE_PHASE, or {{table.exec.disabled-operators = 'HashAgg'}}|1,000,000|0|
1.20.5 gives the same 1,000,000 for the first row, and 0 with adaptive disabled.
*Why the existing test does not catch it:*
{{HashAggITCase#testAdaptiveLocalHashAggWithNullValue}}
has NULL arguments, but with a sampling threshold of 3 its first three rows
share one group, so the
distinct rate stays below 0.5 and the operator never switches to bypass.
*Workarounds until a fix ships:* {{table.exec.local-hash-agg.adaptive.enabled =
false}},
{{table.optimizer.agg-phase-strategy = ONE_PHASE}}, or {{SUM(x) / COUNT(x)}}
instead of {{AVG(x)}}.
I tested the fix in
[apache/flink#29976|https://github.com/apache/flink/pull/29976] (details in my
review there): it fixes all of the cases above.
> AVG returns NULL for a group with a NULL value once adaptive local hash
> aggregation switches to bypass mode
> -----------------------------------------------------------------------------------------------------------
>
> Key: FLINK-41097
> URL: https://issues.apache.org/jira/browse/FLINK-41097
> Project: Flink
> Issue Type: Bug
> Components: Table SQL / Runtime
> Affects Versions: 2.3.0
> Reporter: Yaoxuan Wu
> Priority: Major
> Labels: pull-request-available
>
> {code:python}
> from pyflink.table import EnvironmentSettings, TableEnvironment, DataTypes as
> D
> from pyflink.common import Configuration
> c = Configuration()
> c.set_string('parallelism.default', '1')
> c.set_string('table.optimizer.agg-phase-strategy', 'TWO_PHASE')
> c.set_string('table.exec.local-hash-agg.adaptive.sampling-threshold', '1')
> # switch to bypass early
> c.set_string('table.exec.local-hash-agg.adaptive.distinct-value-rate-threshold',
> '0.0')
> t =
> TableEnvironment.create(EnvironmentSettings.new_instance().in_batch_mode().with_configuration(c).build())
> t.create_temporary_view('t', t.from_elements([(1, 5), (1, None), (2, 7)],
> D.ROW([D.FIELD('g', D.INT()), D.FIELD('x', D.INT())])))
> t.execute_sql("SELECT g, AVG(x) FROM t GROUP BY g").print()
> # expected: (1, 5), (2, 7)
> # actual: (1, NULL), (2, 7)
> {code}
> Once the adaptive local hash aggregate (FLINK-30542) switches to bypass mode,
> a row whose AVG argument is NULL makes the group's AVG NULL. The forced
> thresholds above only make the switch happen early: with the default settings
> (sampling 500000 rows, distinct-value rate 0.5) the same happens, e.g. 600001
> distinct groups with the NULL row last. SUM, COUNT and MAX are correct, and
> so is AVG with {{table.exec.local-hash-agg.adaptive.enabled=false}} or
> one-phase aggregation.
> Cause: after the switch,
> {{ProjectionCodeGenerator.genAdaptiveLocalHashAggValueProjectionExpr}} turns
> each input row into AVG's partial buffer as {{(sum = CAST(x AS sumType),
> count = x IS NULL ? 0 : 1)}}, so a NULL x gives sum = NULL.
> {{AvgAggFunction.mergeExpressions}} merges with {{sum = sum +
> mergeOperand(sum)}} without a NULL check (AVG's sum buffer is otherwise never
> NULL), so the global AVG's sum becomes NULL.
> {{SumAggFunction.mergeExpressions}} checks for NULL, which is why SUM is
> correct. Projecting a NULL argument to 0 for AVG, or a NULL-safe merge, would
> fix it.
> Reproduces on 2.0.1, 2.2.1, 2.3.0 and master.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)