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: add more test cases for join order hint #34740

Merged
merged 56 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
83a054c
planner: refactor the join reorder codes
Reminiscent May 5, 2022
b872695
fix ut
Reminiscent May 5, 2022
427dc16
Merge branch 'master' of github.com:pingcap/tidb into JoinOrderRefactor
Reminiscent May 5, 2022
3d7ef8b
Merge branch 'master' of github.com:pingcap/tidb into JoinOrderRefactor
Reminiscent May 11, 2022
5bf862b
resolve conflicts
Reminiscent May 11, 2022
a46ada2
Merge branch 'master' of github.com:pingcap/tidb into JoinOrderRefactor
Reminiscent May 11, 2022
e7ded10
add more comments
Reminiscent May 11, 2022
8631387
planner: support leading hints in join reorder optimization
Reminiscent May 11, 2022
cdb8639
fix ut
Reminiscent May 11, 2022
5402c20
Merge branch 'master' of github.com:pingcap/tidb into planner-support…
Reminiscent May 11, 2022
f6f1eca
Merge branch 'master' of github.com:pingcap/tidb into planner-support…
Reminiscent May 12, 2022
88a2bb1
fix check_dev
Reminiscent May 12, 2022
919628c
add more test cases
Reminiscent May 12, 2022
867e379
Merge branch 'master' of github.com:pingcap/tidb into planner-support…
Reminiscent May 12, 2022
3226deb
fix ut
Reminiscent May 12, 2022
098ef46
fix ut
Reminiscent May 12, 2022
1bd70df
add the warning messages for the test cases
Reminiscent May 13, 2022
2ec1a7c
fix ut and address comments
Reminiscent May 13, 2022
150ad6f
Merge branch 'master' of github.com:pingcap/tidb into planner-support…
Reminiscent May 13, 2022
bae8681
fix test
Reminiscent May 13, 2022
b3e0ed9
Merge branch 'master' of github.com:pingcap/tidb into planner-support…
Reminiscent May 13, 2022
8bf2548
fix ut
Reminiscent May 13, 2022
1fa1c8c
forbid outer join
Reminiscent May 16, 2022
cf3b5a4
Merge branch 'master' of github.com:pingcap/tidb into planner-support…
Reminiscent May 16, 2022
2ad7cf9
address comments
Reminiscent May 16, 2022
34ad152
Merge branch 'master' of github.com:pingcap/tidb into planner-support…
Reminiscent May 16, 2022
38fdc63
planner: add more test cases for join order hint
Reminiscent May 16, 2022
6b3793f
planner: add more test cases for join order hint
Reminiscent May 17, 2022
65c9b13
Merge branch 'master' of github.com:pingcap/tidb into leadingHintTest…
Reminiscent May 17, 2022
cf00fbb
revert
Reminiscent May 17, 2022
e880d70
rename
Reminiscent May 17, 2022
a54c4c9
add test cases for tiflash
Reminiscent May 17, 2022
cf8fa62
format
Reminiscent May 18, 2022
805e56f
Merge branch 'master' of github.com:pingcap/tidb into leadingHintTest…
Reminiscent May 18, 2022
544d123
add test cases for subquery
Reminiscent May 18, 2022
5811514
Merge branch 'master' of github.com:pingcap/tidb into leadingHintTest…
Reminiscent May 18, 2022
be2e773
refactor the code
Reminiscent May 18, 2022
820273a
Merge branch 'master' into leadingHintTestCases
qw4990 May 18, 2022
8aa6eee
add more comments
Reminiscent May 18, 2022
3af5116
add more test cases for join algorithm
Reminiscent May 18, 2022
8799ae7
add more test cases for different join type
Reminiscent May 18, 2022
ac191a6
add more test cases for different join types
Reminiscent May 18, 2022
1705fac
Merge branch 'master' of github.com:pingcap/tidb into leadingHintTest…
Reminiscent May 18, 2022
85c4357
Merge remote-tracking branch 'origin/leadingHintTestCases' into leadi…
Reminiscent May 18, 2022
56b83ad
fix ut
Reminiscent May 18, 2022
327da4d
update the test results
Reminiscent May 18, 2022
1fb7e1d
update the test results
Reminiscent May 18, 2022
ed2f083
update the test cases
Reminiscent May 18, 2022
c46c495
fix ut
Reminiscent May 19, 2022
1a2b0d1
update the test results
Reminiscent May 19, 2022
a8d416b
Merge branch 'master' of github.com:pingcap/tidb into leadingHintTest…
Reminiscent May 19, 2022
5636cc6
update test cases
Reminiscent May 19, 2022
ed7f2c1
update the test results
Reminiscent May 19, 2022
93faa0b
Merge branch 'master' into leadingHintTestCases
ti-chi-bot May 19, 2022
5555e7c
Merge branch 'master' into leadingHintTestCases
ti-chi-bot May 19, 2022
e5d8157
Merge branch 'master' into leadingHintTestCases
ti-chi-bot May 19, 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
12 changes: 7 additions & 5 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3630,12 +3630,14 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev
// ignore hints that not implemented
}
}
if leadingHintCnt > 1 {
// If there are more leading hints, all leading hints will be invalid.
if leadingHintCnt > 1 || (leadingHintCnt > 0 && b.ctx.GetSessionVars().StmtCtx.StraightJoinOrder) {
// If there are more leading hints or the straight_join hint existes, all leading hints will be invalid.
leadingJoinOrder = leadingJoinOrder[:0]
// Append warning if there are invalid index names.
errMsg := "We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"
b.ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.GenWithStack(errMsg))
if leadingHintCnt > 1 {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.GenWithStack("We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"))
} else if b.ctx.GetSessionVars().StmtCtx.StraightJoinOrder {
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"))
}
}
b.tableHintInfo = append(b.tableHintInfo, tableHintInfo{
sortMergeJoinTables: sortMergeTables,
Expand Down
44 changes: 34 additions & 10 deletions planner/core/rule_join_reorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ import (
// For example: "InnerJoin(InnerJoin(a, b), LeftJoin(c, d))"
// results in a join group {a, b, c, d}.
func extractJoinGroup(p LogicalPlan) (group []LogicalPlan, eqEdges []*expression.ScalarFunction,
otherConds []expression.Expression, joinTypes []JoinType, hintInfo *tableHintInfo, hasOuterJoin bool) {
otherConds []expression.Expression, joinTypes []JoinType, hintInfo []*tableHintInfo, hasOuterJoin bool) {
join, isJoin := p.(*LogicalJoin)
if !isJoin || join.preferJoinType > uint(0) || join.StraightJoin ||
(join.JoinType != InnerJoin && join.JoinType != LeftOuterJoin && join.JoinType != RightOuterJoin) ||
((join.JoinType == LeftOuterJoin || join.JoinType == RightOuterJoin) && join.EqualConditions == nil) {
return []LogicalPlan{p}, nil, nil, nil, nil, false
}
if join.preferJoinOrder {
hintInfo = join.hintInfo
hintInfo = append(hintInfo, join.hintInfo)
}
hasOuterJoin = hasOuterJoin || (join.JoinType != InnerJoin)
if join.JoinType != RightOuterJoin {
Expand Down Expand Up @@ -75,9 +75,7 @@ func extractJoinGroup(p LogicalPlan) (group []LogicalPlan, eqEdges []*expression
eqEdges = append(eqEdges, lhsEqualConds...)
otherConds = append(otherConds, lhsOtherConds...)
joinTypes = append(joinTypes, lhsJoinTypes...)
if hintInfo == nil && lhsHintInfo != nil {
hintInfo = lhsHintInfo
}
hintInfo = append(hintInfo, lhsHintInfo...)
hasOuterJoin = hasOuterJoin || lhsHasOuterJoin
} else {
group = append(group, join.children[0])
Expand Down Expand Up @@ -113,9 +111,7 @@ func extractJoinGroup(p LogicalPlan) (group []LogicalPlan, eqEdges []*expression
eqEdges = append(eqEdges, rhsEqualConds...)
otherConds = append(otherConds, rhsOtherConds...)
joinTypes = append(joinTypes, rhsJoinTypes...)
if hintInfo == nil && rhsHintInfo != nil {
hintInfo = rhsHintInfo
}
hintInfo = append(hintInfo, rhsHintInfo...)
hasOuterJoin = hasOuterJoin || rhsHasOuterJoin
} else {
group = append(group, join.children[1])
Expand Down Expand Up @@ -181,12 +177,17 @@ func (s *joinReOrderSolver) optimizeRecursive(ctx sessionctx.Context, p LogicalP
joinGroupNum := len(curJoinGroup)
useGreedy := joinGroupNum > ctx.GetSessionVars().TiDBOptJoinReorderThreshold || !isSupportDP

if hintInfo != nil && hintInfo.leadingJoinOrder != nil {
leadingHintInfo, hasDiffLeadingHint := checkAndGenerateLeadingHint(hintInfo)
if hasDiffLeadingHint {
ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.GenWithStack("We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"))
}

if leadingHintInfo != nil && leadingHintInfo.leadingJoinOrder != nil {
if hasOuterJoin {
ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.GenWithStack("leading hint is inapplicable when we have outer join"))
} else {
if useGreedy {
ok, leftJoinGroup := baseGroupSolver.generateLeadingJoinGroup(curJoinGroup, hintInfo)
ok, leftJoinGroup := baseGroupSolver.generateLeadingJoinGroup(curJoinGroup, leadingHintInfo)
if !ok {
ctx.GetSessionVars().StmtCtx.AppendWarning(ErrInternal.GenWithStack("leading hint is inapplicable, check if the leading hint table is valid"))
} else {
Expand Down Expand Up @@ -246,6 +247,29 @@ func (s *joinReOrderSolver) optimizeRecursive(ctx sessionctx.Context, p LogicalP
return p, nil
}

// checkAndGenerateLeadingHint used to check and generate the valid leading hint.
// We are allowed to use at most one leading hint in a join group. When more than one,
// all leading hints in the current join group will be invalid.
func checkAndGenerateLeadingHint(hintInfo []*tableHintInfo) (*tableHintInfo, bool) {
leadingHintNum := len(hintInfo)
var leadingHintInfo *tableHintInfo
hasDiffLeadingHint := false
if leadingHintNum > 0 {
leadingHintInfo = hintInfo[0]
// One join group has one leading hint at most. Check whether there are different join order hints.
for i := 1; i < leadingHintNum; i++ {
if hintInfo[i] != hintInfo[i-1] {
hasDiffLeadingHint = true
break
}
}
if hasDiffLeadingHint {
leadingHintInfo = nil
}
}
return leadingHintInfo, hasDiffLeadingHint
}

// nolint:structcheck
type baseSingleGroupJoinOrderSolver struct {
ctx sessionctx.Context
Expand Down
230 changes: 230 additions & 0 deletions planner/core/rule_join_reorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package core_test
import (
"testing"

"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/parser/model"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/testdata"
Expand Down Expand Up @@ -97,3 +99,231 @@ func TestLeadingJoinHint(t *testing.T) {
tk.MustExec("select /*+ leading(t1) leading(t2) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"))
}

func TestJoinOrderHint(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1, t2, t3, t4, t5, t6, t7, t8;")
tk.MustExec("create table t(a int, b int, key(a));")
tk.MustExec("create table t1(a int, b int, key(a));")
tk.MustExec("create table t2(a int, b int, key(a));")
tk.MustExec("create table t3(a int, b int, key(a));")
tk.MustExec("create table t4(a int, b int, key(a));")
tk.MustExec("create table t5(a int, b int, key(a));")
tk.MustExec("create table t6(a int, b int, key(a));")
tk.MustExec("create table t7(a int, b int, key(a));")
tk.MustExec("create table t8(a int, b int, key(a));")

// test cases for using the leading hint and straight_join hint at the same time
tk.MustExec("select /*+ leading(t1) straight_join() */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 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"))

tk.MustExec("select /*+ straight_join() leading(t1) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 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"))

// more join order hints appear in the same time
tk.MustExec("select /*+ leading(t1) leading(t1) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"))

tk.MustExec("select /*+ leading(t1) leading(t2) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"))

tk.MustExec("select /*+ straight_join() straight_join() */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 STRAIGHT_JOIN() is defined more than once, only the last definition takes effect"))

// test cases for table name in hint
// the same table appears in the leading hint
tk.MustExec("select /*+ leading(t1, t1) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t1) in optimizer hint /*+ LEADING(t1, t1) */. Maybe you can use the table alias name",
"Warning 1815 leading hint is inapplicable, check if the leading hint table is valid"))

tk.MustExec("select /*+ leading(t1, t2, t1) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t1) in optimizer hint /*+ LEADING(t1, t2, t1) */. Maybe you can use the table alias name",
"Warning 1815 leading hint is inapplicable, check if the leading hint table is valid"))

// the wrong table appears in the leading hint
tk.MustExec("select /*+ leading(t) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t) in optimizer hint /*+ LEADING(t) */. Maybe you can use the table alias name"))

tk.MustExec("select /*+ leading(t1, t2, t) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t) in optimizer hint /*+ LEADING(t1, t2, t) */. Maybe you can use the table alias name",
"Warning 1815 leading hint is inapplicable, check if the leading hint table is valid"))

// table alias in the leading hint
tk.MustExec("select /*+ leading(t) */ * from t1 t join t2 on t.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows())

tk.MustExec("select /*+ leading(t1) */ * from t1 t join t2 on t.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t1) in optimizer hint /*+ LEADING(t1) */. Maybe you can use the table alias name"))

tk.MustExec("select /*+ leading(t2, t) */ * from t1 t join t2 on t.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows())

tk.MustExec("select /*+ leading(t2, t1) */ * from t1 t join t2 on t.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t1) in optimizer hint /*+ LEADING(t2, t1) */. Maybe you can use the table alias name",
"Warning 1815 leading hint is inapplicable, check if the leading hint table is valid"))

// conflict between table names
tk.MustExec("select /*+ leading(t3) */ * from t1 join t2 on t1.a=t2.a left join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 leading hint is inapplicable when we have outer join"))

tk.MustExec("select /*+ leading(t1, t3) */ * from t1 join t2 on t1.a=t2.a left join t3 on t2.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 leading hint is inapplicable when we have outer join"))

// table name in leading hint cross query block
// Todo: Can not handle this case yet. Because when we extract the join group, it will get the join group {t1, t2, t3}.
// So the table 't4' can not be used.
tk.MustExec("select /*+ leading(t4) */ * from (select t2.b from t1 join t2 on t1.a=t2.a) t4 join t3 on t4.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 leading hint is inapplicable, check if the leading hint table is valid"))

tk.MustExec("select /*+ leading(t3, t2@sel_2) */ * from (select t2.b from t1 join t2 on t1.a=t2.a) t4 join t3 on t4.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t2) in optimizer hint /*+ LEADING(t3, t2) */. Maybe you can use the table alias name"))

tk.MustExec("select * from (select /*+ leading(t1, t3@sel_1) */ t2.b from t1 join t2 on t1.a=t2.a) t4 join t3 on t4.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 There are no matching table names for (t3) in optimizer hint /*+ LEADING(t1, t3) */. Maybe you can use the table alias name"))

tk.MustExec("select /*+ leading(t3) */ * from (select /*+ leading(t1) */ t2.b from t1 join t2 on t1.a=t2.a) t4 join t3 on t4.b=t3.b")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 We can only use one leading hint at most, when multiple leading hints are used, all leading hints will be invalid"))

runJoinReorderTestData(t, tk, "TestJoinOrderHint")
}

func TestJoinOrderHintWithBinding(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1, t2, t3;")
tk.MustExec("create table t(a int, b int, key(a));")
tk.MustExec("create table t1(a int, b int, key(a));")
tk.MustExec("create table t2(a int, b int, key(a));")
tk.MustExec("create table t3(a int, b int, key(a));")

tk.MustExec("select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("0"))
tk.MustExec("create global binding for select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b using select /*+ straight_join() */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustExec("select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1"))
res := tk.MustQuery("show global bindings").Rows()
require.Equal(t, res[0][0], "select * from ( `test` . `t1` join `test` . `t2` on `t1` . `a` = `t2` . `a` ) join `test` . `t3` on `t2` . `b` = `t3` . `b`", "SELECT /*+ straight_join()*/ * FROM (`test`.`t1` JOIN `test`.`t2` ON `t1`.`a` = `t2`.`a`) JOIN `test`.`t3` ON `t2`.`b` = `t3`.`b`")

tk.MustExec("drop global binding for select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustExec("select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("0"))
res = tk.MustQuery("show global bindings").Rows()
require.Equal(t, len(res), 0)

tk.MustExec("create global binding for select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b using select /*+ leading(t3) */ * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustExec("select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1"))
res = tk.MustQuery("show global bindings").Rows()
require.Equal(t, res[0][0], "select * from ( `test` . `t1` join `test` . `t2` on `t1` . `a` = `t2` . `a` ) join `test` . `t3` on `t2` . `b` = `t3` . `b`")

tk.MustExec("drop global binding for select * from t1 join t2 on t1.a=t2.a join t3 on t2.b=t3.b")
}

func TestJoinOrderHint4StaticPartitionTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1, t2, t3;")
tk.MustExec(`create table t(a int, b int) partition by hash(a) partitions 3`)
tk.MustExec(`create table t1(a int, b int) partition by hash(a) partitions 4`)
tk.MustExec(`create table t2(a int, b int) partition by hash(a) partitions 5`)
tk.MustExec(`create table t3(a int, b int) partition by hash(b) partitions 3`)

tk.MustExec(`set @@tidb_partition_prune_mode="static"`)
runJoinReorderTestData(t, tk, "TestJoinOrderHint4StaticPartitionTable")
}

func TestJoinOrderHint4DynamicPartitionTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1, t2, t3;")
tk.MustExec(`create table t(a int, b int) partition by hash(a) partitions 3`)
tk.MustExec(`create table t1(a int, b int) partition by hash(a) partitions 4`)
tk.MustExec(`create table t2(a int, b int) partition by hash(a) partitions 5`)
tk.MustExec(`create table t3(a int, b int) partition by hash(b) partitions 3`)

tk.MustExec(`set @@tidb_partition_prune_mode="dynamic"`)
runJoinReorderTestData(t, tk, "TestJoinOrderHint4DynamicPartitionTable")
}

func TestJoinOrderHint4DifferentJoinType(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1, t2, t3, t4, t5, t6, t7, t8;")
tk.MustExec("create table t(a int, b int, key(a));")
tk.MustExec("create table t1(a int, b int, key(a));")
tk.MustExec("create table t2(a int, b int, key(a));")
tk.MustExec("create table t3(a int, b int, key(a));")
tk.MustExec("create table t4(a int, b int, key(a));")
tk.MustExec("create table t5(a int, b int, key(a));")
tk.MustExec("create table t6(a int, b int, key(a));")
tk.MustExec("create table t7(a int, b int, key(a));")
tk.MustExec("create table t8(a int, b int, key(a));")

runJoinReorderTestData(t, tk, "TestJoinOrderHint4DifferentJoinType")
}

func TestJoinOrderHint4TiFlash(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1, t2, t3;")
tk.MustExec("create table t(a int, b int, key(a));")
tk.MustExec("create table t1(a int, b int, key(a));")
tk.MustExec("create table t2(a int, b int, key(a));")
tk.MustExec("create table t3(a int, b int, key(a));")

// Create virtual tiflash replica info.
dom := domain.GetDomain(tk.Session())
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
require.True(t, exists)
for _, tblInfo := range db.Tables {
tableName := tblInfo.Name.L
if tableName == "t" || tableName == "t1" || tableName == "t2" || tableName == "t3" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
}

tk.MustExec("set @@tidb_allow_mpp=1; set @@tidb_enforce_mpp=1;")
runJoinReorderTestData(t, tk, "TestJoinOrderHint4TiFlash")
}

func TestJoinOrderHint4Subquery(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1, t2, t3, t4, t5, t6, t7, t8;")
tk.MustExec("create table t(a int, b int, key(a));")
tk.MustExec("create table t1(a int, b int, key(a));")
tk.MustExec("create table t2(a int, b int, key(a));")
tk.MustExec("create table t3(a int, b int, key(a));")
tk.MustExec("create table t4(a int, b int, key(a));")
tk.MustExec("create table t5(a int, b int, key(a));")
tk.MustExec("create table t6(a int, b int, key(a));")
tk.MustExec("create table t7(a int, b int, key(a));")
tk.MustExec("create table t8(a int, b int, key(a));")

runJoinReorderTestData(t, tk, "TestJoinOrderHint4Subquery")
}
Loading