Skip to content

Commit

Permalink
hint: avoid duplicate hints in QBHintHandler (#53921) (#53937)
Browse files Browse the repository at this point in the history
close #53767
  • Loading branch information
ti-chi-bot authored Jun 11, 2024
1 parent b7a6c25 commit eabd1de
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 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 = 7,
shard_count = 8,
deps = [
"//pkg/config",
"//pkg/domain",
Expand Down
12 changes: 12 additions & 0 deletions pkg/planner/core/casetest/hint/hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,15 @@ func TestHints(t *testing.T) {
tk.MustQuery("show warnings").Check(testkit.Rows(output[i].Warn...))
}
}

func TestQBHintHandlerDuplicateObjects(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE t_employees (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, fname VARCHAR(25) NOT NULL, lname VARCHAR(25) NOT NULL, store_id INT NOT NULL, department_id INT NOT NULL);")
tk.MustExec("ALTER TABLE t_employees ADD INDEX idx(department_id);")

// Explain statement
tk.MustQuery("EXPLAIN WITH t AS (SELECT /*+ inl_join(e) */ em.* FROM t_employees em JOIN t_employees e WHERE em.store_id = e.department_id) SELECT * FROM t;")
tk.MustQuery("show warnings").Check(testkit.Rows())
}
Original file line number Diff line number Diff line change
Expand Up @@ -1449,10 +1449,7 @@
" └─IndexRangeScan_148 19492.21 cop[tikv] table:t1, index:idx_a(a) range: decided by [eq(test.t1.a, test.t1.a)], 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]There are no matching table names for (t1) in optimizer hint /*+ INL_JOIN(t1, t1) */ or /*+ TIDB_INLJ(t1, t1) */. Maybe you can use the table alias name",
"[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]There are no matching table names for (t1, t1) in optimizer hint /*+ INL_JOIN(t1, t1, t1) */ or /*+ TIDB_INLJ(t1, t1, t1) */. Maybe you can use the table alias name"
"[planner:1815]We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
]
},
{
Expand Down Expand Up @@ -1484,10 +1481,7 @@
" └─TableRowIDScan_69 9990.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]There are no matching table names for (t2) in optimizer hint /*+ MERGE_JOIN(t2, t2) */ or /*+ TIDB_SMJ(t2, t2) */. Maybe you can use the table alias name",
"[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]There are no matching table names for (t2, t2) in optimizer hint /*+ MERGE_JOIN(t2, t2, t2) */ or /*+ TIDB_SMJ(t2, t2, t2) */. Maybe you can use the table alias name"
"[planner:1815]We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,7 @@
" └─Selection 9990.00 mpp[tiflash] not(isnull(test.t.a))",
" └─TableFullScan 10000.00 mpp[tiflash] table:t1 pushed down filter:empty, keep order:false, stats:pseudo"
],
"Warn": [
"[planner:1815]There are no matching table names for (t1, t) in optimizer hint /*+ SHUFFLE_JOIN(t1, t, t1, t) */ or /*+ SHUFFLE_JOIN(t1, t, t1, t) */. Maybe you can use the table alias name",
"[planner:1815]There are no matching table names for (t1, t, t1, t) in optimizer hint /*+ SHUFFLE_JOIN(t1, t, t1, t, t1, t) */ or /*+ SHUFFLE_JOIN(t1, t, t1, t, t1, t) */. Maybe you can use the table alias name"
]
"Warn": null
},
{
"SQL": "WITH CTE AS (SELECT /*+ MERGE(), broadcast_join(t1, t) */ t.a, t.b FROM t join t t1 where t.a = t1.a) SELECT * FROM CTE WHERE CTE.a <18 union select * from cte where cte.b > 1;",
Expand Down Expand Up @@ -868,10 +865,7 @@
" └─Selection(Probe) 9990.00 mpp[tiflash] not(isnull(test.t.a))",
" └─TableFullScan 10000.00 mpp[tiflash] table:t1 pushed down filter:empty, keep order:false, stats:pseudo"
],
"Warn": [
"[planner:1815]There are no matching table names for (t1, t) in optimizer hint /*+ BROADCAST_JOIN(t1, t, t1, t) */ or /*+ TIDB_BCJ(t1, t, t1, t) */. Maybe you can use the table alias name",
"[planner:1815]There are no matching table names for (t1, t, t1, t) in optimizer hint /*+ BROADCAST_JOIN(t1, t, t1, t, t1, t) */ or /*+ TIDB_BCJ(t1, t, t1, t, t1, t) */. Maybe you can use the table alias name"
]
"Warn": null
},
{
"SQL": "select /*+ read_from_storage(tiflash[t1, t2]), broadcast_join(t1, t2), hash_join_build(t2) */ * from t t1 left join t t2 on t1.a=t2.a",
Expand Down
5 changes: 4 additions & 1 deletion pkg/util/hint/hint_query_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package hint

import (
"fmt"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -295,7 +296,9 @@ func (p *QBHintHandler) GetCurrentStmtHints(hints []*ast.TableOptimizerHint, cur
}
continue
}
p.QBOffsetToHints[offset] = append(p.QBOffsetToHints[offset], hint)
if !slices.Contains(p.QBOffsetToHints[offset], hint) {
p.QBOffsetToHints[offset] = append(p.QBOffsetToHints[offset], hint)
}
}
return p.QBOffsetToHints[currentOffset]
}
Expand Down
1 change: 0 additions & 1 deletion tests/integrationtest/r/index_merge.result
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,6 @@ show warnings;
Level Code Message
Warning 1815 use_index_merge(index_merge.t_alias) is inapplicable, check whether the table(index_merge.t_alias) exists
Warning 1815 use_index_merge(index_merge.t_alias) is inapplicable, check whether the table(index_merge.t_alias) exists
Warning 1815 use_index_merge(index_merge.t_alias) is inapplicable, check whether the table(index_merge.t_alias) exists
with recursive cte1 as (select 1 c1, 1 c2, 1 c3 UNION ALL select /*+ use_index_merge(t_alias) */ c1 + 1, c2 + 1, c3 + 1 from cte1 t_alias where c1 < 10 or c2 < 10 and c3 < 10) select * from cte1 order by 1;
c1 c2 c3
1 1 1
Expand Down

0 comments on commit eabd1de

Please sign in to comment.