Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[Bug] Incorrect result of COUNT(DISTINCT expr) FILTER (WHERE condition) in Cloudberry 2.1.0

オープン
#1,916 コメント 0 件 リアクション 4 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
65/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c, postgresql, sql

調査の方向性

まず、gp_enable_multiphase_agg を有効にした場合と無効にした場合で SQL の例を再現し、次に FILTER 句を含む DISTINCT 式に対する multiphase aggregate の処理を追跡します。両方の意味的に等価な式が両モードで同じ値を返し、報告されたケースに対するリグレッションテストのカバレッジが追加されていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

type: Bug

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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/cloudberry のほかの issue

apache/cloudberry の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。