[Bug] Incorrect SUM calculation in CTE with correlated subquery #8724
Labels
analyzer
bug
Something isn't working
customer issue
good repro
Easily reproducible bugs
sql
Issue with SQL
I got some help from an LLM to write this description! So forgive me if it's a little verbose :-)
Description
I've discovered a bug in the
go-mysql-server
implementation where a correlated subquery within a CTE produces incorrect results for theSUM
function.Steps to Reproduce
Expected Behavior
The query should return two rows, each with a
PriceTotal
of 70 (45 + 25).This SQL Fiddle shows the expected behaviour: https://sqlfiddle.com/mysql/online-compiler?id=3885380d-b06e-4334-9a5f-d528dbbaac33
Actual Behavior
The query returns two rows, but each has a
PriceTotal
of 90. It appears that theSUM
function is incorrectly adding the price of the first row twice (45 + 45) instead of summing all rows correctly.Environment
Additional Context
I've verified that this query produces the correct results (PriceTotal of 70) when run against a standard MySQL server (see the SQL Fiddle linked above). The issue seems to be specific to the go-mysql-server implementation.
I've also verified that the issue only occurs once the CTE is introduced. Without the CTE the Subquery works fine.
Possible Cause
There might be an issue with how correlated subqueries are handled within CTEs, specifically when aggregating functions like
SUM
are involved.Suggested Fix
The implementation of correlated subqueries within CTEs may need to be reviewed, particularly focusing on how aggregations are computed in this context.
The text was updated successfully, but these errors were encountered: