From 465f6558893e3d55a3f4f15cc13c9d93e54af540 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 17 Jan 2022 15:11:44 +0800 Subject: [PATCH] * : replace fmt.Sprint with strconv (#31718) ref pingcap/tidb#31716 --- br/pkg/lightning/restore/restore_test.go | 3 ++- br/pkg/logutil/logging_test.go | 9 +++++---- br/pkg/pdutil/utils.go | 4 ++-- br/tests/br_key_locked/locker.go | 3 ++- cmd/importer/db.go | 2 +- dumpling/export/sql.go | 2 +- executor/executor.go | 2 +- expression/builtin_other_vec_generated_test.go | 3 ++- expression/generator/other_vec.go | 3 ++- expression/integration_test.go | 2 +- meta/meta.go | 2 +- planner/core/common_plans.go | 2 +- sessionctx/variable/sysvar.go | 6 +++--- sessionctx/variable/variable.go | 18 +++++++++--------- statistics/handle/bootstrap.go | 4 ++-- store/driver/txn/error.go | 3 ++- table/tables/partition.go | 2 +- 17 files changed, 38 insertions(+), 32 deletions(-) diff --git a/br/pkg/lightning/restore/restore_test.go b/br/pkg/lightning/restore/restore_test.go index 26de777e93d8c..340736e53434d 100644 --- a/br/pkg/lightning/restore/restore_test.go +++ b/br/pkg/lightning/restore/restore_test.go @@ -23,6 +23,7 @@ import ( "os" "path/filepath" "sort" + "strconv" "strings" "sync/atomic" "time" @@ -422,7 +423,7 @@ func (s *tableRestoreSuiteBase) SetUpSuite(c *C) { FileMeta: mydump.SourceFileMeta{ Path: fakeFileName, Type: mydump.SourceTypeSQL, - SortKey: fmt.Sprintf("%d", i), + SortKey: strconv.Itoa(i), FileSize: 37, }, }) diff --git a/br/pkg/logutil/logging_test.go b/br/pkg/logutil/logging_test.go index fc4b415a89735..ff3fc14da8ddc 100644 --- a/br/pkg/logutil/logging_test.go +++ b/br/pkg/logutil/logging_test.go @@ -5,6 +5,7 @@ package logutil_test import ( "context" "fmt" + "strconv" "strings" "testing" "time" @@ -31,15 +32,15 @@ func assertTrimEqual(t *testing.T, f zapcore.Field, expect string) { func newFile(j int) *backuppb.File { return &backuppb.File{ - Name: fmt.Sprint(j), - StartKey: []byte(fmt.Sprint(j)), - EndKey: []byte(fmt.Sprint(j + 1)), + Name: strconv.Itoa(j), + StartKey: []byte(strconv.Itoa(j)), + EndKey: []byte(strconv.Itoa(j + 1)), TotalKvs: uint64(j), TotalBytes: uint64(j), StartVersion: uint64(j), EndVersion: uint64(j + 1), Crc64Xor: uint64(j), - Sha256: []byte(fmt.Sprint(j)), + Sha256: []byte(strconv.Itoa(j)), Cf: "write", Size_: uint64(j), } diff --git a/br/pkg/pdutil/utils.go b/br/pkg/pdutil/utils.go index d06ca08891a31..c07171669bc11 100644 --- a/br/pkg/pdutil/utils.go +++ b/br/pkg/pdutil/utils.go @@ -8,8 +8,8 @@ import ( "crypto/tls" "encoding/hex" "encoding/json" - "fmt" "net/http" + "strconv" "strings" "github.com/pingcap/errors" @@ -36,7 +36,7 @@ const ( func ResetTS(ctx context.Context, pdAddr string, ts uint64, tlsConf *tls.Config) error { payload, err := json.Marshal(struct { TSO string `json:"tso,omitempty"` - }{TSO: fmt.Sprintf("%d", ts)}) + }{TSO: strconv.FormatUint(ts, 10)}) if err != nil { return errors.Trace(err) } diff --git a/br/tests/br_key_locked/locker.go b/br/tests/br_key_locked/locker.go index fc7e72d532093..402d98a112b99 100644 --- a/br/tests/br_key_locked/locker.go +++ b/br/tests/br_key_locked/locker.go @@ -28,6 +28,7 @@ import ( "math/rand" "net" "net/http" + "strconv" "time" "github.com/pingcap/errors" @@ -343,7 +344,7 @@ func randStr() string { length := rand.Intn(128) res := "" for i := 0; i < length; i++ { - res += fmt.Sprint(rand.Intn(10)) + res += strconv.Itoa(rand.Intn(10)) } return res } diff --git a/cmd/importer/db.go b/cmd/importer/db.go index 2379fd3a08943..acba3a0b94013 100644 --- a/cmd/importer/db.go +++ b/cmd/importer/db.go @@ -86,7 +86,7 @@ func nextInt64Value(column *column, min int64, max int64) int64 { } func intToDecimalString(intValue int64, decimal int) string { - data := fmt.Sprintf("%d", intValue) + data := strconv.FormatInt(intValue, 10) // add leading zero if len(data) < decimal { diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index 27a1bfb92c27f..fb8554d3abe4b 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -391,7 +391,7 @@ func SelectTiDBRowID(db *sql.Conn, database, table string) (bool, error) { _, err := db.ExecContext(context.Background(), tiDBRowIDQuery) if err != nil { errMsg := strings.ToLower(err.Error()) - if strings.Contains(errMsg, fmt.Sprintf("%d", errBadFieldCode)) { + if strings.Contains(errMsg, strconv.Itoa(errBadFieldCode)) { return false, nil } return false, errors.Annotatef(err, "sql: %s", tiDBRowIDQuery) diff --git a/executor/executor.go b/executor/executor.go index 6f4867438134a..a5f18a61733ae 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -312,7 +312,7 @@ func (e *CancelDDLJobsExec) Next(ctx context.Context, req *chunk.Chunk) error { } numCurBatch := mathutil.Min(req.Capacity(), len(e.jobIDs)-e.cursor) for i := e.cursor; i < e.cursor+numCurBatch; i++ { - req.AppendString(0, fmt.Sprintf("%d", e.jobIDs[i])) + req.AppendString(0, strconv.FormatInt(e.jobIDs[i], 10)) if e.errs[i] != nil { req.AppendString(1, fmt.Sprintf("error: %v", e.errs[i])) } else { diff --git a/expression/builtin_other_vec_generated_test.go b/expression/builtin_other_vec_generated_test.go index 2c571519086ce..fe5de1ea93d23 100644 --- a/expression/builtin_other_vec_generated_test.go +++ b/expression/builtin_other_vec_generated_test.go @@ -19,6 +19,7 @@ package expression import ( "fmt" "math/rand" + "strconv" "testing" "time" @@ -69,7 +70,7 @@ func (g inGener) gen() interface{} { } return *j case types.ETString: - return fmt.Sprint(randNum) + return strconv.FormatInt(randNum, 10) } return randNum } diff --git a/expression/generator/other_vec.go b/expression/generator/other_vec.go index cb784436f4c47..70806edec60e9 100644 --- a/expression/generator/other_vec.go +++ b/expression/generator/other_vec.go @@ -279,6 +279,7 @@ package expression import ( "fmt" "math/rand" + "strconv" "testing" "time" @@ -329,7 +330,7 @@ func (g inGener) gen() interface{} { } return *j case types.ETString: - return fmt.Sprint(randNum) + return strconv.FormatInt(randNum, 10) } return randNum } diff --git a/expression/integration_test.go b/expression/integration_test.go index c0be08752aebb..e0131d6265ac6 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -5770,7 +5770,7 @@ func TestVitessHash(t *testing.T) { "(1, 30375298039), " + "(2, 1123), " + "(3, 30573721600), " + - "(4, " + fmt.Sprintf("%d", uint64(math.MaxUint64)) + ")," + + "(4, " + strconv.FormatUint(uint64(math.MaxUint64), 10) + ")," + "(5, 116)," + "(6, null);") diff --git a/meta/meta.go b/meta/meta.go index 24ac94733d5fe..a48565ecb9b9f 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -1004,7 +1004,7 @@ func (m *Meta) GetBootstrapVersion() (int64, error) { // FinishBootstrap finishes bootstrap. func (m *Meta) FinishBootstrap(version int64) error { - err := m.txn.Set(mBootstrapKey, []byte(fmt.Sprintf("%d", version))) + err := m.txn.Set(mBootstrapKey, []byte(strconv.FormatInt(version, 10))) return errors.Trace(err) } diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index 39d361ee4045c..9127c9a2335a2 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -1430,7 +1430,7 @@ func getRuntimeInfo(ctx sessionctx.Context, p Plan, runtimeStatsColl *execdetail } copStats := runtimeStatsColl.GetCopStats(explainID) analyzeInfo += copStats.String() - actRows = fmt.Sprint(copStats.GetActRows()) + actRows = strconv.FormatInt(copStats.GetActRows(), 10) } memoryInfo = "N/A" memTracker := ctx.GetSessionVars().StmtCtx.MemTracker.SearchTrackerWithoutLock(p.ID()) diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 4db4b2bd14090..dfd1285b8b228 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -201,7 +201,7 @@ var defaultSysVars = []*SysVar{ if val, err := strconv.ParseUint(normalizedValue, 10, 64); err == nil { if val > uint64(math.MaxUint32) { vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(GroupConcatMaxLen, originalValue)) - return fmt.Sprintf("%d", math.MaxUint32), nil + return strconv.FormatInt(int64(math.MaxUint32), 10), nil } } } @@ -501,7 +501,7 @@ var defaultSysVars = []*SysVar{ return nil }}, {Scope: ScopeSession, Name: TiDBCurrentTS, Value: strconv.Itoa(DefCurretTS), ReadOnly: true, skipInit: true, GetSession: func(s *SessionVars) (string, error) { - return fmt.Sprintf("%d", s.TxnCtx.StartTS), nil + return strconv.FormatUint(s.TxnCtx.StartTS, 10), nil }}, {Scope: ScopeSession, Name: TiDBLastTxnInfo, Value: strconv.Itoa(DefCurretTS), ReadOnly: true, skipInit: true, GetSession: func(s *SessionVars) (string, error) { return s.LastTxnInfo, nil @@ -888,7 +888,7 @@ var defaultSysVars = []*SysVar{ atomic.StoreUint64(&ExpensiveQueryTimeThreshold, uint64(tidbOptPositiveInt32(val, DefTiDBExpensiveQueryTimeThreshold))) return nil }, GetSession: func(s *SessionVars) (string, error) { - return fmt.Sprintf("%d", atomic.LoadUint64(&ExpensiveQueryTimeThreshold)), nil + return strconv.FormatUint(atomic.LoadUint64(&ExpensiveQueryTimeThreshold), 10), nil }}, {Scope: ScopeSession, Name: TiDBMemoryUsageAlarmRatio, Value: strconv.FormatFloat(config.GetGlobalConfig().Performance.MemoryUsageAlarmRatio, 'f', -1, 64), Type: TypeFloat, MinValue: 0.0, MaxValue: 1.0, skipInit: true, SetSession: func(s *SessionVars, val string) error { MemoryUsageAlarmRatio.Store(tidbOptFloat64(val, 0.8)) diff --git a/sessionctx/variable/variable.go b/sessionctx/variable/variable.go index dbf289ef005b1..a17c3f63e1538 100644 --- a/sessionctx/variable/variable.go +++ b/sessionctx/variable/variable.go @@ -15,7 +15,6 @@ package variable import ( - "fmt" "strconv" "strings" "sync" @@ -377,7 +376,7 @@ func (sv *SysVar) checkUInt64SystemVar(value string, vars *SessionVars) (string, return value, ErrWrongTypeForVar.GenWithStackByArgs(sv.Name) } vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(sv.Name, value)) - return fmt.Sprintf("%d", sv.MinValue), nil + return strconv.FormatInt(sv.MinValue, 10), nil } val, err := strconv.ParseUint(value, 10, 64) if err != nil { @@ -385,11 +384,12 @@ func (sv *SysVar) checkUInt64SystemVar(value string, vars *SessionVars) (string, } if val < uint64(sv.MinValue) { vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(sv.Name, value)) - return fmt.Sprintf("%d", sv.MinValue), nil + return strconv.FormatInt(sv.MinValue, 10), nil } if val > sv.MaxValue { vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(sv.Name, value)) - return fmt.Sprintf("%d", sv.MaxValue), nil + return strconv.FormatUint(sv.MaxValue, 10), nil + } return value, nil } @@ -404,11 +404,11 @@ func (sv *SysVar) checkInt64SystemVar(value string, vars *SessionVars) (string, } if val < sv.MinValue { vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(sv.Name, value)) - return fmt.Sprintf("%d", sv.MinValue), nil + return strconv.FormatInt(sv.MinValue, 10), nil } if val > int64(sv.MaxValue) { vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(sv.Name, value)) - return fmt.Sprintf("%d", sv.MaxValue), nil + return strconv.FormatUint(sv.MaxValue, 10), nil } return value, nil } @@ -418,7 +418,7 @@ func (sv *SysVar) checkEnumSystemVar(value string, vars *SessionVars) (string, e // This allows for the behavior 0 = OFF, 1 = ON, 2 = DEMAND etc. var iStr string for i, v := range sv.PossibleValues { - iStr = fmt.Sprintf("%d", i) + iStr = strconv.Itoa(i) if strings.EqualFold(value, v) || strings.EqualFold(value, iStr) { return v, nil } @@ -436,11 +436,11 @@ func (sv *SysVar) checkFloatSystemVar(value string, vars *SessionVars) (string, } if val < float64(sv.MinValue) { vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(sv.Name, value)) - return fmt.Sprintf("%d", sv.MinValue), nil + return strconv.FormatInt(sv.MinValue, 10), nil } if val > float64(sv.MaxValue) { vars.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(sv.Name, value)) - return fmt.Sprintf("%d", sv.MaxValue), nil + return strconv.FormatUint(sv.MaxValue, 10), nil } return value, nil } diff --git a/statistics/handle/bootstrap.go b/statistics/handle/bootstrap.go index bc5cff9a9084f..5449806774f28 100644 --- a/statistics/handle/bootstrap.go +++ b/statistics/handle/bootstrap.go @@ -16,7 +16,7 @@ package handle import ( "context" - "fmt" + "strconv" "github.com/cznic/mathutil" "github.com/pingcap/errors" @@ -428,5 +428,5 @@ func getFullTableName(is infoschema.InfoSchema, tblInfo *model.TableInfo) string } } } - return fmt.Sprintf("%d", tblInfo.ID) + return strconv.FormatInt(tblInfo.ID, 10) } diff --git a/store/driver/txn/error.go b/store/driver/txn/error.go index 6e98708b046d2..3c2d8377b580b 100644 --- a/store/driver/txn/error.go +++ b/store/driver/txn/error.go @@ -19,6 +19,7 @@ import ( "context" "encoding/json" "fmt" + "strconv" "strings" "time" @@ -53,7 +54,7 @@ func extractKeyExistsErrFromHandle(key kv.Key, value []byte, tblInfo *model.Tabl if handle.IsInt() { if pkInfo := tblInfo.GetPkColInfo(); pkInfo != nil { if mysql.HasUnsignedFlag(pkInfo.Flag) { - handleStr := fmt.Sprintf("%d", uint64(handle.IntValue())) + handleStr := strconv.FormatUint(uint64(handle.IntValue()), 10) return genKeyExistsError(name, handleStr, nil) } } diff --git a/table/tables/partition.go b/table/tables/partition.go index a37fb932df9cc..e5f2fe5e72f35 100644 --- a/table/tables/partition.go +++ b/table/tables/partition.go @@ -964,7 +964,7 @@ func (t *partitionedTable) locateRangeColumnPartition(ctx sessionctx.Context, pi if err == nil { val, _, err := e.EvalInt(ctx, chunk.MutRowFromDatums(r).ToRow()) if err == nil { - valueMsg = fmt.Sprintf("%d", val) + valueMsg = strconv.FormatInt(val, 10) } } } else {