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

Same agg func in one sql will gets different result #26958

Closed
wshwsh12 opened this issue Aug 6, 2021 · 2 comments · Fixed by #26959
Closed

Same agg func in one sql will gets different result #26958

wshwsh12 opened this issue Aug 6, 2021 · 2 comments · Fixed by #26959
Labels
severity/critical sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@wshwsh12
Copy link
Contributor

wshwsh12 commented Aug 6, 2021

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

use test;
drop table if exists t1;
create table t1 (c_int int not null);
insert into t1 values (1), (2), (3),(1),(2),(3);
drop table if exists t2;
create table t2 (c_int int not null);
insert into t2 values (1), (2), (3),(1),(2),(3);

select 
(select count(distinct c_int) from t2 where c_int >= t1.c_int) c1, 
(select count(distinct c_int) from t2 where c_int >= t1.c_int) c2
from t1 group by c_int;

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

+------+------+
| c1   | c2   |
+------+------+
|    3 |    3 |
|    2 |    2 |
|    1 |    1 |
+------+------+
3 rows in set (0.001 sec)

3. What did you see instead (Required)

+----+----+
| c1 | c2 |
+----+----+
|  4 |  2 |
|  6 |  3 |
|  2 |  1 |
+----+----+
3 rows in set (0.004 sec)

4. What is your TiDB version? (Required)

v4.0, v5.0, v5.1, master

@wshwsh12 wshwsh12 added the type/bug The issue is confirmed as a bug. label Aug 6, 2021
@wshwsh12
Copy link
Contributor Author

wshwsh12 commented Aug 6, 2021

Only select one column will also gets wrong reuslt.

[tidb]> select 
    -> (select count(distinct c_int) from t2 where c_int >= t1.c_int) c1
    -> from t1 group by c_int;
+----+
| c1 |
+----+
|  6 |
|  2 |
|  4 |
+----+
3 rows in set (0.001 sec)

See the expaln and find the dinstct flag is missing:

[tidb]> desc select  (select count(distinct c_int) from t2 where c_int >= t1.c_int) c1 from t1 group by c_int;
+--------------------------------+-------------+-----------+---------------+------------------------------------------------------------------------+
| id                             | estRows     | task      | access object | operator info                                                          |
+--------------------------------+-------------+-----------+---------------+------------------------------------------------------------------------+
| HashAgg_12                     | 8000.00     | root      |               | group by:test.t1.c_int, funcs:count(test.t2.c_int)->Column#7           |
| └─HashJoin_14                  | 80000000.00 | root      |               | CARTESIAN left outer join, other cond:ge(test.t2.c_int, test.t1.c_int) |
|   ├─HashAgg_17(Build)          | 8000.00     | root      |               | group by:test.t1.c_int, funcs:firstrow(test.t1.c_int)->test.t1.c_int   |
|   │ └─TableReader_22           | 10000.00    | root      |               | data:TableFullScan_21                                                  |
|   │   └─TableFullScan_21       | 10000.00    | cop[tikv] | table:t1      | keep order:false, stats:pseudo                                         |
|   └─TableReader_24(Probe)      | 10000.00    | root      |               | data:TableFullScan_23                                                  |
|     └─TableFullScan_23         | 10000.00    | cop[tikv] | table:t2      | keep order:false, stats:pseudo                                         |
+--------------------------------+-------------+-----------+---------------+------------------------------------------------------------------------+
7 rows in set (0.001 sec)

@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
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.

3 participants