Skip to content

Commit

Permalink
planner: fix data race in the TestInlineProjection4HashJoinIssue15316 (
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored Aug 11, 2022
1 parent e0c6240 commit 4fd3d0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
16 changes: 8 additions & 8 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,8 +2149,8 @@ func TestIssue11390(t *testing.T) {
}

func TestOuterTableBuildHashTableIsuse13933(t *testing.T) {
plannercore.ForceUseOuterBuild4Test = true
defer func() { plannercore.ForceUseOuterBuild4Test = false }()
plannercore.ForceUseOuterBuild4Test.Store(true)
defer func() { plannercore.ForceUseOuterBuild4Test.Store(false) }()
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand Down Expand Up @@ -2235,8 +2235,8 @@ func TestIssue14514(t *testing.T) {
}

func TestOuterMatchStatusIssue14742(t *testing.T) {
plannercore.ForceUseOuterBuild4Test = true
defer func() { plannercore.ForceUseOuterBuild4Test = false }()
plannercore.ForceUseOuterBuild4Test.Store(true)
defer func() { plannercore.ForceUseOuterBuild4Test.Store(false) }()
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand All @@ -2262,8 +2262,8 @@ func TestInlineProjection4HashJoinIssue15316(t *testing.T) {
// Two necessary factors to reproduce this issue:
// (1) taking HashLeftJoin, i.e., letting the probing tuple lay at the left side of joined tuples
// (2) the projection only contains a part of columns from the build side, i.e., pruning the same probe side
plannercore.ForcedHashLeftJoin4Test = true
defer func() { plannercore.ForcedHashLeftJoin4Test = false }()
plannercore.ForcedHashLeftJoin4Test.Store(true)
defer func() { plannercore.ForcedHashLeftJoin4Test.Store(false) }()
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand Down Expand Up @@ -2595,9 +2595,9 @@ func TestIssue20270(t *testing.T) {
err := tk.QueryToErr("select /*+ TIDB_HJ(t, t1) */ * from t left join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
require.Equal(t, executor.ErrQueryInterrupted, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/killedInJoin2Chunk"))
plannercore.ForceUseOuterBuild4Test = true
plannercore.ForceUseOuterBuild4Test.Store(true)
defer func() {
plannercore.ForceUseOuterBuild4Test = false
plannercore.ForceUseOuterBuild4Test.Store(false)
}()
err = failpoint.Enable("github.com/pingcap/tidb/executor/killedInJoin2ChunkForOuterHashJoin", "return(true)")
require.NoError(t, err)
Expand Down
22 changes: 12 additions & 10 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/pingcap/tidb/util/plancodec"
"github.com/pingcap/tidb/util/ranger"
"github.com/pingcap/tidb/util/set"
"go.uber.org/atomic"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -366,11 +367,11 @@ func (p *PhysicalMergeJoin) initCompareFuncs() {

// ForceUseOuterBuild4Test is a test option to control forcing use outer input as build.
// TODO: use hint and remove this variable
var ForceUseOuterBuild4Test = false
var ForceUseOuterBuild4Test = atomic.NewBool(false)

// ForcedHashLeftJoin4Test is a test option to force using HashLeftJoin
// TODO: use hint and remove this variable
var ForcedHashLeftJoin4Test = false
var ForcedHashLeftJoin4Test = atomic.NewBool(false)

func (p *LogicalJoin) getHashJoins(prop *property.PhysicalProperty) []PhysicalPlan {
if !prop.IsSortItemEmpty() { // hash join doesn't promise any orders
Expand All @@ -381,21 +382,21 @@ func (p *LogicalJoin) getHashJoins(prop *property.PhysicalProperty) []PhysicalPl
case SemiJoin, AntiSemiJoin, LeftOuterSemiJoin, AntiLeftOuterSemiJoin:
joins = append(joins, p.getHashJoin(prop, 1, false))
case LeftOuterJoin:
if ForceUseOuterBuild4Test {
if ForceUseOuterBuild4Test.Load() {
joins = append(joins, p.getHashJoin(prop, 1, true))
} else {
joins = append(joins, p.getHashJoin(prop, 1, false))
joins = append(joins, p.getHashJoin(prop, 1, true))
}
case RightOuterJoin:
if ForceUseOuterBuild4Test {
if ForceUseOuterBuild4Test.Load() {
joins = append(joins, p.getHashJoin(prop, 0, true))
} else {
joins = append(joins, p.getHashJoin(prop, 0, false))
joins = append(joins, p.getHashJoin(prop, 0, true))
}
case InnerJoin:
if ForcedHashLeftJoin4Test {
if ForcedHashLeftJoin4Test.Load() {
joins = append(joins, p.getHashJoin(prop, 1, false))
} else {
joins = append(joins, p.getHashJoin(prop, 1, false))
Expand Down Expand Up @@ -1327,11 +1328,12 @@ loopOtherConds:
}

// removeUselessEqAndInFunc removes the useless eq/in conditions. It's designed for the following case:
// t1 join t2 on t1.a=t2.a and t1.c=t2.c where t1.b > t2.b-10 and t1.b < t2.b+10 there's index(a, b, c) on t1.
// In this case the curIdxOff2KeyOff is [0 -1 1] and the notKeyEqAndIn is [].
// It's clearly that the column c cannot be used to access data. So we need to remove it and reset the IdxOff2KeyOff to
// [0 -1 -1].
// So that we can use t1.a=t2.a and t1.b > t2.b-10 and t1.b < t2.b+10 to build ranges then access data.
//
// t1 join t2 on t1.a=t2.a and t1.c=t2.c where t1.b > t2.b-10 and t1.b < t2.b+10 there's index(a, b, c) on t1.
// In this case the curIdxOff2KeyOff is [0 -1 1] and the notKeyEqAndIn is [].
// It's clearly that the column c cannot be used to access data. So we need to remove it and reset the IdxOff2KeyOff to
// [0 -1 -1].
// So that we can use t1.a=t2.a and t1.b > t2.b-10 and t1.b < t2.b+10 to build ranges then access data.
func (ijHelper *indexJoinBuildHelper) removeUselessEqAndInFunc(idxCols []*expression.Column, notKeyEqAndIn []expression.Expression, _ []*expression.Column) (usefulEqAndIn, uselessOnes []expression.Expression) {
ijHelper.curPossibleUsedKeys = make([]*expression.Column, 0, len(idxCols))
for idxColPos, notKeyColPos := 0, 0; idxColPos < len(idxCols); idxColPos++ {
Expand Down

0 comments on commit 4fd3d0b

Please sign in to comment.