From 44f2c2c381bfcc6d2ccfbc28f38424adb529e43f Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 21 Dec 2018 11:12:18 +0800 Subject: [PATCH 1/2] Makefile: make 'make check' faster (#8755) * remove vet check * make lint check on $(FILES) --- Makefile | 5 +++-- cmd/explaintest/main.go | 2 +- executor/executor_test.go | 6 +++--- executor/merge_join_test.go | 8 ++++---- executor/prepared_test.go | 36 +++++++++++++++++------------------ executor/set_test.go | 20 +++++++++---------- executor/show_test.go | 8 ++++---- go.mod | 2 +- go.sum | 19 ++++++++++++++++++ hack/hack_test.go | 19 ++++++++++++++++++ infoschema/infoschema_test.go | 4 ++-- meta/autoid/autoid_test.go | 16 ++++++++-------- planner/core/prepare_test.go | 3 +-- session/session_test.go | 6 +++--- 14 files changed, 96 insertions(+), 58 deletions(-) create mode 100644 hack/hack_test.go diff --git a/Makefile b/Makefile index 2b16c51460d87..58972809d0658 100644 --- a/Makefile +++ b/Makefile @@ -64,7 +64,7 @@ check-setup: @which retool >/dev/null 2>&1 || go get github.com/twitchtv/retool @GO111MODULE=off retool sync -check: check-setup fmt lint vet tidy +check: fmt lint tidy # These need to be fixed before they can be ran regularly check-fail: goword check-static check-slow @@ -92,7 +92,8 @@ check-slow: lint: @echo "linting" - @CGO_ENABLED=0 retool do revive -formatter friendly -config revive.toml $(PACKAGES) + @$(GO) install github.com/mgechev/revive + @CGO_ENABLED=0 revive -formatter friendly -config revive.toml $(FILES) vet: @echo "vet" diff --git a/cmd/explaintest/main.go b/cmd/explaintest/main.go index 556dbd338aa81..2ac30aa2b81cd 100644 --- a/cmd/explaintest/main.go +++ b/cmd/explaintest/main.go @@ -16,6 +16,7 @@ package main import ( "bytes" "database/sql" + "flag" "fmt" "io" "io/ioutil" @@ -24,7 +25,6 @@ import ( "os/exec" "strings" - "flag" "github.com/go-sql-driver/mysql" "github.com/pingcap/errors" "github.com/pingcap/parser/ast" diff --git a/executor/executor_test.go b/executor/executor_test.go index f5fdca08f4155..e97d793c982a0 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -239,12 +239,12 @@ func (s *testSuite) TestAdmin(c *C) { c.Assert(err, IsNil) err = txn.Commit(context.Background()) c.Assert(err, IsNil) - r, err_admin := tk.Exec("admin check table admin_test") - c.Assert(err_admin, NotNil) + r, errAdmin := tk.Exec("admin check table admin_test") + c.Assert(errAdmin, NotNil) if config.CheckTableBeforeDrop { r, err = tk.Exec("drop table admin_test") - c.Assert(err.Error(), Equals, err_admin.Error()) + c.Assert(err.Error(), Equals, errAdmin.Error()) // Drop inconsistency index. tk.MustExec("alter table admin_test drop index c1") diff --git a/executor/merge_join_test.go b/executor/merge_join_test.go index d2a5ee9dc22bc..899164094630c 100644 --- a/executor/merge_join_test.go +++ b/executor/merge_join_test.go @@ -215,8 +215,8 @@ const plan3 = `[[TableScan_12 { } ]]` func checkMergeAndRun(tk *testkit.TestKit, c *C, sql string) *testkit.Result { - explainedSql := "explain " + sql - result := tk.MustQuery(explainedSql) + explainedSQL := "explain " + sql + result := tk.MustQuery(explainedSQL) resultStr := fmt.Sprintf("%v", result.Rows()) if !strings.ContainsAny(resultStr, "MergeJoin") { c.Error("Expected MergeJoin in plannercore.") @@ -225,8 +225,8 @@ func checkMergeAndRun(tk *testkit.TestKit, c *C, sql string) *testkit.Result { } func checkPlanAndRun(tk *testkit.TestKit, c *C, plan string, sql string) *testkit.Result { - explainedSql := "explain " + sql - result := tk.MustQuery(explainedSql) + explainedSQL := "explain " + sql + result := tk.MustQuery(explainedSQL) resultStr := fmt.Sprintf("%v", result.Rows()) if plan != resultStr { // TODO: Reopen it after refactoring explain. diff --git a/executor/prepared_test.go b/executor/prepared_test.go index cbcf249ddee48..d732b275a8f5e 100644 --- a/executor/prepared_test.go +++ b/executor/prepared_test.go @@ -82,58 +82,58 @@ func (s *testSuite) TestPrepared(c *C) { result := tk.MustQuery("select distinct c1, c2 from prepare_test where c1 = ?", 1) result.Check(testkit.Rows("1 ")) - // Call Session PrepareStmt directly to get stmtId. + // Call Session PrepareStmt directly to get stmtID. query := "select c1, c2 from prepare_test where c1 = ?" - stmtId, _, _, err := tk.Se.PrepareStmt(query) + stmtID, _, _, err := tk.Se.PrepareStmt(query) c.Assert(err, IsNil) - rs, err := tk.Se.ExecutePreparedStmt(ctx, stmtId, 1) + rs, err := tk.Se.ExecutePreparedStmt(ctx, stmtID, 1) c.Assert(err, IsNil) tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 ")) tk.MustExec("delete from prepare_test") query = "select c1 from prepare_test where c1 = (select c1 from prepare_test where c1 = ?)" - stmtId, _, _, err = tk.Se.PrepareStmt(query) + stmtID, _, _, err = tk.Se.PrepareStmt(query) c.Assert(err, IsNil) tk1 := testkit.NewTestKitWithInit(c, s.store) tk1.MustExec("insert prepare_test (c1) values (3)") - rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 3) + rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 3) c.Assert(err, IsNil) tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("3")) tk.MustExec("delete from prepare_test") query = "select c1 from prepare_test where c1 = (select c1 from prepare_test where c1 = ?)" - stmtId, _, _, err = tk.Se.PrepareStmt(query) + stmtID, _, _, err = tk.Se.PrepareStmt(query) c.Assert(err, IsNil) - _, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 3) + _, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 3) c.Assert(err, IsNil) tk1.MustExec("insert prepare_test (c1) values (3)") - rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 3) + rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 3) c.Assert(err, IsNil) tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("3")) tk.MustExec("delete from prepare_test") query = "select c1 from prepare_test where c1 in (select c1 from prepare_test where c1 = ?)" - stmtId, _, _, err = tk.Se.PrepareStmt(query) + stmtID, _, _, err = tk.Se.PrepareStmt(query) c.Assert(err, IsNil) - _, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 3) + _, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 3) c.Assert(err, IsNil) tk1.MustExec("insert prepare_test (c1) values (3)") - rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 3) + rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 3) c.Assert(err, IsNil) tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("3")) tk.MustExec("begin") tk.MustExec("insert prepare_test (c1) values (4)") query = "select c1, c2 from prepare_test where c1 = ?" - stmtId, _, _, err = tk.Se.PrepareStmt(query) + stmtID, _, _, err = tk.Se.PrepareStmt(query) c.Assert(err, IsNil) tk.MustExec("rollback") - rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 4) + rs, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 4) c.Assert(err, IsNil) tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows()) // Check that ast.Statement created by executor.CompileExecutePreparedStmt has query text. - stmt, err := executor.CompileExecutePreparedStmt(tk.Se, stmtId, 1) + stmt, err := executor.CompileExecutePreparedStmt(tk.Se, stmtID, 1) c.Assert(err, IsNil) c.Assert(stmt.OriginText(), Equals, query) @@ -153,20 +153,20 @@ func (s *testSuite) TestPrepared(c *C) { tk.Exec("create table prepare2 (a int)") // Should success as the changed schema do not affect the prepared statement. - _, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 1) + _, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 1) c.Assert(err, IsNil) // Drop a column so the prepared statement become invalid. query = "select c1, c2 from prepare_test where c1 = ?" - stmtId, _, _, err = tk.Se.PrepareStmt(query) + stmtID, _, _, err = tk.Se.PrepareStmt(query) c.Assert(err, IsNil) tk.MustExec("alter table prepare_test drop column c2") - _, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 1) + _, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 1) c.Assert(plannercore.ErrUnknownColumn.Equal(err), IsTrue) tk.MustExec("drop table prepare_test") - _, err = tk.Se.ExecutePreparedStmt(ctx, stmtId, 1) + _, err = tk.Se.ExecutePreparedStmt(ctx, stmtID, 1) c.Assert(plannercore.ErrSchemaChanged.Equal(err), IsTrue) // issue 3381 diff --git a/executor/set_test.go b/executor/set_test.go index e5d72c5b8e2ba..f02c594bbe105 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -50,24 +50,24 @@ func (s *testSuite) TestSetVar(c *C) { _, err := tk.Exec(testSQL) c.Assert(err, NotNil) - errTestSql := "SET @@date_format = 1;" - _, err = tk.Exec(errTestSql) + errTestSQL := "SET @@date_format = 1;" + _, err = tk.Exec(errTestSQL) c.Assert(err, NotNil) - errTestSql = "SET @@rewriter_enabled = 1;" - _, err = tk.Exec(errTestSql) + errTestSQL = "SET @@rewriter_enabled = 1;" + _, err = tk.Exec(errTestSQL) c.Assert(err, NotNil) - errTestSql = "SET xxx = abcd;" - _, err = tk.Exec(errTestSql) + errTestSQL = "SET xxx = abcd;" + _, err = tk.Exec(errTestSQL) c.Assert(err, NotNil) - errTestSql = "SET @@global.a = 1;" - _, err = tk.Exec(errTestSql) + errTestSQL = "SET @@global.a = 1;" + _, err = tk.Exec(errTestSQL) c.Assert(err, NotNil) - errTestSql = "SET @@global.timestamp = 1;" - _, err = tk.Exec(errTestSql) + errTestSQL = "SET @@global.timestamp = 1;" + _, err = tk.Exec(errTestSQL) c.Assert(err, NotNil) // For issue 998 diff --git a/executor/show_test.go b/executor/show_test.go index 0fe26f67140f1..67d7f16d664b9 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -621,13 +621,13 @@ func (s *testSuite) TestShow2(c *C) { is := domain.GetDomain(ctx).InfoSchema() tblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) c.Assert(err, IsNil) - create_time := model.TSConvert2Time(tblInfo.Meta().UpdateTS).Format("2006-01-02 15:04:05") + createTime := model.TSConvert2Time(tblInfo.Meta().UpdateTS).Format("2006-01-02 15:04:05") // The Hostname is the actual host tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "192.168.0.1", AuthUsername: "root", AuthHostname: "%"}, nil, []byte("012345678901234567890")) r := tk.MustQuery("show table status from test like 't'") - r.Check(testkit.Rows(fmt.Sprintf("t InnoDB 10 Compact 0 0 0 0 0 0 0 %s utf8mb4_bin 注释", create_time))) + r.Check(testkit.Rows(fmt.Sprintf("t InnoDB 10 Compact 0 0 0 0 0 0 0 %s utf8mb4_bin 注释", createTime))) tk.MustQuery("show databases like 'test'").Check(testkit.Rows("test")) @@ -663,9 +663,9 @@ func (s *testSuite) TestUnprivilegedShow(c *C) { is := domain.GetDomain(ctx).InfoSchema() tblInfo, err := is.TableByName(model.NewCIStr("testshow"), model.NewCIStr("t1")) c.Assert(err, IsNil) - create_time := model.TSConvert2Time(tblInfo.Meta().UpdateTS).Format("2006-01-02 15:04:05") + createTime := model.TSConvert2Time(tblInfo.Meta().UpdateTS).Format("2006-01-02 15:04:05") - tk.MustQuery("show table status from testshow").Check(testkit.Rows(fmt.Sprintf("t1 InnoDB 10 Compact 0 0 0 0 0 0 0 %s utf8mb4_bin ", create_time))) + tk.MustQuery("show table status from testshow").Check(testkit.Rows(fmt.Sprintf("t1 InnoDB 10 Compact 0 0 0 0 0 0 0 %s utf8mb4_bin ", createTime))) } diff --git a/go.mod b/go.mod index fec4611652e7d..6fa2dae0af852 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/jonboulle/clockwork v0.1.0 // indirect github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5 github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/mgechev/revive v0.0.0-20181210140514-b4cc152955fb github.com/montanaflynn/stats v0.0.0-20180911141734-db72e6cae808 // indirect github.com/myesui/uuid v1.0.0 // indirect github.com/ngaut/pools v0.0.0-20180318154953-b7bc8c42aac7 @@ -52,7 +53,6 @@ require ( github.com/pingcap/pd v2.1.0-rc.4+incompatible github.com/pingcap/tidb-tools v2.1.1-0.20181218072513-b2235d442b06+incompatible github.com/pingcap/tipb v0.0.0-20181012112600-11e33c750323 - github.com/pkg/errors v0.8.0 // indirect github.com/prometheus/client_golang v0.9.0 github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 github.com/prometheus/common v0.0.0-20181020173914-7e9e6cabbd39 // indirect diff --git a/go.sum b/go.sum index f144b72e933e0..7aea00b386ac4 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f h1:5ZfJxyXo8KyX8DgGXC5B7ILL8y51fci/qYz2B4j8iLY= @@ -36,6 +37,10 @@ github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaI github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= github.com/etcd-io/gofail v0.0.0-20180808172546-51ce9a71510a h1:QNEenQIsGDEEfFNSnN+h6hE1OwnHqTg7Dl9gEk1Cko4= github.com/etcd-io/gofail v0.0.0-20180808172546-51ce9a71510a/go.mod h1:49H/RkXP8pKaZy4h0d+NW16rSLhyVBt4o6VLJbmOqDE= +github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/structtag v1.0.0 h1:pTHj65+u3RKWYPSGaU290FpI/dXxTaHdVwVwbcPKmEc= +github.com/fatih/structtag v1.0.0/go.mod h1:IKitwq45uXL/yqi5mYghiD3w9H6eTOvI9vnk8tXMphA= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= @@ -79,8 +84,18 @@ github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5 h1:2U0HzY8BJ8hVwDK github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4= +github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mgechev/dots v0.0.0-20180605013149-8e09d8ea2757 h1:KTwJ7Lo3KDKMknRYN5JEFRGIM4IkG59QjFFM2mxsMEU= +github.com/mgechev/dots v0.0.0-20180605013149-8e09d8ea2757/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= +github.com/mgechev/revive v0.0.0-20181210140514-b4cc152955fb h1:bLiKpCHe+ngBsF1o7DjZTmoffHEy2gdQ/+9NunuJ4ZY= +github.com/mgechev/revive v0.0.0-20181210140514-b4cc152955fb/go.mod h1:pVHj2KvxEhotJ6Lmr7zb3YgNMX1QKt8cyp6fdPHOrzU= github.com/montanaflynn/stats v0.0.0-20180911141734-db72e6cae808 h1:pmpDGKLw4n82EtrNiLqB+xSz/JQwFOaZuMALYUHwX5s= github.com/montanaflynn/stats v0.0.0-20180911141734-db72e6cae808/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/myesui/uuid v1.0.0 h1:xCBmH4l5KuvLYc5L7AS7SZg9/jKdIFubM7OVoLqaQUI= @@ -89,6 +104,8 @@ github.com/ngaut/pools v0.0.0-20180318154953-b7bc8c42aac7 h1:7KAv7KMGTTqSmYZtNdc github.com/ngaut/pools v0.0.0-20180318154953-b7bc8c42aac7/go.mod h1:iWMfgwqYW+e8n5lC/jjNEhwcjbRDpl5NT7n2h+4UNcI= github.com/ngaut/sync2 v0.0.0-20141008032647-7a24ed77b2ef h1:K0Fn+DoFqNqktdZtdV3bPQ/0cuYh2H4rkg0tytX/07k= github.com/ngaut/sync2 v0.0.0-20141008032647-7a24ed77b2ef/go.mod h1:7WjlapSfwQyo6LNmIvEWzsW1hbBQfpUO4JWnuQRmva8= +github.com/olekukonko/tablewriter v0.0.0-20180912035003-be2c049b30cc h1:rQ1O4ZLYR2xXHXgBCCfIIGnuZ0lidMQw2S5n1oOv+Wg= +github.com/olekukonko/tablewriter v0.0.0-20180912035003-be2c049b30cc/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -191,6 +208,8 @@ golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuA golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52 h1:JG/0uqcGdTNgq7FdU+61l5Pdmb8putNZlXb65bJBROs= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180911133044-677d2ff680c1 h1:dzEuQYa6+a3gROnSlgly5ERUm4SZKJt+dh+4iSbO+bI= +golang.org/x/tools v0.0.0-20180911133044-677d2ff680c1/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= diff --git a/hack/hack_test.go b/hack/hack_test.go new file mode 100644 index 0000000000000..a665ea73249f1 --- /dev/null +++ b/hack/hack_test.go @@ -0,0 +1,19 @@ +// Copyright 2018 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package hack + +import ( + // Imported for go.mod to cache package to accelerate CI. + _ "github.com/mgechev/revive/lint" +) diff --git a/infoschema/infoschema_test.go b/infoschema/infoschema_test.go index 7baee0a30a980..3381f19d64aa0 100644 --- a/infoschema/infoschema_test.go +++ b/infoschema/infoschema_test.go @@ -247,7 +247,7 @@ func (*testSuite) TestInfoTables(c *C) { is := handle.Get() c.Assert(is, NotNil) - info_tables := []string{ + infoTables := []string{ "SCHEMATA", "TABLES", "COLUMNS", @@ -280,7 +280,7 @@ func (*testSuite) TestInfoTables(c *C) { "COLLATION_CHARACTER_SET_APPLICABILITY", "PROCESSLIST", } - for _, t := range info_tables { + for _, t := range infoTables { tb, err1 := is.TableByName(model.NewCIStr(infoschema.Name), model.NewCIStr(t)) c.Assert(err1, IsNil) c.Assert(tb, NotNil) diff --git a/meta/autoid/autoid_test.go b/meta/autoid/autoid_test.go index cc097dafb7750..c03e351c165c3 100644 --- a/meta/autoid/autoid_test.go +++ b/meta/autoid/autoid_test.go @@ -60,9 +60,9 @@ func (*testSuite) TestT(c *C) { alloc := autoid.NewAllocator(store, 1, false) c.Assert(alloc, NotNil) - globalAutoId, err := alloc.NextGlobalAutoID(1) + globalAutoID, err := alloc.NextGlobalAutoID(1) c.Assert(err, IsNil) - c.Assert(globalAutoId, Equals, int64(1)) + c.Assert(globalAutoID, Equals, int64(1)) id, err := alloc.Alloc(1) c.Assert(err, IsNil) c.Assert(id, Equals, int64(1)) @@ -71,9 +71,9 @@ func (*testSuite) TestT(c *C) { c.Assert(id, Equals, int64(2)) _, err = alloc.Alloc(0) c.Assert(err, NotNil) - globalAutoId, err = alloc.NextGlobalAutoID(1) + globalAutoID, err = alloc.NextGlobalAutoID(1) c.Assert(err, IsNil) - c.Assert(globalAutoId, Equals, int64(autoid.GetStep()+1)) + c.Assert(globalAutoID, Equals, int64(autoid.GetStep()+1)) // rebase err = alloc.Rebase(1, int64(1), true) @@ -151,9 +151,9 @@ func (*testSuite) TestUnsignedAutoid(c *C) { alloc := autoid.NewAllocator(store, 1, true) c.Assert(alloc, NotNil) - globalAutoId, err := alloc.NextGlobalAutoID(1) + globalAutoID, err := alloc.NextGlobalAutoID(1) c.Assert(err, IsNil) - c.Assert(globalAutoId, Equals, int64(1)) + c.Assert(globalAutoID, Equals, int64(1)) id, err := alloc.Alloc(1) c.Assert(err, IsNil) c.Assert(id, Equals, int64(1)) @@ -162,9 +162,9 @@ func (*testSuite) TestUnsignedAutoid(c *C) { c.Assert(id, Equals, int64(2)) _, err = alloc.Alloc(0) c.Assert(err, NotNil) - globalAutoId, err = alloc.NextGlobalAutoID(1) + globalAutoID, err = alloc.NextGlobalAutoID(1) c.Assert(err, IsNil) - c.Assert(globalAutoId, Equals, int64(autoid.GetStep()+1)) + c.Assert(globalAutoID, Equals, int64(autoid.GetStep()+1)) // rebase err = alloc.Rebase(1, int64(1), true) diff --git a/planner/core/prepare_test.go b/planner/core/prepare_test.go index a1fb2c71ef557..382d2065c6a89 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -259,9 +259,8 @@ func (s *testPrepareSuite) TestPrepareOverMaxPreparedStmtCount(c *C) { _, err = tk.Exec(`prepare stmt` + strconv.Itoa(i) + ` from "select 1"`) c.Assert(terror.ErrorEqual(err, variable.ErrMaxPreparedStmtCountReached), IsTrue) break - } else { - tk.Exec(`prepare stmt` + strconv.Itoa(i) + ` from "select 1"`) } + tk.Exec(`prepare stmt` + strconv.Itoa(i) + ` from "select 1"`) } } diff --git a/session/session_test.go b/session/session_test.go index cd8897b747615..8aa98e3d2d6db 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -100,16 +100,16 @@ func (p *mockBinlogPump) WriteBinlog(ctx context.Context, in *binlog.WriteBinlog return &binlog.WriteBinlogResp{}, nil } -type mockPump_PullBinlogsClient struct { +type mockPumpPullBinlogsClient struct { grpc.ClientStream } -func (m mockPump_PullBinlogsClient) Recv() (*binlog.PullBinlogResp, error) { +func (m mockPumpPullBinlogsClient) Recv() (*binlog.PullBinlogResp, error) { return nil, nil } func (p *mockBinlogPump) PullBinlogs(ctx context.Context, in *binlog.PullBinlogReq, opts ...grpc.CallOption) (binlog.Pump_PullBinlogsClient, error) { - return mockPump_PullBinlogsClient{mocktikv.MockGRPCClientStream()}, nil + return mockPumpPullBinlogsClient{mocktikv.MockGRPCClientStream()}, nil } func (s *testSessionSuite) TestForCoverage(c *C) { From 94df8d1e30a98048f057c6d357a972169cb2d294 Mon Sep 17 00:00:00 2001 From: lysu Date: Fri, 21 Dec 2018 13:44:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?*=EF=BC=9A=20ignore=20unknown=20hint=20and?= =?UTF-8?q?=20return=20warning=20instead=20of=20return=20a=20parser=20erro?= =?UTF-8?q?r=20(#8685)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- executor/prepared.go | 6 +++++- expression/integration_test.go | 10 ++++++++++ expression/simple_rewriter.go | 10 ++++++++-- go.mod | 2 +- go.sum | 4 ++-- session/session.go | 8 ++++++-- session/tidb.go | 5 ++++- table/tables/gen_expr.go | 2 +- 8 files changed, 37 insertions(+), 10 deletions(-) diff --git a/executor/prepared.go b/executor/prepared.go index d2821417a77a0..28445b1c770da 100644 --- a/executor/prepared.go +++ b/executor/prepared.go @@ -114,7 +114,11 @@ func (e *PrepareExec) Next(ctx context.Context, chk *chunk.Chunk) error { } else { p := parser.New() p.EnableWindowFunc(vars.EnableWindowFunction) - stmts, err = p.Parse(e.sqlText, charset, collation) + var warns []error + stmts, warns, err = p.Parse(e.sqlText, charset, collation) + for _, warn := range warns { + e.ctx.GetSessionVars().StmtCtx.AppendWarning(warn) + } } if err != nil { return errors.Trace(err) diff --git a/expression/integration_test.go b/expression/integration_test.go index 1e6b24702f599..5e5f73d7770f9 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -3697,6 +3697,16 @@ func (s *testIntegrationSuite) TestDecimalMul(c *C) { res.Check(testkit.Rows("0.55125221922461136")) } +func (s *testIntegrationSuite) TestUnknowHintIgnore(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("USE test") + tk.MustExec("create table t(a int)") + tk.MustQuery("select /*+ unkown_hint(c1)*/ 1").Check(testkit.Rows("1")) + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 line 1 column 28 near \" 1\" (total length 30)")) + _, err := tk.Exec("select 1 from /*+ test1() */ t") + c.Assert(err, NotNil) +} + func (s *testIntegrationSuite) TestValuesInNonInsertStmt(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec(`use test;`) diff --git a/expression/simple_rewriter.go b/expression/simple_rewriter.go index d0674fb87b33f..7172b699e85ee 100644 --- a/expression/simple_rewriter.go +++ b/expression/simple_rewriter.go @@ -37,7 +37,10 @@ type simpleRewriter struct { // The expression string must only reference the column in table Info. func ParseSimpleExprWithTableInfo(ctx sessionctx.Context, exprStr string, tableInfo *model.TableInfo) (Expression, error) { exprStr = "select " + exprStr - stmts, err := parser.New().Parse(exprStr, "", "") + stmts, warns, err := parser.New().Parse(exprStr, "", "") + for _, warn := range warns { + ctx.GetSessionVars().StmtCtx.AppendWarning(warn) + } if err != nil { return nil, err } @@ -72,7 +75,10 @@ func RewriteSimpleExprWithTableInfo(ctx sessionctx.Context, tbl *model.TableInfo // The expression string must only reference the column in the given schema. func ParseSimpleExprsWithSchema(ctx sessionctx.Context, exprStr string, schema *Schema) ([]Expression, error) { exprStr = "select " + exprStr - stmts, err := parser.New().Parse(exprStr, "", "") + stmts, warns, err := parser.New().Parse(exprStr, "", "") + for _, warn := range warns { + ctx.GetSessionVars().StmtCtx.AppendWarning(warn) + } if err != nil { return nil, err } diff --git a/go.mod b/go.mod index 6fa2dae0af852..f5a6c0a7d7b19 100644 --- a/go.mod +++ b/go.mod @@ -49,7 +49,7 @@ require ( github.com/pingcap/gofail v0.0.0-20181217135706-6a951c1e42c3 github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e github.com/pingcap/kvproto v0.0.0-20181105061835-1b5d69cd1d26 - github.com/pingcap/parser v0.0.0-20181218071912-deacf026787e + github.com/pingcap/parser v0.0.0-20181221050948-947d4ab3052f github.com/pingcap/pd v2.1.0-rc.4+incompatible github.com/pingcap/tidb-tools v2.1.1-0.20181218072513-b2235d442b06+incompatible github.com/pingcap/tipb v0.0.0-20181012112600-11e33c750323 diff --git a/go.sum b/go.sum index 7aea00b386ac4..f30d7e601d11e 100644 --- a/go.sum +++ b/go.sum @@ -125,8 +125,8 @@ github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e h1:P73/4dPCL96rG github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw= github.com/pingcap/kvproto v0.0.0-20181105061835-1b5d69cd1d26 h1:JK4VLNYbSn36QSbCnqALi2ySXdH0DfcMssT/zmLf4Ls= github.com/pingcap/kvproto v0.0.0-20181105061835-1b5d69cd1d26/go.mod h1:0gwbe1F2iBIjuQ9AH0DbQhL+Dpr5GofU8fgYyXk+ykk= -github.com/pingcap/parser v0.0.0-20181218071912-deacf026787e h1:jKibIs55HR7OMo62uhjA6Bfx3GK+rbHK4Gfd4/8aTzk= -github.com/pingcap/parser v0.0.0-20181218071912-deacf026787e/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= +github.com/pingcap/parser v0.0.0-20181221050948-947d4ab3052f h1:X2ZRYBERoJ5VDp7CdtXWfwbKqbeYn2kkdGA0b5/VwJ8= +github.com/pingcap/parser v0.0.0-20181221050948-947d4ab3052f/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/pd v2.1.0-rc.4+incompatible h1:/buwGk04aHO5odk/+O8ZOXGs4qkUjYTJ2UpCJXna8NE= github.com/pingcap/pd v2.1.0-rc.4+incompatible/go.mod h1:nD3+EoYes4+aNNODO99ES59V83MZSI+dFbhyr667a0E= github.com/pingcap/tidb-tools v2.1.1-0.20181218072513-b2235d442b06+incompatible h1:Bsd+NHosPVowEGB3BCx+2d8wUQGDTXSSC5ljeNS6cXo= diff --git a/session/session.go b/session/session.go index 1838880eaeff8..814da0c20de46 100644 --- a/session/session.go +++ b/session/session.go @@ -810,7 +810,7 @@ func (s *session) SetGlobalSysVar(name, value string) error { return errors.Trace(err) } -func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string) ([]ast.StmtNode, error) { +func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string) ([]ast.StmtNode, []error, error) { if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil { span1 := span.Tracer().StartSpan("session.ParseSQL", opentracing.ChildOf(span.Context())) defer span1.Finish() @@ -885,7 +885,7 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec // Step1: Compile query string to abstract syntax trees(ASTs). startTS := time.Now() - stmtNodes, err := s.ParseSQL(ctx, sql, charsetInfo, collation) + stmtNodes, warns, err := s.ParseSQL(ctx, sql, charsetInfo, collation) if err != nil { s.rollbackOnError(ctx) log.Warnf("con:%d parse error:\n%v\n%s", connID, err, sql) @@ -922,6 +922,10 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec // return the first recordset if client doesn't support ClientMultiResults. recordSets = recordSets[:1] } + + for _, warn := range warns { + s.sessionVars.StmtCtx.AppendWarning(warn) + } return recordSets, nil } diff --git a/session/tidb.go b/session/tidb.go index 7ef039281bf0b..3df30dc955367 100644 --- a/session/tidb.go +++ b/session/tidb.go @@ -125,7 +125,10 @@ func Parse(ctx sessionctx.Context, src string) ([]ast.StmtNode, error) { p := parser.New() p.EnableWindowFunc(ctx.GetSessionVars().EnableWindowFunction) p.SetSQLMode(ctx.GetSessionVars().SQLMode) - stmts, err := p.Parse(src, charset, collation) + stmts, warns, err := p.Parse(src, charset, collation) + for _, warn := range warns { + ctx.GetSessionVars().StmtCtx.AppendWarning(warn) + } if err != nil { log.Warnf("compiling %s, error: %v", src, err) return nil, errors.Trace(err) diff --git a/table/tables/gen_expr.go b/table/tables/gen_expr.go index 85ea540eac85b..ba11e7e7b4ab3 100644 --- a/table/tables/gen_expr.go +++ b/table/tables/gen_expr.go @@ -61,7 +61,7 @@ func (nr *nameResolver) Leave(inNode ast.Node) (node ast.Node, ok bool) { func parseExpression(expr string) (node ast.ExprNode, err error) { expr = fmt.Sprintf("select %s", expr) charset, collation := charset.GetDefaultCharsetAndCollate() - stmts, err := parser.New().Parse(expr, charset, collation) + stmts, _, err := parser.New().Parse(expr, charset, collation) if err == nil { node = stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr }