From 1a840a7fbc88f88a5a63b45501459c9831c5b295 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Wed, 12 Aug 2020 12:58:54 +0800 Subject: [PATCH] syncer: fix wrong conversion for mysql BIT (#876) --- syncer/dml.go | 2 +- syncer/dml_test.go | 21 ++++++++++----------- tests/all_mode/data/db1.increment.sql | 14 +++++++++++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/syncer/dml.go b/syncer/dml.go index 8e3d7d4ca4..f2cfcf49d9 100644 --- a/syncer/dml.go +++ b/syncer/dml.go @@ -363,7 +363,7 @@ func castUnsigned(data interface{}, unsigned bool, tp string) interface{} { } return uint32(v) case int64: - return strconv.FormatUint(uint64(v), 10) + return uint64(v) } return data diff --git a/syncer/dml_test.go b/syncer/dml_test.go index 4c52988f5d..4d56e0c836 100644 --- a/syncer/dml_test.go +++ b/syncer/dml_test.go @@ -15,7 +15,6 @@ package syncer import ( "math" - "strconv" . "github.com/pingcap/check" ) @@ -28,16 +27,16 @@ func (s *testSyncerSuite) TestCastUnsigned(c *C) { Type string expected interface{} }{ - {int8(-math.Exp2(7)), false, "tinyint(4)", int8(-math.Exp2(7))}, // TINYINT - {int8(-math.Exp2(7)), true, "tinyint(3) unsigned", uint8(math.Exp2(7))}, - {int16(-math.Exp2(15)), false, "smallint(6)", int16(-math.Exp2(15))}, //SMALLINT - {int16(-math.Exp2(15)), true, "smallint(5) unsigned", uint16(math.Exp2(15))}, - {int32(-math.Exp2(23)), false, "mediumint(9)", int32(-math.Exp2(23))}, //MEDIUMINT - {int32(-math.Exp2(23)), true, "mediumint(8) unsigned", uint32(math.Exp2(23))}, - {int32(-math.Exp2(31)), false, "int(11)", int32(-math.Exp2(31))}, // INT - {int32(-math.Exp2(31)), true, "int(10) unsigned", uint32(math.Exp2(31))}, - {int64(-math.Exp2(63)), false, "bigint(20)", int64(-math.Exp2(63))}, // BIGINT - {int64(-math.Exp2(63)), true, "bigint(20) unsigned", strconv.FormatUint(uint64(math.Exp2(63)), 10)}, // special case use string to represent uint64 + {int8(-math.Exp2(7)), false, mysql.TypeTiny, int8(-math.Exp2(7))}, // TINYINT + {int8(-math.Exp2(7)), true, mysql.TypeTiny, uint8(math.Exp2(7))}, + {int16(-math.Exp2(15)), false, mysql.TypeShort, int16(-math.Exp2(15))}, //SMALLINT + {int16(-math.Exp2(15)), true, mysql.TypeShort, uint16(math.Exp2(15))}, + {int32(-math.Exp2(23)), false, mysql.TypeInt24, int32(-math.Exp2(23))}, //MEDIUMINT + {int32(-math.Exp2(23)), true, mysql.TypeInt24, uint32(math.Exp2(23))}, + {int32(-math.Exp2(31)), false, mysql.TypeLong, int32(-math.Exp2(31))}, // INT + {int32(-math.Exp2(31)), true, mysql.TypeLong, uint32(math.Exp2(31))}, + {int64(-math.Exp2(63)), false, mysql.TypeLonglong, int64(-math.Exp2(63))}, // BIGINT + {int64(-math.Exp2(63)), true, mysql.TypeLonglong, uint64(math.Exp2(63))}, } for _, cs := range cases { obtained := castUnsigned(cs.data, cs.unsigned, cs.Type) diff --git a/tests/all_mode/data/db1.increment.sql b/tests/all_mode/data/db1.increment.sql index 940c822664..ce3818aea2 100644 --- a/tests/all_mode/data/db1.increment.sql +++ b/tests/all_mode/data/db1.increment.sql @@ -29,4 +29,16 @@ alter database all_mode CHARACTER SET = utf8mb4; -- test decimal type alter table t1 add column lat decimal(9,6) default '0.000000'; -insert into t1 (id, name, info, lat) values (8, 'gentest', '{"id":127}', '123.123') +insert into t1 (id, name, info, lat) values (8, 'gentest', '{"id":127}', '123.123'); + +-- test bit type +alter table t1 add column bin bit(1) default NULL; +insert into t1 (id, name, info, lat, bin) values (9, 'gentest', '{"id":128}', '123.123', b'0'); +insert into t1 (id, name, info, lat, bin) values (10, 'gentest', '{"id":129}', '123.123', b'1'); + +-- test bigint, min and max value for bigint/bigint unsigned +alter table t1 add column big1 bigint; +alter table t1 add column big2 bigint unsigned; +insert into t1 (id, name, info, lat, big1, big2) values (11, 'gentest', '{"id":130}', '123.123', -9223372036854775808, 0); +insert into t1 (id, name, info, lat, big1, big2) values (12, 'gentest', '{"id":131}', '123.123', 9223372036854775807, 18446744073709551615); +