Skip to content

Commit

Permalink
*: refactor parser initialize method and add tidb_enable_strict_doubl…
Browse files Browse the repository at this point in the history
…e_type_check variable (pingcap#20967)
  • Loading branch information
blacktear23 authored Nov 16, 2020
1 parent 7c88ca7 commit e136429
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 18 deletions.
13 changes: 13 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2577,3 +2577,16 @@ func (s *testIntegrationSuite3) TestEnumAndSetDefaultValue(c *C) {
c.Assert(tbl.Meta().Columns[0].DefaultValue, Equals, "a")
c.Assert(tbl.Meta().Columns[1].DefaultValue, Equals, "a")
}

func (s *testIntegrationSuite3) TestStrictDoubleTypeCheck(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_strict_double_type_check = 'ON'")
sql := "create table double_type_check(id int, c double(10));"
_, err := tk.Exec(sql)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[parser:1149]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use")
tk.MustExec("set @@tidb_enable_strict_double_type_check = 'OFF'")
defer tk.MustExec("set @@tidb_enable_strict_double_type_check = 'ON'")
tk.MustExec(sql)
}
2 changes: 1 addition & 1 deletion executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (e *PrepareExec) Next(ctx context.Context, req *chunk.Chunk) error {
stmts, err = sqlParser.ParseSQL(e.sqlText, charset, collation)
} else {
p := parser.New()
p.EnableWindowFunc(vars.EnableWindowFunction)
p.SetParserConfig(vars.BuildParserConfig())
var warns []error
stmts, warns, err = p.Parse(e.sqlText, charset, collation)
for _, warn := range warns {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989
github.com/pingcap/kvproto v0.0.0-20201023092649-e6d6090277c9
github.com/pingcap/log v0.0.0-20200828042413-fce0951f1463
github.com/pingcap/parser v0.0.0-20201112065012-c9380f220ff9
github.com/pingcap/parser v0.0.0-20201112105242-773b8b74f44e
github.com/pingcap/sysutil v0.0.0-20201021075216-f93ced2829e2
github.com/pingcap/tidb-lightning v4.0.9-0.20201106041742-a1ac97827a27+incompatible
github.com/pingcap/tidb-tools v4.0.5-0.20200820092506-34ea90c93237+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd/go.mod h1:4rbK1p9ILyIf
github.com/pingcap/log v0.0.0-20200511115504-543df19646ad/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20200828042413-fce0951f1463 h1:Jboj+s4jSCp5E1WDgmRUv5rIFKFHaaSWuSZ4wMwXIcc=
github.com/pingcap/log v0.0.0-20200828042413-fce0951f1463/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/parser v0.0.0-20201112065012-c9380f220ff9 h1:/Vd4G/b+sifGUe14+GsxyXlWvJBJwJlTPcyV9IcQYdU=
github.com/pingcap/parser v0.0.0-20201112065012-c9380f220ff9/go.mod h1:GbEr2PgY72/4XqPZzmzstlOU/+il/wrjeTNFs6ihsSE=
github.com/pingcap/parser v0.0.0-20201112105242-773b8b74f44e h1:TCSZlqvNP2Ipt0xUwZ5Z6ehmKo1dlk5KCU1SOLXUYL4=
github.com/pingcap/parser v0.0.0-20201112105242-773b8b74f44e/go.mod h1:GbEr2PgY72/4XqPZzmzstlOU/+il/wrjeTNFs6ihsSE=
github.com/pingcap/sysutil v0.0.0-20200206130906-2bfa6dc40bcd/go.mod h1:EB/852NMQ+aRKioCpToQ94Wl7fktV+FNnxf3CX/TTXI=
github.com/pingcap/sysutil v0.0.0-20200715082929-4c47bcac246a/go.mod h1:EB/852NMQ+aRKioCpToQ94Wl7fktV+FNnxf3CX/TTXI=
github.com/pingcap/sysutil v0.0.0-20201021075216-f93ced2829e2 h1:b2G/eqDeywtdJF3w9nIUdqMmXChsmpLvf4FzUxJ9Vmk=
Expand Down
2 changes: 1 addition & 1 deletion planner/cascades/transformation_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *testTransformationRuleSuite) SetUpSuite(c *C) {
var err error
s.testData, err = testutil.LoadTestSuiteData("testdata", "transformation_rules_suite")
c.Assert(err, IsNil)
s.Parser.EnableWindowFunc(true)
s.Parser.SetParserConfig(parser.ParserConfig{EnableWindowFunction: true, EnableStrictDoubleTypeCheck: true})
}

func (s *testTransformationRuleSuite) TearDownSuite(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3323,7 +3323,7 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model.

charset, collation := b.ctx.GetSessionVars().GetCharsetInfo()
viewParser := parser.New()
viewParser.EnableWindowFunc(b.ctx.GetSessionVars().EnableWindowFunction)
viewParser.SetParserConfig(b.ctx.GetSessionVars().BuildParserConfig())
selectNode, err := viewParser.ParseOneStmt(tableInfo.View.SelectStmt, charset, collation)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *testPlanSuite) SetUpSuite(c *C) {
s.ctx = MockContext()
s.ctx.GetSessionVars().EnableWindowFunction = true
s.Parser = parser.New()
s.Parser.EnableWindowFunc(true)
s.Parser.SetParserConfig(parser.ParserConfig{EnableWindowFunction: true, EnableStrictDoubleTypeCheck: true})

var err error
s.testData, err = testutil.LoadTestSuiteData("testdata", "plan_suite_unexported")
Expand Down
2 changes: 1 addition & 1 deletion planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type testPlanSuiteBase struct {
func (s *testPlanSuiteBase) SetUpSuite(c *C) {
s.is = infoschema.MockInfoSchema([]*model.TableInfo{core.MockSignedTable(), core.MockUnsignedTable()})
s.Parser = parser.New()
s.Parser.EnableWindowFunc(true)
s.Parser.SetParserConfig(parser.ParserConfig{EnableWindowFunction: true, EnableStrictDoubleTypeCheck: true})
}

type testPlanSerialSuite struct {
Expand Down
9 changes: 4 additions & 5 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,11 +830,10 @@ func checkColumn(colDef *ast.ColumnDef) error {
// For FLOAT, the SQL standard permits an optional specification of the precision.
// https://dev.mysql.com/doc/refman/8.0/en/floating-point-types.html
if tp.Decimal == -1 {
if tp.Tp == mysql.TypeDouble {
if tp.Flen != -1 {
return types.ErrSyntax.GenWithStackByArgs()
}
} else {
switch tp.Tp {
case mysql.TypeDouble:
// For Double type Flen and Decimal check is moved to parser component
default:
if tp.Flen > mysql.MaxDoublePrecisionLength {
return types.ErrWrongFieldSpec.GenWithStackByArgs(colDef.Name.Name.O)
}
Expand Down
1 change: 0 additions & 1 deletion planner/core/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (s *testValidatorSuite) TestValidator(c *C) {
{"CREATE TABLE t (a float(53))", true, nil},
{"CREATE TABLE t (a float(54))", false, types.ErrWrongFieldSpec},
{"CREATE TABLE t (a double)", true, nil},
{"CREATE TABLE t (a double(54))", false, types.ErrSyntax},

// FIXME: temporary 'not implemented yet' test for 'CREATE TABLE ... SELECT' (issue 4754)
{"CREATE TABLE t SELECT * FROM u", false, errors.New("'CREATE TABLE ... SELECT' is not implemented yet")},
Expand Down
2 changes: 1 addition & 1 deletion planner/core/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type testStatsSuite struct {

func (s *testStatsSuite) SetUpSuite(c *C) {
s.Parser = parser.New()
s.Parser.EnableWindowFunc(true)
s.Parser.SetParserConfig(parser.ParserConfig{EnableWindowFunction: true, EnableStrictDoubleTypeCheck: true})

var err error
s.testData, err = testutil.LoadTestSuiteData("testdata", "stats_suite")
Expand Down
13 changes: 13 additions & 0 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ const (
version51 = 51
// version52 change mysql.stats_histograms cm_sketch column from blob to blob(6291456)
version52 = 52
// version53 introduce Global variable tidb_enable_strict_double_type_check
version53 = 53
)

var (
Expand Down Expand Up @@ -482,6 +484,7 @@ var (
upgradeToVer50,
upgradeToVer51,
upgradeToVer52,
upgradeToVer53,
}
)

Expand Down Expand Up @@ -1190,6 +1193,16 @@ func upgradeToVer52(s Session, ver int64) {
doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms MODIFY cm_sketch BLOB(6291456)")
}

func upgradeToVer53(s Session, ver int64) {
if ver >= version53 {
return
}
// when upgrade from old tidb and no `tidb_enable_strict_double_type_check` in GLOBAL_VARIABLES, init it with 1`
sql := fmt.Sprintf("INSERT IGNORE INTO %s.%s (`VARIABLE_NAME`, `VARIABLE_VALUE`) VALUES ('%s', '%d')",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableStrictDoubleTypeCheck, 0)
mustExecute(s, sql)
}

// updateBootstrapVer updates bootstrap version variable in mysql.TiDB table.
func updateBootstrapVer(s Session) {
// Update bootstrap version.
Expand Down
5 changes: 3 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string)
}
defer trace.StartRegion(ctx, "ParseSQL").End()
s.parser.SetSQLMode(s.sessionVars.SQLMode)
s.parser.EnableWindowFunc(s.sessionVars.EnableWindowFunction)
s.parser.SetParserConfig(s.sessionVars.BuildParserConfig())
return s.parser.Parse(sql, charset, collation)
}

Expand Down Expand Up @@ -2067,7 +2067,7 @@ func CreateSessionWithDomain(store kv.Storage, dom *domain.Domain) (*session, er

const (
notBootstrapped = 0
currentBootstrapVersion = version52
currentBootstrapVersion = version53
)

func getStoreBootstrapVersion(store kv.Storage) int64 {
Expand Down Expand Up @@ -2184,6 +2184,7 @@ var builtinGlobalVariable = []string{
variable.TiDBRetryLimit,
variable.TiDBDisableTxnAutoRetry,
variable.TiDBEnableWindowFunction,
variable.TiDBEnableStrictDoubleTypeCheck,
variable.TiDBEnableTablePartition,
variable.TiDBEnableVectorizedExpression,
variable.TiDBEnableFastAnalyze,
Expand Down
2 changes: 1 addition & 1 deletion session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func Parse(ctx sessionctx.Context, src string) ([]ast.StmtNode, error) {
logutil.BgLogger().Debug("compiling", zap.String("source", src))
charset, collation := ctx.GetSessionVars().GetCharsetInfo()
p := parser.New()
p.EnableWindowFunc(ctx.GetSessionVars().EnableWindowFunction)
p.SetParserConfig(ctx.GetSessionVars().BuildParserConfig())
p.SetSQLMode(ctx.GetSessionVars().SQLMode)
stmts, warns, err := p.Parse(src, charset, collation)
for _, warn := range warns {
Expand Down
14 changes: 14 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/klauspost/cpuid"
"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/charset"
Expand Down Expand Up @@ -552,6 +553,9 @@ type SessionVars struct {
// EnableWindowFunction enables the window function.
EnableWindowFunction bool

// EnableStrictDoubleTypeCheck enables table field double type check.
EnableStrictDoubleTypeCheck bool

// EnableVectorizedExpression enables the vectorized expression evaluation.
EnableVectorizedExpression bool

Expand Down Expand Up @@ -741,6 +745,14 @@ func (s *SessionVars) UseDynamicPartitionPrune() bool {
return PartitionPruneMode(s.PartitionPruneMode.Load()) == DynamicOnly
}

// BuildParserConfig generate parser.ParserConfig for initial parser
func (s *SessionVars) BuildParserConfig() parser.ParserConfig {
return parser.ParserConfig{
EnableWindowFunction: s.EnableWindowFunction,
EnableStrictDoubleTypeCheck: s.EnableStrictDoubleTypeCheck,
}
}

// PartitionPruneMode presents the prune mode used.
type PartitionPruneMode string

Expand Down Expand Up @@ -1404,6 +1416,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.EnableRadixJoin = TiDBOptOn(val)
case TiDBEnableWindowFunction:
s.EnableWindowFunction = TiDBOptOn(val)
case TiDBEnableStrictDoubleTypeCheck:
s.EnableStrictDoubleTypeCheck = TiDBOptOn(val)
case TiDBEnableVectorizedExpression:
s.EnableVectorizedExpression = TiDBOptOn(val)
case TiDBOptJoinReorderThreshold:
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ var defaultSysVars = []*SysVar{
{Scope: ScopeGlobal, Name: TiDBRowFormatVersion, Value: strconv.Itoa(DefTiDBRowFormatV1), Type: TypeUnsigned, MinValue: 1, MaxValue: 2},
{Scope: ScopeSession, Name: TiDBOptimizerSelectivityLevel, Value: strconv.Itoa(DefTiDBOptimizerSelectivityLevel), Type: TypeUnsigned, MinValue: 1, MaxValue: math.MaxUint64},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableWindowFunction, Value: BoolToOnOff(DefEnableWindowFunction), Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableStrictDoubleTypeCheck, Value: BoolToOnOff(DefEnableStrictDoubleTypeCheck), Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableVectorizedExpression, Value: BoolToOnOff(DefEnableVectorizedExpression), Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableFastAnalyze, Value: BoolToOnOff(DefTiDBUseFastAnalyze), Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBSkipIsolationLevelCheck, Value: BoolToOnOff(DefTiDBSkipIsolationLevelCheck), Type: TypeBool},
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ const (
// tidb_enable_window_function is used to control whether to enable the window function.
TiDBEnableWindowFunction = "tidb_enable_window_function"

// tidb_enable_strict_double_type_check is used to control table field double type syntax check.
TiDBEnableStrictDoubleTypeCheck = "tidb_enable_strict_double_type_check"

// tidb_enable_vectorized_expression is used to control whether to enable the vectorized expression evaluation.
TiDBEnableVectorizedExpression = "tidb_enable_vectorized_expression"

Expand Down Expand Up @@ -539,6 +542,7 @@ const (
DefTiDBForcePriority = mysql.NoPriority
DefTiDBUseRadixJoin = false
DefEnableWindowFunction = true
DefEnableStrictDoubleTypeCheck = true
DefEnableVectorizedExpression = true
DefTiDBOptJoinReorderThreshold = 0
DefTiDBDDLSlowOprThreshold = 300
Expand Down

0 comments on commit e136429

Please sign in to comment.