Skip to content

Commit

Permalink
Merge branch 'master' into fix-cast-str-to-time
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Jun 22, 2022
2 parents dbc91dc + fc833a3 commit 198acdc
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 1,749 deletions.
9 changes: 9 additions & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,15 @@ func (b *executorBuilder) buildAnalyzeSamplingPushdown(task plannercore.AnalyzeC
}

// getAdjustedSampleRate calculate the sample rate by the table size. If we cannot get the table size. We use the 0.001 as the default sample rate.
// From the paper "Random sampling for histogram construction: how much is enough?"'s Corollary 1 to Theorem 5,
// for a table size n, histogram size k, maximum relative error in bin size f, and error probability gamma,
// the minimum random sample size is
// r = 4 * k * ln(2*n/gamma) / f^2
// If we take f = 0.5, gamma = 0.01, n =1e6, we would got r = 305.82* k.
// Since the there's log function over the table size n, the r grows slowly when the n increases.
// If we take n = 1e12, a 300*k sample still gives <= 0.66 bin size error with probability 0.99.
// So if we don't consider the top-n values, we can keep the sample size at 300*256.
// But we may take some top-n before building the histogram, so we increase the sample a little.
func (b *executorBuilder) getAdjustedSampleRate(sctx sessionctx.Context, task plannercore.AnalyzeColumnsTask) float64 {
statsHandle := domain.GetDomain(sctx).StatsHandle()
defaultRate := 0.001
Expand Down
6 changes: 5 additions & 1 deletion expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4129,9 +4129,13 @@ func (c *unixTimestampFunctionClass) getFunction(ctx sessionctx.Context, args []

// goTimeToMysqlUnixTimestamp converts go time into MySQL's Unix timestamp.
// MySQL's Unix timestamp ranges in int32. Values out of range should be rewritten to 0.
// https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp
func goTimeToMysqlUnixTimestamp(t time.Time, decimal int) (*types.MyDecimal, error) {
nanoSeconds := t.UnixNano()
if nanoSeconds < 0 || (nanoSeconds/1e3) >= (math.MaxInt32+1)*1e6 {
// Prior to MySQL 8.0.28, the valid range of argument values is the same as for the TIMESTAMP data type:
// '1970-01-01 00:00:01.000000' UTC to '2038-01-19 03:14:07.999999' UTC.
// This is also the case in MySQL 8.0.28 and later for 32-bit platforms.
if nanoSeconds < 1e9 || (nanoSeconds/1e3) >= (math.MaxInt32+1)*1e6 {
return new(types.MyDecimal), nil
}
dec := new(types.MyDecimal)
Expand Down
6 changes: 6 additions & 0 deletions expression/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,12 @@ func TestTimeBuiltin(t *testing.T) {

// for unix_timestamp
tk.MustExec("SET time_zone = '+00:00';")
tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-01-01 00:00:00.000001');").Check(testkit.Rows("0.000000"))
tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-01-01 00:00:00.999999');").Check(testkit.Rows("0.000000"))
tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-01-01 00:00:01.000000');").Check(testkit.Rows("1.000000"))
tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:07.999999');").Check(testkit.Rows("2147483647.999999"))
tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:08.000000');").Check(testkit.Rows("0.000000"))

result = tk.MustQuery("SELECT UNIX_TIMESTAMP(151113);")
result.Check(testkit.Rows("1447372800"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP(20151113);")
Expand Down
13 changes: 8 additions & 5 deletions tests/globalkilltest/global_kill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ var (

pdClientPath = flag.String("pd", "127.0.0.1:2379", "pd client path")

lostConnectionToPDTimeout = flag.Int("conn_lost", 5, "lost connection to PD timeout, should be the same as TiDB ldflag <ldflagLostConnectionToPDTimeout>")
// nolint: unused, deadcode
lostConnectionToPDTimeout = flag.Int("conn_lost", 5, "lost connection to PD timeout, should be the same as TiDB ldflag <ldflagLostConnectionToPDTimeout>")

// nolint: unused, deadcode
timeToCheckPDConnectionRestored = flag.Int("conn_restored", 1, "time to check PD connection restored, should be the same as TiDB ldflag <ldflagServerIDTimeToCheckPDConnectionRestored>")
)

Expand All @@ -64,7 +67,7 @@ type GlobalKillSuite struct {
pdCli *clientv3.Client
pdErr error

clusterId string
clusterID string
pdProc *exec.Cmd
tikvProc *exec.Cmd
}
Expand All @@ -74,7 +77,7 @@ func createGloabalKillSuite(t *testing.T) (s *GlobalKillSuite, clean func()) {
err := logutil.InitLogger(&logutil.LogConfig{Config: log.Config{Level: *logLevel}})
require.NoError(t, err)

s.clusterId = time.Now().Format(time.RFC3339Nano)
s.clusterID = time.Now().Format(time.RFC3339Nano)
err = s.startCluster()
require.NoError(t, err)
s.pdCli, s.pdErr = s.connectPD()
Expand Down Expand Up @@ -157,12 +160,12 @@ func (s *GlobalKillSuite) startPD(dataDir string) (err error) {
}

func (s *GlobalKillSuite) startCluster() (err error) {
err = s.startPD(s.clusterId)
err = s.startPD(s.clusterID)
if err != nil {
return
}

err = s.startTiKV(s.clusterId)
err = s.startTiKV(s.clusterID)
if err != nil {
return
}
Expand Down
46 changes: 0 additions & 46 deletions tests/globalkilltest/go.mod

This file was deleted.

Loading

0 comments on commit 198acdc

Please sign in to comment.