sbroeder commented on PR #5382:
URL: https://github.com/apache/calcite/pull/5382#issuecomment-6993270529

   > The agent review of this PR claims it has uncovered several bugs too:
   > 
   > * [ ]  WITHIN DISTINCT is dropped (wrong result). The rebuild at lines 
133-154 copies distinct, filter, sort and other fields of the call, but not 
distinctKeys. It also drops the aggregate's hints.
   > * Query: SUM(d2) WITHIN DISTINCT (deptno) over emp(deptno, deptno*2) UNION 
ALL dept(deptno, NULL).
   > * On scott, the result is 120 without the rule and 620 with it.
   > * [ ]  isPureGroupBy accepts GROUP BY () (lines 246-250). An 
Aggregate(group={}) returns 1 row even when its input is empty. The projection 
that replaces it returns 0 rows.
   > * Plan: Project(99, NULL) over Aggregate(group={}) over Filter(EMPNO < 0). 
The rule turns this into Project over Filter, and the group with key 99 is lost.
   > * Risk is low. RelBuilder folds this shape into VALUES, so only 
LogicalAggregate.create or plans built with pruneInputOfAggregate=false reach 
it.
   > * Fix: require a non-empty group set.
   > * [ ]  Type narrowing (a weak case). When the removed branch set the union 
column type, the rebuilt aggregate runs at the narrower type and then casts the 
result.
   > * A RelBuilder plan that unions SMALLINT with null:BIGINT becomes 
SUM(SMALLINT) and fails with "Value 37609 out of range".
   > * SQL cannot reach this: SqlToRel casts the kept branch, which I checked 
for BIGINT. The original RelBuilder plan also fails in Enumerable, with a Short 
to Long cast error.
   
   Thanks. I reproduced all three against scott and fixed them in 
670e28a0d58c099ec98ccced1bacefb24b3ecf56.
   
   Results:
   1. SUM(d2) WITHIN DISTINCT (deptno) over emp UNION ALL dept: 120 without the 
rule, 620 with the original rule as reported, 120 with the fixed rule.
   2. SUM(SMALLINT) UNION ALL null:BIGINT, built with RelBuilder: without the 
rule it fails with "ClassCastException: Short cannot be cast to Long", with the 
original rule it fails with "Value 37609 out of range", and with the fixed rule 
it returns 108172.
   
   Fixes:
   - WITHIN DISTINCT: the rule now skips any call that has distinctKeys (and 
rexList), and the rebuilt aggregate keeps the original hints.
   - GROUP BY (): isPureGroupBy now requires a non-empty group set.
   - Type narrowing: the remaining branch is cast to the union's column types 
when a type really differs. This plan already fails without the rule, so the 
rewrite is also more robust than the input.
   
   Tests: testAggregateUnionNullBranchWithinDistinct, 
testAggregateUnionNullBranchGlobalGroupByInBranch and 
testAggregateUnionNullBranchTypeNarrowing check the plan shapes. There are also 
execution tests in JdbcTest, which returns 120 with and without the rule, and 
RelBuilderTest, which returns 108172.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to