Skip to content

Commit

Permalink
planner: add more test cases for non-prep plan cache (#41914)
Browse files Browse the repository at this point in the history
ref #36598
  • Loading branch information
qw4990 authored Mar 5, 2023
1 parent 00ccee2 commit 196ddc0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
11 changes: 11 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ func TestNonPreparedPlanCacheInListChange(t *testing.T) {
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}

func TestNonPreparedPlanCacheMemoryTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")

tk.MustExec(`select data_type from INFORMATION_SCHEMA.columns where table_name = 'v'`)
tk.MustExec(`select data_type from INFORMATION_SCHEMA.columns where table_name = 'v'`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}

func TestNonPreparedPlanCacheTooManyConsts(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
33 changes: 23 additions & 10 deletions planner/core/plan_cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,38 @@ func (checker *nonPreparedPlanCacheableChecker) Enter(in ast.Node) (out ast.Node
case *ast.TableName:
checker.tableNode = node
if checker.schema != nil {
if isPartitionTable(checker.schema, node) {
tb, err := checker.schema.TableByName(node.Schema, node.Name)
if err != nil {
checker.cacheable = false
checker.reason = "queries that access partitioning table are not supported"
checker.reason = "table cannot be found in schema"
return in, !checker.cacheable
}
if hasGeneratedCol(checker.schema, node) {
if tb.Meta().GetPartitionInfo() != nil {
checker.cacheable = false
checker.reason = "queries that have generated columns are not supported"
checker.reason = "queries that access partitioning table are not supported"
return in, !checker.cacheable
}
if isTempTable(checker.schema, node) {
for _, col := range tb.Cols() {
if col.IsGenerated() {
checker.cacheable = false
checker.reason = "queries that have generated columns are not supported"
return in, !checker.cacheable
}
}
if tb.Meta().TempTableType != model.TempTableNone {
checker.cacheable = false
checker.reason = "queries that access temporary tables are not supported"
return in, !checker.cacheable
}
if isView(checker.schema, node) {
if tb.Meta().IsView() {
checker.cacheable = false
checker.reason = "queries that access views are not supported"
return in, !checker.cacheable
}
if !tb.Type().IsNormalTable() {
checker.cacheable = false
checker.reason = "queries that access in-memory tables"
return in, !checker.cacheable
}
}
return in, !checker.cacheable
Expand Down Expand Up @@ -376,10 +393,6 @@ func getColType(schema infoschema.InfoSchema, tbl *ast.TableName, col *ast.Colum
return 0, false
}

func isView(schema infoschema.InfoSchema, tn *ast.TableName) bool {
return schema.TableIsView(tn.Schema, tn.Name)
}

func isTempTable(schema infoschema.InfoSchema, tn *ast.TableName) bool {
tb, err := schema.TableByName(tn.Schema, tn.Name)
if err != nil {
Expand Down

0 comments on commit 196ddc0

Please sign in to comment.