tidb icon indicating copy to clipboard operation
tidb copied to clipboard

Query failed to execute in TiDB but succeeded in MySQL

Open FullDuplexFish opened this issue 1 year ago • 5 comments

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

FullDuplexFish avatar Jun 03 '24 12:06 FullDuplexFish

https://github.com/pingcap/tidb/blob/1a24c032126dce4a79ea14b51108dbf4caf02a03/pkg/planner/core/expression_rewriter.go#L2410 https://github.com/pingcap/tidb/blob/1a24c032126dce4a79ea14b51108dbf4caf02a03/pkg/planner/core/expression_rewriter.go#L2441-L2457

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 avatar Jun 20 '24 03:06 dash12653

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 avatar Jun 20 '24 03:06 dash12653

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?

dash12653 avatar Jun 26 '24 08:06 dash12653

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?

FullDuplexFish avatar Jun 28 '24 03:06 FullDuplexFish

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~

dash12653 avatar Jun 28 '24 10:06 dash12653