Skip to content

Commit

Permalink
cherry pick #37264 to release-6.1
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
winoros authored and ti-srebot committed Aug 22, 2022
1 parent 7ddd028 commit 540604c
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/explaintest/r/clustered_index.result
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set @@tidb_enable_outer_join_reorder=true;
drop database if exists with_cluster_index;
create database with_cluster_index;
drop database if exists wout_cluster_index;
Expand Down Expand Up @@ -127,3 +128,4 @@ StreamAgg_17 1.00 root funcs:count(Column#9)->Column#7
└─IndexReader_18 1.00 root index:StreamAgg_9
└─StreamAgg_9 1.00 cop[tikv] funcs:count(1)->Column#9
└─IndexRangeScan_16 109.70 cop[tikv] table:tbl_0, index:idx_3(col_0) range:[803163,+inf], keep order:false
set @@tidb_enable_outer_join_reorder=false;
2 changes: 2 additions & 0 deletions cmd/explaintest/r/select.result
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set @@tidb_enable_outer_join_reorder=true;
DROP TABLE IF EXISTS t;
CREATE TABLE t (
c1 int,
Expand Down Expand Up @@ -654,3 +655,4 @@ create table t3(a char(10), primary key (a));
insert into t3 values ('a');
select * from t3 where a > 0x80;
Error 1105: Cannot convert string '\x80' from binary to utf8mb4
set @@tidb_enable_outer_join_reorder=false;
2 changes: 2 additions & 0 deletions cmd/explaintest/t/clustered_index.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set @@tidb_enable_outer_join_reorder=true;
drop database if exists with_cluster_index;
create database with_cluster_index;
drop database if exists wout_cluster_index;
Expand Down Expand Up @@ -53,3 +54,4 @@ explain select count(*) from wout_cluster_index.tbl_0 where col_0 <= 0 ;

explain select count(*) from with_cluster_index.tbl_0 where col_0 >= 803163 ;
explain select count(*) from wout_cluster_index.tbl_0 where col_0 >= 803163 ;
set @@tidb_enable_outer_join_reorder=false;
2 changes: 2 additions & 0 deletions cmd/explaintest/t/select.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set @@tidb_enable_outer_join_reorder=true;
DROP TABLE IF EXISTS t;

CREATE TABLE t (
Expand Down Expand Up @@ -279,3 +280,4 @@ create table t3(a char(10), primary key (a));
insert into t3 values ('a');
--error 1105
select * from t3 where a > 0x80;
set @@tidb_enable_outer_join_reorder=false;
32 changes: 32 additions & 0 deletions planner/core/rule_join_reorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func TestJoinOrderHint4StaticPartitionTable(t *testing.T) {
tk.MustExec(`create table t3(a int, b int) partition by hash(b) partitions 3`)

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

Expand All @@ -255,6 +256,7 @@ func TestJoinOrderHint4DynamicPartitionTable(t *testing.T) {
tk.MustExec(`create table t3(a int, b int) partition by hash(b) partitions 3`)

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

Expand All @@ -274,6 +276,7 @@ func TestJoinOrderHint4DifferentJoinType(t *testing.T) {
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));")
tk.MustExec("set @@tidb_enable_outer_join_reorder=true")

runJoinReorderTestData(t, tk, "TestJoinOrderHint4DifferentJoinType")
}
Expand All @@ -288,6 +291,13 @@ func TestJoinOrderHint4TiFlash(t *testing.T) {
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));")
<<<<<<< HEAD
=======
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("set @@tidb_enable_outer_join_reorder=true")
>>>>>>> 21847fe58... planner: set EnableOuterJoinReorder to false by default (#37264)

// Create virtual tiflash replica info.
dom := domain.GetDomain(tk.Session())
Expand Down Expand Up @@ -330,6 +340,28 @@ func TestJoinOrderHint4Subquery(t *testing.T) {
runJoinReorderTestData(t, tk, "TestJoinOrderHint4Subquery")
}

<<<<<<< HEAD
=======
func TestLeadingJoinHint4OuterJoin(t *testing.T) {
store := testkit.CreateMockStore(t)

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));")
tk.MustExec("set @@tidb_enable_outer_join_reorder=true")
runJoinReorderTestData(t, tk, "TestLeadingJoinHint4OuterJoin")
}

>>>>>>> 21847fe58... planner: set EnableOuterJoinReorder to false by default (#37264)
func TestOuterJoinWIthEqCondCrossInnerJoin(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
1 change: 1 addition & 0 deletions planner/core/rule_result_reorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func TestOrderedResultModeOnJoin(t *testing.T) {
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (a int primary key, b int, c int, d int, key(b))")
tk.MustExec("create table t2 (a int primary key, b int, c int, d int, key(b))")
tk.MustExec("set @@tidb_enable_outer_join_reorder=true")
runTestData(t, tk, "TestOrderedResultModeOnJoin")
}

Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ var defaultSysVars = []*SysVar{
s.OptimizerSelectivityLevel = tidbOptPositiveInt32(val, DefTiDBOptimizerSelectivityLevel)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptimizerEnableOuterJoinReorder, Value: BoolToOnOff(DefTiDBEnableOuterJoinReorder), skipInit: true, Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptimizerEnableOuterJoinReorder, Value: BoolToOnOff(DefTiDBEnableOuterJoinReorder), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
s.EnableOuterJoinReorder = TiDBOptOn(val)
return nil
}},
Expand Down
62 changes: 62 additions & 0 deletions sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,68 @@ func TestSettersandGetters(t *testing.T) {
}
}

<<<<<<< HEAD
=======
// TestSkipInitIsUsed ensures that no new variables are added with skipInit: true.
// This feature is deprecated, and if you need to run code to differentiate between init and "SET" (rare),
// you can instead check if s.StmtCtx.StmtType == "Set".
// The reason it is deprecated is that the behavior is typically wrong:
// it means session settings won't inherit from global and don't apply until you first set
// them in each session. This is a very weird behavior.
// See: https://github.com/pingcap/tidb/issues/35051
func TestSkipInitIsUsed(t *testing.T) {
for _, sv := range GetSysVars() {
if sv.skipInit {
// skipInit only ever applied to session scope, so if anyone is setting it on
// a variable without session, that doesn't make sense.
require.True(t, sv.HasSessionScope(), fmt.Sprintf("skipInit has no effect on a variable without session scope: %s", sv.Name))
// Since SetSession is the "init function" there is no init function to skip.
require.NotNil(t, sv.SetSession, fmt.Sprintf("skipInit has no effect on variables without an init (setsession) func: %s", sv.Name))
// Skipinit has no use on noop funcs, since noop funcs always skipinit.
require.False(t, sv.IsNoop, fmt.Sprintf("skipInit has no effect on noop variables: %s", sv.Name))

// Test for variables that have a default of "0" or "OFF"
// If it is session-only scoped there is likely no bug now.
// If it is also global-scoped, then there is a bug as soon as the global changes.
if !(sv.Name == RandSeed1 || sv.Name == RandSeed2) {
// The bug is because the tests might not realize the SetSession func was not called on init,
// because it would initialize some session field to the empty value anyway.
require.NotEqual(t, "0", sv.Value, fmt.Sprintf("default value is zero: %s", sv.Name))
require.NotEqual(t, "OFF", sv.Value, fmt.Sprintf("default value is OFF: %s", sv.Name))
}

// Many of these variables might allow skipInit to be removed,
// they need to be checked first. The purpose of this test is to make
// sure we don't introduce any new variables with skipInit, which seems
// to be a problem.
switch sv.Name {
case TiDBTxnScope,
TiDBSnapshot,
TiDBEnableChunkRPC,
TxnIsolationOneShot,
TiDBDDLReorgPriority,
TiDBSlowQueryFile,
TiDBWaitSplitRegionFinish,
TiDBWaitSplitRegionTimeout,
TiDBMetricSchemaStep,
TiDBMetricSchemaRangeDuration,
RandSeed1,
RandSeed2,
CollationDatabase,
CollationConnection,
CharsetDatabase,
CharacterSetConnection,
CharacterSetServer,
TiDBOptTiFlashConcurrencyFactor,
TiDBOptSeekFactor:
continue
}
require.Equal(t, false, sv.skipInit, fmt.Sprintf("skipInit should not be set on new system variables. variable %s is in violation", sv.Name))
}
}
}

>>>>>>> 21847fe58... planner: set EnableOuterJoinReorder to false by default (#37264)
func TestSecureAuth(t *testing.T) {
sv := GetSysVar(SecureAuth)
vars := NewSessionVars()
Expand Down
Loading

0 comments on commit 540604c

Please sign in to comment.