Skip to content

Commit

Permalink
planner/core: add a range check against display width of BIT column t…
Browse files Browse the repository at this point in the history
…ype (#11942) (#13511)
  • Loading branch information
Tanner authored and sre-bot committed Nov 18, 2019
1 parent c5e8d81 commit fc1824c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020084629-8d44bfdf1030
github.com/pingcap/kvproto v0.0.0-20190826051950-fc8799546726
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596
github.com/pingcap/parser v0.0.0-20191023041603-32865d31ae3f
github.com/pingcap/parser v0.0.0-20191114042422-832cacfd0721
github.com/pingcap/pd v2.1.12+incompatible
github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible
github.com/pingcap/tipb v0.0.0-20180910045846-371b48b15d93
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ github.com/pingcap/kvproto v0.0.0-20190826051950-fc8799546726 h1:AzGIEmaYVYMtmki
github.com/pingcap/kvproto v0.0.0-20190826051950-fc8799546726/go.mod h1:0gwbe1F2iBIjuQ9AH0DbQhL+Dpr5GofU8fgYyXk+ykk=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw=
github.com/pingcap/parser v0.0.0-20191023041603-32865d31ae3f h1:kvIfudXU5KGun9X5Bz6LnIbhfun6UM+NzVvkl8aPKuY=
github.com/pingcap/parser v0.0.0-20191023041603-32865d31ae3f/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/parser v0.0.0-20191114042422-832cacfd0721 h1:1aZaH/7FQwGKwCYPP7pHkxZ4HfIdQ+ca263kzOrJIcw=
github.com/pingcap/parser v0.0.0-20191114042422-832cacfd0721/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/pd v2.1.12+incompatible h1:6N3LBxx2aSZqT+IWEG730EDNDttP7dXO8J6yvBh+HXw=
github.com/pingcap/pd v2.1.12+incompatible/go.mod h1:nD3+EoYes4+aNNODO99ES59V83MZSI+dFbhyr667a0E=
github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible h1:e9Gi/LP9181HT3gBfSOeSBA+5JfemuE4aEAhqNgoE4k=
Expand Down
21 changes: 21 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,24 @@ func (s *testIntegrationSuite) TestPpdWithSetVar(c *C) {
tk.MustQuery("select t01.c1,t01.c2,t01.c3 from (select t1.*,@c3:=@c3+1 as c3 from (select t.*,@c3:=0 from t order by t.c1)t1)t01 where t01.c3=1 and t01.c2='d'").Check(testkit.Rows())
tk.MustQuery("select t01.c1,t01.c2,t01.c3 from (select t1.*,@c3:=@c3+1 as c3 from (select t.*,@c3:=0 from t order by t.c1)t1)t01 where t01.c3=2 and t01.c2='d'").Check(testkit.Rows("2 d 2"))
}

func (s *testIntegrationSuite) BitColErrorMessage(c *C) {
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()

tk.MustExec("use test")
tk.MustExec("drop table if exists bit_col_t")
tk.MustExec("create table bit_col_t (a bit(64))")
tk.MustExec("drop table bit_col_t")
tk.MustExec("create table bit_col_t (a bit(1))")
tk.MustExec("drop table bit_col_t")
_, err = tk.Exec("create table bit_col_t (a bit(0))")
c.Assert(err, NotNil)
_, err = tk.Exec("create table bit_col_t (a bit(65))")
c.Assert(err, NotNil)
}
9 changes: 8 additions & 1 deletion planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,20 @@ func checkColumn(colDef *ast.ColumnDef) error {
if tp.Flen > mysql.MaxDecimalWidth {
return types.ErrTooBigPrecision.GenWithStackByArgs(tp.Flen, colDef.Name.Name.O, mysql.MaxDecimalWidth)
}
case mysql.TypeBit:
if tp.Flen <= 0 {
return types.ErrInvalidFieldSize.GenWithStackByArgs(colDef.Name.Name.O)
}
if tp.Flen > mysql.MaxBitDisplayWidth {
return types.ErrTooBigDisplayWidth.GenWithStackByArgs(colDef.Name.Name.O, mysql.MaxBitDisplayWidth)
}
default:
// TODO: Add more types.
}
return nil
}

// isDefaultValNowSymFunc checks whether defaul value is a NOW() builtin function.
// isDefaultValNowSymFunc checks whether default value is a NOW() builtin function.
func isDefaultValNowSymFunc(expr ast.ExprNode) bool {
if funcCall, ok := expr.(*ast.FuncCallExpr); ok {
// Default value NOW() is transformed to CURRENT_TIMESTAMP() in parser.
Expand Down
5 changes: 4 additions & 1 deletion types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
// ErrDivByZero is return when do division by 0.
ErrDivByZero = terror.ClassTypes.New(codeDivByZero, "Division by 0")
// ErrTooBigDisplayWidth is return when display width out of range for column.
ErrTooBigDisplayWidth = terror.ClassTypes.New(codeTooBigDisplayWidth, "Too Big Display width")
ErrTooBigDisplayWidth = terror.ClassTypes.New(codeTooBigDisplayWidth, mysql.MySQLErrName[mysql.ErrTooBigDisplaywidth])
// ErrTooBigFieldLength is return when column length too big for column.
ErrTooBigFieldLength = terror.ClassTypes.New(codeTooBigFieldLength, "Too Big Field length")
// ErrTooBigSet is returned when too many strings for column.
Expand All @@ -49,6 +49,8 @@ var (
ErrBadNumber = terror.ClassTypes.New(codeBadNumber, "Bad Number")
// ErrInvalidDefault is returned when meet a invalid default value.
ErrInvalidDefault = parser_types.ErrInvalidDefault
// ErrInvalidFieldSize is returned when the precision of a column is out of range.
ErrInvalidFieldSize = terror.ClassTypes.New(codeInvalidFieldSize, mysql.MySQLErrName[mysql.ErrInvalidFieldSize])
// ErrCastAsSignedOverflow is returned when positive out-of-range integer, and convert to it's negative complement.
ErrCastAsSignedOverflow = terror.ClassTypes.New(codeUnknown, msgCastAsSignedOverflow)
// ErrCastNegIntAsUnsigned is returned when a negative integer be casted to an unsigned int.
Expand Down Expand Up @@ -93,6 +95,7 @@ const (
codeTruncatedWrongValue = terror.ErrCode(mysql.ErrTruncatedWrongValue)
codeUnknown = terror.ErrCode(mysql.ErrUnknown)
codeInvalidDefault = terror.ErrCode(mysql.ErrInvalidDefault)
codeInvalidFieldSize = terror.ErrCode(mysql.ErrInvalidFieldSize)
codeMBiggerThanD = terror.ErrCode(mysql.ErrMBiggerThanD)
codeDataOutOfRange = terror.ErrCode(mysql.ErrWarnDataOutOfRange)
codeDuplicatedValueInType = terror.ErrCode(mysql.ErrDuplicatedValueInType)
Expand Down

0 comments on commit fc1824c

Please sign in to comment.