diff --git a/planner/core/casetest/rule/BUILD.bazel b/planner/core/casetest/rule/BUILD.bazel index b735973b2d3bd..50d121108f1d1 100644 --- a/planner/core/casetest/rule/BUILD.bazel +++ b/planner/core/casetest/rule/BUILD.bazel @@ -12,7 +12,7 @@ go_test( ], data = glob(["testdata/**"]), flaky = True, - shard_count = 21, + shard_count = 22, deps = [ "//domain", "//expression", diff --git a/planner/core/casetest/rule/rule_join_reorder_test.go b/planner/core/casetest/rule/rule_join_reorder_test.go index 140740aebf679..aa3c49eae8cfb 100644 --- a/planner/core/casetest/rule/rule_join_reorder_test.go +++ b/planner/core/casetest/rule/rule_join_reorder_test.go @@ -72,6 +72,17 @@ func TestNoHashJoinHint(t *testing.T) { runJoinReorderTestData(t, tk, "TestNoHashJoinHint") } +func TestNoMergeJoinHint(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t1(a int, key(a));") + tk.MustExec("create table t2(a int, key(a));") + tk.MustExec("create table t3(a int, key(a));") + tk.MustExec("create table t4(a int, key(a));") + runJoinReorderTestData(t, tk, "TestNoMergeJoinHint") +} + func TestLeadingJoinHint(t *testing.T) { store := testkit.CreateMockStore(t) diff --git a/planner/core/casetest/rule/testdata/join_reorder_suite_in.json b/planner/core/casetest/rule/testdata/join_reorder_suite_in.json index 6c5750386e87a..9c3bdf93492f8 100644 --- a/planner/core/casetest/rule/testdata/join_reorder_suite_in.json +++ b/planner/core/casetest/rule/testdata/join_reorder_suite_in.json @@ -49,6 +49,19 @@ "select /*+ leading(t1, t2, t3, t4), hash_join(t1, t2), no_hash_join(t3), hash_join(t4) */ * from t1, t2, t3, t4" ] }, + { + "name": "TestNoMergeJoinHint", + "cases": [ + "select /*+ no_merge_join() */ * from t1, t2 where t1.a=t2.a", + "select /*+ no_merge_join(t1), merge_join(t1) */ * from t1, t2 where t1.a=t2.a", + "select /*+ no_merge_join(t1), merge_join(t2) */ * from t1, t2 where t1.a=t2.a", + "select /*+ no_merge_join(t1) */ * from t1, t2 where t1.a=t2.a", + "select /*+ no_merge_join(t1, t2) */ * from t1, t2 where t1.a=t2.a", + "select /*+ no_merge_join(t2) */ * from t1 right join t2 on t1.a=t2.a", + "select /*+ leading(t4, t3, t2, t1), no_merge_join(t2, t3) */ * from t1, t2, t3, t4 where t1.a=t2.a and t2.a=t3.a and t3.a=t4.a", + "select /*+ leading(t1, t2, t3, t4), merge_join(t1, t2), no_merge_join(t3), merge_join(t4) */ * from t1, t2, t3, t4 where t1.a=t2.a and t2.a=t3.a and t3.a=t4.a" + ] + }, { "name": "TestLeadingJoinHint", "cases": [ diff --git a/planner/core/casetest/rule/testdata/join_reorder_suite_out.json b/planner/core/casetest/rule/testdata/join_reorder_suite_out.json index 71d7277dec714..a057b18caebf0 100644 --- a/planner/core/casetest/rule/testdata/join_reorder_suite_out.json +++ b/planner/core/casetest/rule/testdata/join_reorder_suite_out.json @@ -782,6 +782,119 @@ } ] }, + { + "Name": "TestNoMergeJoinHint", + "Cases": [ + { + "SQL": "select /*+ no_merge_join() */ * from t1, t2 where t1.a=t2.a", + "Plan": [ + "MergeJoin 12487.50 root inner join, left key:test.t1.a, right key:test.t2.a", + "├─IndexReader(Build) 9990.00 root index:IndexFullScan", + "│ └─IndexFullScan 9990.00 cop[tikv] table:t2, index:a(a) keep order:true, stats:pseudo", + "└─IndexReader(Probe) 9990.00 root index:IndexFullScan", + " └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:true, stats:pseudo" + ], + "Warning": [ + "Warning 1815 Hint no_merge_join() is inapplicable. Please specify the table names in the arguments." + ] + }, + { + "SQL": "select /*+ no_merge_join(t1), merge_join(t1) */ * from t1, t2 where t1.a=t2.a", + "Plan": [ + "MergeJoin 12487.50 root inner join, left key:test.t1.a, right key:test.t2.a", + "├─IndexReader(Build) 9990.00 root index:IndexFullScan", + "│ └─IndexFullScan 9990.00 cop[tikv] table:t2, index:a(a) keep order:true, stats:pseudo", + "└─IndexReader(Probe) 9990.00 root index:IndexFullScan", + " └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:true, stats:pseudo" + ], + "Warning": [ + "Warning 1815 Some MERGE_JOIN and NO_MERGE_JOIN hints conflict, NO_MERGE_JOIN is ignored" + ] + }, + { + "SQL": "select /*+ no_merge_join(t1), merge_join(t2) */ * from t1, t2 where t1.a=t2.a", + "Plan": [ + "MergeJoin 12487.50 root inner join, left key:test.t1.a, right key:test.t2.a", + "├─IndexReader(Build) 9990.00 root index:IndexFullScan", + "│ └─IndexFullScan 9990.00 cop[tikv] table:t2, index:a(a) keep order:true, stats:pseudo", + "└─IndexReader(Probe) 9990.00 root index:IndexFullScan", + " └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:true, stats:pseudo" + ], + "Warning": [ + "Warning 1815 Some MERGE_JOIN and NO_MERGE_JOIN hints conflict, NO_MERGE_JOIN is ignored" + ] + }, + { + "SQL": "select /*+ no_merge_join(t1) */ * from t1, t2 where t1.a=t2.a", + "Plan": [ + "HashJoin 12487.50 root inner join, equal:[eq(test.t1.a, test.t2.a)]", + "├─IndexReader(Build) 9990.00 root index:IndexFullScan", + "│ └─IndexFullScan 9990.00 cop[tikv] table:t2, index:a(a) keep order:false, stats:pseudo", + "└─IndexReader(Probe) 9990.00 root index:IndexFullScan", + " └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:false, stats:pseudo" + ], + "Warning": null + }, + { + "SQL": "select /*+ no_merge_join(t1, t2) */ * from t1, t2 where t1.a=t2.a", + "Plan": [ + "HashJoin 12487.50 root inner join, equal:[eq(test.t1.a, test.t2.a)]", + "├─IndexReader(Build) 9990.00 root index:IndexFullScan", + "│ └─IndexFullScan 9990.00 cop[tikv] table:t2, index:a(a) keep order:false, stats:pseudo", + "└─IndexReader(Probe) 9990.00 root index:IndexFullScan", + " └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:false, stats:pseudo" + ], + "Warning": null + }, + { + "SQL": "select /*+ no_merge_join(t2) */ * from t1 right join t2 on t1.a=t2.a", + "Plan": [ + "HashJoin 12487.50 root right outer join, equal:[eq(test.t1.a, test.t2.a)]", + "├─IndexReader(Build) 9990.00 root index:IndexFullScan", + "│ └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:false, stats:pseudo", + "└─IndexReader(Probe) 10000.00 root index:IndexFullScan", + " └─IndexFullScan 10000.00 cop[tikv] table:t2, index:a(a) keep order:false, stats:pseudo" + ], + "Warning": null + }, + { + "SQL": "select /*+ leading(t4, t3, t2, t1), no_merge_join(t2, t3) */ * from t1, t2, t3, t4 where t1.a=t2.a and t2.a=t3.a and t3.a=t4.a", + "Plan": [ + "Projection 19511.72 root test.t1.a, test.t2.a, test.t3.a, test.t4.a", + "└─HashJoin 19511.72 root inner join, equal:[eq(test.t2.a, test.t1.a)]", + " ├─IndexReader(Build) 9990.00 root index:IndexFullScan", + " │ └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:false, stats:pseudo", + " └─HashJoin(Probe) 15609.38 root inner join, equal:[eq(test.t3.a, test.t2.a)]", + " ├─IndexReader(Build) 9990.00 root index:IndexFullScan", + " │ └─IndexFullScan 9990.00 cop[tikv] table:t2, index:a(a) keep order:false, stats:pseudo", + " └─HashJoin(Probe) 12487.50 root inner join, equal:[eq(test.t4.a, test.t3.a)]", + " ├─IndexReader(Build) 9990.00 root index:IndexFullScan", + " │ └─IndexFullScan 9990.00 cop[tikv] table:t3, index:a(a) keep order:false, stats:pseudo", + " └─IndexReader(Probe) 9990.00 root index:IndexFullScan", + " └─IndexFullScan 9990.00 cop[tikv] table:t4, index:a(a) keep order:false, stats:pseudo" + ], + "Warning": null + }, + { + "SQL": "select /*+ leading(t1, t2, t3, t4), merge_join(t1, t2), no_merge_join(t3), merge_join(t4) */ * from t1, t2, t3, t4 where t1.a=t2.a and t2.a=t3.a and t3.a=t4.a", + "Plan": [ + "MergeJoin 19511.72 root inner join, left key:test.t3.a, right key:test.t4.a", + "├─IndexReader(Build) 9990.00 root index:IndexFullScan", + "│ └─IndexFullScan 9990.00 cop[tikv] table:t4, index:a(a) keep order:true, stats:pseudo", + "└─Sort(Probe) 15609.38 root test.t3.a", + " └─HashJoin 15609.38 root inner join, equal:[eq(test.t2.a, test.t3.a)]", + " ├─IndexReader(Build) 9990.00 root index:IndexFullScan", + " │ └─IndexFullScan 9990.00 cop[tikv] table:t3, index:a(a) keep order:false, stats:pseudo", + " └─MergeJoin(Probe) 12487.50 root inner join, left key:test.t1.a, right key:test.t2.a", + " ├─IndexReader(Build) 9990.00 root index:IndexFullScan", + " │ └─IndexFullScan 9990.00 cop[tikv] table:t2, index:a(a) keep order:true, stats:pseudo", + " └─IndexReader(Probe) 9990.00 root index:IndexFullScan", + " └─IndexFullScan 9990.00 cop[tikv] table:t1, index:a(a) keep order:true, stats:pseudo" + ], + "Warning": null + } + ] + }, { "Name": "TestLeadingJoinHint", "Cases": [ diff --git a/planner/core/exhaust_physical_plans.go b/planner/core/exhaust_physical_plans.go index d80a84295658d..6bf46e432f104 100644 --- a/planner/core/exhaust_physical_plans.go +++ b/planner/core/exhaust_physical_plans.go @@ -231,6 +231,15 @@ func (p *LogicalJoin) GetMergeJoin(prop *property.PhysicalProperty, schema *expr joins = append(joins, mergeJoin) } } + + if p.preferJoinType&preferNoMergeJoin > 0 { + if p.preferJoinType&preferMergeJoin == 0 { + return nil + } + p.SCtx().GetSessionVars().StmtCtx.AppendWarning(ErrInternal.GenWithStack( + "Some MERGE_JOIN and NO_MERGE_JOIN hints conflict, NO_MERGE_JOIN is ignored")) + } + // If TiDB_SMJ hint is existed, it should consider enforce merge join, // because we can't trust lhsChildProperty completely. if (p.preferJoinType&preferMergeJoin) > 0 || diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 3b26d96e38a19..1b37caa6fe278 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -75,6 +75,8 @@ const ( TiDBMergeJoin = "tidb_smj" // HintSMJ is hint enforce merge join. HintSMJ = "merge_join" + // HintNoMergeJoin is the hint to enforce the query not to use merge join. + HintNoMergeJoin = "no_merge_join" // TiDBBroadCastJoin indicates applying broadcast join by force. TiDBBroadCastJoin = "tidb_bcj" @@ -723,6 +725,14 @@ func (p *LogicalJoin) setPreferredJoinTypeAndOrder(hintInfo *tableHintInfo) { p.preferJoinType |= preferMergeJoin p.rightPreferJoinType |= preferMergeJoin } + if hintInfo.ifPreferNoMergeJoin(lhsAlias) { + p.preferJoinType |= preferNoMergeJoin + p.leftPreferJoinType |= preferNoMergeJoin + } + if hintInfo.ifPreferNoMergeJoin(rhsAlias) { + p.preferJoinType |= preferNoMergeJoin + p.rightPreferJoinType |= preferNoMergeJoin + } if hintInfo.ifPreferBroadcastJoin(lhsAlias) { p.preferJoinType |= preferBCJoin p.leftPreferJoinType |= preferBCJoin @@ -3924,7 +3934,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev hints = b.hintProcessor.GetCurrentStmtHints(hints, currentLevel) var ( sortMergeTables, inljTables, inlhjTables, inlmjTables, hashJoinTables, bcTables []hintTableInfo - noHashJoinTables []hintTableInfo + noHashJoinTables, noMergeJoinTables []hintTableInfo shuffleJoinTables []hintTableInfo indexHintList, indexMergeHintList []indexHintInfo tiflashTables, tikvTables []hintTableInfo @@ -3939,7 +3949,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev for _, hint := range hints { // Set warning for the hint that requires the table name. switch hint.HintName.L { - case TiDBMergeJoin, HintSMJ, TiDBIndexNestedLoopJoin, HintINLJ, HintINLHJ, HintINLMJ, HintNoHashJoin, + case TiDBMergeJoin, HintSMJ, TiDBIndexNestedLoopJoin, HintINLJ, HintINLHJ, HintINLMJ, HintNoHashJoin, HintNoMergeJoin, TiDBHashJoin, HintHJ, HintUseIndex, HintIgnoreIndex, HintForceIndex, HintOrderIndex, HintNoOrderIndex, HintIndexMerge, HintLeading: if len(hint.Tables) == 0 { b.pushHintWithoutTableWarning(hint) @@ -3964,6 +3974,8 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev hashJoinTables = append(hashJoinTables, tableNames2HintTableInfo(b.ctx, hint.HintName.L, hint.Tables, b.hintProcessor, currentLevel)...) case HintNoHashJoin: noHashJoinTables = append(noHashJoinTables, tableNames2HintTableInfo(b.ctx, hint.HintName.L, hint.Tables, b.hintProcessor, currentLevel)...) + case HintNoMergeJoin: + noMergeJoinTables = append(noMergeJoinTables, tableNames2HintTableInfo(b.ctx, hint.HintName.L, hint.Tables, b.hintProcessor, currentLevel)...) case HintMPP1PhaseAgg: aggHints.preferAggType |= preferMPP1PhaseAgg case HintMPP2PhaseAgg: @@ -4075,6 +4087,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev indexNestedLoopJoinTables: indexNestedLoopJoinTables{inljTables, inlhjTables, inlmjTables}, hashJoinTables: hashJoinTables, noHashJoinTables: noHashJoinTables, + noMergeJoinTables: noMergeJoinTables, indexHintList: indexHintList, tiflashTables: tiflashTables, tikvTables: tikvTables, @@ -7320,6 +7333,7 @@ func getInnerFromParenthesesAndUnaryPlus(expr ast.ExprNode) ast.ExprNode { // join types. func containDifferentJoinTypes(preferJoinType uint) bool { preferJoinType &= ^preferNoHashJoin + preferJoinType &= ^preferNoMergeJoin inlMask := preferRightAsINLJInner ^ preferLeftAsINLJInner inlhjMask := preferRightAsINLHJInner ^ preferLeftAsINLHJInner diff --git a/planner/core/logical_plans.go b/planner/core/logical_plans.go index 6b170e61218b1..7fe8193f26e43 100644 --- a/planner/core/logical_plans.go +++ b/planner/core/logical_plans.go @@ -124,6 +124,7 @@ const ( preferHashJoin preferNoHashJoin preferMergeJoin + preferNoMergeJoin preferBCJoin preferShuffleJoin preferRewriteSemiJoin diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 05db11ba202f7..9b5039091b37a 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -97,6 +97,7 @@ type tableHintInfo struct { shuffleJoinTables []hintTableInfo hashJoinTables []hintTableInfo noHashJoinTables []hintTableInfo + noMergeJoinTables []hintTableInfo indexHintList []indexHintInfo tiflashTables []hintTableInfo tikvTables []hintTableInfo @@ -245,6 +246,10 @@ func (info *tableHintInfo) ifPreferNoHashJoin(tableNames ...*hintTableInfo) bool return info.matchTableName(tableNames, info.noHashJoinTables) } +func (info *tableHintInfo) ifPreferNoMergeJoin(tableNames ...*hintTableInfo) bool { + return info.matchTableName(tableNames, info.noMergeJoinTables) +} + func (info *tableHintInfo) ifPreferHJBuild(tableNames ...*hintTableInfo) bool { return info.matchTableName(tableNames, info.hjBuildTables) }