diff --git a/cmd/importer/config.go b/cmd/importer/config.go index 92a8fe64b6077..2917aca41bdff 100644 --- a/cmd/importer/config.go +++ b/cmd/importer/config.go @@ -32,6 +32,8 @@ func NewConfig() *Config { fs.StringVar(&cfg.DDLCfg.TableSQL, "t", "", "create table sql") fs.StringVar(&cfg.DDLCfg.IndexSQL, "i", "", "create index sql") + fs.StringVar(&cfg.StatsCfg.Path, "s", "", "load stats file path") + fs.IntVar(&cfg.SysCfg.WorkerCount, "c", 2, "parallel worker count") fs.IntVar(&cfg.SysCfg.JobCount, "n", 10000, "total job count") fs.IntVar(&cfg.SysCfg.Batch, "b", 1000, "insert batch commit count") @@ -85,6 +87,11 @@ type SysConfig struct { Batch int `toml:"batch" json:"batch"` } +// StatsConfig is the configuration for statistics file. +type StatsConfig struct { + Path string `toml:"stats-file-path" json:"stats-file-path"` +} + // Config is the configuration. type Config struct { *flag.FlagSet `json:"-"` @@ -93,6 +100,8 @@ type Config struct { DDLCfg DDLConfig `toml:"ddl" json:"ddl"` + StatsCfg StatsConfig `toml:"stats" json:"stats"` + SysCfg SysConfig `toml:"sys" json:"sys"` configFile string diff --git a/cmd/importer/parser.go b/cmd/importer/parser.go index 617c010d9f3d4..4c8d8c9ebbb1e 100644 --- a/cmd/importer/parser.go +++ b/cmd/importer/parser.go @@ -20,8 +20,11 @@ import ( "github.com/juju/errors" "github.com/pingcap/tidb/ast" + "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/parser" "github.com/pingcap/tidb/types" + "github.com/pingcap/tidb/util/mock" log "github.com/sirupsen/logrus" ) @@ -117,12 +120,12 @@ func (col *column) parseColumnOptions(ops []*ast.ColumnOption) { } type table struct { - name string - columns []*column - columnList string - indices map[string]*column - uniqIndices map[string]*column - unsignedCols map[string]*column + name string + columns []*column + columnList string + indices map[string]*column + uniqIndices map[string]*column + tblInfo *model.TableInfo } func (t *table) printColumns() string { @@ -160,9 +163,8 @@ func (t *table) String() string { func newTable() *table { return &table{ - indices: make(map[string]*column), - uniqIndices: make(map[string]*column), - unsignedCols: make(map[string]*column), + indices: make(map[string]*column), + uniqIndices: make(map[string]*column), } } @@ -204,6 +206,12 @@ func parseTable(t *table, stmt *ast.CreateTableStmt) error { t.name = stmt.Table.Name.L t.columns = make([]*column, 0, len(stmt.Cols)) + mockTbl, err := ddl.MockTableInfo(mock.NewContext(), stmt, 1) + if err != nil { + return errors.Trace(err) + } + t.tblInfo = mockTbl + for i, col := range stmt.Cols { column := &column{idx: i + 1, table: t, step: defaultStep, data: newDatum()} column.parseColumn(col) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 8798d54f85e4f..a5f69460255f7 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -591,13 +591,17 @@ func checkConstraintNames(constraints []*ast.Constraint) error { return nil } -func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*table.Column, constraints []*ast.Constraint, ctx context.Context) (tbInfo *model.TableInfo, err error) { +func buildTableInfo(d *ddl, tableName model.CIStr, cols []*table.Column, constraints []*ast.Constraint, ctx context.Context) (tbInfo *model.TableInfo, err error) { tbInfo = &model.TableInfo{ Name: tableName, } - tbInfo.ID, err = d.genGlobalID() - if err != nil { - return nil, errors.Trace(err) + // When this function is called by MockTableInfo, we should set a particular table id. + // So the `ddl` structure may be nil. + if d != nil { + tbInfo.ID, err = d.genGlobalID() + if err != nil { + return nil, errors.Trace(err) + } } for _, v := range cols { v.ID = allocateColumnID(tbInfo) @@ -768,7 +772,7 @@ func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.C return errors.Trace(err) } - tbInfo, err := d.buildTableInfo(ident.Name, cols, newConstraints, ctx) + tbInfo, err := buildTableInfo(d, ident.Name, cols, newConstraints, ctx) if err != nil { return errors.Trace(err) } diff --git a/ddl/mock.go b/ddl/mock.go index c91fbc7791b6d..c7626ee452053 100644 --- a/ddl/mock.go +++ b/ddl/mock.go @@ -19,6 +19,8 @@ import ( "github.com/coreos/etcd/clientv3" "github.com/juju/errors" + "github.com/pingcap/tidb/ast" + "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/model" goctx "golang.org/x/net/context" ) @@ -119,3 +121,17 @@ func (dr *mockDelRange) start() { func (dr *mockDelRange) clear() { return } + +// MockTableInfo mocks a table info by create table stmt ast and a specified table id. +func MockTableInfo(ctx context.Context, stmt *ast.CreateTableStmt, tableID int64) (*model.TableInfo, error) { + cols, newConstraints, err := buildColumnsAndConstraints(ctx, stmt.Cols, stmt.Constraints) + if err != nil { + return nil, errors.Trace(err) + } + tbl, err := buildTableInfo(nil, stmt.Table.Name, cols, newConstraints, ctx) + if err != nil { + return nil, errors.Trace(err) + } + tbl.ID = tableID + return tbl, nil +}