Skip to content

Commit 8f0baf4

Browse files
authored
*: remove mock.NewContext() usage when building table meta in production code (#56348)
ref #53388
1 parent 65d740f commit 8f0baf4

File tree

18 files changed

+46
-39
lines changed

18 files changed

+46
-39
lines changed

cmd/importer/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_library(
1616
visibility = ["//visibility:private"],
1717
deps = [
1818
"//pkg/ddl",
19+
"//pkg/meta/metabuild",
1920
"//pkg/meta/model",
2021
"//pkg/parser",
2122
"//pkg/parser/ast",
@@ -26,7 +27,6 @@ go_library(
2627
"//pkg/statistics/handle/util",
2728
"//pkg/types",
2829
"//pkg/util/mathutil",
29-
"//pkg/util/mock",
3030
"@com_github_burntsushi_toml//:toml",
3131
"@com_github_go_sql_driver_mysql//:mysql",
3232
"@com_github_pingcap_errors//:errors",

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/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ go_library(
5252
"//pkg/lightning/verification",
5353
"//pkg/lightning/worker",
5454
"//pkg/meta/autoid",
55+
"//pkg/meta/metabuild",
5556
"//pkg/meta/model",
5657
"//pkg/parser",
5758
"//pkg/parser/ast",
@@ -72,7 +73,6 @@ go_library(
7273
"//pkg/util/engine",
7374
"//pkg/util/etcd",
7475
"//pkg/util/extsort",
75-
"//pkg/util/mock",
7676
"//pkg/util/redact",
7777
"//pkg/util/regexpr-router",
7878
"//pkg/util/set",
@@ -150,6 +150,7 @@ go_test(
150150
"//pkg/lightning/worker",
151151
"//pkg/meta",
152152
"//pkg/meta/autoid",
153+
"//pkg/meta/metabuild",
153154
"//pkg/meta/model",
154155
"//pkg/parser",
155156
"//pkg/parser/ast",

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
)
@@ -392,8 +391,8 @@ func findTableIDFromStore(t *meta.Mutator, schemaID int64, tableName string) (in
392391

393392
// BuildTableInfoFromAST builds model.TableInfo from a SQL statement.
394393
// Note: TableID and PartitionID are left as uninitialized value.
395-
func BuildTableInfoFromAST(s *ast.CreateTableStmt) (*model.TableInfo, error) {
396-
return buildTableInfoWithCheck(NewMetaBuildContextWithSctx(mock.NewContext()), s, mysql.DefaultCharset, "", nil)
394+
func BuildTableInfoFromAST(ctx *metabuild.Context, s *ast.CreateTableStmt) (*model.TableInfo, error) {
395+
return buildTableInfoWithCheck(ctx, s, mysql.DefaultCharset, "", nil)
397396
}
398397

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

pkg/infoschema/perfschema/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
"//pkg/infoschema",
1616
"//pkg/kv",
1717
"//pkg/meta/autoid",
18+
"//pkg/meta/metabuild",
1819
"//pkg/meta/model",
1920
"//pkg/parser",
2021
"//pkg/parser/ast",

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 := ddlhelper.BuildTableInfoFromASTForTest(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 := ddlhelper.BuildTableInfoFromASTForTest(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)

0 commit comments

Comments
 (0)