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

parser: XXXDatabaseStmt now use CIStr for DB name #35668

Merged
merged 8 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion br/pkg/lightning/restore/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func createIfNotExistsStmt(p *parser.Parser, createTable, dbName, tblName string
for _, stmt := range stmts {
switch node := stmt.(type) {
case *ast.CreateDatabaseStmt:
node.Name = dbName
node.Name = model.NewCIStr(dbName)
node.IfNotExists = true
case *ast.CreateTableStmt:
node.Table.Schema = model.NewCIStr(dbName)
Expand Down
6 changes: 3 additions & 3 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (d *ddl) ModifySchemaCharsetAndCollate(ctx sessionctx.Context, stmt *ast.Al
}

// Check if need to change charset/collation.
dbName := model.NewCIStr(stmt.Name)
dbName := stmt.Name
is := d.GetInfoSchemaWithInterceptor(ctx)
dbInfo, ok := is.SchemaByName(dbName)
if !ok {
Expand All @@ -181,7 +181,7 @@ func (d *ddl) ModifySchemaCharsetAndCollate(ctx sessionctx.Context, stmt *ast.Al
}

func (d *ddl) ModifySchemaDefaultPlacement(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt, placementPolicyRef *model.PolicyRefInfo) (err error) {
dbName := model.NewCIStr(stmt.Name)
dbName := stmt.Name
is := d.GetInfoSchemaWithInterceptor(ctx)
dbInfo, ok := is.SchemaByName(dbName)
if !ok {
Expand Down Expand Up @@ -276,7 +276,7 @@ func (d *ddl) waitPendingTableThreshold(sctx sessionctx.Context, schemaID int64,
}

func (d *ddl) ModifySchemaSetTiFlashReplica(sctx sessionctx.Context, stmt *ast.AlterDatabaseStmt, tiflashReplica *ast.TiFlashReplicaSpec) error {
dbName := model.NewCIStr(stmt.Name)
dbName := stmt.Name
is := d.GetInfoSchemaWithInterceptor(sctx)
dbInfo, ok := is.SchemaByName(dbName)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (e *DDLExec) executeCreateDatabase(s *ast.CreateDatabaseStmt) error {

}

err = domain.GetDomain(e.ctx).DDL().CreateSchema(e.ctx, model.NewCIStr(s.Name), opt, placementPolicyRef)
err = domain.GetDomain(e.ctx).DDL().CreateSchema(e.ctx, s.Name, opt, placementPolicyRef)
if err != nil {
if infoschema.ErrDatabaseExists.Equal(err) && s.IfNotExists {
err = nil
Expand Down Expand Up @@ -375,7 +375,7 @@ func (e *DDLExec) executeCreateIndex(s *ast.CreateIndexStmt) error {
}

func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
dbName := model.NewCIStr(s.Name)
dbName := s.Name

// Protect important system table from been dropped by a mistake.
// I can hardly find a case that a user really need to do this.
Expand Down
12 changes: 6 additions & 6 deletions parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type CreateDatabaseStmt struct {
ddlNode

IfNotExists bool
Name string
Name model.CIStr
Options []*DatabaseOption
}

Expand All @@ -142,7 +142,7 @@ func (n *CreateDatabaseStmt) Restore(ctx *format.RestoreCtx) error {
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
}
ctx.WriteName(n.Name)
ctx.WriteName(n.Name.O)
for i, option := range n.Options {
ctx.WritePlain(" ")
err := option.Restore(ctx)
Expand All @@ -168,7 +168,7 @@ func (n *CreateDatabaseStmt) Accept(v Visitor) (Node, bool) {
type AlterDatabaseStmt struct {
ddlNode

Name string
Name model.CIStr
AlterDefaultDatabase bool
Options []*DatabaseOption
}
Expand All @@ -191,7 +191,7 @@ func (n *AlterDatabaseStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("ALTER DATABASE")
if !n.AlterDefaultDatabase {
ctx.WritePlain(" ")
ctx.WriteName(n.Name)
ctx.WriteName(n.Name.O)
}
for i, option := range n.Options {
ctx.WritePlain(" ")
Expand Down Expand Up @@ -230,7 +230,7 @@ type DropDatabaseStmt struct {
ddlNode

IfExists bool
Name string
Name model.CIStr
}

// Restore implements Node interface.
Expand All @@ -239,7 +239,7 @@ func (n *DropDatabaseStmt) Restore(ctx *format.RestoreCtx) error {
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
ctx.WriteName(n.Name)
ctx.WriteName(n.Name.O)
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13251,15 +13251,15 @@ yynewstate:
case 330:
{
parser.yyVAL.statement = &ast.AlterDatabaseStmt{
Name: yyS[yypt-1].ident,
Name: model.NewCIStr(yyS[yypt-1].ident),
AlterDefaultDatabase: false,
Options: yyS[yypt-0].item.([]*ast.DatabaseOption),
}
}
case 331:
{
parser.yyVAL.statement = &ast.AlterDatabaseStmt{
Name: "",
Name: model.NewCIStr(""),
AlterDefaultDatabase: true,
Options: yyS[yypt-0].item.([]*ast.DatabaseOption),
}
Expand All @@ -13268,7 +13268,7 @@ yynewstate:
{
parser.yyVAL.statement = &ast.CreateDatabaseStmt{
IfNotExists: yyS[yypt-2].item.(bool),
Name: yyS[yypt-1].ident,
Name: model.NewCIStr(yyS[yypt-1].ident),
Options: yyS[yypt-0].item.([]*ast.DatabaseOption),
}
}
Expand Down Expand Up @@ -13915,7 +13915,7 @@ yynewstate:
}
case 449:
{
parser.yyVAL.statement = &ast.DropDatabaseStmt{IfExists: yyS[yypt-1].item.(bool), Name: yyS[yypt-0].ident}
parser.yyVAL.statement = &ast.DropDatabaseStmt{IfExists: yyS[yypt-1].item.(bool), Name: model.NewCIStr(yyS[yypt-0].ident)}
}
case 450:
{
Expand Down
8 changes: 4 additions & 4 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3655,15 +3655,15 @@ AlterDatabaseStmt:
"ALTER" DatabaseSym DBName DatabaseOptionList
{
$$ = &ast.AlterDatabaseStmt{
Name: $3,
Name: model.NewCIStr($3),
AlterDefaultDatabase: false,
Options: $4.([]*ast.DatabaseOption),
}
}
| "ALTER" DatabaseSym DatabaseOptionList
{
$$ = &ast.AlterDatabaseStmt{
Name: "",
Name: model.NewCIStr(""),
AlterDefaultDatabase: true,
Options: $3.([]*ast.DatabaseOption),
}
Expand All @@ -3685,7 +3685,7 @@ CreateDatabaseStmt:
{
$$ = &ast.CreateDatabaseStmt{
IfNotExists: $3.(bool),
Name: $4,
Name: model.NewCIStr($4),
Options: $5.([]*ast.DatabaseOption),
}
}
Expand Down Expand Up @@ -4463,7 +4463,7 @@ DatabaseSym:
DropDatabaseStmt:
"DROP" DatabaseSym IfExists DBName
{
$$ = &ast.DropDatabaseStmt{IfExists: $3.(bool), Name: $4}
$$ = &ast.DropDatabaseStmt{IfExists: $3.(bool), Name: model.NewCIStr($4)}
}

/******************************************************************
Expand Down
10 changes: 5 additions & 5 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4159,16 +4159,16 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err
switch v := node.(type) {
case *ast.AlterDatabaseStmt:
if v.AlterDefaultDatabase {
v.Name = b.ctx.GetSessionVars().CurrentDB
v.Name = model.NewCIStr(b.ctx.GetSessionVars().CurrentDB)
}
if v.Name == "" {
if v.Name.O == "" {
return nil, ErrNoDB
}
if b.ctx.GetSessionVars().User != nil {
authErr = ErrDBaccessDenied.GenWithStackByArgs("ALTER", b.ctx.GetSessionVars().User.AuthUsername,
b.ctx.GetSessionVars().User.AuthHostname, v.Name)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AlterPriv, v.Name, "", "", authErr)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using v.Name.L is better.

b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AlterPriv, v.Name.L, "", "", authErr)
case *ast.AlterTableStmt:
if b.ctx.GetSessionVars().User != nil {
authErr = ErrTableaccessDenied.GenWithStackByArgs("ALTER", b.ctx.GetSessionVars().User.AuthUsername,
Expand Down Expand Up @@ -4246,7 +4246,7 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err
authErr = ErrDBaccessDenied.GenWithStackByArgs(b.ctx.GetSessionVars().User.AuthUsername,
b.ctx.GetSessionVars().User.AuthHostname, v.Name)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreatePriv, v.Name,
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreatePriv, v.Name.L,
"", "", authErr)
case *ast.CreateIndexStmt:
if b.ctx.GetSessionVars().User != nil {
Expand Down Expand Up @@ -4340,7 +4340,7 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err
authErr = ErrDBaccessDenied.GenWithStackByArgs(b.ctx.GetSessionVars().User.AuthUsername,
b.ctx.GetSessionVars().User.AuthHostname, v.Name)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.DropPriv, v.Name,
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.DropPriv, v.Name.L,
"", "", authErr)
case *ast.DropIndexStmt:
if b.ctx.GetSessionVars().User != nil {
Expand Down
6 changes: 3 additions & 3 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,20 +716,20 @@ func (p *preprocessor) checkSetOprSelectList(stmt *ast.SetOprSelectList) {
}

func (p *preprocessor) checkCreateDatabaseGrammar(stmt *ast.CreateDatabaseStmt) {
if isIncorrectName(stmt.Name) {
if isIncorrectName(stmt.Name.L) {
p.err = dbterror.ErrWrongDBName.GenWithStackByArgs(stmt.Name)
}
}

func (p *preprocessor) checkAlterDatabaseGrammar(stmt *ast.AlterDatabaseStmt) {
// for 'ALTER DATABASE' statement, database name can be empty to alter default database.
if isIncorrectName(stmt.Name) && !stmt.AlterDefaultDatabase {
if isIncorrectName(stmt.Name.L) && !stmt.AlterDefaultDatabase {
p.err = dbterror.ErrWrongDBName.GenWithStackByArgs(stmt.Name)
}
}

func (p *preprocessor) checkDropDatabaseGrammar(stmt *ast.DropDatabaseStmt) {
if isIncorrectName(stmt.Name) {
if isIncorrectName(stmt.Name.L) {
p.err = dbterror.ErrWrongDBName.GenWithStackByArgs(stmt.Name)
}
}
Expand Down