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) #21476

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -2195,7 +2195,6 @@ func (s *testIntegrationSuite) 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 @@ -2539,12 +2538,9 @@ func (s *testIntegrationSuite) 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
33 changes: 32 additions & 1 deletion 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 All @@ -29,7 +30,7 @@ import (
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/parser_driver"
driver "github.com/pingcap/tidb/types/parser_driver"
)

// PreprocessOpt presents optional parameters to `Preprocess` method.
Expand Down Expand Up @@ -128,6 +129,8 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
// So skip check table name here, otherwise, recover table [table_name] syntax will return
// table not exists error. But recover table statement is use to recover the dropped table. So skip children here.
return in, true
case *ast.FuncCastExpr:
p.checkFuncCastExpr(node)
default:
p.flag &= ^parentIsJoin
}
Expand Down Expand Up @@ -776,3 +779,31 @@ func (p *preprocessor) resolveAlterTableStmt(node *ast.AlterTableStmt) {
}
}
}

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/infoschema"
Expand Down Expand Up @@ -206,6 +207,15 @@ func (s *testValidatorSuite) TestValidator(c *C) {
{"CREATE TABLE t1 (id INT NOT NULL, c1 VARCHAR(20) AS ('foo') VIRTUAL KEY NULL, PRIMARY KEY (id));", false, core.ErrUnsupportedOnGeneratedColumn},
{"CREATE TABLE t1 (id INT NOT NULL, c1 VARCHAR(20) AS ('foo') VIRTUAL KEY NOT NULL, PRIMARY KEY (id));", false, core.ErrUnsupportedOnGeneratedColumn},
{"create table t (a DOUBLE NULL, b_sto DOUBLE GENERATED ALWAYS AS (a + 2) STORED UNIQUE KEY NOT NULL PRIMARY KEY);", 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")},
}

store, dom, err := newStoreWithBootstrap()
Expand Down