Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importer: rand other types by stats #5848

Merged
merged 3 commits into from Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions cmd/importer/db.go
Expand Up @@ -193,11 +193,18 @@ func genColumnData(table *table, column *column) (string, error) {

data = append(data, '\'')
return string(data), nil
case mysql.TypeFloat, mysql.TypeDouble, mysql.TypeDecimal:
case mysql.TypeFloat, mysql.TypeDouble:
var data float64
if isUnique {
data = float64(uniqInt64Value(column, 0, math.MaxInt64))
} else {
if column.hist != nil {
if tp.Tp == mysql.TypeDouble {
data = column.hist.randFloat64()
} else {
data = float64(column.hist.randFloat32())
}
}
if isUnsigned {
data = float64(randInt64Value(column, 0, math.MaxInt64-1))
} else {
Expand All @@ -220,7 +227,7 @@ func genColumnData(table *table, column *column) (string, error) {
if isUnique {
data = append(data, []byte(column.data.uniqTimestamp())...)
} else {
data = append(data, []byte(randTimestamp(column.min, column.max))...)
data = append(data, []byte(randTimestamp(column))...)
}

data = append(data, '\'')
Expand All @@ -230,7 +237,7 @@ func genColumnData(table *table, column *column) (string, error) {
if isUnique {
data = append(data, []byte(column.data.uniqTime())...)
} else {
data = append(data, []byte(randTime(column.min, column.max))...)
data = append(data, []byte(randTime(column))...)
}

data = append(data, '\'')
Expand All @@ -240,12 +247,12 @@ func genColumnData(table *table, column *column) (string, error) {
if isUnique {
data = append(data, []byte(column.data.uniqYear())...)
} else {
data = append(data, []byte(randYear(column.min, column.max))...)
data = append(data, []byte(randYear(column))...)
}

data = append(data, '\'')
return string(data), nil
case mysql.TypeNewDecimal:
case mysql.TypeNewDecimal, mysql.TypeDecimal:
var limit = int64(math.Pow10(tp.Flen))
var intVal int64
if limit < 0 {
Expand Down
25 changes: 16 additions & 9 deletions cmd/importer/rand.go
Expand Up @@ -69,14 +69,9 @@ func randString(n int) string {
return string(b)
}

func randDuration(n time.Duration) time.Duration {
duration := randInt(0, int(n))
return time.Duration(duration)
}

func randDate(col *column) string {
if col.hist != nil {
return col.hist.randDate()
return col.hist.randDate("DAY", "%Y-%m-%d", dateFormat)
}

min, max := col.min, col.max
Expand Down Expand Up @@ -105,7 +100,11 @@ func randDate(col *column) string {
return fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day())
}

func randTime(min string, max string) string {
func randTime(col *column) string {
if col.hist != nil {
return col.hist.randDate("SECOND", "%H:%i:%s", timeFormat)
}
min, max := col.min, col.max
if len(min) == 0 || len(max) == 0 {
hour := randInt(0, 23)
min := randInt(0, 59)
Expand All @@ -126,7 +125,11 @@ func randTime(min string, max string) string {
return fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
}

func randTimestamp(min string, max string) string {
func randTimestamp(col *column) string {
if col.hist != nil {
return col.hist.randDate("SECOND", "%Y-%m-%d %H:%i:%s", dateTimeFormat)
}
min, max := col.min, col.max
if len(min) == 0 {
year := time.Now().Year()
month := randInt(1, 12)
Expand Down Expand Up @@ -155,7 +158,11 @@ func randTimestamp(min string, max string) string {
return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
}

func randYear(min string, max string) string {
func randYear(col *column) string {
if col.hist != nil {
return col.hist.randDate("YEAR", "%Y", yearFormat)
}
min, max := col.min, col.max
if len(min) == 0 || len(max) == 0 {
return fmt.Sprintf("%04d", time.Now().Year()-randInt(0, 10))
}
Expand Down
56 changes: 51 additions & 5 deletions cmd/importer/stats.go
Expand Up @@ -88,6 +88,52 @@ func (h *histogram) randInt() int64 {
return h.Bounds.GetRow(idx).GetInt64(0)
}

func (h *histogram) randFloat64() float64 {
idx := h.getRandomBoundIdx()
if idx%2 == 0 {
lower := h.Bounds.GetRow(idx).GetFloat64(0)
upper := h.Bounds.GetRow(idx + 1).GetFloat64(0)
rd := rand.Float64()
return lower + rd*(upper-lower)
}
return h.Bounds.GetRow(idx).GetFloat64(0)
}

func (h *histogram) randFloat32() float32 {
idx := h.getRandomBoundIdx()
if idx%2 == 0 {
lower := h.Bounds.GetRow(idx).GetFloat32(0)
upper := h.Bounds.GetRow(idx + 1).GetFloat32(0)
rd := rand.Float32()
return lower + rd*(upper-lower)
}
return h.Bounds.GetRow(idx).GetFloat32(0)
}

func (h *histogram) randDecimal() *types.MyDecimal {
idx := h.getRandomBoundIdx()
if idx%2 == 0 {
lower := h.Bounds.GetRow(idx).GetMyDecimal(0)
upper := h.Bounds.GetRow(idx + 1).GetMyDecimal(0)
rd := rand.Float64()
l, err := lower.ToFloat64()
if err != nil {
log.Fatal(err)
}
r, err := upper.ToFloat64()
if err != nil {
log.Fatal(err)
}
dec := &types.MyDecimal{}
err = dec.FromFloat64(l + rd*(r-l))
if err != nil {
log.Fatal(err)
}
return dec
}
return h.Bounds.GetRow(idx).GetMyDecimal(0)
}

func getValidPrefix(lower, upper string) string {
for i := range lower {
if i >= len(upper) {
Expand Down Expand Up @@ -136,14 +182,14 @@ func (h *histogram) randString() string {
}

// randDate randoms a bucket and random a date between upper and lower bound.
func (h *histogram) randDate() string {
func (h *histogram) randDate(unit string, mysqlFmt string, dateFmt string) 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)
diff := types.TimestampDiff(unit, lower, upper)
if diff == 0 {
str, err := lower.DateFormat("%Y-%m-%d")
str, err := lower.DateFormat(mysqlFmt)
if err != nil {
log.Fatal(err)
}
Expand All @@ -155,9 +201,9 @@ func (h *histogram) randDate() string {
log.Fatal(err)
}
l = l.AddDate(0, 0, delta)
return l.Format(dateFormat)
return l.Format(dateFmt)
}
str, err := h.Bounds.GetRow(idx).GetTime(0).DateFormat("%Y-%m-%d")
str, err := h.Bounds.GetRow(idx).GetTime(0).DateFormat(mysqlFmt)
if err != nil {
log.Fatal(err)
}
Expand Down