-
Bug
-
Resolution: Done
-
Blocker
-
10.2
-
None
Teiid returns different results for a query with GROUP BY and SUM(1) (summation of the number 1) when there is a LEFT or INNER JOIN involved.
There are two problems: a) incorrect check in MergeJoinStrategy.compareToPrevious method which generates TEIID31202 exception and b) a bug in algorithm of join itself which leads to incorrect results.
To reproduce the bug, please, run the following queries and compare theSum and theCount column values:
select r.city, sum(1) as theSum ,count(*) as theCount from "dsp.address_pg" r left join "adventureworks.stateprovince" c on "r.stateprovinceid" = "c.stateprovinceid" group by r.city order by r.city ;;
select r.city, sum(1) as theSum ,count(*) as theCount from "dsp.address_pg" r inner join "adventureworks.stateprovince" c on "r.stateprovinceid" = "c.stateprovinceid" group by r.city order by r.city ;;
but the following queries return correct results:
select r.city, sum(1) as theSum ,count(*) as theCount from "dsp.address_pg" r right join "adventureworks.stateprovince" c on "r.stateprovinceid" = "c.stateprovinceid" group by r.city order by r.city ;;
the second one is also correct (LEFT JOIN) but uses window function:
select distinct city, sum(1) OVER (PARTITION BY city) as theSum ,count(*) OVER (PARTITION BY city) as theCount from "dsp.address_pg" r left join "adventureworks.stateprovince" c on "r.stateprovinceid" = "c.stateprovinceid" order by r.city ;;