Skip to content

Commit 1b13108

Browse files
authored
planner: fix leading hint cannot take effect in UNION ALL statements (#50277) (#50322)
close #50067
1 parent 061a7da commit 1b13108

File tree

5 files changed

+51
-60
lines changed

5 files changed

+51
-60
lines changed

planner/core/casetest/testdata/integration_suite_out.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6134,7 +6134,7 @@
61346134
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
61356135
],
61366136
"Warn": [
6137-
"[planner:1815]We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
6137+
"[planner:1815]leading hint is inapplicable, check if the leading hint table is valid"
61386138
]
61396139
},
61406140
{
@@ -6155,7 +6155,7 @@
61556155
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
61566156
],
61576157
"Warn": [
6158-
"[planner:1815]We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
6158+
"[planner:1815]leading hint is inapplicable, check if the leading hint table is valid"
61596159
]
61606160
},
61616161
{

planner/core/casetest/testdata/join_reorder_suite_out.json

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,6 @@
922922
}
923923
]
924924
},
925-
926925
{
927926
"Name": "TestNoIndexJoinHint",
928927
"Cases": [
@@ -7970,24 +7969,21 @@
79707969
{
79717970
"SQL": "select /*+ leading(t2, t3@sel_2) */ * from t1 join t2 on t1.a=t2.a where t1.a in (select t3.a from t3)",
79727971
"Plan": [
7973-
"IndexHashJoin 4.69 root inner join, inner:IndexLookUp, outer key:test.t1.a, inner key:test.t2.a, equal cond:eq(test.t1.a, test.t2.a)",
7974-
"├─IndexHashJoin(Build) 3.75 root inner join, inner:IndexLookUp, outer key:test.t3.a, inner key:test.t1.a, equal cond:eq(test.t3.a, test.t1.a)",
7975-
"│ ├─StreamAgg(Build) 3.00 root group by:test.t3.a, funcs:firstrow(test.t3.a)->test.t3.a",
7976-
"│ │ └─IndexReader 3.00 root index:StreamAgg",
7977-
"│ │ └─StreamAgg 3.00 cop[tikv] group by:test.t3.a, ",
7978-
"│ │ └─IndexFullScan 3.00 cop[tikv] table:t3, index:a(a) keep order:true",
7979-
"│ └─IndexLookUp(Probe) 3.75 root ",
7980-
"│ ├─Selection(Build) 3.75 cop[tikv] not(isnull(test.t1.a))",
7981-
"│ │ └─IndexRangeScan 3.75 cop[tikv] table:t1, index:a(a) range: decided by [eq(test.t1.a, test.t3.a)], keep order:false, stats:pseudo",
7982-
"│ └─TableRowIDScan(Probe) 3.75 cop[tikv] table:t1 keep order:false, stats:pseudo",
7983-
"└─IndexLookUp(Probe) 4.69 root ",
7984-
" ├─Selection(Build) 4.69 cop[tikv] not(isnull(test.t2.a))",
7985-
" │ └─IndexRangeScan 4.69 cop[tikv] table:t2, index:a(a) range: decided by [eq(test.t2.a, test.t1.a)], keep order:false, stats:pseudo",
7986-
" └─TableRowIDScan(Probe) 4.69 cop[tikv] table:t2 keep order:false, stats:pseudo"
7972+
"Projection 37462.50 root test.t1.a, test.t1.b, test.t2.a, test.t2.b",
7973+
"└─HashJoin 37462.50 root inner join, equal:[eq(test.t2.a, test.t1.a) eq(test.t3.a, test.t1.a)]",
7974+
" ├─TableReader(Build) 9990.00 root data:Selection",
7975+
" │ └─Selection 9990.00 cop[tikv] not(isnull(test.t1.a))",
7976+
" │ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
7977+
" └─HashJoin(Probe) 29970.00 root CARTESIAN inner join",
7978+
" ├─StreamAgg(Build) 3.00 root group by:test.t3.a, funcs:firstrow(test.t3.a)->test.t3.a",
7979+
" │ └─IndexReader 3.00 root index:StreamAgg",
7980+
" │ └─StreamAgg 3.00 cop[tikv] group by:test.t3.a, ",
7981+
" │ └─IndexFullScan 3.00 cop[tikv] table:t3, index:a(a) keep order:true",
7982+
" └─TableReader(Probe) 9990.00 root data:Selection",
7983+
" └─Selection 9990.00 cop[tikv] not(isnull(test.t2.a))",
7984+
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
79877985
],
7988-
"Warning": [
7989-
"Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
7990-
]
7986+
"Warning": null
79917987
},
79927988
{
79937989
"SQL": "select /*+ leading(t1, t3@sel_2) */ * from t1 join t2 on t1.a=t2.a where t1.a in (select t3.a from t3)",
@@ -8007,31 +8003,26 @@
80078003
" │ └─IndexRangeScan 4.69 cop[tikv] table:t2, index:a(a) range: decided by [eq(test.t2.a, test.t1.a)], keep order:false, stats:pseudo",
80088004
" └─TableRowIDScan(Probe) 4.69 cop[tikv] table:t2 keep order:false, stats:pseudo"
80098005
],
8010-
"Warning": [
8011-
"Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
8012-
]
8006+
"Warning": null
80138007
},
80148008
{
80158009
"SQL": "select /*+ leading(t3@sel_2, t2) */ * from t1 join t2 on t1.a=t2.a where t1.a in (select t3.a from t3)",
80168010
"Plan": [
8017-
"IndexHashJoin 4.69 root inner join, inner:IndexLookUp, outer key:test.t1.a, inner key:test.t2.a, equal cond:eq(test.t1.a, test.t2.a)",
8018-
"├─IndexHashJoin(Build) 3.75 root inner join, inner:IndexLookUp, outer key:test.t3.a, inner key:test.t1.a, equal cond:eq(test.t3.a, test.t1.a)",
8019-
"│ ├─StreamAgg(Build) 3.00 root group by:test.t3.a, funcs:firstrow(test.t3.a)->test.t3.a",
8020-
"│ │ └─IndexReader 3.00 root index:StreamAgg",
8021-
"│ │ └─StreamAgg 3.00 cop[tikv] group by:test.t3.a, ",
8022-
"│ │ └─IndexFullScan 3.00 cop[tikv] table:t3, index:a(a) keep order:true",
8023-
"│ └─IndexLookUp(Probe) 3.75 root ",
8024-
"│ ├─Selection(Build) 3.75 cop[tikv] not(isnull(test.t1.a))",
8025-
"│ │ └─IndexRangeScan 3.75 cop[tikv] table:t1, index:a(a) range: decided by [eq(test.t1.a, test.t3.a)], keep order:false, stats:pseudo",
8026-
"│ └─TableRowIDScan(Probe) 3.75 cop[tikv] table:t1 keep order:false, stats:pseudo",
8027-
"└─IndexLookUp(Probe) 4.69 root ",
8028-
" ├─Selection(Build) 4.69 cop[tikv] not(isnull(test.t2.a))",
8029-
" │ └─IndexRangeScan 4.69 cop[tikv] table:t2, index:a(a) range: decided by [eq(test.t2.a, test.t1.a)], keep order:false, stats:pseudo",
8030-
" └─TableRowIDScan(Probe) 4.69 cop[tikv] table:t2 keep order:false, stats:pseudo"
8011+
"Projection 37462.50 root test.t1.a, test.t1.b, test.t2.a, test.t2.b",
8012+
"└─HashJoin 37462.50 root inner join, equal:[eq(test.t2.a, test.t1.a) eq(test.t3.a, test.t1.a)]",
8013+
" ├─TableReader(Build) 9990.00 root data:Selection",
8014+
" │ └─Selection 9990.00 cop[tikv] not(isnull(test.t1.a))",
8015+
" │ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
8016+
" └─HashJoin(Probe) 29970.00 root CARTESIAN inner join",
8017+
" ├─StreamAgg(Build) 3.00 root group by:test.t3.a, funcs:firstrow(test.t3.a)->test.t3.a",
8018+
" │ └─IndexReader 3.00 root index:StreamAgg",
8019+
" │ └─StreamAgg 3.00 cop[tikv] group by:test.t3.a, ",
8020+
" │ └─IndexFullScan 3.00 cop[tikv] table:t3, index:a(a) keep order:true",
8021+
" └─TableReader(Probe) 9990.00 root data:Selection",
8022+
" └─Selection 9990.00 cop[tikv] not(isnull(test.t2.a))",
8023+
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
80318024
],
8032-
"Warning": [
8033-
"Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
8034-
]
8025+
"Warning": null
80358026
},
80368027
{
80378028
"SQL": "select /*+ leading(t3@sel_2, t1) */ * from t1 join t2 on t1.a=t2.a where t1.a in (select t3.a from t3)",
@@ -8051,9 +8042,7 @@
80518042
" │ └─IndexRangeScan 4.69 cop[tikv] table:t2, index:a(a) range: decided by [eq(test.t2.a, test.t1.a)], keep order:false, stats:pseudo",
80528043
" └─TableRowIDScan(Probe) 4.69 cop[tikv] table:t2 keep order:false, stats:pseudo"
80538044
],
8054-
"Warning": [
8055-
"Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
8056-
]
8045+
"Warning": null
80578046
},
80588047
{
80598048
"SQL": "select /*+ leading(t4) */ * from t1 join t2 on t1.a=t2.a join t4 on t1.b = t4.b where t1.a not in (select t3.a from t3)",
@@ -10036,22 +10025,21 @@
1003610025
{
1003710026
"SQL": "select /*+ leading(t2, t3@sel_2) */ * from t1 join t2 on t1.a=t2.a where t1.a in (select t3.a from t3)",
1003810027
"Plan": [
10039-
"HashJoin 12487.50 root inner join, equal:[eq(test.t1.a, test.t3.a)]",
10040-
"├─StreamAgg(Build) 7992.00 root group by:test.t3.a, funcs:firstrow(test.t3.a)->test.t3.a",
10041-
"│ └─IndexReader 7992.00 root index:StreamAgg",
10042-
"│ └─StreamAgg 7992.00 cop[tikv] group by:test.t3.a, ",
10043-
"│ └─IndexFullScan 9990.00 cop[tikv] table:t3, index:a(a) keep order:true, stats:pseudo",
10044-
"└─HashJoin(Probe) 12487.50 root inner join, equal:[eq(test.t1.a, test.t2.a)]",
10028+
"Projection 99800100.00 root test.t1.a, test.t1.b, test.t2.a, test.t2.b",
10029+
"└─HashJoin 99800100.00 root inner join, equal:[eq(test.t2.a, test.t1.a) eq(test.t3.a, test.t1.a)]",
1004510030
" ├─TableReader(Build) 9990.00 root data:Selection",
10046-
" │ └─Selection 9990.00 cop[tikv] not(isnull(test.t2.a))",
10047-
" │ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
10048-
" └─TableReader(Probe) 9990.00 root data:Selection",
10049-
" └─Selection 9990.00 cop[tikv] not(isnull(test.t1.a))",
10050-
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo"
10031+
" │ └─Selection 9990.00 cop[tikv] not(isnull(test.t1.a))",
10032+
" │ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
10033+
" └─HashJoin(Probe) 79840080.00 root CARTESIAN inner join",
10034+
" ├─StreamAgg(Build) 7992.00 root group by:test.t3.a, funcs:firstrow(test.t3.a)->test.t3.a",
10035+
" │ └─IndexReader 7992.00 root index:StreamAgg",
10036+
" │ └─StreamAgg 7992.00 cop[tikv] group by:test.t3.a, ",
10037+
" │ └─IndexFullScan 9990.00 cop[tikv] table:t3, index:a(a) keep order:true, stats:pseudo",
10038+
" └─TableReader(Probe) 9990.00 root data:Selection",
10039+
" └─Selection 9990.00 cop[tikv] not(isnull(test.t2.a))",
10040+
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
1005110041
],
10052-
"Warning": [
10053-
"Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
10054-
]
10042+
"Warning": null
1005510043
},
1005610044
{
1005710045
"SQL": "select /*+ leading(t4) */ * from t1 left join t2 on t1.a=t2.a right join t4 on t1.b = t4.b where t1.a not in (select t3.a from t3)",

planner/core/logical_plan_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3933,7 +3933,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev
39333933
b.ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.FastGen("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"))
39343934
}
39353935
}
3936-
b.tableHintInfo = append(b.tableHintInfo, tableHintInfo{
3936+
b.tableHintInfo = append(b.tableHintInfo, &tableHintInfo{
39373937
sortMergeJoinTables: sortMergeTables,
39383938
broadcastJoinTables: bcTables,
39393939
shuffleJoinTables: shuffleJoinTables,
@@ -4032,7 +4032,7 @@ func (b *PlanBuilder) TableHints() *tableHintInfo {
40324032
if len(b.tableHintInfo) == 0 {
40334033
return nil
40344034
}
4035-
return &(b.tableHintInfo[len(b.tableHintInfo)-1])
4035+
return b.tableHintInfo[len(b.tableHintInfo)-1]
40364036
}
40374037

40384038
func (b *PlanBuilder) buildSelect(ctx context.Context, sel *ast.SelectStmt) (p LogicalPlan, err error) {

planner/core/planbuilder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ type PlanBuilder struct {
528528
colMapper map[*ast.ColumnNameExpr]int
529529
// visitInfo is used for privilege check.
530530
visitInfo []visitInfo
531-
tableHintInfo []tableHintInfo
531+
tableHintInfo []*tableHintInfo
532532
// optFlag indicates the flags of the optimizer rules.
533533
optFlag uint64
534534
// capFlag indicates the capability flags.

planner/core/rule_join_reorder_greedy.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func (s *joinReorderGreedySolver) solve(joinNodePlans []LogicalPlan, tracer *joi
7979
// Getting here means that there is no join condition between the table used in the leading hint and other tables
8080
// For example: select /*+ leading(t3) */ * from t1 join t2 on t1.a=t2.a cross join t3
8181
// We can not let table t3 join first.
82+
// TODO(hawkingrei): we find the problem in the TestHint.
83+
// `select * from t1, t2, t3 union all select /*+ leading(t3, t2) */ * from t1, t2, t3 union all select * from t1, t2, t3`
84+
// this sql should not return the warning. but It will not affect the result. so we will fix it as soon as possible.
8285
s.ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.FastGen("leading hint is inapplicable, check if the leading hint table has join conditions with other tables"))
8386
}
8487
cartesianGroup = append(cartesianGroup, newNode.p)

0 commit comments

Comments
 (0)