[Bug] Incorrect result of COUNT(DISTINCT expr) FILTER (WHERE condition) in Cloudberry 2.1.0
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 65/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
まず、gp_enable_multiphase_agg を有効にした場合と無効にした場合で SQL の例を再現し、次に FILTER 句を含む DISTINCT 式に対する multiphase aggregate の処理を追跡します。両方の意味的に等価な式が両モードで同じ値を返し、報告されたケースに対するリグレッションテストのカバレッジが追加されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Issue Body
Describe the bug
Apache Cloudberry returns incorrect results for COUNT(DISTINCT ...) FILTER (WHERE ...)
when gp_enable_multiphase_agg is enabled.
Two semantically equivalent aggregate expressions return different results.
Example:
COUNT(DISTINCT goods_gid)
FILTER (WHERE invqty = 0)
COUNT(DISTINCT CASE WHEN invqty = 0 THEN goods_gid END)
According to SQL semantics, these two expressions should always return the same value.
However, with gp_enable_multiphase_agg=on, Cloudberry returns different results.
To Reproduce
Create test table:
CREATE TABLE test_cnt
(
goods_gid int,
invqty int
)
DISTRIBUTED BY (goods_gid);
Insert test data:
INSERT INTO test_cnt VALUES
(1,0),
(1,1),
(2,0),
(3,1);
Run query with multiphase aggregation enabled:
SET gp_enable_multiphase_agg=on;
SELECT
COUNT(DISTINCT goods_gid)
FILTER (WHERE invqty = 0) AS cnt1,
COUNT(DISTINCT CASE
WHEN invqty = 0 THEN goods_gid
END) AS cnt2
FROM test_cnt;
query plan :
Aggregate (cost=1.12..1.13 rows=1 width=16) (actual time=1.000..1.000 rows=1 loops=1)
-> Gather Motion 8:1 (slice1; segments: 8) (cost=0.00..1.07 rows=5 width=8) (actual time=0.000..0.000 rows=5 loops=1)
-> Seq Scan on test_cnt (cost=0.00..1.01 rows=1 width=8) (actual time=0.000..0.000 rows=2 loops=1)
Planning Time: 1.111 ms
(slice0) Executor memory: 31K bytes.
(slice1) Executor memory: 39K bytes avg x 8x(0) workers, 112K bytes max (seg0).
Memory used: 128000kB
Optimizer: Postgres query optimizer
Execution Time: 4.177 ms
Actual result:
cnt1 | cnt2
-----+-----
3 | 2
This result is incorrect.
Disable multiphase aggregation:
SET gp_enable_multiphase_agg=off;
SELECT
COUNT(DISTINCT goods_gid)
FILTER (WHERE invqty = 0) AS cnt1,
COUNT(DISTINCT CASE
WHEN invqty = 0 THEN goods_gid
END) AS cnt2
FROM test_cnt;
Result:
cnt1 | cnt2
-----+-----
2 | 2
The result is correct.
Expected behavior
The following expressions should always return identical results:
COUNT(DISTINCT expression)
FILTER (WHERE condition)
and
COUNT(DISTINCT CASE WHEN condition THEN expression END)
The value should not depend on whether multiphase aggregation is enabled.
Actual behavior
When:
gp_enable_multiphase_agg=on
Cloudberry returns incorrect aggregate results.
When:
gp_enable_multiphase_agg=off
the result is correct.
Additional information
The issue is reproducible in our production environment.
The problem appears related to the combination of:
COUNT(DISTINCT ...)
FILTER clause
Multiphase aggregation
MPP aggregate execution
The workaround is to rewrite:
COUNT(DISTINCT col)
FILTER (WHERE condition)
as:
COUNT(DISTINCT CASE WHEN condition THEN col END)
or disable:
SET gp_enable_multiphase_agg=off;
- 主要言語
- C
- スター
- 1.4k
- フォーク
- 248
- 平均マージ
- 4日 10時間
- マージ済み PR(30日)
- 40
環境構築
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/cloudberry のほかの issue
-
type: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
apache/cloudberry#1885 · リアクション 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
apache/cloudberry#1825 ·
メンテナーはふだん 1 日以内に返信
-
type: Bug
難易度 3/5 1〜2日 初心者へのやさしさ 65/100
apache/cloudberry#2048 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
type: Bug
難易度 4/5 3〜5日 初心者へのやさしさ 40/100
apache/cloudberry#2047 ·
メンテナーはふだん 1 日以内に返信
-
type: Bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
apache/cloudberry#2046 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
apache/cloudberry の issue をすべて見る
似ている issue
-
area:lint-tooling bug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
InauguralSystems/EigenScript#1340 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: chunk_span_bounds and _validated_chunk_spans reject Pydantic models ChunkSpan and AudioFileオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
BasedHardware/omi#19047 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
obsproject/obs-studio#13936 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
category:port-update
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信