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

Table alias is not propagated to subquery #26945

Closed
wolf31o2 opened this issue Aug 5, 2021 · 9 comments · Fixed by #33640
Closed

Table alias is not propagated to subquery #26945

wolf31o2 opened this issue Aug 5, 2021 · 9 comments · Fixed by #33640
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 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@wolf31o2
Copy link

wolf31o2 commented Aug 5, 2021

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

DROP TABLE IF EXISTS t1, t2;
CREATE TABLE t1 (a INT, b INT);
CREATE TABLE t2 (a INT, b INT);
INSERT INTO t1 VALUES (1, 1);
INSERT INTO t2 VALUES (1, 1);
SELECT one.a FROM t1 one ORDER BY (SELECT two.b FROM t2 two WHERE two.a = one.b);

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

+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

ERROR 1054 (42S22): Unknown column 'one.b' in 'where clause'

4. What is your TiDB version? (Required)

Release Version: v5.1.0
Edition: Community
Git Commit Hash: 8acd5c88471cb7b4d4c4a8ed73b4d53d6833f13e
Git Branch: heads/refs/tags/v5.1.0
UTC Build Time: 2021-06-24 07:10:32
GoVersion: go1.16.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
@wolf31o2 wolf31o2 added the type/bug The issue is confirmed as a bug. label Aug 5, 2021
@kolbe
Copy link
Contributor

kolbe commented Aug 5, 2021

@wolf31o2 you said you identified this problem due to a query generated by Hibernate, is that correct? I don't know a great deal about Hibernate, but is it possible for you to share information about the code path, methods, etc., that cause Hibernate to generate this query?

@wolf31o2
Copy link
Author

wolf31o2 commented Aug 6, 2021

Hibernate is a Java ORM. I could probably create some code to generate this. This is a simplified test case of a single query which is failing in TiDB, but passes in MySQL, and is automatically generated in Hibernate using the org.hibernate.dialect.MySQL57InnoDBDialect dialect on version 5.5.6.Final but the issue is readily reproducible without Hibernate involved.

@hawkingrei
Copy link
Member

@wolf31o2 I've located the cause of the problem and have started working on a fix. When the fix is complete, issue will be updated.

@rebelice
Copy link
Contributor

rebelice commented Aug 9, 2021

@wolf31o2 Thanks for your issue. I found that order by (subquery) is not legal. See details in https://dev.mysql.com/doc/internals/en/optimizer-order-by-clauses.html. Could you please tell me the meaning of this query?

@kolbe
Copy link
Contributor

kolbe commented Aug 9, 2021

@rebelice how did you come to the conclusion that "order by (subquery) is not legal"? The document you linked to discusses certain order by optimizations, it certainly does not try to cover all the legal syntax of the order by clause.

Please try testing the reproduce steps in MySQL, and you will find that they work correctly there.

@wolf31o2
Copy link
Author

wolf31o2 commented Aug 9, 2021

Also, this syntax DOES work in TiDB:

SELECT one.a one.b FROM t1 one ORDER BY (SELECT two.b FROM t2 two WHERE two.a = one.b);

So having one.b in the outer query produces a correct query and is processed the same in TiDB as in MySQL. Both syntax work in MySQL.

@hawkingrei hawkingrei removed their assignment Aug 22, 2021
@yudongusa
Copy link

yudongusa commented Aug 23, 2021

@wolf31o2 I've located the cause of the problem and have started working on a fix. When the fix is complete, issue will be updated.

@hawkingrei is there any update on the fix? I'm assigning this back to you to follow up for now.

@yudongusa yudongusa added the sig/sql-infra SIG: SQL Infra label Aug 23, 2021
@bb7133 bb7133 removed the sig/sql-infra SIG: SQL Infra label Aug 24, 2021
@rebelice
Copy link
Contributor

Currently TiDB does not support the following statements
SELECT one.a one.b FROM t1 one ORDER BY (SELECT two.b FROM t2 two WHERE two.a = one.b);
Although it can pass the parser, it cannot get the correct answer like in MySQL. In TiDB, the following order by (sub-query) will become a constant. This is determined by our execution framework.
We will further evaluate this feature and fix this issue while supporting this feature.

@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
@chrysan
Copy link
Contributor

chrysan commented Mar 18, 2022

/assign @AilinKid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment