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: outer merge join cannot keep the prop of its inner child (#33359) #33374

Merged
merged 21 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5742215
cherry pick #33359 to release-5.3
winoros Mar 24, 2022
003b24d
fix merge conflict
winoros Jun 15, 2022
fcc7554
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 16, 2022
25c9091
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 16, 2022
c3c1848
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 16, 2022
79ca91a
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 16, 2022
11c58c4
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 16, 2022
a08b17f
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 16, 2022
53880ab
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
1a3d4ee
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
c37e184
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
6274ad5
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
7029e15
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
85da312
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
b8c0e61
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
804a164
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
2d95abf
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
acc8c61
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
e3f55b3
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
6b2636f
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
f8dedfc
Merge branch 'release-5.3' into release-5.3-1287eab595d0
ti-chi-bot Jun 17, 2022
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
11 changes: 10 additions & 1 deletion planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,16 @@ func (p *LogicalJoin) getEnforcedMergeJoin(prop *property.PhysicalProperty, sche
return nil
}
for _, item := range prop.SortItems {
isExist := false
isExist, hasLeftColInProp, hasRightColInProp := false, false, false
for joinKeyPos := 0; joinKeyPos < len(leftJoinKeys); joinKeyPos++ {
var key *expression.Column
if item.Col.Equal(p.ctx, leftJoinKeys[joinKeyPos]) {
key = leftJoinKeys[joinKeyPos]
hasLeftColInProp = true
}
if item.Col.Equal(p.ctx, rightJoinKeys[joinKeyPos]) {
key = rightJoinKeys[joinKeyPos]
hasRightColInProp = true
}
if key == nil {
continue
Expand All @@ -313,6 +315,13 @@ func (p *LogicalJoin) getEnforcedMergeJoin(prop *property.PhysicalProperty, sche
if !isExist {
return nil
}
// If the output wants the order of the inner side. We should reject it since we might add null-extend rows of that side.
if p.JoinType == LeftOuterJoin && hasRightColInProp {
return nil
}
if p.JoinType == RightOuterJoin && hasLeftColInProp {
return nil
}
}
// Generate the enforced sort merge join
leftKeys := getNewJoinKeysByOffsets(leftJoinKeys, offsets)
Expand Down
19 changes: 19 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4939,3 +4939,22 @@ func (s *testIntegrationSerialSuite) TestIssue30271(c *C) {
tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci;")
tk.MustQuery("select * from t where (a>'a' and b='a') or (b = 'A' and a < 'd') order by a,c;").Check(testkit.Rows("b a 1", "b A 2", "c a 3"))
}

func (s *testIntegrationSerialSuite) TestIssue33042(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("create table t1(id int primary key, col1 int)")
tk.MustExec("create table t2(id int primary key, col1 int)")
tk.MustQuery("explain format='brief' SELECT /*+ merge_join(t1, t2)*/ * FROM (t1 LEFT JOIN t2 ON t1.col1=t2.id) order by t2.id;").Check(
testkit.Rows(
"Sort 12500.00 root test.t2.id",
"└─MergeJoin 12500.00 root left outer join, left key:test.t1.col1, right key:test.t2.id",
" ├─TableReader(Build) 10000.00 root data:TableFullScan",
" │ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:true, stats:pseudo",
" └─Sort(Probe) 10000.00 root test.t1.col1",
" └─TableReader 10000.00 root data:TableFullScan",
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
),
)
}
2 changes: 1 addition & 1 deletion planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@
},
{
"SQL": "select /*+ TIDB_SMJ(t1,t2,t3)*/ * from t t1 left outer join t t2 on t1.a = t2.a left outer join t t3 on t2.a = t3.a",
"Best": "MergeLeftOuterJoin{MergeLeftOuterJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.a,test.t.a)->TableReader(Table(t))}(test.t.a,test.t.a)"
"Best": "MergeLeftOuterJoin{MergeLeftOuterJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.a,test.t.a)->Sort->TableReader(Table(t))}(test.t.a,test.t.a)"
},
{
"SQL": "select /*+ TIDB_SMJ(t1,t2,t3)*/ * from t t1 left outer join t t2 on t1.a = t2.a left outer join t t3 on t1.a = t3.a",
Expand Down