Skip to content

Commit

Permalink
This is an automated cherry-pick of #50145
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
hawkingrei authored and ti-chi-bot committed Jan 10, 2024
1 parent 4a8c7b2 commit 6604bc1
Show file tree
Hide file tree
Showing 8 changed files with 770 additions and 57 deletions.
2 changes: 1 addition & 1 deletion pkg/planner/core/casetest/hint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ go_test(
],
data = glob(["testdata/**"]),
flaky = True,
shard_count = 6,
shard_count = 7,
deps = [
"//pkg/config",
"//pkg/domain",
Expand Down
26 changes: 26 additions & 0 deletions pkg/planner/core/casetest/hint/hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,29 @@ func TestOptimizeHintOnPartitionTable(t *testing.T) {
tk.MustQuery("SELECT /*+ MAX_EXECUTION_TIME(10), dtc(name=tt) unknow(t1,t2) */ SLEEP(5)").Check(testkit.Rows("0"))
require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 2)
}

func TestHints(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (a int);")
tk.MustExec("create table t2 (a int);")
tk.MustExec("create table t3 (a int);")
var input []string
var output []struct {
SQL string
Plan []string
Warn []string
}
integrationSuiteData := GetIntegrationSuiteData()
integrationSuiteData.LoadTestCases(t, &input, &output)
for i, tt := range input {
testdata.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = testdata.ConvertRowsToStrings(tk.MustQuery("explain format = 'brief' " + tt).Rows())
output[i].Warn = testdata.ConvertRowsToStrings(tk.MustQuery("show warnings").Rows())
})
tk.MustQuery("explain format = 'brief' " + tt).Check(testkit.Rows(output[i].Plan...))
tk.MustQuery("show warnings").Check(testkit.Rows(output[i].Warn...))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,11 @@
"explain format = 'brief' select /*+ use_index(t, idx)*/ * from t",
"explain format = 'brief' select /*+ use_index(t)*/ * from t"
]
},
{
"name": "TestHints",
"cases": [
"select * from t1, t2, t3 union all select /*+ leading(t3, t2) */ * from t1, t2, t3 union all select * from t1, t2, t3"
]
}
]
43 changes: 41 additions & 2 deletions pkg/planner/core/casetest/hint/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
],
"Warn": [
"[planner:1815]We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
"[planner:1815]leading hint is inapplicable, check if the leading hint table is valid"
]
},
{
Expand All @@ -347,7 +347,7 @@
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
],
"Warn": [
"[planner:1815]We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
"[planner:1815]leading hint is inapplicable, check if the leading hint table is valid"
]
},
{
Expand Down Expand Up @@ -1862,5 +1862,44 @@
"Warn": null
}
]
},
{
"Name": "TestHints",
"Cases": [
{
"SQL": "select * from t1, t2, t3 union all select /*+ leading(t3, t2) */ * from t1, t2, t3 union all select * from t1, t2, t3",
"Plan": [
"Union 3000000000000.00 root ",
"├─HashJoin 1000000000000.00 root CARTESIAN inner join",
"│ ├─TableReader(Build) 10000.00 root data:TableFullScan",
"│ │ └─TableFullScan 10000.00 cop[tikv] table:t3 keep order:false, stats:pseudo",
"│ └─HashJoin(Probe) 100000000.00 root CARTESIAN inner join",
"│ ├─TableReader(Build) 10000.00 root data:TableFullScan",
"│ │ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
"│ └─TableReader(Probe) 10000.00 root data:TableFullScan",
"│ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
"├─Projection 1000000000000.00 root test.t1.a->Column#19, test.t2.a->Column#20, test.t3.a->Column#21",
"│ └─HashJoin 1000000000000.00 root CARTESIAN inner join",
"│ ├─TableReader(Build) 10000.00 root data:TableFullScan",
"│ │ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
"│ └─HashJoin(Probe) 100000000.00 root CARTESIAN inner join",
"│ ├─TableReader(Build) 10000.00 root data:TableFullScan",
"│ │ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
"│ └─TableReader(Probe) 10000.00 root data:TableFullScan",
"│ └─TableFullScan 10000.00 cop[tikv] table:t3 keep order:false, stats:pseudo",
"└─HashJoin 1000000000000.00 root CARTESIAN inner join",
" ├─TableReader(Build) 10000.00 root data:TableFullScan",
" │ └─TableFullScan 10000.00 cop[tikv] table:t3 keep order:false, stats:pseudo",
" └─HashJoin(Probe) 100000000.00 root CARTESIAN inner join",
" ├─TableReader(Build) 10000.00 root data:TableFullScan",
" │ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
" └─TableReader(Probe) 10000.00 root data:TableFullScan",
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo"
],
"Warn": [
"Warning 1815 leading hint is inapplicable, check if the leading hint table has join conditions with other tables"
]
}
]
}
]
21 changes: 20 additions & 1 deletion pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4199,6 +4199,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev
b.ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.GenWithStack("We can only use the straight_join hint, when we use the leading hint and straight_join hint at the same time, all leading hints will be invalid"))
}
}
<<<<<<< HEAD
b.tableHintInfo = append(b.tableHintInfo, tableHintInfo{
sortMergeJoinTables: sortMergeTables,
broadcastJoinTables: bcTables,
Expand All @@ -4215,6 +4216,24 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev
indexMergeHintList: indexMergeHintList,
timeRangeHint: timeRangeHint,
limitHints: limitHints,
=======
b.tableHintInfo = append(b.tableHintInfo, &h.TableHintInfo{
SortMergeJoinTables: sortMergeTables,
BroadcastJoinTables: bcTables,
ShuffleJoinTables: shuffleJoinTables,
IndexNestedLoopJoinTables: h.IndexNestedLoopJoinTables{INLJTables: inljTables, INLHJTables: inlhjTables, INLMJTables: inlmjTables},
NoIndexJoinTables: h.IndexNestedLoopJoinTables{INLJTables: noIndexJoinTables, INLHJTables: noIndexHashJoinTables, INLMJTables: noIndexMergeJoinTables},
HashJoinTables: hashJoinTables,
NoHashJoinTables: noHashJoinTables,
NoMergeJoinTables: noMergeJoinTables,
IndexHintList: indexHintList,
TiFlashTables: tiflashTables,
TiKVTables: tikvTables,
AggHints: aggHints,
IndexMergeHintList: indexMergeHintList,
TimeRangeHint: timeRangeHint,
LimitHints: limitHints,
>>>>>>> c55105e0442 (planner: fix leading hint cannot take effect in UNION ALL statements (#50145))
MergeHints: MergeHints,
leadingJoinOrder: leadingJoinOrder,
hjBuildTables: hjBuildTables,
Expand Down Expand Up @@ -4298,7 +4317,7 @@ func (b *PlanBuilder) TableHints() *tableHintInfo {
if len(b.tableHintInfo) == 0 {
return nil
}
return &(b.tableHintInfo[len(b.tableHintInfo)-1])
return b.tableHintInfo[len(b.tableHintInfo)-1]
}

func (b *PlanBuilder) buildSelect(ctx context.Context, sel *ast.SelectStmt) (p LogicalPlan, err error) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,11 @@ type PlanBuilder struct {
colMapper map[*ast.ColumnNameExpr]int
// visitInfo is used for privilege check.
visitInfo []visitInfo
<<<<<<< HEAD
tableHintInfo []tableHintInfo
=======
tableHintInfo []*hint.TableHintInfo
>>>>>>> c55105e0442 (planner: fix leading hint cannot take effect in UNION ALL statements (#50145))
// optFlag indicates the flags of the optimizer rules.
optFlag uint64
// capFlag indicates the capability flags.
Expand Down
Loading

0 comments on commit 6604bc1

Please sign in to comment.