[ 
https://issues.apache.org/jira/browse/CALCITE-6403?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18041751#comment-18041751
 ] 

Zhen Chen commented on CALCITE-6403:
------------------------------------

I ran the test on the main branch, and the results appear to be consistent with 
PostgreSQL. I'm not sure if this issue has been fixed already, or if I might 
have misunderstood something. If you have time, could you please verify this 
again?
{code:java}
!use blank
create table T (col1 INT, col2 INT);
(0 rows modified)!update
SELECT  COUNT (*), COUNT (DISTINCT COL1) FROM T WHERE false;
+--------+--------+
| EXPR$0 | EXPR$1 |
+--------+--------+
|   0   |    0 |
+--------+--------+
(1 row)!ok
EnumerableCalc(expr#0..1=[{inputs}], expr#2=[CAST($t0):BIGINT NOT NULL], 
EXPR$0=[$t2], EXPR$1=[$t1])
  EnumerableAggregate(group=[{}], EXPR$0_g0=[MIN($1) FILTER $3], 
EXPR$1_g0=[COUNT($0) FILTER $2])
    EnumerableValues(tuples=[[]])
!plan {code}

> Rule AGGREGATE_EXPAND_DISTINCT_AGGREGATES is unsound
> ----------------------------------------------------
>
>                 Key: CALCITE-6403
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6403
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.37.0
>            Reporter: Mihai Budiu
>            Priority: Minor
>
> Here is a simple SQL program that operates on a collection with no input rows:
> {code:sql}
> SELECT COUNT(*), COUNT(DISTINCT COL1) FROM T WHERE false
> {code}
> The rewrite rule expands this into:
> {code}
>     LogicalProject(EXPR$0=[CAST($0):BIGINT NOT NULL], EXPR$1=[$1]), id = 39
>       LogicalAggregate(group=[{}], EXPR$0=[MIN($1) FILTER $3], 
> EXPR$1=[COUNT($0) FILTER $2]), id = 37
>         LogicalProject(COL1=[$0], EXPR$0=[$1], $g_0=[=($2, 0)], $g_1=[=($2, 
> 1)]), id = 35
>           LogicalAggregate(group=[{0}], groups=[[{0}, {}]], EXPR$0=[COUNT()], 
> $g=[GROUPING($0)]), id = 30
>             LogicalValues(tuples=[[]]), id = 22
> {code}
> Notice that there is an inner group-by aggregate that produces an empty set, 
> and an outer aggregation that uses MIN. MIN for an empty collection is NULL, 
> whereas the original query should have produced 0.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to