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: forbid point get with select lock choose tiflash as datasource path #39388

Merged
merged 24 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
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
33 changes: 33 additions & 0 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,12 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter
p: dual,
}, cntPlan, nil
}

// if the path is the point get range path with for update lock, we should forbid tiflash as it's store path.
if path.StoreType == kv.TiFlash && ds.isPointGetPath(path) && ds.isForUpdateRead {
continue
}

canConvertPointGet := len(path.Ranges) > 0 && path.StoreType == kv.TiKV && ds.isPointGetConvertableSchema()

if canConvertPointGet && expression.MaybeOverOptimized4PlanCache(ds.ctx, path.AccessConds) {
Expand Down Expand Up @@ -1052,6 +1058,33 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter
return
}

func (ds *DataSource) isPointGetPath(path *util.AccessPath) bool {
if len(path.Ranges) < 1 {
return false
}
for _, ran := range path.Ranges {
if !ran.IsPointNonNullable(ds.ctx) {
return false
}
}
if path.IsIntHandlePath {
return true
}
if path.Index == nil {
return false
}
if path.Index.HasPrefixIndex() || !path.Index.Unique {
return false
}
idxColsLen := len(path.Index.Columns)
for _, ran := range path.Ranges {
if len(ran.LowVal) != idxColsLen {
return false
}
}
return true
}

func (ds *DataSource) canConvertToPointGetForPlanCache(path *util.AccessPath) bool {
// PointGet might contain some over-optimized assumptions, like `a>=1 and a<=1` --> `a=1`, but
// these assumptions may be broken after parameters change.
Expand Down
20 changes: 20 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7554,6 +7554,26 @@ func TestEnableTiFlashReadForWriteStmt(t *testing.T) {
checkMpp(rs)
}

func TestPointGetWithSelectLock(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not just a test for a primary key, a test for a unique key is also necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int, c int, primary key(a, b));")
tk.MustExec("insert into t values(1,2,3), (4,5,6);")
tbl, err := dom.InfoSchema().TableByName(model.CIStr{O: "test", L: "test"}, model.CIStr{O: "t", L: "t"})
require.NoError(t, err)
// Set the hacked TiFlash replica for explain tests.
tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{Count: 1, Available: true}
tk.MustExec("set @@tidb_enable_tiflash_read_for_write_stmt = on;")
tk.MustExec("set @@tidb_isolation_read_engines='tidb,tiflash';")
tk.MustExec("begin;")
tk.MustGetErrMsg("explain select a, b from t where a = 1 and b = 2 for update;", "[planner:1815]Internal : Can't find a proper physical plan for this query")
tk.MustQuery("explain select a, b from t where a = 1 for update;")
tk.MustExec("set tidb_isolation_read_engines='tidb,tikv,tiflash';")
tk.MustQuery("explain select a, b from t where a = 1 and b = 2 for update;")
tk.MustExec("commit")
}

func TestTableRangeFallback(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down