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

ddl, planner: support ALTER TABLE without use db #18784

Merged
merged 15 commits into from
Aug 26, 2020
Merged

Conversation

xhebox
Copy link
Contributor

@xhebox xhebox commented Jul 25, 2020

What problem does this PR solve?

Issue Number: close #18756

Problem Summary: alter table db.t1 add constraint fk foreign key (c2) references t2(c1) without first executing use db, TiDB will report No database selected. That is because c2 has no schema(dbName), nor do we have a global CurrentDB. But mysql will try to use dbName from db.t1. Manually tested.

Check List

Tests

  • Unit test
  • Integration test

Release note

  • allow alter table db.t1 add constraint fk foreign key (c2) references t2(c1) like statements without first executing use db

@xhebox xhebox requested a review from a team as a code owner July 25, 2020 11:04
@xhebox xhebox requested review from lzmhhh123 and removed request for a team July 25, 2020 11:04
@ti-srebot ti-srebot added the contribution This PR is from a community contributor. label Jul 25, 2020
@codecov
Copy link

codecov bot commented Jul 25, 2020

Codecov Report

Merging #18784 into master will decrease coverage by 0.1866%.
The diff coverage is 100.0000%.

@@               Coverage Diff                @@
##             master     #18784        +/-   ##
================================================
- Coverage   79.3692%   79.1825%   -0.1867%     
================================================
  Files           552        549         -3     
  Lines        149369     148222      -1147     
================================================
- Hits         118553     117366      -1187     
- Misses        21343      21376        +33     
- Partials       9473       9480         +7     

planner/core/preprocess.go Outdated Show resolved Hide resolved
Comment on lines 877 to 881
if currentDB := p.ctx.GetSessionVars().CurrentDB; currentDB != "" {
tn.Schema = model.NewCIStr(currentDB)
} else if p.tmpTs != "" {
tn.Schema = model.NewCIStr(p.tmpTs)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't check whether it's the renamed table name. It will affect all the ALTER TABLE statements. Does they all conform to this rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have verified the case of rename, and the case in the original issue. I do not know other cases.

I just checked the source code of mysql, it is of great possibility. The related code is here, specifically the function mysql_opt_change_db.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to rename the table on MySQL 8.0.18 without executing the use test. It will return the error of No database selected. What is the version of MySQL you tested?

mysql> alter table test.t1 rename to t2;
ERROR 1046 (3D000): No database selected

@xhebox
Copy link
Contributor Author

xhebox commented Aug 4, 2020

I do not remember that. I now retest it with mysql 8.0.20. And I can not reproduce that again. Maybe there was a mistake :(.

Foreign key works, rename/exchange partition do not work. For now, I modified it to only take effects on foreign key references.

`alter table db.t1 .... table t2` without `use db`, mysql will try to use dbName from `db.t1`.
Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ti-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 20, 2020
Co-authored-by: Lynn <zimu_xia@126.com>
planner/core/preprocess.go Outdated Show resolved Hide resolved
planner/core/preprocess.go Show resolved Hide resolved
@xhebox
Copy link
Contributor Author

xhebox commented Aug 20, 2020

@ti-srebot /run-unit-test

@ti-srebot
Copy link
Contributor

/run-unit-test

Copy link
Contributor

@djshow832 djshow832 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the code has nothing to do with rename to as the the description says.

@xhebox
Copy link
Contributor Author

xhebox commented Aug 25, 2020

The description is updated.


s.runSQL(c, "ALTER TABLE test.t ADD CONSTRAINT fk FOREIGN KEY (c3) REFERENCES t (c1)", false, nil)

s.runSQL(c, "ALTER TABLE test.t ADD CONSTRAINT fk FOREIGN KEY t3(c3) REFERENCES t (c1)", false, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, REFERENCES test2.t(c1), where test2 is another schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that is a mistake.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure test2.t exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the underlying store is read only. Since the code here is not actually executed, but only parsed/resolved. I think it is ok to just use test.t.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you can't judge from the test case that whether test is read from ALTER TABLE test.t or REFERENCES test.t(c1).

@xhebox
Copy link
Contributor Author

xhebox commented Aug 26, 2020

@zz-jason Sounds good. I will do it later sometime.

@xhebox xhebox changed the title ddl, planner: support ALTER TABLE without use db [DNM]ddl, planner: support ALTER TABLE without use db Aug 26, 2020
@djshow832
Copy link
Contributor

@zz-jason Sounds good. I will do it later sometime.

You can do it simply by adding a needs-cherry-pick-4.0 or needs-cherry-pick-3.0 label. The robot will cherry-pick and commit a PR.

@xhebox
Copy link
Contributor Author

xhebox commented Aug 26, 2020

You can do it simply by adding a label

I thought that I do not have the permission to add a label?

@xhebox
Copy link
Contributor Author

xhebox commented Aug 26, 2020

@tangenta I think that should be tested in DDL package.

Here, this PR is just a change in preprocess. The tests here are enough.

@xhebox xhebox changed the title [DNM]ddl, planner: support ALTER TABLE without use db ddl, planner: support ALTER TABLE without use db Aug 26, 2020
Copy link
Contributor

@djshow832 djshow832 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ti-srebot ti-srebot added status/LGT3 The PR has already had 3 LGTM. and removed status/LGT2 Indicates that a PR has LGTM 2. labels Aug 26, 2020
@djshow832
Copy link
Contributor

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Aug 26, 2020
@ti-srebot
Copy link
Contributor

Your auto merge job has been accepted, waiting for:

  • 19447

@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot
Copy link
Contributor

@xhebox merge failed.

@djshow832 djshow832 merged commit c6b80ea into pingcap:master Aug 26, 2020
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Aug 26, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #19471

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. sig/sql-infra SIG: SQL Infra status/can-merge Indicates a PR has been approved by a committer. status/LGT3 The PR has already had 3 LGTM.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

different behavior on foreign key with MySQL
7 participants