-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Conversation
Codecov Report
@@ 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
if currentDB := p.ctx.GetSessionVars().CurrentDB; currentDB != "" { | ||
tn.Schema = model.NewCIStr(currentDB) | ||
} else if p.tmpTs != "" { | ||
tn.Schema = model.NewCIStr(p.tmpTs) | ||
} else { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@djshow832 Confirmed. It is at this line https://github.com/mysql/mysql-server/blob/7d10c82196c8e45554f27c00681474a9fb86d137/sql/sql_alter.cc#L142. So it conforms to all rules.
There was a problem hiding this 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
I do not remember that. I now retest it with 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`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Lynn <zimu_xia@126.com>
@ti-srebot /run-unit-test |
/run-unit-test |
There was a problem hiding this 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.
The description is updated. |
planner/core/preprocess_test.go
Outdated
|
||
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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)
.
@zz-jason Sounds good. I will do it later sometime. |
ALTER TABLE
without use dbALTER TABLE
without use db
You can do it simply by adding a |
I thought that I do not have the permission to add a label? |
@tangenta I think that should be tested in DDL package. Here, this PR is just a change in preprocess. The tests here are enough. |
ALTER TABLE
without use dbALTER TABLE
without use db
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/merge |
Your auto merge job has been accepted, waiting for:
|
/run-all-tests |
/run-all-tests |
@xhebox merge failed. |
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
cherry pick to release-4.0 in PR #19471 |
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 executinguse db
, TiDB will reportNo database selected
. That is becausec2
has no schema(dbName), nor do we have a globalCurrentDB
. But mysql will try to use dbName fromdb.t1
. Manually tested.Check List
Tests
Release note
alter table db.t1 add constraint fk foreign key (c2) references t2(c1)
like statements without first executinguse db