Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: check for decimal format in cast expr (#20836) #21477

Merged
merged 6 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,6 @@ func (s *testIntegrationSuite2) TestBuiltin(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
ctx := context.Background()

// for is true && is false
tk.MustExec("drop table if exists t")
Expand Down Expand Up @@ -2736,12 +2735,9 @@ func (s *testIntegrationSuite2) TestBuiltin(c *C) {
c.Assert(err, NotNil)

// test case decimal precision less than the scale.
rs, err := tk.Exec("select cast(12.1 as decimal(3, 4));")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(ctx, tk.Se, rs)
_, err = tk.Exec("select cast(12.1 as decimal(3, 4));")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[types:1427]For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column '').")
c.Assert(rs.Close(), IsNil)
c.Assert(err.Error(), Equals, "[types:1427]For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column '12.1').")

// test unhex and hex
result = tk.MustQuery("select unhex('4D7953514C')")
Expand Down
31 changes: 31 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/format"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/ddl"
Expand Down Expand Up @@ -181,6 +182,8 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
case *ast.DropSequenceStmt:
p.flag |= inCreateOrDropTable
p.checkDropSequenceGrammar(node)
case *ast.FuncCastExpr:
p.checkFuncCastExpr(node)
case *ast.FuncCallExpr:
if node.FnName.L == ast.NextVal || node.FnName.L == ast.LastVal || node.FnName.L == ast.SetVal {
p.flag |= inSequenceFunction
Expand Down Expand Up @@ -1031,3 +1034,31 @@ func (p *preprocessor) resolveCreateSequenceStmt(stmt *ast.CreateSequenceStmt) {
return
}
}

func (p *preprocessor) checkFuncCastExpr(node *ast.FuncCastExpr) {
if node.Tp.EvalType() == types.ETDecimal {
if node.Tp.Flen >= node.Tp.Decimal && node.Tp.Flen <= mysql.MaxDecimalWidth && node.Tp.Decimal <= mysql.MaxDecimalScale {
// valid
return
}

var buf strings.Builder
restoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &buf)
if err := node.Expr.Restore(restoreCtx); err != nil {
p.err = err
return
}
if node.Tp.Flen < node.Tp.Decimal {
p.err = types.ErrMBiggerThanD.GenWithStackByArgs(buf.String())
return
}
if node.Tp.Flen > mysql.MaxDecimalWidth {
p.err = types.ErrTooBigPrecision.GenWithStackByArgs(node.Tp.Flen, buf.String(), mysql.MaxDecimalWidth)
return
}
if node.Tp.Decimal > mysql.MaxDecimalScale {
p.err = types.ErrTooBigScale.GenWithStackByArgs(node.Tp.Decimal, buf.String(), mysql.MaxDecimalScale)
return
}
}
}
10 changes: 10 additions & 0 deletions planner/core/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
Expand Down Expand Up @@ -255,6 +256,15 @@ func (s *testValidatorSuite) TestValidator(c *C) {
{"CREATE TABLE origin (a int primary key auto_increment, b int);", false, nil},
{"CREATE TABLE origin (a int unique auto_increment, b int);", false, nil},
{"CREATE TABLE origin (a int key auto_increment, b int);", false, nil},

// issue 20295
// issue 11193
{"select cast(1.23 as decimal(65,65))", true, types.ErrTooBigScale.GenWithStackByArgs(65, "1.23", mysql.MaxDecimalScale)},
{"select CONVERT( 2, DECIMAL(62,60) )", true, types.ErrTooBigScale.GenWithStackByArgs(60, "2", mysql.MaxDecimalScale)},
{"select CONVERT( 2, DECIMAL(66,29) )", true, types.ErrTooBigPrecision.GenWithStackByArgs(66, "2", mysql.MaxDecimalWidth)},
{"select CONVERT( 2, DECIMAL(28,29) )", true, types.ErrMBiggerThanD.GenWithStackByArgs("2")},
{"select CONVERT( 2, DECIMAL(30,65) )", true, types.ErrMBiggerThanD.GenWithStackByArgs("2")},
{"select CONVERT( 2, DECIMAL(66,99) )", true, types.ErrMBiggerThanD.GenWithStackByArgs("2")},
}

_, err := s.se.Execute(context.Background(), "use test")
Expand Down