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

Order by with aggregation function in sub query not working #30025

Open
Alkaagr81 opened this issue Nov 22, 2021 · 3 comments
Open

Order by with aggregation function in sub query not working #30025

Alkaagr81 opened this issue Nov 22, 2021 · 3 comments
Assignees
Labels
affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 affects-6.6 affects-7.0 affects-7.1 affects-7.5 affects-8.1 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@Alkaagr81
Copy link
Collaborator

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

set @@sql_mode = default;
drop table if exists t1,t2;
CREATE TABLE t1(a INTEGER);
INSERT INTO t1 VALUES (1), (2);
SELECT a FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t2.a) FROM t1 AS t2);

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

mysql> set @@sql_mode = default;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table if exists t1,t2;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE t1(a INTEGER);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO t1 VALUES (1), (2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT a FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
ERROR 3029 (HY000): Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
+--------+
| SUM(a) |
+--------+
|      3 |
+--------+

mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t2.a) FROM t1 AS t2);
+--------+
| SUM(a) |
+--------+
|      3 |
+--------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

mysql> set @@sql_mode = default;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table if exists t1,t2;
Query OK, 0 rows affected (0.51 sec)

mysql> CREATE TABLE t1(a INTEGER);
Query OK, 0 rows affected (0.07 sec)

mysql> INSERT INTO t1 VALUES (1), (2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT a FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t2.a) FROM t1 AS t2);
+--------+
| SUM(a) |
+--------+
|      3 |
+--------+
1 row in set (0.01 sec)

4. What is your TiDB version? (Required)

| Release Version: v5.2.2
Edition: Community
Git Commit Hash: da1c21fd45a4ea5900ac16d2f4a248143f378d18
Git Branch: heads/refs/tags/v5.2.2
UTC Build Time: 2021-10-20 06:03:45
GoVersion: go1.16.5
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
@Alkaagr81 Alkaagr81 added the type/bug The issue is confirmed as a bug. label Nov 22, 2021
@sylzd
Copy link
Contributor

sylzd commented Nov 29, 2021

it can be optimized by logic plan, when sum encounter order by, we can prune the order by, make it a simple query.
any suggestions or duplicated task? @winoros @XuHuaiyu

mysql > explain SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t2.a) FROM t1 AS t2);
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

Mon Nov 29 20:20:01 2021
mysql > show warnings;
+-------+------+-------------------------------------------------------------------------+
| Level | Code | Message                                                                 |
+-------+------+-------------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select sum(`test`.`t1`.`a`) AS `SUM(a)` from `test`.`t1` |
+-------+------+-------------------------------------------------------------------------+
1 row in set (0.00 sec)

@sylzd sylzd removed their assignment Dec 1, 2021
@ChenPeng2013 ChenPeng2013 added sig/planner SIG: Planner and removed sig/execution SIG execution labels Dec 1, 2021
@jebter jebter added affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. labels Jan 11, 2022
@AilinKid
Copy link
Contributor

after order by FD

mysql> SELECT a FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
ERROR 3029 (HY000): Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t2.a) FROM t1 AS t2);
+--------+
| SUM(a) |
+--------+
|      3 |
+--------+
1 row in set (0.00 sec)

@AilinKid
Copy link
Contributor

for the second query, as sylzd said, COUNT(t1.a) is always the same since outer query is seen as a whole group. In this way, order by clause can be eliminated, leading unnecessary to checking multi-row of a subquery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 affects-6.6 affects-7.0 affects-7.1 affects-7.5 affects-8.1 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

No branches or pull requests

8 participants