Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
YufeiCh authored Oct 28, 2021
2 parents e33b1d0 + 18b47bc commit 0e4f2e7
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 80 deletions.
30 changes: 28 additions & 2 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,16 +933,42 @@ func (s *testSuite5) TestValidateSetVar(c *C) {
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("167772161"))

tk.MustExec("set @@tmp_table_size=9223372036854775807")
tk.MustExec("set @@tmp_table_size=18446744073709551615")
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("9223372036854775807"))
result.Check(testkit.Rows("18446744073709551615"))

_, err = tk.Exec("set @@tmp_table_size=18446744073709551616")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

_, err = tk.Exec("set @@tmp_table_size='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@tidb_tmp_table_max_size=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_tmp_table_max_size value: '-1'"))
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("1048576"))

tk.MustExec("set @@tidb_tmp_table_max_size=1048575")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_tmp_table_max_size value: '1048575'"))
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("1048576"))

tk.MustExec("set @@tidb_tmp_table_max_size=167772161")
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("167772161"))

tk.MustExec("set @@tidb_tmp_table_max_size=137438953472")
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("137438953472"))

tk.MustExec("set @@tidb_tmp_table_max_size=137438953473")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_tmp_table_max_size value: '137438953473'"))
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("137438953472"))

_, err = tk.Exec("set @@tidb_tmp_table_max_size='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@global.connect_timeout=1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect connect_timeout value: '1'"))
result = tk.MustQuery("select @@global.connect_timeout;")
Expand Down
51 changes: 28 additions & 23 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,11 @@ func (s *testSessionSuite3) TestSetVarHint(c *C) {
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 0)
tk.MustQuery("SELECT @@max_heap_table_size;").Check(testkit.Rows("16777216"))

tk.Se.GetSessionVars().SetSystemVar("tmp_table_size", "16777216")
tk.MustQuery("SELECT /*+ SET_VAR(tmp_table_size=16384) */ @@tmp_table_size;").Check(testkit.Rows("16384"))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 0)
tk.MustQuery("SELECT @@tmp_table_size;").Check(testkit.Rows("16777216"))

tk.Se.GetSessionVars().SetSystemVar("div_precision_increment", "4")
tk.MustQuery("SELECT /*+ SET_VAR(div_precision_increment=0) */ @@div_precision_increment;").Check(testkit.Rows("0"))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 0)
Expand Down Expand Up @@ -4739,47 +4744,47 @@ func (s *testSessionSuite) TestInTxnPSProtoPointGet(c *C) {
}

func (s *testSessionSuite) TestTMPTableSize(c *C) {
// Test the @@tmp_table_size system variable.
// Test the @@tidb_tmp_table_max_size system variable.
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create global temporary table t (c1 int, c2 varchar(512)) on commit delete rows")
tk.MustExec("create temporary table tl (c1 int, c2 varchar(512))")
tk.MustExec("create global temporary table t (c1 int, c2 mediumtext) on commit delete rows")
tk.MustExec("create temporary table tl (c1 int, c2 mediumtext)")

tk.MustQuery("select @@global.tmp_table_size").Check(testkit.Rows(strconv.Itoa(variable.DefTMPTableSize)))
c.Assert(tk.Se.GetSessionVars().TMPTableSize, Equals, int64(variable.DefTMPTableSize))
tk.MustQuery("select @@global.tidb_tmp_table_max_size").Check(testkit.Rows(strconv.Itoa(variable.DefTiDBTmpTableMaxSize)))
c.Assert(tk.Se.GetSessionVars().TMPTableSize, Equals, int64(variable.DefTiDBTmpTableMaxSize))

// Min value 1024, so the result is change to 1024, with a warning.
tk.MustExec("set @@global.tmp_table_size = 123")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tmp_table_size value: '123'"))
// Min value 1M, so the result is change to 1M, with a warning.
tk.MustExec("set @@global.tidb_tmp_table_max_size = 123")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_tmp_table_max_size value: '123'"))

// Change the session scope value.
tk.MustExec("set @@session.tmp_table_size = 2097152")
// Change the session scope value to 2M.
tk.MustExec("set @@session.tidb_tmp_table_max_size = 2097152")
c.Assert(tk.Se.GetSessionVars().TMPTableSize, Equals, int64(2097152))

// Check in another sessin, change session scope value does not affect the global scope.
// Check in another session, change session scope value does not affect the global scope.
tk1 := testkit.NewTestKit(c, s.store)
tk1.MustQuery("select @@global.tmp_table_size").Check(testkit.Rows("1024"))
tk1.MustQuery("select @@global.tidb_tmp_table_max_size").Check(testkit.Rows(strconv.Itoa(1 << 20)))

// The value is now 1024, check the error when table size exceed it.
tk.MustExec("set @@session.tmp_table_size = 1024")
// The value is now 1M, check the error when table size exceed it.
tk.MustExec(fmt.Sprintf("set @@session.tidb_tmp_table_max_size = %d", 1<<20))
tk.MustExec("begin")
tk.MustExec("insert into t values (1, repeat('x', 512))")
tk.MustExec("insert into t values (1, repeat('x', 512))")
tk.MustGetErrCode("insert into t values (1, repeat('x', 512))", errno.ErrRecordFileFull)
tk.MustExec("insert into t values (1, repeat('x', 512*1024))")
tk.MustExec("insert into t values (1, repeat('x', 512*1024))")
tk.MustGetErrCode("insert into t values (1, repeat('x', 512*1024))", errno.ErrRecordFileFull)
tk.MustExec("rollback")

// Check local temporary table
tk.MustExec("begin")
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512))", errno.ErrRecordFileFull)
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512*1024))", errno.ErrRecordFileFull)
tk.MustExec("rollback")

// Check local temporary table with some data in session
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustExec("begin")
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512))", errno.ErrRecordFileFull)
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512*1024))", errno.ErrRecordFileFull)
tk.MustExec("rollback")
}

Expand Down
83 changes: 40 additions & 43 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,114 +333,107 @@ type TableEntry struct {
// AddAffectedRows adds affected rows.
func (sc *StatementContext) AddAffectedRows(rows uint64) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.affectedRows += rows
sc.mu.Unlock()
}

// AffectedRows gets affected rows.
func (sc *StatementContext) AffectedRows() uint64 {
sc.mu.Lock()
rows := sc.mu.affectedRows
sc.mu.Unlock()
return rows
defer sc.mu.Unlock()
return sc.mu.affectedRows
}

// FoundRows gets found rows.
func (sc *StatementContext) FoundRows() uint64 {
sc.mu.Lock()
rows := sc.mu.foundRows
sc.mu.Unlock()
return rows
defer sc.mu.Unlock()
return sc.mu.foundRows
}

// AddFoundRows adds found rows.
func (sc *StatementContext) AddFoundRows(rows uint64) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.foundRows += rows
sc.mu.Unlock()
}

// RecordRows is used to generate info message
func (sc *StatementContext) RecordRows() uint64 {
sc.mu.Lock()
rows := sc.mu.records
sc.mu.Unlock()
return rows
defer sc.mu.Unlock()
return sc.mu.records
}

// AddRecordRows adds record rows.
func (sc *StatementContext) AddRecordRows(rows uint64) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.records += rows
sc.mu.Unlock()
}

// UpdatedRows is used to generate info message
func (sc *StatementContext) UpdatedRows() uint64 {
sc.mu.Lock()
rows := sc.mu.updated
sc.mu.Unlock()
return rows
defer sc.mu.Unlock()
return sc.mu.updated
}

// AddUpdatedRows adds updated rows.
func (sc *StatementContext) AddUpdatedRows(rows uint64) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.updated += rows
sc.mu.Unlock()
}

// CopiedRows is used to generate info message
func (sc *StatementContext) CopiedRows() uint64 {
sc.mu.Lock()
rows := sc.mu.copied
sc.mu.Unlock()
return rows
defer sc.mu.Unlock()
return sc.mu.copied
}

// AddCopiedRows adds copied rows.
func (sc *StatementContext) AddCopiedRows(rows uint64) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.copied += rows
sc.mu.Unlock()
}

// TouchedRows is used to generate info message
func (sc *StatementContext) TouchedRows() uint64 {
sc.mu.Lock()
rows := sc.mu.touched
sc.mu.Unlock()
return rows
defer sc.mu.Unlock()
return sc.mu.touched
}

// AddTouchedRows adds touched rows.
func (sc *StatementContext) AddTouchedRows(rows uint64) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.touched += rows
sc.mu.Unlock()
}

// GetMessage returns the extra message of the last executed command, if there is no message, it returns empty string
func (sc *StatementContext) GetMessage() string {
sc.mu.Lock()
msg := sc.mu.message
sc.mu.Unlock()
return msg
defer sc.mu.Unlock()
return sc.mu.message
}

// SetMessage sets the info message generated by some commands
func (sc *StatementContext) SetMessage(msg string) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.message = msg
sc.mu.Unlock()
}

// GetWarnings gets warnings.
func (sc *StatementContext) GetWarnings() []SQLWarn {
sc.mu.Lock()
defer sc.mu.Unlock()
warns := make([]SQLWarn, len(sc.mu.warnings))
copy(warns, sc.mu.warnings)
sc.mu.Unlock()
return warns
}

Expand All @@ -464,67 +457,66 @@ func (sc *StatementContext) WarningCount() uint16 {
return 0
}
sc.mu.Lock()
wc := uint16(len(sc.mu.warnings))
sc.mu.Unlock()
return wc
defer sc.mu.Unlock()
return uint16(len(sc.mu.warnings))
}

// NumErrorWarnings gets warning and error count.
func (sc *StatementContext) NumErrorWarnings() (ec uint16, wc int) {
sc.mu.Lock()
defer sc.mu.Unlock()
ec = sc.mu.errorCount
wc = len(sc.mu.warnings)
sc.mu.Unlock()
return
}

// SetWarnings sets warnings.
func (sc *StatementContext) SetWarnings(warns []SQLWarn) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.warnings = warns
for _, w := range warns {
if w.Level == WarnLevelError {
sc.mu.errorCount++
}
}
sc.mu.Unlock()
}

// AppendWarning appends a warning with level 'Warning'.
func (sc *StatementContext) AppendWarning(warn error) {
sc.mu.Lock()
defer sc.mu.Unlock()
if len(sc.mu.warnings) < math.MaxUint16 {
sc.mu.warnings = append(sc.mu.warnings, SQLWarn{WarnLevelWarning, warn})
}
sc.mu.Unlock()
}

// AppendWarnings appends some warnings.
func (sc *StatementContext) AppendWarnings(warns []SQLWarn) {
sc.mu.Lock()
defer sc.mu.Unlock()
if len(sc.mu.warnings) < math.MaxUint16 {
sc.mu.warnings = append(sc.mu.warnings, warns...)
}
sc.mu.Unlock()
}

// AppendNote appends a warning with level 'Note'.
func (sc *StatementContext) AppendNote(warn error) {
sc.mu.Lock()
defer sc.mu.Unlock()
if len(sc.mu.warnings) < math.MaxUint16 {
sc.mu.warnings = append(sc.mu.warnings, SQLWarn{WarnLevelNote, warn})
}
sc.mu.Unlock()
}

// AppendError appends a warning with level 'Error'.
func (sc *StatementContext) AppendError(warn error) {
sc.mu.Lock()
defer sc.mu.Unlock()
if len(sc.mu.warnings) < math.MaxUint16 {
sc.mu.warnings = append(sc.mu.warnings, SQLWarn{WarnLevelError, warn})
sc.mu.errorCount++
}
sc.mu.Unlock()
}

// HandleTruncate ignores or returns the error based on the StatementContext state.
Expand Down Expand Up @@ -557,9 +549,10 @@ func (sc *StatementContext) HandleOverflow(err error, warnErr error) error {
return err
}

// ResetForRetry resets the changed states during execution.
func (sc *StatementContext) ResetForRetry() {
// resetMuForRetry resets the changed states of sc.mu during execution.
func (sc *StatementContext) resetMuForRetry() {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.affectedRows = 0
sc.mu.foundRows = 0
sc.mu.records = 0
Expand All @@ -571,7 +564,11 @@ func (sc *StatementContext) ResetForRetry() {
sc.mu.warnings = nil
sc.mu.execDetails = execdetails.ExecDetails{}
sc.mu.allExecDetails = make([]*execdetails.ExecDetails, 0, 4)
sc.mu.Unlock()
}

// ResetForRetry resets the changed states during execution.
func (sc *StatementContext) ResetForRetry() {
sc.resetMuForRetry()
sc.MaxRowID = 0
sc.BaseRowID = 0
sc.TableIDs = sc.TableIDs[:0]
Expand Down Expand Up @@ -622,21 +619,21 @@ func (sc *StatementContext) MergeTimeDetail(timeDetail util.TimeDetail) {
// MergeLockKeysExecDetails merges lock keys execution details into self.
func (sc *StatementContext) MergeLockKeysExecDetails(lockKeys *util.LockKeysDetails) {
sc.mu.Lock()
defer sc.mu.Unlock()
if sc.mu.execDetails.LockKeysDetail == nil {
sc.mu.execDetails.LockKeysDetail = lockKeys
} else {
sc.mu.execDetails.LockKeysDetail.Merge(lockKeys)
}
sc.mu.Unlock()
}

// GetExecDetails gets the execution details for the statement.
func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails {
var details execdetails.ExecDetails
sc.mu.Lock()
defer sc.mu.Unlock()
details = sc.mu.execDetails
details.LockKeysDuration = time.Duration(atomic.LoadInt64(&sc.LockKeysDuration))
sc.mu.Unlock()
return details
}

Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ var noopSysVars = []*SysVar{
{Scope: ScopeGlobal, Name: "innodb_max_dirty_pages_pct_lwm", Value: "0"},
{Scope: ScopeGlobal, Name: LogQueriesNotUsingIndexes, Value: Off, Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: "max_heap_table_size", Value: "16777216", IsHintUpdatable: true},
{Scope: ScopeGlobal | ScopeSession, Name: "tmp_table_size", Value: "16777216", Type: TypeUnsigned, MinValue: 1024, MaxValue: math.MaxUint64, IsHintUpdatable: true},
{Scope: ScopeGlobal | ScopeSession, Name: "div_precision_increment", Value: "4", IsHintUpdatable: true},
{Scope: ScopeGlobal, Name: "innodb_lru_scan_depth", Value: "1024"},
{Scope: ScopeGlobal, Name: "innodb_purge_rseg_truncate_frequency", Value: ""},
Expand Down
Loading

0 comments on commit 0e4f2e7

Please sign in to comment.