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: add more test cases for non-prep plan cache #43083

Merged
merged 7 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion executor/issuetest/executor_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func TestIssue22231(t *testing.T) {
tk.MustExec("create table t_issue_22231(a datetime)")
tk.MustExec("insert into t_issue_22231 values('2020--05-20 01:22:12')")
tk.MustQuery("select * from t_issue_22231 where a >= '2020-05-13 00:00:00 00:00:00' and a <= '2020-05-28 23:59:59 00:00:00'").Check(testkit.Rows("2020-05-20 01:22:12"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '2020-05-13 00:00:00 00:00:00'", "Warning 1292 Truncated incorrect datetime value: '2020-05-28 23:59:59 00:00:00'"))
tk.MustQuery("show warnings").MultiCheckContain([]string{"Truncated incorrect datetime value: '2020-05-13 00:00:00 00:00:00'", "Truncated incorrect datetime value: '2020-05-28 23:59:59 00:00:00'"})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prepare to enable this switch by default in the future.


tk.MustQuery("select cast('2020-10-22 10:31-10:12' as datetime)").Check(testkit.Rows("2020-10-22 10:31:10"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '2020-10-22 10:31-10:12'"))
Expand Down
6 changes: 3 additions & 3 deletions expression/integration_serial_test/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,7 @@ func TestTimeBuiltin(t *testing.T) {
tk.MustExec(`update t set a = dayOfMonth("0000-00-00")`)
tk.MustExec("set sql_mode = 'NO_ZERO_DATE';")
tk.MustExec("insert into t value(dayOfMonth('0000-00-00'))")
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
tk.MustQuery("show warnings").CheckContain("Incorrect datetime value: '0000-00-00 00:00:00.000000'")
tk.MustExec(`update t set a = dayOfMonth("0000-00-00")`)
tk.MustExec("set sql_mode = 'NO_ZERO_DATE,STRICT_TRANS_TABLES';")
_, err = tk.Exec("insert into t value(dayOfMonth('0000-00-00'))")
Expand Down Expand Up @@ -2395,7 +2395,7 @@ func TestTimeBuiltin(t *testing.T) {
tk.MustExec(`update t set a = monthname("0000-00-00")`)
tk.MustExec("set sql_mode = 'NO_ZERO_DATE'")
tk.MustExec("insert into t value(monthname('0000-00-00'))")
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
tk.MustQuery("show warnings").CheckContain("Incorrect datetime value: '0000-00-00 00:00:00.000000'")
tk.MustExec(`update t set a = monthname("0000-00-00")`)
tk.MustExec("set sql_mode = ''")
tk.MustExec("insert into t value(monthname('0000-00-00'))")
Expand All @@ -2406,7 +2406,7 @@ func TestTimeBuiltin(t *testing.T) {
require.NoError(t, err)
result = tk.MustQuery(`select monthname("2017-12-01"), monthname("0000-00-00"), monthname("0000-01-00"), monthname("0000-01-00 00:00:00")`)
result.Check(testkit.Rows("December <nil> January January"))
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
tk.MustQuery("show warnings").CheckContain("Incorrect datetime value: '0000-00-00 00:00:00.000000'")

// for dayname
tk.MustExec(`drop table if exists t`)
Expand Down
4 changes: 2 additions & 2 deletions expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ func TestArithmeticBuiltin(t *testing.T) {
tk.MustExec("drop table if exists t;")
tk.MustExec("CREATE TABLE t (v int);")
tk.MustExec("INSERT IGNORE INTO t VALUE(12 MOD 0);")
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1365 Division by 0"))
tk.MustQuery("show warnings;").CheckContain("Division by 0")
tk.MustQuery("select v from t;").Check(testkit.Rows("<nil>"))
tk.MustQuery("select 0.000 % 0.11234500000000000000;").Check(testkit.Rows("0.00000000000000000000"))

Expand Down Expand Up @@ -7172,7 +7172,7 @@ func TestIssue29708(t *testing.T) {
})

tk.MustExec("INSERT IGNORE INTO t1 VALUES (REPEAT(0125,200000000));")
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1301 Result of repeat() was larger than max_allowed_packet (67108864) - truncated"))
tk.MustQuery("show warnings;").CheckContain("Result of repeat() was larger than max_allowed_packet (67108864) - truncated")
tk.MustQuery("select a from t1 order by a;").Check([][]interface{}{
{nil},
{"a"},
Expand Down
6 changes: 4 additions & 2 deletions parser/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (ctx *RestoreCtx) WriteKeyWord(keyWord string) {
case ctx.Flags.HasKeyWordLowercaseFlag():
keyWord = strings.ToLower(keyWord)
}
fmt.Fprint(ctx.In, keyWord)
ctx.In.WriteString(keyWord)
}

// WriteWithSpecialComments writes a string with a special comment wrapped.
Expand Down Expand Up @@ -412,7 +412,9 @@ func (ctx *RestoreCtx) WriteString(str string) {
str = strings.Replace(str, `"`, `""`, -1)
quotes = `"`
}
fmt.Fprint(ctx.In, quotes, str, quotes)
ctx.In.WriteString(quotes)
ctx.In.WriteString(str)
ctx.In.WriteString(quotes)
}

// WriteName writes the name into writer
Expand Down
2 changes: 1 addition & 1 deletion planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4940,7 +4940,7 @@ func TestIssue37760(t *testing.T) {
tk.MustExec("insert into t values (2), (4), (6)")
tk.MustExec("set @@tidb_opt_range_max_size=1")
tk.MustQuery("select * from t where a").Check(testkit.Rows("2", "4", "6"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Memory capacity of 1 bytes for 'tidb_opt_range_max_size' exceeded when building ranges. Less accurate ranges such as full range are chosen"))
tk.MustQuery("show warnings").CheckContain("Memory capacity of 1 bytes for 'tidb_opt_range_max_size' exceeded when building ranges. Less accurate ranges such as full range are chosen")
}

// TestExplainAnalyzeDMLCommit covers the issue #37373.
Expand Down
4 changes: 2 additions & 2 deletions planner/core/plan_cache_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (pr *paramReplacer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
}
case *driver.ValueExpr:
pr.params = append(pr.params, n)
param := ast.NewParamMarkerExpr(len(pr.params) - 1) // offset is used as order in non-prepared plan cache.
param.(*driver.ParamMarkerExpr).Datum = *n.Datum.Clone() // init the ParamMakerExpr's Datum
param := ast.NewParamMarkerExpr(len(pr.params) - 1) // offset is used as order in non-prepared plan cache.
n.Datum.Copy(&param.(*driver.ParamMarkerExpr).Datum) // init the ParamMakerExpr's Datum
Comment on lines +75 to +76
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce one time of data copy.

return param, true
}
return in, false
Expand Down
11 changes: 9 additions & 2 deletions planner/core/plan_cache_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func GeneratePlanCacheStmtWithAST(ctx context.Context, sctx sessionctx.Context,
// Put the parameters that may affect the plan in planCacheValue.
// However, due to some compatibility reasons, we will temporarily keep some system variable-related values in planCacheKey.
// At the same time, because these variables have a small impact on plan, we will move them to PlanCacheValue later if necessary.
// TODO: maintain a sync.pool for this structure.
type planCacheKey struct {
database string
connID uint64
Expand Down Expand Up @@ -215,8 +216,8 @@ type planCacheKey struct {
func (key *planCacheKey) Hash() []byte {
if len(key.hash) == 0 {
var (
dbBytes = hack.Slice(key.database)
bufferSize = len(dbBytes) + 8*6 + 3*8
dbBytes = hack.Slice(key.stmtText)
bufferSize = len(dbBytes) * 2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce times of buffer re-allocation.

)
if key.hash == nil {
key.hash = make([]byte, 0, bufferSize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may use sync.Pool to improve performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I add a TODO comment there.

Expand Down Expand Up @@ -496,6 +497,12 @@ func (checker *matchOptsExtractor) Enter(in ast.Node) (out ast.Node, skipChildre
}
tStats := getStatsTable(checker.sctx, t.Meta(), t.Meta().ID)
checker.statsVersionHash += tableStatsVersionForPlanCache(tStats) // use '+' as the hash function for simplicity
case *ast.InsertStmt:
if node.Select != nil {
node.Select.Accept(checker)
}
// skip node.Table for performance.
return in, true
}
return in, false
}
Expand Down