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

tidb sql count bug #9125

Closed
jiucaiProductions opened this issue Jan 21, 2019 · 2 comments · Fixed by #10974
Closed

tidb sql count bug #9125

jiucaiProductions opened this issue Jan 21, 2019 · 2 comments · Fixed by #10974
Assignees
Labels
sig/execution SIG execution type/bug The issue is confirmed as a bug.

Comments

@jiucaiProductions
Copy link

-- user default test db
use test;

select @@Version;
-- 5.7.10-TiDB-v2.1.0-5-g4dad722

CREATE TABLE t1 (
name varchar(100) DEFAULT NULL
);

insert into t1 (name) values ('test');

-- return 0
SELECT COUNT(1)
FROM (
SELECT COUNT(1)
FROM (
SELECT *
FROM t1
WHERE name = 'test') t)
t2;

insert into t1 (name) values ('test2');

-- return 1
SELECT COUNT(1)
FROM (
SELECT COUNT(1)
FROM (
SELECT *
FROM t1
WHERE name = 'test') t)
t2;

truncate table t1;

-- repeat above, the seconds count sql return 0;

@eurekaka
Copy link
Contributor

eurekaka commented Jan 21, 2019

@jiucaiProductions thanks for the report, we will fix this.

I reproduced this issue on latest master branch. Here is the simplified reproduction step:

mysql> CREATE TABLE t1 (name varchar(100) DEFAULT NULL);

mysql> insert into t1 (name) values ('test');
Query OK, 1 row affected (0.00 sec)

mysql> explain analyze SELECT COUNT(1) FROM ( SELECT COUNT(1) FROM ( SELECT * FROM t1 WHERE name = 'test') t) t2;
+----------------------------+----------+------+-------------------------------------------------------------+----------------------------------+
| id                         | count    | task | operator info                                               | execution info                   |
+----------------------------+----------+------+-------------------------------------------------------------+----------------------------------+
| StreamAgg_13               | 1.00     | root | funcs:count(1)                                              | time:242.867µs, loops:2, rows:1  |
| └─StreamAgg_28             | 1.00     | root |                                                             | time:239.484µs, loops:1, rows:0  |
|   └─TableReader_29         | 1.00     | root | data:StreamAgg_17                                           | time:236.995µs, loops:1, rows:0  |
|     └─StreamAgg_17         | 1.00     | cop  |                                                             |                                  |
|       └─Selection_27       | 10.00    | cop  | eq(test.t1.name, "test")                                    |                                  |
|         └─TableScan_26     | 10000.00 | cop  | table:t1, range:[-inf,+inf], keep order:false, stats:pseudo | time:0s, loops:0, rows:1         |
+----------------------------+----------+------+-------------------------------------------------------------+----------------------------------+
6 rows in set (0.00 sec)

mysql> explain analyze SELECT COUNT(1) FROM ( SELECT COUNT(1) FROM ( SELECT * FROM t1 WHERE name = 'test') t) t2;
+----------------------------+----------+------+-------------------------------------------------------------+----------------------------------+
| id                         | count    | task | operator info                                               | execution info                   |
+----------------------------+----------+------+-------------------------------------------------------------+----------------------------------+
| StreamAgg_13               | 1.00     | root | funcs:count(1)                                              | time:262.616µs, loops:2, rows:1  |
| └─StreamAgg_28             | 1.00     | root |                                                             | time:258.004µs, loops:1, rows:0  |
|   └─TableReader_29         | 1.00     | root | data:StreamAgg_17                                           | time:255.293µs, loops:1, rows:0  |
|     └─StreamAgg_17         | 1.00     | cop  |                                                             |                                  |
|       └─Selection_27       | 10.00    | cop  | eq(test.t1.name, "test")                                    |                                  |
|         └─TableScan_26     | 10000.00 | cop  | table:t1, range:[-inf,+inf], keep order:false, stats:pseudo | time:0s, loops:0, rows:1         |
+----------------------------+----------+------+-------------------------------------------------------------+----------------------------------+
6 rows in set (0.00 sec)

mysql> explain analyze SELECT COUNT(1) FROM ( SELECT COUNT(1) FROM ( SELECT * FROM t1 WHERE name = 'test') t) t2;
+--------------------------+-------+------+-------------------------------------------------------------+----------------------------------+
| id                       | count | task | operator info                                               | execution info                   |
+--------------------------+-------+------+-------------------------------------------------------------+----------------------------------+
| StreamAgg_13             | 1.00  | root | funcs:count(1)                                              | time:267.2µs, loops:2, rows:1    |
| └─StreamAgg_18           | 1.00  | root |                                                             | time:258.924µs, loops:2, rows:1  |
|   └─TableReader_25       | 0.00  | root | data:Selection_24                                           | time:252.562µs, loops:2, rows:1  |
|     └─Selection_24       | 0.00  | cop  | eq(test.t1.name, "test")                                    |                                  |
|       └─TableScan_23     | 1.00  | cop  | table:t1, range:[-inf,+inf], keep order:false, stats:pseudo | time:0s, loops:0, rows:1         |
+--------------------------+-------+------+-------------------------------------------------------------+----------------------------------+
5 rows in set (0.00 sec)

After auto analyze has been executed in background, plan generated would change to not push aggregation down to storage layer. Looks like the problem is caused by TableReader_29 and StreamAgg_17. Note that I am using mockTikv here.

@eurekaka eurekaka added type/bug The issue is confirmed as a bug. sig/execution SIG execution labels Jan 21, 2019
@winoros
Copy link
Member

winoros commented Jan 21, 2019

We've located the root cause of this problem. It will be fixed soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants