Skip to content

Commit

Permalink
importer: support rand date by stats (pingcap#5830)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored and alivxxx committed Feb 9, 2018
1 parent e6eb3ae commit 45f00e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/importer/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func genColumnData(table *table, column *column) (string, error) {
if isUnique {
data = append(data, []byte(column.data.uniqDate())...)
} else {
data = append(data, []byte(randDate(column.min, column.max))...)
data = append(data, []byte(randDate(column))...)
}

data = append(data, '\'')
Expand Down
7 changes: 6 additions & 1 deletion cmd/importer/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func randDuration(n time.Duration) time.Duration {
return time.Duration(duration)
}

func randDate(min string, max string) string {
func randDate(col *column) string {
if col.hist != nil {
return col.hist.randDate()
}

min, max := col.min, col.max
if len(min) == 0 {
year := time.Now().Year()
month := randInt(1, 12)
Expand Down
31 changes: 31 additions & 0 deletions cmd/importer/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
"encoding/json"
"io/ioutil"
"math/rand"
"time"

"github.com/juju/errors"
"github.com/pingcap/tidb/model"
stats "github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mock"
Expand Down Expand Up @@ -132,3 +134,32 @@ func (h *histogram) randString() string {
}
return h.Bounds.GetRow(idx).GetString(0)
}

// randDate randoms a bucket and random a date between upper and lower bound.
func (h *histogram) randDate() string {
idx := h.getRandomBoundIdx()
if idx%2 == 0 {
lower := h.Bounds.GetRow(idx).GetTime(0)
upper := h.Bounds.GetRow(idx + 1).GetTime(0)
diff := types.TimestampDiff("DAY", lower, upper)
if diff == 0 {
str, err := lower.DateFormat("%Y-%m-%d")
if err != nil {
log.Fatal(err)
}
return str
}
delta := randInt(0, int(diff)-1)
l, err := lower.Time.GoTime(time.Local)
if err != nil {
log.Fatal(err)
}
l = l.AddDate(0, 0, delta)
return l.Format(dateFormat)
}
str, err := h.Bounds.GetRow(idx).GetTime(0).DateFormat("%Y-%m-%d")
if err != nil {
log.Fatal(err)
}
return str
}

0 comments on commit 45f00e8

Please sign in to comment.