Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong result when evaluating correlated aggregate functions #18350

Closed
XuHuaiyu opened this issue Jul 3, 2020 · 3 comments · Fixed by #21431
Closed

wrong result when evaluating correlated aggregate functions #18350

XuHuaiyu opened this issue Jul 3, 2020 · 3 comments · Fixed by #21431
Assignees
Labels
challenge-program help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. severity/critical sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Milestone

Comments

@XuHuaiyu
Copy link
Contributor

XuHuaiyu commented Jul 3, 2020

Description

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE tab(i INT); 
CREATE TABLE tab2(j INT); 

insert into tab values(1),(2),(3);
insert into tab2 values(1),(2),(3),(15);

SELECT m.i, 
       (SELECT COUNT(n.j) 
           FROM tab2 WHERE j=15) AS o 
    FROM tab m, tab2 n GROUP BY 1 order by m.i;

2. What did you expect to see? (Required)

mysql> SELECT m.i,
         (SELECT COUNT(n.j)
             FROM tab2 WHERE j=15) AS o
      FROM tab m, tab2 n GROUP BY 1 order by m.i;
+------+------+
| i    | o    |
+------+------+
|    1 |    4 |
|    2 |    4 |
|    3 |    4 |
+------+------+
3 rows in set (0.00 sec)

3. What did you see instead (Required)

tidb> SELECT m.i,
         (SELECT COUNT(n.j)
             FROM tab2 WHERE j=15) AS o
      FROM tab m, tab2 n GROUP BY 1 order by m.i;
+------+---+
| i    | o |
+------+---+
|    1 | 1 |
|    2 | 1 |
|    3 | 1 |
+------+---+
3 rows in set (0.00 sec)

4. Affected version (Required)

all the versions

5. Root Cause Analysis

TiDB does not handle correlated aggregate in subqueries correctly. I do not find any references from MySQL official manual, but this doc can be referred.

SIG slack channel

#sig-planner

Score

  • 900

Mentor

@XuHuaiyu XuHuaiyu added type/bug The issue is confirmed as a bug. sig/planner SIG: Planner labels Jul 3, 2020
@XuHuaiyu XuHuaiyu changed the title wrong result when aggregate has a correlated column in subquery wrong result when evaluating correlated aggregate functions Jul 6, 2020
@XuHuaiyu
Copy link
Contributor Author

XuHuaiyu commented Sep 23, 2020

We may rewrite the SQL to

SELECT m.i, 
       (SELECT (select COUNT(tab2.j) from tab2) 
           FROM tab2 WHERE j=15) AS o 
    FROM tab m, tab2 n GROUP BY 1 order by m.i;

@lzmhhh123 lzmhhh123 added challenge-program help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. labels Oct 30, 2020
@dyzsr
Copy link
Contributor

dyzsr commented Nov 10, 2020

/assign

@ti-srebot
Copy link
Contributor

Please edit this comment or add a new comment to complete the following information

Not a bug

  1. Remove the 'type/bug' label
  2. Add notes to indicate why it is not a bug

Duplicate bug

  1. Add the 'type/duplicate' label
  2. Add the link to the original bug

Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: #20100

1. Root Cause Analysis (RCA) (optional)

2. Symptom (optional)

3. All Trigger Conditions (optional)

4. Workaround (optional)

5. Affected versions

6. Fixed versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenge-program help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. severity/critical sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants