Skip to content

Commit

Permalink
Resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
joechenrh committed Dec 6, 2024
1 parent 487d083 commit 83a6129
Show file tree
Hide file tree
Showing 28 changed files with 52 additions and 9,082 deletions.
1 change: 1 addition & 0 deletions br/pkg/lightning/backend/kv/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func NewSession(options *encode.SessionOptions, logger log.Logger) *Session {
vars.StmtCtx.InInsertStmt = true
vars.StmtCtx.BatchCheck = true
vars.StmtCtx.BadNullAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.NoDefaultAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.TruncateAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode()
Expand Down
4 changes: 2 additions & 2 deletions br/pkg/lightning/errormanager/errormanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const (
create_time datetime(6) NOT NULL DEFAULT now(6),
table_name varchar(261) NOT NULL,
index_name varchar(128) NOT NULL,
key_data text NOT NULL COMMENT 'decoded from raw_key, human readable only, not for machine use',
row_data text NOT NULL COMMENT 'decoded from raw_row, human readable only, not for machine use',
key_data text COMMENT 'decoded from raw_key, human readable only, not for machine use',
row_data text COMMENT 'decoded from raw_row, human readable only, not for machine use',
raw_key mediumblob NOT NULL COMMENT 'the conflicted key',
raw_value mediumblob NOT NULL COMMENT 'the value of the conflicted key',
raw_handle mediumblob NOT NULL COMMENT 'the data handle derived from the conflicted key or value',
Expand Down
1 change: 1 addition & 0 deletions ddl/backfilling_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func initSessCtx(sessCtx sessionctx.Context, sqlMode mysql.SQLMode, tzLocation *
return errors.Trace(err)
}
sessCtx.GetSessionVars().StmtCtx.BadNullAsWarning = !sqlMode.HasStrictMode()
sessCtx.GetSessionVars().StmtCtx.NoDefaultAsWarning = !sqlMode.HasStrictMode()
sessCtx.GetSessionVars().StmtCtx.TruncateAsWarning = !sqlMode.HasStrictMode()
sessCtx.GetSessionVars().StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode()
sessCtx.GetSessionVars().StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode()
Expand Down
161 changes: 0 additions & 161 deletions ddl/reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,169 +99,8 @@ func newContext(store kv.Storage) sessionctx.Context {

const defaultWaitReorgTimeout = 10 * time.Second

<<<<<<< HEAD:ddl/reorg.go
// ReorgWaitTimeout is the timeout that wait ddl in write reorganization stage.
var ReorgWaitTimeout = 5 * time.Second
=======
ctx := newReorgExprCtx()
evalCtx := ctx.GetStaticEvalCtx().Apply(
exprstatic.WithSQLMode(reorgMeta.SQLMode),
exprstatic.WithLocation(loc),
exprstatic.WithTypeFlags(reorgTypeFlagsWithSQLMode(reorgMeta.SQLMode)),
exprstatic.WithErrLevelMap(reorgErrLevelsWithSQLMode(reorgMeta.SQLMode)),
exprstatic.WithWarnHandler(warnHandler),
)
return ctx.Apply(exprstatic.WithEvalCtx(evalCtx)), nil
}

// reorgTableMutateContext implements table.MutateContext for reorganization.
type reorgTableMutateContext struct {
exprCtx exprctx.ExprContext
encodingConfig tblctx.RowEncodingConfig
mutateBuffers *tblctx.MutateBuffers
shardID *variable.RowIDShardGenerator
reservedRowIDAlloc stmtctx.ReservedRowIDAlloc
}

// AlternativeAllocators implements table.MutateContext.AlternativeAllocators.
func (*reorgTableMutateContext) AlternativeAllocators(*model.TableInfo) (autoid.Allocators, bool) {
// No alternative allocators for all tables because temporary tables
// are not supported (temporary tables do not have any data in TiKV) in reorganization.
return autoid.Allocators{}, false
}

// GetExprCtx implements table.MutateContext.GetExprCtx.
func (ctx *reorgTableMutateContext) GetExprCtx() exprctx.ExprContext {
return ctx.exprCtx
}

// ConnectionID implements table.MutateContext.ConnectionID.
func (*reorgTableMutateContext) ConnectionID() uint64 {
return 0
}

// InRestrictedSQL implements table.MutateContext.InRestrictedSQL.
func (*reorgTableMutateContext) InRestrictedSQL() bool {
return false
}

// TxnAssertionLevel implements table.MutateContext.TxnAssertionLevel.
func (*reorgTableMutateContext) TxnAssertionLevel() variable.AssertionLevel {
// Because only `index.Create` and `index.Delete` are invoked in reorganization which does not use this method,
// we can just return `AssertionLevelOff`.
return variable.AssertionLevelOff
}

// EnableMutationChecker implements table.MutateContext.EnableMutationChecker.
func (*reorgTableMutateContext) EnableMutationChecker() bool {
// Because only `index.Create` and `index.Delete` are invoked in reorganization which does not use this method,
// we can just return false.
return false
}

// GetRowEncodingConfig implements table.MutateContext.GetRowEncodingConfig.
func (ctx *reorgTableMutateContext) GetRowEncodingConfig() tblctx.RowEncodingConfig {
return ctx.encodingConfig
}

// GetMutateBuffers implements table.MutateContext.GetMutateBuffers.
func (ctx *reorgTableMutateContext) GetMutateBuffers() *tblctx.MutateBuffers {
return ctx.mutateBuffers
}

// GetRowIDShardGenerator implements table.MutateContext.GetRowIDShardGenerator.
func (ctx *reorgTableMutateContext) GetRowIDShardGenerator() *variable.RowIDShardGenerator {
return ctx.shardID
}

// GetReservedRowIDAlloc implements table.MutateContext.GetReservedRowIDAlloc.
func (ctx *reorgTableMutateContext) GetReservedRowIDAlloc() (*stmtctx.ReservedRowIDAlloc, bool) {
return &ctx.reservedRowIDAlloc, true
}

// GetStatisticsSupport implements table.MutateContext.GetStatisticsSupport.
func (*reorgTableMutateContext) GetStatisticsSupport() (tblctx.StatisticsSupport, bool) {
// We can just return `(nil, false)` because:
// - Only `index.Create` and `index.Delete` are invoked in reorganization which does not use this method.
// - DDL reorg do need to collect statistics in this way.
return nil, false
}

// GetCachedTableSupport implements table.MutateContext.GetCachedTableSupport.
func (*reorgTableMutateContext) GetCachedTableSupport() (tblctx.CachedTableSupport, bool) {
// We can just return `(nil, false)` because:
// - Only `index.Create` and `index.Delete` are invoked in reorganization which does not use this method.
// - It is not allowed to execute DDL on a cached table.
return nil, false
}

// GetTemporaryTableSupport implements table.MutateContext.GetTemporaryTableSupport.
func (*reorgTableMutateContext) GetTemporaryTableSupport() (tblctx.TemporaryTableSupport, bool) {
// We can just return `(nil, false)` because:
// - Only `index.Create` and `index.Delete` are invoked in reorganization which does not use this method.
// - Temporary tables do not have any data in TiKV.
return nil, false
}

// GetExchangePartitionDMLSupport implements table.MutateContext.GetExchangePartitionDMLSupport.
func (*reorgTableMutateContext) GetExchangePartitionDMLSupport() (tblctx.ExchangePartitionDMLSupport, bool) {
// We can just return `(nil, false)` because:
// - Only `index.Create` and `index.Delete` are invoked in reorganization which does not use this method.
return nil, false
}

// newReorgTableMutateContext creates a new table.MutateContext for reorganization.
func newReorgTableMutateContext(exprCtx exprctx.ExprContext) table.MutateContext {
rowEncoder := &rowcodec.Encoder{
Enable: variable.GetDDLReorgRowFormat() != variable.DefTiDBRowFormatV1,
}

encodingConfig := tblctx.RowEncodingConfig{
IsRowLevelChecksumEnabled: rowEncoder.Enable,
RowEncoder: rowEncoder,
}

return &reorgTableMutateContext{
exprCtx: exprCtx,
encodingConfig: encodingConfig,
mutateBuffers: tblctx.NewMutateBuffers(&variable.WriteStmtBufs{}),
// Though currently, `RowIDShardGenerator` is not required in DDL reorg,
// we still provide a valid one to keep the context complete and to avoid panic if it is used in the future.
shardID: variable.NewRowIDShardGenerator(
rand.New(rand.NewSource(time.Now().UnixNano())), // #nosec G404
variable.DefTiDBShardAllocateStep,
),
}
}

func reorgTypeFlagsWithSQLMode(mode mysql.SQLMode) types.Flags {
return types.StrictFlags.
WithTruncateAsWarning(!mode.HasStrictMode()).
WithIgnoreInvalidDateErr(mode.HasAllowInvalidDatesMode()).
WithIgnoreZeroInDate(!mode.HasStrictMode() || mode.HasAllowInvalidDatesMode()).
WithCastTimeToYearThroughConcat(true)
}

func reorgErrLevelsWithSQLMode(mode mysql.SQLMode) errctx.LevelMap {
return errctx.LevelMap{
errctx.ErrGroupTruncate: errctx.ResolveErrLevel(false, !mode.HasStrictMode()),
errctx.ErrGroupBadNull: errctx.ResolveErrLevel(false, !mode.HasStrictMode()),
errctx.ErrGroupNoDefault: errctx.ResolveErrLevel(false, !mode.HasStrictMode()),
errctx.ErrGroupDividedByZero: errctx.ResolveErrLevel(
!mode.HasErrorForDivisionByZeroMode(),
!mode.HasStrictMode(),
),
}
}

func reorgTimeZoneWithTzLoc(tzLoc *model.TimeZoneLocation) (*time.Location, error) {
if tzLoc == nil {
// It is set to SystemLocation to be compatible with nil LocationInfo.
return timeutil.SystemLocation(), nil
}
return tzLoc.GetLocation()
}
>>>>>>> 91beef4bb14 (*: disable insert null to not-null column for single-row insertion in non-strict mode (#55477)):pkg/ddl/reorg.go

func (rc *reorgCtx) notifyJobState(state model.JobState) {
atomic.StoreInt32((*int32)(&rc.jobState), int32(state))
Expand Down
8 changes: 7 additions & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,11 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
// should make TruncateAsWarning and DividedByZeroAsWarning,
// but should not make DupKeyAsWarning.
sc.DupKeyAsWarning = stmt.IgnoreErr
sc.BadNullAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
// For single-row INSERT statements, ignore non-strict mode
// See https://dev.mysql.com/doc/refman/5.7/en/constraint-invalid-data.html
isSingleInsert := len(stmt.Lists) == 1 || len(stmt.Setlist) > 0
sc.NoDefaultAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.BadNullAsWarning = (!vars.StrictSQLMode && !isSingleInsert) || stmt.IgnoreErr
// see https://dev.mysql.com/doc/refman/8.0/en/out-of-range-and-overflow.html
sc.OverflowAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.IgnoreNoPartition = stmt.IgnoreErr
Expand Down Expand Up @@ -2305,6 +2309,7 @@ func ResetUpdateStmtCtx(sc *stmtctx.StatementContext, stmt *ast.UpdateStmt, vars
sc.InUpdateStmt = true
sc.DupKeyAsWarning = stmt.IgnoreErr
sc.BadNullAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.NoDefaultAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.TruncateAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.DividedByZeroAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode()
Expand All @@ -2318,6 +2323,7 @@ func ResetDeleteStmtCtx(sc *stmtctx.StatementContext, stmt *ast.DeleteStmt, vars
sc.InDeleteStmt = true
sc.DupKeyAsWarning = stmt.IgnoreErr
sc.BadNullAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.NoDefaultAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.TruncateAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.DividedByZeroAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode()
Expand Down
30 changes: 30 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/execdetails"
Expand Down Expand Up @@ -1680,3 +1681,32 @@ func TestMySQLInsertID(t *testing.T) {
tk.MustExec("insert into tb(a, b) values(1,2) on duplicate key update b = 2;")
require.Equal(t, tk.Session().LastInsertID(), uint64(0))
}

func TestInsertNullInNonStrictMode(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (id int primary key, col1 varchar(10) not null default '')")
tk.MustExec("create table t2 (id int primary key, col1 varchar(10))")
tk.MustExec("insert into t2 values (1, null)")
tk.MustExec("insert ignore into t1 values(5, null)")

tk.MustExec("set session sql_mode = ''")

err := tk.ExecToErr("insert into t1 values(1, null)")
require.EqualError(t, err, table.ErrColumnCantNull.GenWithStackByArgs("col1").Error())

err = tk.ExecToErr("insert into t1 set id = 1, col1 = null")
require.EqualError(t, err, table.ErrColumnCantNull.GenWithStackByArgs("col1").Error())

err = tk.ExecToErr("insert t1 VALUES (5, 5) ON DUPLICATE KEY UPDATE col1 = null")
require.EqualError(t, err, table.ErrColumnCantNull.GenWithStackByArgs("col1").Error())

tk.MustExec("insert t1 VALUES (5, 5), (6, null) ON DUPLICATE KEY UPDATE col1 = null")

tk.MustExec("insert into t1 select * from t2")
tk.MustExec("insert into t1 values(2, null), (3, 3), (4, 4)")
tk.MustExec("update t1 set col1 = null where id = 3")
tk.MustExec("insert ignore t1 VALUES (4, 4) ON DUPLICATE KEY UPDATE col1 = null")
tk.MustQuery("select * from t1").Check(testkit.RowsWithSep("|", "1|", "2|", "3|", "4|", "5|", "6|"))
}
1 change: 1 addition & 0 deletions executor/load_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func setNonRestrictiveFlags(stmtCtx *stmtctx.StatementContext) {
stmtCtx.DupKeyAsWarning = true
stmtCtx.TruncateAsWarning = true
stmtCtx.BadNullAsWarning = true
stmtCtx.NoDefaultAsWarning = true
}

// NewLoadDataWorker creates a new LoadDataWorker that is ready to work.
Expand Down
1 change: 1 addition & 0 deletions executor/writetest/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,7 @@ func TestIssue18681(t *testing.T) {
selectSQL := "select bin(a), bin(b), bin(c), bin(d) from load_data_test;"
ctx.GetSessionVars().StmtCtx.DupKeyAsWarning = true
ctx.GetSessionVars().StmtCtx.BadNullAsWarning = true
ctx.GetSessionVars().StmtCtx.NoDefaultAsWarning = true

sc := ctx.GetSessionVars().StmtCtx
originIgnoreTruncate := sc.IgnoreTruncate.Load()
Expand Down
15 changes: 2 additions & 13 deletions expression/builtin_miscellaneous_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,8 @@ func TestSleepVectorized(t *testing.T) {
warnCnt := counter{}

// non-strict model
<<<<<<< HEAD:expression/builtin_miscellaneous_vec_test.go
sessVars.StmtCtx.BadNullAsWarning = true
=======
var levels errctx.LevelMap
levels[errctx.ErrGroupBadNull] = errctx.LevelWarn
levels[errctx.ErrGroupNoDefault] = errctx.LevelWarn
sessVars.StmtCtx.SetErrLevels(levels)
>>>>>>> 91beef4bb14 (*: disable insert null to not-null column for single-row insertion in non-strict mode (#55477)):pkg/expression/builtin_miscellaneous_vec_test.go
sessVars.StmtCtx.NoDefaultAsWarning = true
input.AppendFloat64(0, 1)
err = f.vecEvalInt(input, result)
require.NoError(t, err)
Expand Down Expand Up @@ -191,13 +185,8 @@ func TestSleepVectorized(t *testing.T) {
require.Equal(t, uint16(warnCnt.add(2)), sessVars.StmtCtx.WarningCount())

// for error case under the strict model
<<<<<<< HEAD:expression/builtin_miscellaneous_vec_test.go
sessVars.StmtCtx.BadNullAsWarning = false
=======
levels[errctx.ErrGroupBadNull] = errctx.LevelError
levels[errctx.ErrGroupNoDefault] = errctx.LevelError
sessVars.StmtCtx.SetErrLevels(levels)
>>>>>>> 91beef4bb14 (*: disable insert null to not-null column for single-row insertion in non-strict mode (#55477)):pkg/expression/builtin_miscellaneous_vec_test.go
sessVars.StmtCtx.NoDefaultAsWarning = false
input.Reset()
input.AppendNull(0)
err = f.vecEvalInt(input, result)
Expand Down
15 changes: 2 additions & 13 deletions expression/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,8 @@ func TestSleep(t *testing.T) {

fc := funcs[ast.Sleep]
// non-strict model
<<<<<<< HEAD:expression/evaluator_test.go
sessVars.StmtCtx.BadNullAsWarning = true
=======
var levels errctx.LevelMap
levels[errctx.ErrGroupBadNull] = errctx.LevelWarn
levels[errctx.ErrGroupNoDefault] = errctx.LevelWarn
sessVars.StmtCtx.SetErrLevels(levels)
>>>>>>> 91beef4bb14 (*: disable insert null to not-null column for single-row insertion in non-strict mode (#55477)):pkg/expression/evaluator_test.go
sessVars.StmtCtx.NoDefaultAsWarning = true
d := make([]types.Datum, 1)
f, err := fc.getFunction(ctx, datumsToConstants(d))
require.NoError(t, err)
Expand All @@ -128,13 +122,8 @@ func TestSleep(t *testing.T) {
require.Equal(t, int64(0), ret)

// for error case under the strict model
<<<<<<< HEAD:expression/evaluator_test.go
sessVars.StmtCtx.BadNullAsWarning = false
=======
levels[errctx.ErrGroupBadNull] = errctx.LevelError
levels[errctx.ErrGroupNoDefault] = errctx.LevelError
sessVars.StmtCtx.SetErrLevels(levels)
>>>>>>> 91beef4bb14 (*: disable insert null to not-null column for single-row insertion in non-strict mode (#55477)):pkg/expression/evaluator_test.go
sessVars.StmtCtx.NoDefaultAsWarning = false
d[0].SetNull()
_, err = fc.getFunction(ctx, datumsToConstants(d))
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 83a6129

Please sign in to comment.