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

Query failed to execute in TiDB but succeeded in MySQL #53766

Closed
Nickelth opened this issue Jun 3, 2024 · 5 comments · Fixed by #54334
Closed

Query failed to execute in TiDB but succeeded in MySQL #53766

Nickelth opened this issue Jun 3, 2024 · 5 comments · Fixed by #54334
Labels
affects-6.1 affects-6.5 affects-7.1 affects-7.5 affects-8.1 compatibility-mysql8 This is a compatibility issue with MySQL 8.0(but NOT 5.7) severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@Nickelth
Copy link

Nickelth commented Jun 3, 2024

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE t0(c0 int);
CREATE TABLE t1(c0 int);
SELECT t0.c0, t1.c0 FROM t0 NATURAL JOIN t1 WHERE '1' AND (t0.c0 IN (SELECT c0 FROM t0));

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

Query executed successfully.

3. What did you see instead (Required)

MySQL [d0]> SELECT t0.c0, t1.c0 FROM t0 NATURAL JOIN t1 WHERE '1' AND (t0.c0 IN (SELECT c0 FROM t0));
ERROR 1054 (42S22): Unknown column 't1.c0' in 'field list'

4. What is your TiDB version? (Required)

Release Version: v8.2.0-alpha-234-gdf64c343dd-dirty
Edition: Community
Git Commit Hash: df64c343ddfc4063891900acf95e2b3640c79f29
Git Branch: master
UTC Build Time: 2024-05-27 08:15:16
GoVersion: go1.21.7
Race Enabled: false
Check Table Before Drop: false
Store: tikv
@Nickelth Nickelth added the type/bug The issue is confirmed as a bug. label Jun 3, 2024
@jebter jebter added sig/planner SIG: Planner severity/major compatibility-mysql8 This is a compatibility issue with MySQL 8.0(but NOT 5.7) labels Jun 4, 2024
@dash12653
Copy link
Contributor

dash12653 commented Jun 20, 2024

col, name, err := findFieldNameFromNaturalUsingJoin(planCtx.plan, v)

func findFieldNameFromNaturalUsingJoin(p base.LogicalPlan, v *ast.ColumnName) (col *expression.Column, name *types.FieldName, err error) {
switch x := p.(type) {
case *LogicalLimit, *LogicalSelection, *LogicalTopN, *LogicalSort, *LogicalMaxOneRow:
return findFieldNameFromNaturalUsingJoin(p.Children()[0], v)
case *LogicalJoin:
if x.fullSchema != nil {
idx, err := expression.FindFieldName(x.fullNames, v)
if err != nil {
return nil, nil, err
}
if idx >= 0 {
return x.fullSchema.Columns[idx], x.fullNames[idx], nil
}
}
}
return nil, nil, nil
}

It seems that when rewriting an expression, if the SQL statement is a natural join, the field will be retrieved again based on the plan's fullNames. However, at this point, the plan's fullSchema is empty, and null is directly returned, resulting in the inability to find t1.c0.
asdasdasd.png

@dash12653
Copy link
Contributor

And when there is a natural join and the where clause is converted to (* github. com/pingcap/tidb/pkg/planner/core. LogicalAggregation), this bug will appear

tidb> SELECT t1.c0, t0.c0 FROM t0 NATURAL JOIN t1 where (select c0 from t2 group by c0);
ERROR 1054 (42S22): Unknown column 't1.c0' in 'field list'
tidb> SELECT t1.c0, t0.c0 FROM t0 NATURAL JOIN t1 where (t0.c0 in (select c0 from t2 group by c0));
ERROR 1054 (42S22): Unknown column 't1.c0' in 'field list'
tidb> SELECT t1.c0, t0.c0 FROM t0 NATURAL JOIN t1 where (select distinct c0 from t2);
ERROR 1054 (42S22): Unknown column 't1.c0' in 'field list'

@dash12653
Copy link
Contributor

I have modified some code in my repository, but I am not familiar with the TIDB test process. Can anyone help to check which kind of test I should add and how to add test cases?

@Nickelth
Copy link
Author

I have modified some code in my repository, but I am not familiar with the TIDB test process. Can anyone help to check which kind of test I should add and how to add test cases?

I'm not fimiliar with development of TIDB, maybe this doc will help?

@dash12653
Copy link
Contributor

I have modified some code in my repository, but I am not familiar with the TIDB test process. Can anyone help to check which kind of test I should add and how to add test cases?

I'm not fimiliar with development of TIDB, maybe this doc will help?

Thanks for reminding~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-6.1 affects-6.5 affects-7.1 affects-7.5 affects-8.1 compatibility-mysql8 This is a compatibility issue with MySQL 8.0(but NOT 5.7) severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
4 participants