Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pingcap/tidb into explain…
Browse files Browse the repository at this point in the history
…Verbose
  • Loading branch information
Reminiscent committed Aug 5, 2021
2 parents 7010794 + 0398c3f commit b52115a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
13 changes: 12 additions & 1 deletion executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ func (s *testSerialSuite) TestAggInDisk(c *C) {
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(a int)")
sql := "insert into t values (0)"
for i := 1; i <= 300; i++ {
for i := 1; i <= 200; i++ {
sql += fmt.Sprintf(",(%v)", i)
}
sql += ";"
Expand All @@ -1488,4 +1488,15 @@ func (s *testSerialSuite) TestAggInDisk(c *C) {
strings.Contains(disk, "Bytes"), IsTrue)
}
}

// Add code cover
// Test spill chunk. Add a line to avoid tmp spill chunk is always full.
tk.MustExec("insert into t values(0)")
tk.MustQuery("select sum(tt.b) from ( select /*+ HASH_AGG() */ avg(t1.a) as b from t t1 join t t2 group by t1.a, t2.a) as tt").Check(
testkit.Rows("4040100.0000"))
// Test no groupby and no data.
tk.MustExec("drop table t;")
tk.MustExec("create table t(c int, c1 int);")
tk.MustQuery("select /*+ HASH_AGG() */ count(c) from t;").Check(testkit.Rows("0"))
tk.MustQuery("select /*+ HASH_AGG() */ count(c) from t group by c1;").Check(testkit.Rows())
}
56 changes: 53 additions & 3 deletions expression/expr_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func (s *testEvaluatorSuite) TestExprPushDownToFlash(c *C) {
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_CeilDecimalToDecimal
// ScalarFuncSig_CeilDecToDec
function, err = NewFunction(mock.NewContext(), ast.Ceil, types.NewFieldType(mysql.TypeNewDecimal), decimalColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)
Expand All @@ -770,16 +770,66 @@ func (s *testEvaluatorSuite) TestExprPushDownToFlash(c *C) {
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_FloorDecimalToInt
// ScalarFuncSig_FloorDecToInt
function, err = NewFunction(mock.NewContext(), ast.Floor, types.NewFieldType(mysql.TypeLonglong), decimalColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_FloorDecimalToDecimal
// ScalarFuncSig_FloorDecToDec
function, err = NewFunction(mock.NewContext(), ast.Floor, types.NewFieldType(mysql.TypeNewDecimal), decimalColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Log1Arg
function, err = NewFunction(mock.NewContext(), ast.Log, types.NewFieldType(mysql.TypeDouble), realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Log2Args
function, err = NewFunction(mock.NewContext(), ast.Log, types.NewFieldType(mysql.TypeDouble), realColumn, realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Log2
function, err = NewFunction(mock.NewContext(), ast.Log2, types.NewFieldType(mysql.TypeDouble), realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Log10
function, err = NewFunction(mock.NewContext(), ast.Log10, types.NewFieldType(mysql.TypeDouble), realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Exp
function, err = NewFunction(mock.NewContext(), ast.Exp, types.NewFieldType(mysql.TypeDouble), realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Pow
function, err = NewFunction(mock.NewContext(), ast.Pow, types.NewFieldType(mysql.TypeDouble), realColumn, realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Radians
function, err = NewFunction(mock.NewContext(), ast.Radians, types.NewFieldType(mysql.TypeDouble), realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Degrees
function, err = NewFunction(mock.NewContext(), ast.Degrees, types.NewFieldType(mysql.TypeDouble), realColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_CRC32
function, err = NewFunction(mock.NewContext(), ast.CRC32, types.NewFieldType(mysql.TypeLonglong), stringColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ScalarFuncSig_Conv
function, err = NewFunction(mock.NewContext(), ast.Conv, types.NewFieldType(mysql.TypeDouble), stringColumn, intColumn, intColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// Replace
function, err = NewFunction(mock.NewContext(), ast.Replace, types.NewFieldType(mysql.TypeString), stringColumn, stringColumn, stringColumn)
c.Assert(err, IsNil)
Expand Down
7 changes: 4 additions & 3 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,9 +1015,10 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool {
ast.Plus, ast.Minus, ast.Div, ast.Mul, ast.Abs, ast.Mod,
ast.If, ast.Ifnull, ast.Case,
ast.Concat, ast.ConcatWS,
ast.Year, ast.Month, ast.Day,
ast.Date, ast.Year, ast.Month, ast.Day,
ast.DateDiff, ast.TimestampDiff, ast.DateFormat, ast.FromUnixTime,
ast.Sqrt,
ast.Sqrt, ast.Log, ast.Log2, ast.Log10, ast.Ln, ast.Exp, ast.Pow, ast.Sign,
ast.Radians, ast.Degrees, ast.Conv, ast.CRC32,
ast.JSONLength:
return true
case ast.Substr, ast.Substring, ast.Left, ast.Right, ast.CharLength:
Expand All @@ -1044,7 +1045,7 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool {
}
case ast.DateAdd, ast.AddDate:
switch function.Function.PbCode() {
case tipb.ScalarFuncSig_AddDateDatetimeInt, tipb.ScalarFuncSig_AddDateStringInt:
case tipb.ScalarFuncSig_AddDateDatetimeInt, tipb.ScalarFuncSig_AddDateStringInt, tipb.ScalarFuncSig_AddDateStringReal:
return true
}
case ast.DateSub, ast.SubDate:
Expand Down
36 changes: 18 additions & 18 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,11 +901,11 @@ func (s *testStatsSuite) prepareForGlobalStatsWithOpts(c *C, tk *testkit.TestKit
}

// nolint:unused
func (s *testStatsSuite) checkForGlobalStatsWithOpts(c *C, tk *testkit.TestKit, p string, topn, buckets int) {
func (s *testStatsSuite) checkForGlobalStatsWithOpts(c *C, tk *testkit.TestKit, t string, p string, topn, buckets int) {
delta := buckets/2 + 1
for _, isIdx := range []int{0, 1} {
c.Assert(len(tk.MustQuery(fmt.Sprintf("show stats_topn where partition_name='%v' and is_index=%v", p, isIdx)).Rows()), Equals, topn)
numBuckets := len(tk.MustQuery(fmt.Sprintf("show stats_buckets where partition_name='%v' and is_index=%v", p, isIdx)).Rows())
c.Assert(len(tk.MustQuery(fmt.Sprintf("show stats_topn where table_name='%v' and partition_name='%v' and is_index=%v", t, p, isIdx)).Rows()), Equals, topn)
numBuckets := len(tk.MustQuery(fmt.Sprintf("show stats_buckets where table_name='%v' and partition_name='%v' and is_index=%v", t, p, isIdx)).Rows())
// since the hist-building algorithm doesn't stipulate the final bucket number to be equal to the expected number exactly,
// we have to check the results by a range here.
c.Assert(numBuckets >= buckets-delta, IsTrue)
Expand Down Expand Up @@ -942,9 +942,9 @@ func (s *testStatsSuite) TestAnalyzeGlobalStatsWithOpts(c *C) {
sql := fmt.Sprintf("analyze table test_gstats_opt with %v topn, %v buckets", ca.topn, ca.buckets)
if !ca.err {
tk.MustExec(sql)
s.checkForGlobalStatsWithOpts(c, tk, "global", ca.topn, ca.buckets)
s.checkForGlobalStatsWithOpts(c, tk, "p0", ca.topn, ca.buckets)
s.checkForGlobalStatsWithOpts(c, tk, "p1", ca.topn, ca.buckets)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt", "global", ca.topn, ca.buckets)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt", "p0", ca.topn, ca.buckets)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt", "p1", ca.topn, ca.buckets)
} else {
err := tk.ExecToErr(sql)
c.Assert(err, NotNil)
Expand All @@ -961,25 +961,25 @@ func (s *testStatsSuite) TestAnalyzeGlobalStatsWithOpts2(c *C) {
s.prepareForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "test_gstats_opt2")

tk.MustExec("analyze table test_gstats_opt2 with 20 topn, 50 buckets, 1000 samples")
s.checkForGlobalStatsWithOpts(c, tk, "global", 2, 50)
s.checkForGlobalStatsWithOpts(c, tk, "p0", 1, 50)
s.checkForGlobalStatsWithOpts(c, tk, "p1", 1, 50)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "global", 2, 50)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p0", 1, 50)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p1", 1, 50)

// analyze a partition to let its options be different with others'
tk.MustExec("analyze table test_gstats_opt2 partition p0 with 10 topn, 20 buckets")
s.checkForGlobalStatsWithOpts(c, tk, "global", 10, 20) // use new options
s.checkForGlobalStatsWithOpts(c, tk, "p0", 10, 20)
s.checkForGlobalStatsWithOpts(c, tk, "p1", 1, 50)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "global", 10, 20) // use new options
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p0", 10, 20)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p1", 1, 50)

tk.MustExec("analyze table test_gstats_opt2 partition p1 with 100 topn, 200 buckets")
s.checkForGlobalStatsWithOpts(c, tk, "global", 100, 200)
s.checkForGlobalStatsWithOpts(c, tk, "p0", 10, 20)
s.checkForGlobalStatsWithOpts(c, tk, "p1", 100, 200)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "global", 100, 200)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p0", 10, 20)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p1", 100, 200)

tk.MustExec("analyze table test_gstats_opt2 partition p0 with 20 topn") // change back to 20 topn
s.checkForGlobalStatsWithOpts(c, tk, "global", 20, 256)
s.checkForGlobalStatsWithOpts(c, tk, "p0", 20, 256)
s.checkForGlobalStatsWithOpts(c, tk, "p1", 100, 200)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "global", 20, 256)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p0", 20, 256)
s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p1", 100, 200)
}

func (s *testStatsSuite) TestGlobalStatsHealthy(c *C) {
Expand Down

0 comments on commit b52115a

Please sign in to comment.