Skip to content

Commit c1b70d5

Browse files
authored
planner: add more test cases for plan cache with generated columns (#51323)
ref #45798
1 parent 21cd1eb commit c1b70d5

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

pkg/planner/core/indexmerge_path_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,110 @@ func TestMVIndexRandom(t *testing.T) {
232232
}
233233
}
234234

235+
func TestPlanCacheMVIndex(t *testing.T) {
236+
store := testkit.CreateMockStore(t)
237+
tk := testkit.NewTestKit(t, store)
238+
tk.MustExec("use test")
239+
tk.MustExec(`CREATE TABLE ti (
240+
item_pk varbinary(255) NOT NULL,
241+
item_id varchar(45) DEFAULT NULL,
242+
item_set_id varchar(45) DEFAULT NULL,
243+
m_id bigint(20) DEFAULT NULL,
244+
m_item_id varchar(127) DEFAULT NULL,
245+
m_item_set_id varchar(127) DEFAULT NULL,
246+
country varchar(2) DEFAULT NULL,
247+
domains json DEFAULT NULL,
248+
signatures json DEFAULT NULL,
249+
short_link json DEFAULT NULL,
250+
long_link json DEFAULT NULL,
251+
f_item_ids json DEFAULT NULL,
252+
f_profile_ids json DEFAULT NULL,
253+
product_sources json DEFAULT NULL,
254+
PRIMARY KEY (item_pk) /*T![clustered_index] CLUSTERED */,
255+
UNIQUE KEY item_id (item_id),
256+
KEY m_item_id (m_item_id),
257+
KEY m_item_set_id (m_item_set_id),
258+
KEY m_id (m_id),
259+
KEY item_set_id (item_set_id),
260+
KEY m_item_and_m_id (m_item_id, m_id),
261+
KEY domains ((cast(domains as char(253) array))),
262+
KEY signatures ((cast(signatures as char(32) array))),
263+
KEY f_profile_ids ((cast(f_profile_ids as unsigned array))),
264+
KEY short_link_old ((cast(short_link as char(1000) array))),
265+
KEY long_link ((cast(long_link as char(1000) array))),
266+
KEY f_item_ids ((cast(f_item_ids as unsigned array))),
267+
KEY short_link ((cast(short_link as char(1000) array)),country))`)
268+
269+
for i := 0; i < 50; i++ {
270+
var insertVals []string
271+
insertVals = append(insertVals, fmt.Sprintf("'%v'", i)) // item_pk varbinary(255) NOT NULL,
272+
insertVals = append(insertVals, fmt.Sprintf("'%v'", i)) // item_id varchar(45) DEFAULT NULL,
273+
insertVals = append(insertVals, fmt.Sprintf("'%v'", rand.Intn(30))) // item_set_id varchar(45) DEFAULT NULL,
274+
insertVals = append(insertVals, fmt.Sprintf("%v", rand.Intn(30))) // m_id bigint(20) DEFAULT NULL,
275+
insertVals = append(insertVals, fmt.Sprintf("'%v'", rand.Intn(30))) // m_item_id varchar(127) DEFAULT NULL,
276+
insertVals = append(insertVals, fmt.Sprintf("'%v'", rand.Intn(30))) // m_item_set_id varchar(127) DEFAULT NULL,
277+
insertVals = append(insertVals, fmt.Sprintf("'%v'", rand.Intn(3))) // country varchar(2) DEFAULT NULL,
278+
insertVals = append(insertVals, fmt.Sprintf("'%v'", randArray(randMVIndexValOpts{valType: "string", maxStrLen: 5, distinct: 10}))) // domains json DEFAULT NULL,
279+
insertVals = append(insertVals, fmt.Sprintf("'%v'", randArray(randMVIndexValOpts{valType: "string", maxStrLen: 5, distinct: 10}))) // signatures json DEFAULT NULL,
280+
insertVals = append(insertVals, fmt.Sprintf("'%v'", randArray(randMVIndexValOpts{valType: "string", maxStrLen: 5, distinct: 10}))) // short_link json DEFAULT NULL,
281+
insertVals = append(insertVals, fmt.Sprintf("'%v'", randArray(randMVIndexValOpts{valType: "string", maxStrLen: 5, distinct: 10}))) // long_link json DEFAULT NULL,
282+
insertVals = append(insertVals, fmt.Sprintf("'%v'", randArray(randMVIndexValOpts{valType: "unsigned", distinct: 10}))) // f_item_ids json DEFAULT NULL,
283+
insertVals = append(insertVals, fmt.Sprintf("'%v'", randArray(randMVIndexValOpts{valType: "unsigned", distinct: 10}))) // f_profile_ids json DEFAULT NULL,
284+
insertVals = append(insertVals, fmt.Sprintf("'%v'", randArray(randMVIndexValOpts{valType: "string", maxStrLen: 5, distinct: 10}))) // product_sources json DEFAULT NULL,
285+
tk.MustExec(fmt.Sprintf("insert into ti values (%v)", strings.Join(insertVals, ",")))
286+
}
287+
288+
tk.MustExec(`set @@tidb_opt_fix_control = "45798:on"`)
289+
290+
check := func(sql string, params ...string) {
291+
sqlWithoutParam := sql
292+
var setStmt, usingStmt string
293+
for i, p := range params {
294+
sqlWithoutParam = strings.Replace(sqlWithoutParam, "?", p, 1)
295+
if i > 0 {
296+
setStmt += ", "
297+
usingStmt += ", "
298+
}
299+
setStmt += fmt.Sprintf("@a%v=%v", i, p)
300+
usingStmt += fmt.Sprintf("@a%v", i)
301+
}
302+
result := tk.MustQuery(sqlWithoutParam).Sort()
303+
tk.MustExec(fmt.Sprintf("set %v", setStmt))
304+
tk.MustExec(fmt.Sprintf("prepare stmt from '%v'", sql))
305+
result1 := tk.MustQuery(fmt.Sprintf("execute stmt using %v", usingStmt)).Sort()
306+
result.Check(result1.Rows())
307+
result2 := tk.MustQuery(fmt.Sprintf("execute stmt using %v", usingStmt)).Sort()
308+
result.Check(result2.Rows())
309+
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
310+
}
311+
randV := func(vs ...string) string {
312+
return vs[rand.Intn(len(vs))]
313+
}
314+
315+
for i := 0; i < 50; i++ {
316+
check(`select * from ti where (? member of (short_link)) and (ti.country = ?)`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(3)))
317+
check(`select * from ti where (? member of (f_profile_ids) AND (ti.m_item_set_id = ?) AND (ti.country = ?))`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(3)))
318+
check(`select * from ti where (? member of (short_link))`, fmt.Sprintf("'%v'", rand.Intn(30)))
319+
check(`select * from ti where (? member of (long_link)) AND (ti.country = ?)`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(3)))
320+
check(`select * from ti where (? member of (long_link))`, fmt.Sprintf("'%v'", rand.Intn(30)))
321+
check(`select * from ti where (? member of (f_profile_ids) AND (ti.m_item_set_id = ?) AND (ti.country = ?))`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(3)))
322+
check(`select * from ti where (m_id = ? and m_item_id = ? and country = ?) OR (? member of (short_link) and not json_overlaps(product_sources, ?) and country = ?)`,
323+
fmt.Sprintf("%v", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(3)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(3)), randV(`'["0","1","2"]'`, `'["0"]'`, `'["1","2"]'`))
324+
check(`select * from ti where ? member of (domains) AND ? member of (signatures) AND ? member of (f_profile_ids) AND ? member of (short_link) AND ? member of (long_link) AND ? member of (f_item_ids)`,
325+
fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("%v", rand.Intn(30)))
326+
check(`select * from ti where ? member of (domains) AND ? member of (signatures) AND ? member of (f_profile_ids) AND ? member of (short_link) AND ? member of (long_link) AND ? member of (f_item_ids) AND m_item_id IS NULL AND m_id = ? AND country IS NOT NULL`,
327+
fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("%v", rand.Intn(30)), fmt.Sprintf("%v", rand.Intn(30)))
328+
check(`select * from ti where ? member of (f_profile_ids) AND ? member of (short_link) AND json_overlaps(product_sources, ?)`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), randV(`'["0","1","2"]'`, `'["0"]'`, `'["1","2"]'`))
329+
check(`select * from ti where ? member of (short_link) AND ti.country = "0" AND NOT ? member of (long_link) AND ti.m_item_id = "0"`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)))
330+
check(`select * from ti WHERE ? member of (domains) AND ? member of (signatures) AND json_contains(f_profile_ids, ?)`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), randV(`'["0","1","2"]'`, `'["0"]'`, `'["1","2"]'`))
331+
check(`select * from ti where ? member of (short_link) AND ? member of (long_link) OR ? member of (f_profile_ids) AND ti.m_item_id = "0" OR ti.m_item_set_id = ?`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)))
332+
check(`select * from ti where ? member of (domains) OR ? member of (signatures) OR ? member of (f_profile_ids) OR json_contains(f_profile_ids, ?)`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("%v", rand.Intn(30)), randV(`"[0,1]"`, `"[0,1,2]"`, `"[0]"`))
333+
check(`select * from ti WHERE ? member of (domains) OR ? member of (signatures) OR ? member of (long_link) OR json_contains(f_profile_ids, ?)`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), randV(`"[0,1]"`, `"[0,1,2]"`, `"[0]"`))
334+
check(`select * from ti where ? member of (domains) OR ? member of (signatures) OR (? member of (f_profile_ids))`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("%v", rand.Intn(30)))
335+
check(`select * from ti where ? member of (domains) OR ? member of (signatures) OR json_overlaps(f_profile_ids, ?)`, fmt.Sprintf("'%v'", rand.Intn(30)), fmt.Sprintf("'%v'", rand.Intn(30)), randV(`"[0,1]"`, `"[0,1,2]"`, `"[0]"`))
336+
}
337+
}
338+
235339
func randMVIndexCondsXNF4MemberOf(nConds int, valOpts randMVIndexValOpts, CNF bool, randJCol, randNCol func() string) (conds, conds4PlanCache string, params []string) {
236340
for i := 0; i < nConds; i++ {
237341
if i > 0 {

0 commit comments

Comments
 (0)