@@ -19,7 +19,9 @@ import (
19
19
"testing"
20
20
"time"
21
21
22
+ "github.com/pingcap/tidb/pkg/parser/auth"
22
23
"github.com/pingcap/tidb/pkg/testkit"
24
+ "github.com/stretchr/testify/require"
23
25
)
24
26
25
27
func TestInstancePlanCacheVars (t * testing.T ) {
@@ -231,3 +233,100 @@ func TestInstancePlanCacheSchemaChange(t *testing.T) {
231
233
tk .MustExec (`execute st` )
232
234
tk .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
233
235
}
236
+
237
+ func TestInstancePlanCachePrivilegeChanges (t * testing.T ) {
238
+ store := testkit .CreateMockStore (t )
239
+ tk := testkit .NewTestKit (t , store )
240
+ tk .MustExec (`use test` )
241
+ tk .MustExec (`set global tidb_enable_instance_plan_cache=1` )
242
+
243
+ tk .MustExec (`create table t (a int, primary key(a))` )
244
+ tk .MustExec (`CREATE USER 'u1'` )
245
+ tk .MustExec (`grant select on test.t to 'u1'` )
246
+
247
+ u1 := testkit .NewTestKit (t , store )
248
+ require .NoError (t , u1 .Session ().Auth (& auth.UserIdentity {Username : "u1" , Hostname : "%" }, nil , nil , nil ))
249
+
250
+ u1 .MustExec (`prepare st from 'select a from test.t where a<1'` )
251
+ u1 .MustExec (`execute st` )
252
+ u1 .MustExec (`execute st` )
253
+ u1 .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
254
+
255
+ tk .MustExec (`revoke select on test.t from 'u1'` )
256
+ u1 .MustExecToErr (`execute st` ) // no privilege
257
+
258
+ tk .MustExec (`grant select on test.t to 'u1'` )
259
+ u1 .MustExec (`execute st` )
260
+ u1 .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" )) // hit the cache again
261
+ }
262
+
263
+ func TestInstancePlanCacheDifferentUsers (t * testing.T ) {
264
+ store := testkit .CreateMockStore (t )
265
+ tk := testkit .NewTestKit (t , store )
266
+ tk .MustExec (`use test` )
267
+ tk .MustExec (`set global tidb_enable_instance_plan_cache=1` )
268
+
269
+ tk .MustExec (`create table t (a int, primary key(a))` )
270
+ tk .MustExec (`CREATE USER 'u1'` )
271
+ tk .MustExec (`grant select on test.t to 'u1'` )
272
+ tk .MustExec (`CREATE USER 'u1'@'localhost'` )
273
+ tk .MustExec (`grant select on test.t to 'u1'@'localhost'` )
274
+ tk .MustExec (`CREATE USER 'u2'` )
275
+ tk .MustExec (`grant select on test.t to 'u2'` )
276
+
277
+ u1 := testkit .NewTestKit (t , store )
278
+ require .NoError (t , u1 .Session ().Auth (& auth.UserIdentity {Username : "u1" , Hostname : "%" }, nil , nil , nil ))
279
+ u1Local := testkit .NewTestKit (t , store )
280
+ require .NoError (t , u1Local .Session ().Auth (& auth.UserIdentity {Username : "u1" , Hostname : "localhost" }, nil , nil , nil ))
281
+ u2 := testkit .NewTestKit (t , store )
282
+ require .NoError (t , u2 .Session ().Auth (& auth.UserIdentity {Username : "u2" , Hostname : "%" }, nil , nil , nil ))
283
+ u1Dup := testkit .NewTestKit (t , store )
284
+ require .NoError (t , u1Dup .Session ().Auth (& auth.UserIdentity {Username : "u1" , Hostname : "%" }, nil , nil , nil ))
285
+
286
+ u1 .MustExec (`prepare st from 'select a from test.t where a=1'` )
287
+ u1 .MustExec (`execute st` )
288
+ u1 .MustExec (`execute st` )
289
+ u1 .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
290
+
291
+ u1Local .MustExec (`prepare st from 'select a from test.t where a=1'` )
292
+ u1Local .MustExec (`execute st` )
293
+ u1Local .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("0" )) // can't hit, different localhost
294
+ u1Local .MustExec (`execute st` )
295
+ u1Local .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
296
+
297
+ u2 .MustExec (`prepare st from 'select a from test.t where a=1'` )
298
+ u2 .MustExec (`execute st` )
299
+ u2 .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("0" )) // can't hit, different user
300
+ u2 .MustExec (`execute st` )
301
+ u2 .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
302
+
303
+ u1Dup .MustExec (`prepare st from 'select a from test.t where a=1'` )
304
+ u1Dup .MustExec (`execute st` )
305
+ u1Dup .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" )) // can hit, same user and localhost
306
+ u1Dup .MustExec (`execute st` )
307
+ u1Dup .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
308
+ }
309
+
310
+ func TestInstancePlanCachePartitioning (t * testing.T ) {
311
+ store := testkit .CreateMockStore (t )
312
+ tk := testkit .NewTestKit (t , store )
313
+ tk .MustExec ("use test" )
314
+ tk .MustExec (`set global tidb_enable_instance_plan_cache=1` )
315
+ tk .MustExec (`set @@tidb_partition_prune_mode='dynamic'` )
316
+
317
+ tk .MustExec (`create table t (a int, b varchar(255)) partition by hash(a) partitions 3` )
318
+ tk .MustExec (`insert into t values (1,"a"),(2,"b"),(3,"c"),(4,"d"),(5,"e"),(6,"f")` )
319
+ tk .MustExec (`prepare stmt from 'select a,b from t where a = ?;'` )
320
+ tk .MustExec (`set @a=1` )
321
+ tk .MustQuery (`execute stmt using @a` ).Check (testkit .Rows ("1 a" ))
322
+ // Same partition works, due to pruning is not affected
323
+ tk .MustExec (`set @a=4` )
324
+ tk .MustQuery (`execute stmt using @a` ).Check (testkit .Rows ("4 d" ))
325
+ tk .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
326
+
327
+ tk .MustExec (`set @@tidb_partition_prune_mode='static'` )
328
+ tk .MustQuery (`execute stmt using @a` ).Check (testkit .Rows ("4 d" ))
329
+ tk .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("0" ))
330
+ tk .MustQuery (`execute stmt using @a` ).Check (testkit .Rows ("4 d" ))
331
+ tk .MustQuery (`show warnings` ).Check (testkit .Rows ("Warning 1105 skip prepared plan-cache: Static partition pruning mode" ))
332
+ }
0 commit comments