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

Incompatible error when grouping on join #25041

Closed
kolbe opened this issue Jun 2, 2021 · 6 comments · Fixed by #25094
Closed

Incompatible error when grouping on join #25041

kolbe opened this issue Jun 2, 2021 · 6 comments · Fixed by #25094
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. severity/moderate sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@kolbe
Copy link
Contributor

kolbe commented Jun 2, 2021

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

select region_id from information_schema.tikv_region_status join information_schema.tikv_region_peers using (region_id) group by region_id;
create table t1 (id int);
create table t2 (id int);
select id, count(*) from t1 join t2 using (id) group by id;

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

This query should return results, because there cannot be any ambiguity when grouping on a column that is covered by the join condition.

3. What did you see instead (Required)

ERROR 1052 (23000): Column 'region_id' in field list is ambiguous

This type of query can execute without any error in MySQL:

8.0.23 (root) [test]> create table t1 (id int); create table t2 (id int); select id, count(*) from t1 join t2 using (id) group by id;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Empty set (0.00 sec)

4. What is your TiDB version? (Required)

Release Version: v5.0.1
Edition: Community
Git Commit Hash: 1145e347d3469d8e89f88dce86f6926ca44b3cd8
Git Branch: heads/refs/tags/v5.0.1
UTC Build Time: 2021-04-23 05:51:17
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
@kolbe kolbe added the type/bug The issue is confirmed as a bug. label Jun 2, 2021
@morgo
Copy link
Contributor

morgo commented Jun 2, 2021

This is specific to the use of using. If you use the alternative join syntax it doesn't work in MySQL:

mysql [localhost:8024] {msandbox} (test) > select id, count(*) from t1 join t2 using (id) group by id;
Empty set (0.00 sec)

mysql [localhost:8024] {msandbox} (test) > select id, count(*) from t1 join t2 on t1.id=t2.id group by id;
ERROR 1052 (23000): Column 'id' in field list is ambiguous

I am not sure how these are different. I would have thought it's functionally the same.

@kolbe
Copy link
Contributor Author

kolbe commented Jun 2, 2021

The behavior of NATURAL JOIN is the same as USING.

I tested this at https://www.db-fiddle.com/ on Postgres and SQLite and they have the same behavior as MySQL, so we're the outlier here. Let's fix it!

@eurekaka eurekaka self-assigned this Jun 2, 2021
@kolbe
Copy link
Contributor Author

kolbe commented Jun 2, 2021

Morgan found that MySQL actually rewrites the query with USING() so that id becomes t1.id in the field list:

https://gist.github.com/morgo/cc229e891c0abd0563b78b93ffb10cca

@eurekaka
Copy link
Contributor

eurekaka commented Jun 3, 2021

The problem is incurred by #21922 when trying to fix #6712, I have had a patch to fix this issue, but found another related problem on ONLY_FULL_GROUP_BY check, still working on it.

@eurekaka
Copy link
Contributor

eurekaka commented Jun 3, 2021

Filed a PR #25094 to solve this issue, the related incompatibility problems regarding only_full_group_by check are recorded in a separate issue #25089.

@ti-srebot
Copy link
Contributor

Please edit this comment or add a new comment to complete the following information

Not a bug

  1. Remove the 'type/bug' label
  2. Add notes to indicate why it is not a bug

Duplicate bug

  1. Add the 'type/duplicate' label
  2. Add the link to the original bug

Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: #20100

1. Root Cause Analysis (RCA) (optional)

2. Symptom (optional)

3. All Trigger Conditions (optional)

4. Workaround (optional)

5. Affected versions

6. Fixed versions

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. severity/moderate sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants