Skip to content
Permalink
Browse files

Merge pull request #213 from pingcap/siddontang/bit-boundary-check

support boundary check for bit type
  • Loading branch information
qiuyesuifeng committed Sep 21, 2015
2 parents 2a67b61 + d5457cb commit 5c1320fcdf26802d3a1a05f7221df9f03c8f0bd5
Showing with 44 additions and 1 deletion.
  1. +11 −0 tidb_test.go
  2. +20 −1 util/types/convert.go
  3. +13 −0 util/types/convert_test.go
@@ -823,6 +823,17 @@ func (s *testSessionSuite) TestShow(c *C) {
match(c, rows[0], "c", "INT", "YES", "", nil, "")
}

func (s *testSessionSuite) TestBit(c *C) {
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)

mustExecSQL(c, se, "drop table if exists t")
mustExecSQL(c, se, "create table t (c1 bit(2))")
mustExecSQL(c, se, "insert into t values (0), (1), (2), (3)")
_, err := exec(c, se, "insert into t values (4)")
c.Assert(err, NotNil)
}

func newSession(c *C, store kv.Storage, dbName string) Session {
se, err := CreateSession(store)
c.Assert(err, IsNil)
@@ -370,7 +370,26 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { //
}
return convertToInt(val, target)
case mysql.TypeBit:
return convertToUint(val, target)
x, err := convertToUint(val, target)
if err != nil {
return x, errors.Trace(err)
}

// check bit boundary, if bit has n width, the boundary is
// in [0, (1 << n) - 1]
width := target.Flen
if width == 0 {
width = mysql.MinBitWidth
} else if width == mysql.UnspecifiedBitWidth {
width = mysql.MaxBitWidth
}

maxValue := uint64(1)<<uint64(width) - 1

if x > maxValue {
return maxValue, overflow(val, tp)
}
return x, nil
case mysql.TypeDecimal, mysql.TypeNewDecimal:
x, err := ToDecimal(val)
if err != nil {
@@ -169,6 +169,7 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) {

// For TypeBit
ft = NewFieldType(mysql.TypeBit)
ft.Flen = 8
v, err = Convert("100", ft)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(100))
@@ -181,6 +182,18 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) {
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(100))

ft.Flen = 1
v, err = Convert(1, ft)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(1))

_, err = Convert(2, ft)
c.Assert(err, NotNil)

ft.Flen = 0
_, err = Convert(2, ft)
c.Assert(err, NotNil)

// For TypeNewDecimal
ft = NewFieldType(mysql.TypeNewDecimal)
ft.Decimal = 5

0 comments on commit 5c1320f

Please sign in to comment.
You can’t perform that action at this time.