Skip to content

Commit

Permalink
Merge branch 'master' into fixBRGlueDDL
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu authored Feb 26, 2021
2 parents 8e80ac5 + 8a490e9 commit 9e256e7
Show file tree
Hide file tree
Showing 142 changed files with 4,129 additions and 2,965 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,19 @@ gosec:tools/bin/gosec
tools/bin/gosec $$($(PACKAGE_DIRECTORIES))

check-static: tools/bin/golangci-lint
tools/bin/golangci-lint run -v --disable-all --deadline=3m \
@git fetch https://github.com/pingcap/tidb
tools/bin/golangci-lint run -v --disable-all --deadline=5m \
--enable=misspell \
--enable=ineffassign \
--enable=deadcode \
--enable=errcheck \
--enable=gosimple \
--enable=staticcheck \
--enable=typecheck \
--enable=unused \
--enable=varcheck \
--enable=structcheck \
--new-from-rev=FETCH_HEAD \
$$($(PACKAGE_DIRECTORIES))

check-slow:tools/bin/gometalinter tools/bin/gosec
Expand Down
154 changes: 77 additions & 77 deletions bindinfo/bind_test.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cmd/benchdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store"
"github.com/pingcap/tidb/store/driver"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
Expand Down Expand Up @@ -55,7 +56,7 @@ func main() {
flag.PrintDefaults()
err := logutil.InitZapLogger(logutil.NewLogConfig(*logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false))
terror.MustNil(err)
err = store.Register("tikv", store.TiKVDriver{})
err = store.Register("tikv", driver.TiKVDriver{})
terror.MustNil(err)
ut := newBenchDB()
works := strings.Split(*runJobs, "|")
Expand Down
4 changes: 2 additions & 2 deletions cmd/benchkv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/kv"
storepkg "github.com/pingcap/tidb/store"
"github.com/pingcap/tidb/store/driver"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
Expand Down Expand Up @@ -69,7 +69,7 @@ var (

// Init initializes information.
func Init() {
driver := storepkg.TiKVDriver{}
driver := driver.TiKVDriver{}
var err error
store, err = driver.Open(fmt.Sprintf("tikv://%s?cluster=1", *pdAddr))
terror.MustNil(err)
Expand Down
3 changes: 2 additions & 1 deletion cmd/ddltest/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store"
tidbdriver "github.com/pingcap/tidb/store/driver"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/logutil"
Expand Down Expand Up @@ -1060,5 +1061,5 @@ func addEnvPath(newPath string) {

func init() {
rand.Seed(time.Now().UnixNano())
store.Register("tikv", store.TiKVDriver{})
store.Register("tikv", tidbdriver.TiKVDriver{})
}
3 changes: 1 addition & 2 deletions cmd/ddltest/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/gcworker"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -88,7 +87,7 @@ func (s *TestDDLSuite) checkAddIndex(c *C, indexInfo *model.IndexInfo) {
}

func (s *TestDDLSuite) checkDropIndex(c *C, indexInfo *model.IndexInfo) {
gcWorker, err := gcworker.NewMockGCWorker(s.store.(tikv.Storage))
gcWorker, err := gcworker.NewMockGCWorker(s.store)
c.Assert(err, IsNil)
err = gcWorker.DeleteRanges(goctx.Background(), uint64(math.MaxInt32))
c.Assert(err, IsNil)
Expand Down
17 changes: 17 additions & 0 deletions cmd/explaintest/r/explain_indexmerge.result
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,20 @@ IndexMerge 4999999.00 root
├─TableRangeScan(Build) 49.00 cop[tikv] table:t range:[-inf,50), keep order:false
├─IndexRangeScan(Build) 4999999.00 cop[tikv] table:t, index:tb(b) range:[-inf,5000000), keep order:false
└─TableRowIDScan(Probe) 4999999.00 cop[tikv] table:t keep order:false
set session tidb_enable_index_merge = on;
drop table if exists t;
CREATE TABLE t (
`id` int(11) NOT NULL,
`aid` bigint,
`c1` varchar(255) DEFAULT NULL,
`c2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `aid_c1` (`aid`,`c1`),
KEY `aid_c2` (`aid`,`c2`)
);
desc select /*+ USE_INDEX_MERGE(t, aid_c1, aid_c2) */ * from t where (aid = 1 and c1='aaa') or (aid = 2 and c2='bbb');
id estRows task access object operator info
IndexMerge_8 269.49 root
├─IndexRangeScan_5(Build) 0.10 cop[tikv] table:t, index:aid_c1(aid, c1) range:[1 "aaa",1 "aaa"], keep order:false, stats:pseudo
├─IndexRangeScan_6(Build) 0.10 cop[tikv] table:t, index:aid_c2(aid, c2) range:[2 "bbb",2 "bbb"], keep order:false, stats:pseudo
└─TableRowIDScan_7(Probe) 269.49 cop[tikv] table:t keep order:false, stats:pseudo
240 changes: 120 additions & 120 deletions cmd/explaintest/r/generated_columns.result

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions cmd/explaintest/t/explain_indexmerge.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ explain format = 'brief' select /*+ use_index_merge(t, tb) */ * from t where b <
explain format = 'brief' select /*+ no_index_merge(), use_index_merge(t, tb, tc) */ * from t where b < 50 or c < 5000000;
# tableScan can be a partial path to fetch handle
explain format = 'brief' select /*+ use_index_merge(t, primary, tb) */ * from t where a < 50 or b < 5000000;
# composed index
set session tidb_enable_index_merge = on;
drop table if exists t;
CREATE TABLE t (
`id` int(11) NOT NULL,
`aid` bigint,
`c1` varchar(255) DEFAULT NULL,
`c2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `aid_c1` (`aid`,`c1`),
KEY `aid_c2` (`aid`,`c2`)
);
desc select /*+ USE_INDEX_MERGE(t, aid_c1, aid_c2) */ * from t where (aid = 1 and c1='aaa') or (aid = 2 and c2='bbb');
48 changes: 24 additions & 24 deletions cmd/explaintest/t/generated_columns.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CREATE TABLE person (
KEY (city)
);

EXPLAIN SELECT name, id FROM person WHERE city = 'Beijing';
EXPLAIN format = 'brief' SELECT name, id FROM person WHERE city = 'Beijing';


DROP TABLE IF EXISTS `sgc`;
Expand All @@ -27,10 +27,10 @@ CREATE TABLE `sgc` (
KEY `idx_a_b` (`a`,`b`)
);

EXPLAIN SELECT a FROM sgc where a < 3;
EXPLAIN SELECT a, b FROM sgc where a < 3;
EXPLAIN SELECT a, b from sgc where b < 3;
EXPLAIN SELECT a, b from sgc where a < 3 and b < 3;
EXPLAIN format = 'brief' SELECT a FROM sgc where a < 3;
EXPLAIN format = 'brief' SELECT a, b FROM sgc where a < 3;
EXPLAIN format = 'brief' SELECT a, b from sgc where b < 3;
EXPLAIN format = 'brief' SELECT a, b from sgc where a < 3 and b < 3;

DROP TABLE IF EXISTS sgc1,
sgc2;
Expand Down Expand Up @@ -68,8 +68,8 @@ VALUES ('{"a": 1}', '{"1": "1"}');

ANALYZE TABLE sgc1, sgc2;

EXPLAIN SELECT /*+ TIDB_INLJ(sgc1, sgc2) */ * from sgc1 join sgc2 on sgc1.a=sgc2.a;
EXPLAIN SELECT * from sgc1 join sgc2 on sgc1.a=sgc2.a;
EXPLAIN format = 'brief' SELECT /*+ TIDB_INLJ(sgc1, sgc2) */ * from sgc1 join sgc2 on sgc1.a=sgc2.a;
EXPLAIN format = 'brief' SELECT * from sgc1 join sgc2 on sgc1.a=sgc2.a;


-- Stored generated columns as partition columns
Expand All @@ -88,19 +88,19 @@ PARTITION p4 VALUES LESS THAN (5),
PARTITION p5 VALUES LESS THAN (6),
PARTITION max VALUES LESS THAN MAXVALUE);

EXPLAIN SELECT * FROM sgc3 WHERE a <= 1;
EXPLAIN SELECT * FROM sgc3 WHERE a < 7;
EXPLAIN format = 'brief' SELECT * FROM sgc3 WHERE a <= 1;
EXPLAIN format = 'brief' SELECT * FROM sgc3 WHERE a < 7;

-- Virtual generated columns as indices

DROP TABLE IF EXISTS t1;
CREATE TABLE t1(a INT, b INT AS (a+1) VIRTUAL, c INT AS (b+1) VIRTUAL, d INT AS (c+1) VIRTUAL, KEY(b), INDEX IDX(c, d));
INSERT INTO t1 (a) VALUES (0);

EXPLAIN SELECT b FROM t1 WHERE b=1;
EXPLAIN SELECT b, c, d FROM t1 WHERE b=1;
EXPLAIN SELECT * FROM t1 WHERE b=1;
EXPLAIN SELECT c FROM t1 WHERE c=2 AND d=3;
EXPLAIN format = 'brief' SELECT b FROM t1 WHERE b=1;
EXPLAIN format = 'brief' SELECT b, c, d FROM t1 WHERE b=1;
EXPLAIN format = 'brief' SELECT * FROM t1 WHERE b=1;
EXPLAIN format = 'brief' SELECT c FROM t1 WHERE c=2 AND d=3;

DROP TABLE IF EXISTS person;
CREATE TABLE person (
Expand All @@ -111,7 +111,7 @@ city_no INT AS (JSON_EXTRACT(address_info, '$.city_no')) VIRTUAL,
KEY(city_no));

INSERT INTO person (name, address_info) VALUES ("John", CAST('{"city_no": 1}' AS JSON));
EXPLAIN SELECT name FROM person where city_no=1;
EXPLAIN format = 'brief' SELECT name FROM person where city_no=1;

-- Virtual generated columns in aggregate statement
-- ISSUE https://github.com/pingcap/tidb/issues/14072
Expand All @@ -123,18 +123,18 @@ CREATE TABLE t1 (a INT,
index (c));
INSERT INTO t1 (a) VALUES (2), (1), (1), (3), (NULL);

EXPLAIN SELECT sum(a) FROM t1 GROUP BY b;
EXPLAIN SELECT sum(a) FROM t1 GROUP BY c;
EXPLAIN SELECT sum(b) FROM t1 GROUP BY a;
EXPLAIN SELECT sum(b) FROM t1 GROUP BY c;
EXPLAIN SELECT sum(c) FROM t1 GROUP BY a;
EXPLAIN SELECT sum(c) FROM t1 GROUP BY b;
EXPLAIN format = 'brief' SELECT sum(a) FROM t1 GROUP BY b;
EXPLAIN format = 'brief' SELECT sum(a) FROM t1 GROUP BY c;
EXPLAIN format = 'brief' SELECT sum(b) FROM t1 GROUP BY a;
EXPLAIN format = 'brief' SELECT sum(b) FROM t1 GROUP BY c;
EXPLAIN format = 'brief' SELECT sum(c) FROM t1 GROUP BY a;
EXPLAIN format = 'brief' SELECT sum(c) FROM t1 GROUP BY b;

-- Virtual generated column for point get and batch point get
DROP TABLE IF EXISTS tu;
CREATE TABLE tu (a INT, b INT, c INT GENERATED ALWAYS AS (a + b) VIRTUAL, primary key (a), unique key uk(c));
INSERT INTO tu(a, b) VALUES(1, 2);
EXPLAIN SELECT * FROM tu WHERE c = 1;
EXPLAIN SELECT a, c FROM tu WHERE c = 1;
EXPLAIN SELECT * FROM tu WHERE c in(1, 2, 3);
EXPLAIN SELECT c, a FROM tu WHERE c in(1, 2, 3);
EXPLAIN format = 'brief' SELECT * FROM tu WHERE c = 1;
EXPLAIN format = 'brief' SELECT a, c FROM tu WHERE c = 1;
EXPLAIN format = 'brief' SELECT * FROM tu WHERE c in(1, 2, 3);
EXPLAIN format = 'brief' SELECT c, a FROM tu WHERE c in(1, 2, 3);
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const (
DefTableColumnCountLimit = 1017
// Def TableColumnCountLimit is maximum limitation of the number of columns in a table
DefMaxOfTableColumnCountLimit = 4096
// DefTxnScope is the default value for TxnScope
DefTxnScope = "global"
)

// Valid config maps
Expand Down
14 changes: 9 additions & 5 deletions config/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,15 @@ func flatten(flatMap map[string]interface{}, nested interface{}, prefix string)
}
}

// GetTxnScopeFromConfig extract txn_scope default value from config
func GetTxnScopeFromConfig() string {
const (
globalTxnScope = "global"
)

// GetTxnScopeFromConfig extracts @@txn_scope value from config
func GetTxnScopeFromConfig() (bool, string) {
v, ok := GetGlobalConfig().Labels["zone"]
if ok {
return v
if ok && len(v) > 0 {
return false, v
}
return DefTxnScope
return true, globalTxnScope
}
15 changes: 15 additions & 0 deletions config/config_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,18 @@ engines = ["tikv", "tiflash", "tidb"]
c.Assert(toJSONStr(flatMap["log.format"]), Equals, `"text"`)
c.Assert(toJSONStr(flatMap["isolation-read.engines"]), Equals, `["tikv","tiflash","tidb"]`)
}

func (s *testConfigSuite) TestTxnScopeValue(c *C) {
GetGlobalConfig().Labels["zone"] = "bj"
isGlobal, v := GetTxnScopeFromConfig()
c.Assert(isGlobal, IsFalse)
c.Assert(v, Equals, "bj")
GetGlobalConfig().Labels["zone"] = ""
isGlobal, v = GetTxnScopeFromConfig()
c.Assert(isGlobal, IsTrue)
c.Assert(v, Equals, "global")
GetGlobalConfig().Labels["zone"] = "global"
isGlobal, v = GetTxnScopeFromConfig()
c.Assert(isGlobal, IsFalse)
c.Assert(v, Equals, "global")
}
39 changes: 37 additions & 2 deletions ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store/helper"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
Expand Down Expand Up @@ -1562,7 +1561,7 @@ func (s *testColumnTypeChangeSuite) TestRowFormat(c *C) {
tbl := testGetTableByName(c, tk.Se, "test", "t")
encodedKey := tablecodec.EncodeRowKeyWithHandle(tbl.Meta().ID, kv.IntHandle(1))

h := helper.NewHelper(s.store.(tikv.Storage))
h := helper.NewHelper(s.store.(helper.Storage))
data, err := h.GetMvccByEncodedKey(encodedKey)
c.Assert(err, IsNil)
// The new format will start with CodecVer = 128 (0x80).
Expand Down Expand Up @@ -1684,3 +1683,39 @@ func (s *testColumnTypeChangeSuite) TestChangingColOriginDefaultValue(c *C) {
tk.MustQuery("select * from t order by a").Check(testkit.Rows("1 -1", "2 -2", "3 3", "3 3", "3 3"))
tk.MustExec("drop table if exists t")
}

// Close issue #22820
func (s *testColumnTypeChangeSuite) TestChangingAttributeOfColumnWithFK(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

prepare := func() {
tk.MustExec("drop table if exists users")
tk.MustExec("drop table if exists orders")
tk.MustExec("CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);")
tk.MustExec("CREATE TABLE orders (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, doc JSON, FOREIGN KEY fk_user_id (user_id) REFERENCES users(id));")
}

prepare()
// For column with FK, alter action can be performed for changing null/not null, default value, comment and so on, but column type.
tk.MustExec("alter table orders modify user_id int null;")
tbl := testGetTableByName(c, tk.Se, "test", "orders")
c.Assert(parser_mysql.HasNotNullFlag(tbl.Meta().Columns[1].Flag), Equals, false)

prepare()
tk.MustExec("alter table orders change user_id user_id2 int null")
tbl = testGetTableByName(c, tk.Se, "test", "orders")
c.Assert(tbl.Meta().Columns[1].Name.L, Equals, "user_id2")
c.Assert(parser_mysql.HasNotNullFlag(tbl.Meta().Columns[1].Flag), Equals, false)

prepare()
tk.MustExec("alter table orders modify user_id int default -1 comment \"haha\"")
tbl = testGetTableByName(c, tk.Se, "test", "orders")
c.Assert(tbl.Meta().Columns[1].Comment, Equals, "haha")
c.Assert(tbl.Meta().Columns[1].DefaultValue.(string), Equals, "-1")

prepare()
tk.MustGetErrCode("alter table orders modify user_id bigint", mysql.ErrFKIncompatibleColumns)

tk.MustExec("drop table if exists orders, users")
}
1 change: 1 addition & 0 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ func (s *testStateChangeSuite) TestAppendEnum(c *C) {
c.Assert(err, IsNil)

_, err = s.se.Execute(context.Background(), "insert into t values('a', 'A', '2018-09-19', 9)")
c.Assert(err.Error(), Equals, "[types:1265]Data truncated for column 'c2' at row 1")
failAlterTableSQL1 := "alter table t change c2 c2 enum('N') DEFAULT 'N'"
_, err = s.se.Execute(context.Background(), failAlterTableSQL1)
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported modify column: the number of enum column's elements is less than the original: 2, and tidb_enable_change_column_type is false")
Expand Down
Loading

0 comments on commit 9e256e7

Please sign in to comment.