Skip to content

Commit

Permalink
fix restore view with lower case table names equals to 0(#2.0-dev) (#…
Browse files Browse the repository at this point in the history
…20668)

fix restore view with lower case table names equals to 0

Approved by: @daviszhen, @heni02, @aressu1985, @sukki37
  • Loading branch information
YANGGMM authored Dec 9, 2024
1 parent 2c325a4 commit 25b06b2
Show file tree
Hide file tree
Showing 5 changed files with 486 additions and 16 deletions.
30 changes: 22 additions & 8 deletions pkg/frontend/pitr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,15 @@ func restoreViewsWithPitr(
viewMap map[string]*tableInfo,
accountName string,
curAccount uint32) error {
snapshot := &pbplan.Snapshot{
getLogger(ses.GetService()).Info(fmt.Sprintf("[%s] start to restore views", pitrName))
var (
err error
stmts []tree.Statement
sortedViews []string
snapshot *pbplan.Snapshot
oldSnapshot *pbplan.Snapshot
)
snapshot = &pbplan.Snapshot{
TS: &timestamp.Timestamp{PhysicalTime: ts},
Tenant: &pbplan.SnapshotTenant{
TenantName: accountName,
Expand All @@ -1696,23 +1704,29 @@ func restoreViewsWithPitr(
}

compCtx := ses.GetTxnCompileCtx()
oldSnapshot := compCtx.GetSnapshot()
oldSnapshot = compCtx.GetSnapshot()
compCtx.SetSnapshot(snapshot)
defer func() {
compCtx.SetSnapshot(oldSnapshot)
}()

g := toposort{next: make(map[string][]string)}
for key, view := range viewMap {
stmts, err := parsers.Parse(ctx, dialect.MYSQL, view.createSql, 1)
for key, viewEntry := range viewMap {
getLogger(ses.GetService()).Info(fmt.Sprintf("[%s] start to restore view: %v", pitrName, viewEntry.tblName))
stmts, err = parsers.Parse(ctx, dialect.MYSQL, viewEntry.createSql, 1)
if err != nil {
return err
}

compCtx.SetDatabase(view.dbName)
compCtx.SetDatabase(viewEntry.dbName)
// build create sql to find dependent views
if _, err = plan.BuildPlan(compCtx, stmts[0], false); err != nil {
return err
_, err = plan.BuildPlan(compCtx, stmts[0], false)
if err != nil {
stmts, _ = parsers.Parse(ctx, dialect.MYSQL, viewEntry.createSql, 0)
_, err = plan.BuildPlan(compCtx, stmts[0], false)
if err != nil {
return err
}
}

g.addVertex(key)
Expand All @@ -1722,7 +1736,7 @@ func restoreViewsWithPitr(
}

// topsort
sortedViews, err := g.sort()
sortedViews, err = g.sort()
if err != nil {
return err
}
Expand Down
135 changes: 135 additions & 0 deletions pkg/frontend/pitr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2902,3 +2902,138 @@ func TestCheckDbIsSubDb(t *testing.T) {
})
}
}

func Test_restoreViews(t *testing.T) {
convey.Convey("restoreViews", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

ses := newTestSession(t, ctrl)
defer ses.Close()

bh := &backgroundExecTest{}
bh.init()

bhStub := gostub.StubFunc(&NewBackgroundExec, bh)
defer bhStub.Reset()

pu := config.NewParameterUnit(&config.FrontendParameters{}, nil, nil, nil)
pu.SV.SetDefaultValues()
pu.SV.KillRountinesInterval = 0
setPu("", pu)
ctx := context.WithValue(context.TODO(), config.ParameterUnitKey, pu)
rm, _ := NewRoutineManager(ctx, "")
ses.rm = rm

tenant := &TenantInfo{
Tenant: sysAccountName,
User: rootName,
DefaultRole: moAdminRoleName,
TenantID: sysAccountID,
UserID: rootID,
DefaultRoleID: moAdminRoleID,
}
ses.SetTenantInfo(tenant)

ctx = context.WithValue(ctx, defines.TenantIDKey{}, uint32(sysAccountID))

//no result set
bh.sql2result["begin;"] = nil
bh.sql2result["commit;"] = nil
bh.sql2result["rollback;"] = nil

viewMap := map[string]*tableInfo{}
err := restoreViews(ctx, ses, bh, "sp01", viewMap, 0)
assert.Error(t, err)

sql := "select * from mo_catalog.mo_snapshots where sname = 'sp01'"
// string/ string/ int64/ string/ string/ string/ string/ uint64
mrs := newMrsForPitrRecord([][]interface{}{{"1", "sp01", int64(0), "ACCOUNT", "sys", "", "", uint64(1)}})
bh.sql2result[sql] = mrs

sql = "select account_id, account_name, status, version, suspended_time from mo_catalog.mo_account where 1=1 and account_name = 'sys'"
mrs = newMrsForPitrRecord([][]interface{}{{uint64(0), "sys", "open", uint64(1), ""}})
bh.sql2result[sql] = mrs

err = restoreViews(ctx, ses, bh, "sp01", viewMap, 0)
assert.NoError(t, err)

viewMap = map[string]*tableInfo{
"view01": {
dbName: "db01",
tblName: "tbl01",
typ: "VIEW",
createSql: "create view view01",
},
}
err = restoreViews(ctx, ses, bh, "sp01", viewMap, 0)
assert.Error(t, err)
})
}

func Test_restoreViewsWithPitr(t *testing.T) {
convey.Convey("restoreViewsWithPitr", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

ses := newTestSession(t, ctrl)
defer ses.Close()

bh := &backgroundExecTest{}
bh.init()

bhStub := gostub.StubFunc(&NewBackgroundExec, bh)
defer bhStub.Reset()

pu := config.NewParameterUnit(&config.FrontendParameters{}, nil, nil, nil)
pu.SV.SetDefaultValues()
pu.SV.KillRountinesInterval = 0
setPu("", pu)
ctx := context.WithValue(context.TODO(), config.ParameterUnitKey, pu)
rm, _ := NewRoutineManager(ctx, "")
ses.rm = rm

tenant := &TenantInfo{
Tenant: sysAccountName,
User: rootName,
DefaultRole: moAdminRoleName,
TenantID: sysAccountID,
UserID: rootID,
DefaultRoleID: moAdminRoleID,
}
ses.SetTenantInfo(tenant)

ctx = context.WithValue(ctx, defines.TenantIDKey{}, uint32(sysAccountID))

//no result set
bh.sql2result["begin;"] = nil
bh.sql2result["commit;"] = nil
bh.sql2result["rollback;"] = nil

viewMap := map[string]*tableInfo{}
err := restoreViewsWithPitr(ctx, ses, bh, "sp01", 0, viewMap, "sys", 0)
assert.NoError(t, err)

viewMap = map[string]*tableInfo{
"view01": {
dbName: "db01",
tblName: "tbl01",
typ: "VIEW",
createSql: "create view view01",
},
}
err = restoreViewsWithPitr(ctx, ses, bh, "sp01", 0, viewMap, "sys", 0)
assert.Error(t, err)

viewMap = map[string]*tableInfo{
"view01": {
dbName: "db01",
tblName: "tbl01",
typ: "VIEW",
createSql: "create database db02",
},
}
err = restoreViewsWithPitr(ctx, ses, bh, "sp01", 0, viewMap, "sys", 0)
assert.NoError(t, err)
})
}
31 changes: 23 additions & 8 deletions pkg/frontend/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,29 +985,44 @@ func restoreViews(
snapshotName string,
viewMap map[string]*tableInfo,
toAccountId uint32) error {
snapshot, err := getSnapshotPlanWithSharedBh(ctx, bh, snapshotName)
getLogger(ses.GetService()).Info("start to restore views")
var (
err error
snapshot *plan.Snapshot
stmts []tree.Statement
sortedViews []string
oldSnapshot *plan.Snapshot
)
snapshot, err = getSnapshotPlanWithSharedBh(ctx, bh, snapshotName)
if err != nil {
return err
}

compCtx := ses.GetTxnCompileCtx()
oldSnapshot := compCtx.GetSnapshot()
oldSnapshot = compCtx.GetSnapshot()
compCtx.SetSnapshot(snapshot)
defer func() {
compCtx.SetSnapshot(oldSnapshot)
}()

g := toposort{next: make(map[string][]string)}
for key, view := range viewMap {
stmts, err := parsers.Parse(ctx, dialect.MYSQL, view.createSql, 1)
for key, viewEntry := range viewMap {
getLogger(ses.GetService()).Info(fmt.Sprintf("[%s] start to restore view: %v", snapshotName, viewEntry.tblName))
stmts, err = parsers.Parse(ctx, dialect.MYSQL, viewEntry.createSql, 1)
if err != nil {
return err
}

compCtx.SetDatabase(view.dbName)
compCtx.SetDatabase(viewEntry.dbName)
// build create sql to find dependent views
if _, err = plan.BuildPlan(compCtx, stmts[0], false); err != nil {
return err
_, err = plan.BuildPlan(compCtx, stmts[0], false)
if err != nil {
getLogger(ses.GetService()).Info(fmt.Sprintf("try to build view %v failed, try to build it again", viewEntry.tblName))
stmts, _ = parsers.Parse(ctx, dialect.MYSQL, viewEntry.createSql, 0)
_, err = plan.BuildPlan(compCtx, stmts[0], false)
if err != nil {
return err
}
}

g.addVertex(key)
Expand All @@ -1017,7 +1032,7 @@ func restoreViews(
}

// toposort
sortedViews, err := g.sort()
sortedViews, err = g.sort()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 25b06b2

Please sign in to comment.