[
https://issues.apache.org/jira/browse/CALCITE-6403?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17844802#comment-17844802
]
Mihai Budiu commented on CALCITE-6403:
--------------------------------------
In this instance indeed I have applied the rule to a plan where prior rules had
discovered that the input was empty.
This is the input plan for the rule:
{code}
LogicalAggregate(group=[{}], EXPR$0=[COUNT()], EXPR$1=[COUNT(DISTINCT
$0)]), id = 25
LogicalValues(tuples=[[]]), id = 22
{code}
But the same problem exists when the predicate is not statically "false". The
rule still expands into a MIN, which is still NULL at runtime.
For my query this is the input plan in this case:
{code}
LogicalAggregate(group=[{}], EXPR$0=[COUNT()], EXPR$1=[COUNT(DISTINCT
$0)]), id = 26
LogicalProject(COL1=[$0]), id = 24
LogicalFilter(condition=[>($0, 10000)]), id = 22
LogicalTableScan(table=[[schema, T]]), id = 1
{code}
and here is the output plan:
{code}
LogicalProject(EXPR$0=[CAST($0):BIGINT NOT NULL], EXPR$1=[$1]), id = 44
LogicalAggregate(group=[{}], EXPR$0=[MIN($1) FILTER $3],
EXPR$1=[COUNT($0) FILTER $2]), id = 42
LogicalProject(COL1=[$0], EXPR$0=[$1], $g_0=[=($2, 0)], $g_1=[=($2,
1)]), id = 40
LogicalAggregate(group=[{0}], groups=[[{0}, {}]], EXPR$0=[COUNT()],
$g=[GROUPING($0)]), id = 35
LogicalProject(COL1=[$0]), id = 31
LogicalFilter(condition=[>($0, 10000)]), id = 29
LogicalTableScan(table=[[schema, T]]), id = 1
{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)