Skip to content

Commit

Permalink
Merge branch 'master' into issue-28323
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyj authored Oct 19, 2021
2 parents 0abc18f + c336f95 commit f6a4ed2
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 95 deletions.
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ endif
# Usage:
# make bench-daily TO=/path/to/file.json
bench-daily:
go test github.com/pingcap/tidb/session -bench BenchmarkDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/executor -bench BenchmarkDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/tablecodec -bench BenchmarkDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/expression -bench BenchmarkDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/util/rowcodec -bench BenchmarkDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/util/codec -bench BenchmarkDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/distsql -bench BenchmarkDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/util/benchdaily -bench BenchmarkDaily \
go test github.com/pingcap/tidb/session -run TestBenchDaily -bench Ignore --outfile bench_daily.json
go test github.com/pingcap/tidb/executor -run TestBenchDaily -bench Ignore --outfile bench_daily.json
go test github.com/pingcap/tidb/tablecodec -run TestBenchDaily -bench Ignore --outfile bench_daily.json
go test github.com/pingcap/tidb/expression -run TestBenchDaily -bench Ignore --outfile bench_daily.json
go test github.com/pingcap/tidb/util/rowcodec -run TestBenchDaily -bench Ignore --outfile bench_daily.json
go test github.com/pingcap/tidb/util/codec -run TestBenchDaily -bench Ignore --outfile bench_daily.json
go test github.com/pingcap/tidb/distsql -run TestBenchDaily -bench Ignore --outfile bench_daily.json
go test github.com/pingcap/tidb/util/benchdaily -run TestBenchDaily -bench Ignore \
-date `git log -n1 --date=unix --pretty=format:%cd` \
-commit `git log -n1 --pretty=format:%h` \
-outfile $(TO)
Expand Down
2 changes: 1 addition & 1 deletion distsql/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func BenchmarkSelectResponseChunk_SmallResponse(b *testing.B) {
}
}

func BenchmarkDaily(b *testing.B) {
func TestBenchDaily(t *testing.T) {
benchdaily.Run(
BenchmarkSelectResponseChunk_BigResponse,
BenchmarkSelectResponseChunk_SmallResponse,
Expand Down
19 changes: 13 additions & 6 deletions executor/aggfuncs/func_percentile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package aggfuncs_test

import (
"fmt"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/executor/aggfuncs"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)

type testSlice []int
Expand All @@ -30,7 +32,9 @@ func (a testSlice) Len() int { return len(a) }
func (a testSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a testSlice) Less(i, j int) bool { return a[i] < a[j] }

func (s *testSuite) TestPercentile(c *C) {
func TestPercentile(t *testing.T) {
t.Parallel()

tests := []aggTest{
buildAggTester(ast.AggFuncApproxPercentile, mysql.TypeLonglong, 5, nil, 2),
buildAggTester(ast.AggFuncApproxPercentile, mysql.TypeFloat, 5, nil, 2.0),
Expand All @@ -39,16 +43,19 @@ func (s *testSuite) TestPercentile(c *C) {
buildAggTester(ast.AggFuncApproxPercentile, mysql.TypeDate, 5, nil, types.TimeFromDays(367)),
buildAggTester(ast.AggFuncApproxPercentile, mysql.TypeDuration, 5, nil, types.Duration{Duration: time.Duration(2)}),
}
for _, test := range tests {
s.testAggFunc(c, test)
for i, test := range tests {
t.Run(fmt.Sprintf("%s_%d", test.funcName, i), func(t *testing.T) {
testAggFunc(t, test)
})
}

data := testSlice{}
for i := 1; i <= 28; i++ {
want := 28
for i := 1; i <= want; i++ {
data = append(data, i)
}
for i := 0; i < 10; i++ {
index := aggfuncs.PercentileForTesting(data, 100)
c.Assert(28, Equals, data[index])
require.Equal(t, want, data[index])
}
}
36 changes: 26 additions & 10 deletions executor/aggfuncs/func_sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,48 @@
package aggfuncs_test

import (
. "github.com/pingcap/check"
"fmt"
"testing"

"github.com/pingcap/tidb/executor/aggfuncs"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/set"
)

func (s *testSuite) TestMergePartialResult4Sum(c *C) {
func TestMergePartialResult4Sum(t *testing.T) {
t.Parallel()

tests := []aggTest{
buildAggTester(ast.AggFuncSum, mysql.TypeNewDecimal, 5, types.NewDecFromInt(10), types.NewDecFromInt(9), types.NewDecFromInt(19)),
buildAggTester(ast.AggFuncSum, mysql.TypeDouble, 5, 10.0, 9.0, 19.0),
}
for _, test := range tests {
s.testMergePartialResult(c, test)

for i, test := range tests {
t.Run(fmt.Sprintf("%s_%d", test.funcName, i), func(t *testing.T) {
testMergePartialResult(t, test)
})
}
}

func (s *testSuite) TestSum(c *C) {
func TestSum(t *testing.T) {
t.Parallel()

tests := []aggTest{
buildAggTester(ast.AggFuncSum, mysql.TypeNewDecimal, 5, nil, types.NewDecFromInt(10)),
buildAggTester(ast.AggFuncSum, mysql.TypeDouble, 5, nil, 10.0),
}
for _, test := range tests {
s.testAggFunc(c, test)
for i, test := range tests {
t.Run(fmt.Sprintf("%s_%d", test.funcName, i), func(t *testing.T) {
testAggFunc(t, test)
})
}
}

func (s *testSuite) TestMemSum(c *C) {
func TestMemSum(t *testing.T) {
t.Parallel()

tests := []aggMemTest{
buildAggMemTester(ast.AggFuncSum, mysql.TypeDouble, 5,
aggfuncs.DefPartialResult4SumFloat64Size, defaultUpdateMemDeltaGens, false),
Expand All @@ -54,7 +67,10 @@ func (s *testSuite) TestMemSum(c *C) {
buildAggMemTester(ast.AggFuncSum, mysql.TypeNewDecimal, 5,
aggfuncs.DefPartialResult4SumDistinctDecimalSize+set.DefStringSetBucketMemoryUsage, distinctUpdateMemDeltaGens, true),
}
for _, test := range tests {
s.testAggMemFunc(c, test)

for i, test := range tests {
t.Run(fmt.Sprintf("%s_%d", test.aggTest.funcName, i), func(t *testing.T) {
testAggMemFunc(t, test)
})
}
}
2 changes: 1 addition & 1 deletion executor/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,7 @@ func BenchmarkPipelinedRowNumberWindowFunctionExecution(b *testing.B) {

}

func BenchmarkDaily(b *testing.B) {
func TestBenchDaily(t *testing.T) {
benchdaily.Run(
BenchmarkReadLastLinesOfHugeLine,
)
Expand Down
14 changes: 9 additions & 5 deletions executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/privilege/privileges"
"github.com/pingcap/tidb/sessionctx"
Expand Down Expand Up @@ -76,19 +77,19 @@ func (e *GrantExec) Next(ctx context.Context, req *chunk.Chunk) error {
dbNameStr := model.NewCIStr(dbName)
schema := e.ctx.GetInfoSchema().(infoschema.InfoSchema)
tbl, err := schema.TableByName(dbNameStr, model.NewCIStr(e.Level.TableName))
if err != nil {
// Allow GRANT on non-existent table, see issue #28533
if err != nil && !terror.ErrorEqual(err, infoschema.ErrTableNotExists) {
return err
}
err = infoschema.ErrTableNotExists.GenWithStackByArgs(dbName, e.Level.TableName)
// Note the table name compare is case sensitive here.
if tbl.Meta().Name.String() != e.Level.TableName {
return err
if tbl != nil && tbl.Meta().Name.String() != e.Level.TableName {
return infoschema.ErrTableNotExists.GenWithStackByArgs(dbName, e.Level.TableName)
}
if len(e.Level.DBName) > 0 {
// The database name should also match.
db, succ := schema.SchemaByName(dbNameStr)
if !succ || db.Name.L != dbNameStr.L {
return err
return infoschema.ErrTableNotExists.GenWithStackByArgs(dbName, e.Level.TableName)
}
}
}
Expand Down Expand Up @@ -769,6 +770,9 @@ func getTargetSchemaAndTable(ctx sessionctx.Context, dbName, tableName string, i
}
name := model.NewCIStr(tableName)
tbl, err := is.TableByName(model.NewCIStr(dbName), name)
if terror.ErrorEqual(err, infoschema.ErrTableNotExists) {
return dbName, nil, err
}
if err != nil {
return "", nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions executor/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ func (s *testSuite3) TestGrantOnNonExistTable(c *C) {
tk.MustExec("use test")
_, err := tk.Exec("select * from nonexist")
c.Assert(terror.ErrorEqual(err, infoschema.ErrTableNotExists), IsTrue)
_, err = tk.Exec("grant Select,Insert on nonexist to 'genius'")
c.Assert(terror.ErrorEqual(err, infoschema.ErrTableNotExists), IsTrue)
// GRANT ON non-existent table success, see issue #28533
tk.MustExec("grant Select,Insert on nonexist to 'genius'")

tk.MustExec("create table if not exists xx (id int)")
// Case sensitive
Expand Down
12 changes: 9 additions & 3 deletions executor/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
Expand Down Expand Up @@ -241,17 +242,22 @@ func (e *RevokeExec) revokeDBPriv(internalSession sessionctx.Context, priv *ast.

func (e *RevokeExec) revokeTablePriv(internalSession sessionctx.Context, priv *ast.PrivElem, user, host string) error {
dbName, tbl, err := getTargetSchemaAndTable(e.ctx, e.Level.DBName, e.Level.TableName, e.is)
if err != nil {
if err != nil && !terror.ErrorEqual(err, infoschema.ErrTableNotExists) {
return err
}

// Allow REVOKE on non-existent table, see issue #28533
tblName := e.Level.TableName
if tbl != nil {
tblName = tbl.Meta().Name.O
}
sql := new(strings.Builder)
sqlexec.MustFormatSQL(sql, "UPDATE %n.%n SET ", mysql.SystemDB, mysql.TablePrivTable)
err = composeTablePrivUpdateForRevoke(internalSession, sql, priv.Priv, user, host, dbName, tbl.Meta().Name.O)
err = composeTablePrivUpdateForRevoke(internalSession, sql, priv.Priv, user, host, dbName, tblName)
if err != nil {
return err
}
sqlexec.MustFormatSQL(sql, " WHERE User=%? AND Host=%? AND DB=%? AND Table_name=%?", user, host, dbName, tbl.Meta().Name.O)
sqlexec.MustFormatSQL(sql, " WHERE User=%? AND Host=%? AND DB=%? AND Table_name=%?", user, host, dbName, tblName)

_, err = internalSession.(sqlexec.SQLExecutor).ExecuteInternal(context.Background(), sql.String())
return err
Expand Down
22 changes: 22 additions & 0 deletions executor/revoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,25 @@ func (s *testSuite1) TestRevokeDynamicPrivs(c *C) {
tk.MustExec("REVOKE BACKUP_ADMIN, SELECT, GRANT OPTION ON *.* FROM dyn")
tk.MustQuery("SELECT * FROM mysql.global_grants WHERE `Host` = '%' AND `User` = 'dyn' ORDER BY user,host,priv,with_grant_option").Check(testkit.Rows("dyn % SYSTEM_VARIABLES_ADMIN Y"))
}

func (s *testSuite1) TestRevokeOnNonExistTable(c *C) {
// issue #28533
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("CREATE DATABASE d1;")
defer tk.MustExec("DROP DATABASE IF EXISTS d1;")
tk.MustExec("USE d1;")
tk.MustExec("CREATE TABLE t1 (a int)")
defer tk.MustExec("DROP TABLE IF EXISTS t1")
tk.MustExec("CREATE USER issue28533")
defer tk.MustExec("DROP USER issue28533")

// GRANT ON existent table success
tk.MustExec("GRANT ALTER ON d1.t1 TO issue28533;")
// GRANT ON non-existent table success
tk.MustExec("GRANT INSERT, CREATE ON d1.t2 TO issue28533;")

// REVOKE ON non-existent table success
tk.MustExec("DROP TABLE t1;")
tk.MustExec("REVOKE ALTER ON d1.t1 FROM issue28533;")
}
2 changes: 1 addition & 1 deletion expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,7 @@ func (s *testVectorizeSuite2) TestVectorizedFilterConsiderNull(c *C) {
ctx.GetSessionVars().EnableVectorizedExpression = dafaultEnableVectorizedExpressionVar
}

func BenchmarkDaily(b *testing.B) {
func TestBenchDaily(t *testing.T) {
benchdaily.Run(
BenchmarkCastIntAsIntRow,
BenchmarkCastIntAsIntVec,
Expand Down
2 changes: 1 addition & 1 deletion session/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ func BenchmarkCompileExecutePreparedStmt(b *testing.B) {
// The format of the json output is described by the BenchOutput.
// Used by this command in the Makefile
// make bench-daily TO=xxx.json
func BenchmarkDaily(b *testing.B) {
func TestBenchDaily(t *testing.T) {
benchdaily.Run(
BenchmarkPreparedPointGet,
BenchmarkPointGet,
Expand Down
Loading

0 comments on commit f6a4ed2

Please sign in to comment.