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

SQL Mode only_full_group_by incorrectly errors with DISTINCT + ORDER BY #29418

Open
espresso98 opened this issue Nov 3, 2021 · 3 comments
Open
Assignees
Labels
feature/developing the related feature is in development severity/minor sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@espresso98
Copy link
Collaborator

espresso98 commented Nov 3, 2021

Bug Report

1. Minimal reproduce step (Required)

DROP TABLE IF EXISTS t, tj;
CREATE TABLE t(a bigint, b bigint, c bigint);
INSERT INTO t VALUES (1, 2, 1), (1, 2, 2), (1, 3, 1), (1, 3, 2);
SELECT DISTINCT a, b FROM t ORDER BY c;  # error in both TiDB + MySQL (requires only full group by unset)

CREATE TABLE tj(j JSON, i INT DEFAULT 7);
INSERT INTO tj(j) VALUES ('1');
INSERT INTO tj(j) VALUES ('2');
INSERT INTO tj(j) VALUES ('3');
INSERT INTO tj(j) VALUES ('4');
INSERT INTO tj(j) VALUES ('5');
INSERT INTO tj(j) VALUES (NULL);
INSERT INTO tj(j) VALUES ('3.14');
INSERT INTO tj(j) VALUES ('[1,2,3]');
INSERT INTO tj(j) VALUES (NULL);
SELECT DISTINCT i,NTILE(3) OVER (ORDER BY i), MAX(i) OVER (), COUNT(*) OVER () FROM tj ORDER BY NTILE(3) OVER (ORDER BY i); # works in MySQL; error in TiDB (requires only full group by unset)

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

..

mysql [localhost:8024] {root} (test) > select distinct a, b from t order by c;
ERROR 3065 (HY000): Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t.c' which is not in SELECT list; this is incompatible with DISTINCT
..
mysql [localhost:8024] {root} (test) > SELECT DISTINCT i,NTILE(3) OVER (ORDER BY i), MAX(i) OVER (), COUNT(*) OVER () FROM tj ORDER BY NTILE(3) OVER (ORDER BY i);
+------+----------------------------+----------------+------------------+
| i    | NTILE(3) OVER (ORDER BY i) | MAX(i) OVER () | COUNT(*) OVER () |
+------+----------------------------+----------------+------------------+
|    7 |                          1 |              7 |               11 |
|    7 |                          2 |              7 |               11 |
|    7 |                          3 |              7 |               11 |
+------+----------------------------+----------------+------------------+
3 rows in set (0.00 sec)

3. What did you see instead (Required)

..
tidb> select distinct a, b from t order by c;
ERROR 3065 (HY000): Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t.c' which is not in SELECT list; this is incompatible with DISTINCT
..
tidb> SELECT DISTINCT i,NTILE(3) OVER (ORDER BY i), MAX(i) OVER (), COUNT(*) OVER () FROM tj ORDER BY NTILE(3) OVER (ORDER BY i);
ERROR 3065 (HY000): Expression #1 of ORDER BY clause is not in SELECT list, references column '' which is not in SELECT list; this is incompatible with DISTINCT

4. What is your TiDB version? (Required)

+-------------------------+--------------------------------------------------------------------------+
| Variable_name           | Value                                                                    |
+-------------------------+--------------------------------------------------------------------------+
| innodb_version          | 5.6.25                                                                   |
| protocol_version        | 10                                                                       |
| tidb_analyze_version    | 2                                                                        |
| tidb_row_format_version | 2                                                                        |
| tls_version             | TLSv1,TLSv1.1,TLSv1.2                                                    |
| version                 | 5.7.25-TiDB-v5.2.2                                                       |
| version_comment         | TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible |
| version_compile_machine | x86_64                                                                   |
| version_compile_os      | osx10.8                                                                  |
+-------------------------+--------------------------------------------------------------------------+
@espresso98 espresso98 added the type/bug The issue is confirmed as a bug. label Nov 3, 2021
@morgo morgo changed the title Query that has DISTINCT and ORDER BY returns an error on default mode SQL Mode only_full_group_by incorrectly errors with DISTINCT + ORDER BY Nov 3, 2021
@chrysan
Copy link
Contributor

chrysan commented Nov 10, 2021

/assign xuyifangreeneyes

@winoros
Copy link
Member

winoros commented Nov 23, 2021

We are using the #29766 tracking the whole thing. Move the severity down.

@winoros winoros added feature/developing the related feature is in development severity/minor and removed severity/moderate labels Dec 15, 2021
@xuyifangreeneyes
Copy link
Contributor

The problem still exists when set tidb_enable_new_only_full_group_by_check to 1. /cc @AilinKid @winoros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/developing the related feature is in development severity/minor sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

No branches or pull requests

5 participants