Skip to content

Commit 894c162

Browse files
committed
*: remove mock.NewContext() usage when building table meta in production code
1 parent a3a4511 commit 894c162

File tree

12 files changed

+35
-74
lines changed

12 files changed

+35
-74
lines changed

cmd/importer/parser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"github.com/pingcap/errors"
2323
"github.com/pingcap/log"
2424
"github.com/pingcap/tidb/pkg/ddl"
25+
"github.com/pingcap/tidb/pkg/meta/metabuild"
2526
"github.com/pingcap/tidb/pkg/meta/model"
2627
"github.com/pingcap/tidb/pkg/parser"
2728
"github.com/pingcap/tidb/pkg/parser/ast"
2829
_ "github.com/pingcap/tidb/pkg/planner/core"
2930
"github.com/pingcap/tidb/pkg/types"
30-
"github.com/pingcap/tidb/pkg/util/mock"
3131
"go.uber.org/zap"
3232
)
3333

@@ -237,7 +237,8 @@ func parseTable(t *table, stmt *ast.CreateTableStmt) error {
237237
t.name = stmt.Table.Name.L
238238
t.columns = make([]*column, 0, len(stmt.Cols))
239239

240-
mockTbl, err := ddl.MockTableInfo(mock.NewContext(), stmt, 1)
240+
mockTbl, err := ddl.BuildTableInfoFromAST(metabuild.NewNonStrictContext(), stmt)
241+
mockTbl.ID = 1
241242
if err != nil {
242243
return errors.Trace(err)
243244
}

lightning/pkg/importer/chunk_process_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/pingcap/tidb/pkg/lightning/log"
4242
"github.com/pingcap/tidb/pkg/lightning/mydump"
4343
"github.com/pingcap/tidb/pkg/lightning/worker"
44+
"github.com/pingcap/tidb/pkg/meta/metabuild"
4445
"github.com/pingcap/tidb/pkg/meta/model"
4546
"github.com/pingcap/tidb/pkg/parser"
4647
"github.com/pingcap/tidb/pkg/parser/ast"
@@ -710,7 +711,7 @@ func TestCompressChunkRestore(t *testing.T) {
710711
)
711712
`, "", "")
712713
require.NoError(t, err)
713-
core, err := ddl.BuildTableInfoFromAST(node.(*ast.CreateTableStmt))
714+
core, err := ddl.BuildTableInfoFromAST(metabuild.NewContext(), node.(*ast.CreateTableStmt))
714715
require.NoError(t, err)
715716
core.State = model.StatePublic
716717

lightning/pkg/importer/get_pre_info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ import (
4343
"github.com/pingcap/tidb/pkg/lightning/mydump"
4444
"github.com/pingcap/tidb/pkg/lightning/verification"
4545
"github.com/pingcap/tidb/pkg/lightning/worker"
46+
"github.com/pingcap/tidb/pkg/meta/metabuild"
4647
"github.com/pingcap/tidb/pkg/meta/model"
4748
"github.com/pingcap/tidb/pkg/parser"
4849
"github.com/pingcap/tidb/pkg/parser/ast"
4950
_ "github.com/pingcap/tidb/pkg/planner/core" // to setup expression.EvalAstExpr. Otherwise we cannot parse the default value
5051
"github.com/pingcap/tidb/pkg/table/tables"
5152
"github.com/pingcap/tidb/pkg/types"
5253
"github.com/pingcap/tidb/pkg/util/dbterror"
53-
"github.com/pingcap/tidb/pkg/util/mock"
5454
pdhttp "github.com/tikv/pd/client/http"
5555
"go.uber.org/zap"
5656
)
@@ -428,15 +428,15 @@ func newTableInfo(createTblSQL string, tableID int64) (*model.TableInfo, error)
428428
log.L().Error(errMsg, zap.Error(err), zap.String("sql", createTblSQL))
429429
return nil, errors.Trace(err)
430430
}
431-
sctx := mock.NewContext()
432431
createTableStmt, ok := astNode.(*ast.CreateTableStmt)
433432
if !ok {
434433
return nil, errors.New("cannot transfer the parsed SQL as an CREATE TABLE statement")
435434
}
436-
info, err := ddl.MockTableInfo(sctx, createTableStmt, tableID)
435+
info, err := ddl.BuildTableInfoFromAST(metabuild.NewNonStrictContext(), createTableStmt)
437436
if err != nil {
438437
return nil, errors.Trace(err)
439438
}
439+
info.ID = tableID
440440
info.State = model.StatePublic
441441
return info, nil
442442
}

pkg/ddl/create_table.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import (
4848
"github.com/pingcap/tidb/pkg/types"
4949
driver "github.com/pingcap/tidb/pkg/types/parser_driver"
5050
"github.com/pingcap/tidb/pkg/util/dbterror"
51-
"github.com/pingcap/tidb/pkg/util/mock"
5251
"github.com/pingcap/tidb/pkg/util/set"
5352
"go.uber.org/zap"
5453
)
@@ -390,8 +389,8 @@ func findTableIDFromStore(t *meta.Meta, schemaID int64, tableName string) (int64
390389

391390
// BuildTableInfoFromAST builds model.TableInfo from a SQL statement.
392391
// Note: TableID and PartitionID are left as uninitialized value.
393-
func BuildTableInfoFromAST(s *ast.CreateTableStmt) (*model.TableInfo, error) {
394-
return buildTableInfoWithCheck(NewMetaBuildContextWithSctx(mock.NewContext()), s, mysql.DefaultCharset, "", nil)
392+
func BuildTableInfoFromAST(ctx *metabuild.Context, s *ast.CreateTableStmt) (*model.TableInfo, error) {
393+
return buildTableInfoWithCheck(ctx, s, mysql.DefaultCharset, "", nil)
395394
}
396395

397396
// buildTableInfoWithCheck builds model.TableInfo from a SQL statement.

pkg/infoschema/perfschema/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/pingcap/tidb/pkg/expression"
2323
"github.com/pingcap/tidb/pkg/infoschema"
2424
"github.com/pingcap/tidb/pkg/meta/autoid"
25+
"github.com/pingcap/tidb/pkg/meta/metabuild"
2526
"github.com/pingcap/tidb/pkg/meta/model"
2627
"github.com/pingcap/tidb/pkg/parser"
2728
"github.com/pingcap/tidb/pkg/parser/ast"
@@ -43,12 +44,13 @@ func Init() {
4344
p := parser.New()
4445
tbls := make([]*model.TableInfo, 0)
4546
dbID := autoid.PerformanceSchemaDBID
47+
ctx := metabuild.NewNonStrictContext()
4648
for _, sql := range perfSchemaTables {
4749
stmt, err := p.ParseOneStmt(sql, "", "")
4850
if err != nil {
4951
panic(err)
5052
}
51-
meta, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
53+
meta, err := ddl.BuildTableInfoFromAST(ctx, stmt.(*ast.CreateTableStmt))
5254
if err != nil {
5355
panic(err)
5456
}
@@ -62,6 +64,7 @@ func Init() {
6264
c.ID = int64(i) + 1
6365
}
6466
meta.DBID = dbID
67+
meta.State = model.StatePublic
6568
}
6669
dbInfo := &model.DBInfo{
6770
ID: dbID,

pkg/meta/metabuild/context.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ func NewContext(opts ...Option) *Context {
132132
return ctx
133133
}
134134

135+
// NewNonStrictContext creates a new context for meta-building with non-strict mode.
136+
func NewNonStrictContext() *Context {
137+
evalCtx := exprstatic.NewEvalContext(
138+
// use mysql.ModeNone to avoid some special values like datetime `0000-00-00 00:00:00`
139+
exprstatic.WithSQLMode(mysql.ModeNone),
140+
)
141+
return NewContext(WithExprCtx(exprstatic.NewExprContext(
142+
exprstatic.WithEvalCtx(evalCtx),
143+
)))
144+
}
145+
135146
// GetExprCtx returns the expression context of the session.
136147
func (ctx *Context) GetExprCtx() exprctx.ExprContext {
137148
return ctx.exprCtx

pkg/planner/core/partition_pruning_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import (
2020
"strings"
2121
"testing"
2222

23+
"github.com/pingcap/tidb/pkg/ddl"
2324
"github.com/pingcap/tidb/pkg/expression"
2425
"github.com/pingcap/tidb/pkg/parser"
2526
"github.com/pingcap/tidb/pkg/parser/ast"
2627
"github.com/pingcap/tidb/pkg/parser/model"
27-
"github.com/pingcap/tidb/pkg/testkit/ddlhelper"
2828
"github.com/pingcap/tidb/pkg/types"
2929
"github.com/pingcap/tidb/pkg/util/mock"
3030
"github.com/stretchr/testify/require"
@@ -183,7 +183,7 @@ func prepareBenchCtx(createTable string, partitionExpr string) *testCtx {
183183
return nil
184184
}
185185
sctx := mock.NewContext()
186-
tblInfo, err := ddlhelper.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
186+
tblInfo, err := ddl.BuildTableInfoFromAST(ddl.NewMetaBuildContextWithSctx(sctx), stmt.(*ast.CreateTableStmt))
187187
if err != nil {
188188
return nil
189189
}
@@ -213,7 +213,7 @@ func prepareTestCtx(t *testing.T, createTable string, partitionExpr string) *tes
213213
stmt, err := p.ParseOneStmt(createTable, "", "")
214214
require.NoError(t, err)
215215
sctx := mock.NewContext()
216-
tblInfo, err := ddlhelper.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
216+
tblInfo, err := ddl.BuildTableInfoFromAST(ddl.NewMetaBuildContextWithSctx(sctx), stmt.(*ast.CreateTableStmt))
217217
require.NoError(t, err)
218218
columns, names, err := expression.ColumnInfos2ColumnsAndNames(sctx, model.NewCIStr("t"), tblInfo.Name, tblInfo.Cols(), tblInfo)
219219
require.NoError(t, err)

pkg/session/session.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import (
6262
infoschemactx "github.com/pingcap/tidb/pkg/infoschema/context"
6363
"github.com/pingcap/tidb/pkg/kv"
6464
"github.com/pingcap/tidb/pkg/meta"
65+
"github.com/pingcap/tidb/pkg/meta/metabuild"
6566
"github.com/pingcap/tidb/pkg/meta/model"
6667
"github.com/pingcap/tidb/pkg/metrics"
6768
"github.com/pingcap/tidb/pkg/owner"
@@ -3253,7 +3254,7 @@ func createAndSplitTables(store kv.Storage, t *meta.Meta, dbID int64, tables []t
32533254
if err != nil {
32543255
return errors.Trace(err)
32553256
}
3256-
tblInfo, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
3257+
tblInfo, err := ddl.BuildTableInfoFromAST(metabuild.NewContext(), stmt.(*ast.CreateTableStmt))
32573258
if err != nil {
32583259
return errors.Trace(err)
32593260
}
@@ -3286,7 +3287,7 @@ func InitMDLTable(store kv.Storage) error {
32863287
if err != nil {
32873288
return errors.Trace(err)
32883289
}
3289-
tblInfo, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
3290+
tblInfo, err := ddl.BuildTableInfoFromAST(metabuild.NewContext(), stmt.(*ast.CreateTableStmt))
32903291
if err != nil {
32913292
return errors.Trace(err)
32923293
}

pkg/table/tables/index_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/pingcap/tidb/pkg/kv"
2525
"github.com/pingcap/tidb/pkg/lightning/backend/encode"
2626
lkv "github.com/pingcap/tidb/pkg/lightning/backend/kv"
27+
"github.com/pingcap/tidb/pkg/meta/metabuild"
2728
"github.com/pingcap/tidb/pkg/meta/model"
2829
"github.com/pingcap/tidb/pkg/parser"
2930
"github.com/pingcap/tidb/pkg/parser/ast"
@@ -170,7 +171,7 @@ func TestSingleColumnCommonHandle(t *testing.T) {
170171
func buildTableInfo(t *testing.T, sql string) *model.TableInfo {
171172
stmt, err := parser.New().ParseOneStmt(sql, "", "")
172173
require.NoError(t, err)
173-
tblInfo, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
174+
tblInfo, err := ddl.BuildTableInfoFromAST(metabuild.NewContext(), stmt.(*ast.CreateTableStmt))
174175
require.NoError(t, err)
175176
return tblInfo
176177
}

pkg/testkit/ddlhelper/BUILD.bazel

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)