Skip to content

Commit a22c66e

Browse files
Yisaerti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#41319
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
1 parent b3967f8 commit a22c66e

File tree

5 files changed

+307
-0
lines changed

5 files changed

+307
-0
lines changed

executor/index_advise_test.go

+134
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,137 @@ func TestIndexAdvise(t *testing.T) {
6565
require.Equal(t, uint64(4), ia.MaxIndexNum.PerTable)
6666
require.Equal(t, uint64(5), ia.MaxIndexNum.PerDB)
6767
}
68+
<<<<<<< HEAD
69+
=======
70+
71+
func TestIndexJoinProjPattern(t *testing.T) {
72+
store := testkit.CreateMockStore(t)
73+
tk := testkit.NewTestKit(t, store)
74+
tk.MustExec("use test")
75+
tk.MustExec(`create table t1(
76+
pnbrn_cnaps varchar(5) not null,
77+
new_accno varchar(18) not null,
78+
primary key(pnbrn_cnaps,new_accno) nonclustered
79+
);`)
80+
tk.MustExec(`create table t2(
81+
pnbrn_cnaps varchar(5) not null,
82+
txn_accno varchar(18) not null,
83+
txn_dt date not null,
84+
yn_frz varchar(1) default null
85+
);`)
86+
tk.MustExec(`insert into t1(pnbrn_cnaps,new_accno) values ("40001","123")`)
87+
tk.MustExec(`insert into t2(pnbrn_cnaps, txn_accno, txn_dt, yn_frz) values ("40001","123","20221201","0");`)
88+
89+
sql := `update
90+
/*+ inl_join(a) */
91+
t2 b,
92+
(
93+
select t1.pnbrn_cnaps,
94+
t1.new_accno
95+
from t1
96+
where t1.pnbrn_cnaps = '40001'
97+
) a
98+
set b.yn_frz = '1'
99+
where b.txn_dt = str_to_date('20221201', '%Y%m%d')
100+
and b.pnbrn_cnaps = a.pnbrn_cnaps
101+
and b.txn_accno = a.new_accno;`
102+
rows := [][]interface{}{
103+
{"Update_8"},
104+
{"└─IndexJoin_14"},
105+
{" ├─TableReader_25(Build)"},
106+
{" │ └─Selection_24"},
107+
{" │ └─TableFullScan_23"},
108+
{" └─IndexReader_12(Probe)"},
109+
{" └─Selection_11"},
110+
{" └─IndexRangeScan_10"},
111+
}
112+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
113+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
114+
rows = [][]interface{}{
115+
{"Update_8"},
116+
{"└─HashJoin_10"},
117+
{" ├─IndexReader_17(Build)"},
118+
{" │ └─IndexRangeScan_16"},
119+
{" └─TableReader_14(Probe)"},
120+
{" └─Selection_13"},
121+
{" └─TableFullScan_12"},
122+
}
123+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
124+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
125+
126+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
127+
tk.MustExec(sql)
128+
tk.MustQuery("select yn_frz from t2").Check(testkit.Rows("1"))
129+
}
130+
131+
func TestIndexJoinSelPattern(t *testing.T) {
132+
store := testkit.CreateMockStore(t)
133+
tk := testkit.NewTestKit(t, store)
134+
tk.MustExec("use test")
135+
tk.MustExec(` create table tbl_miss(
136+
id bigint(20) unsigned not null
137+
,txn_dt date default null
138+
,perip_sys_uuid varchar(32) not null
139+
,rvrs_idr varchar(1) not null
140+
,primary key(id) clustered
141+
,key idx1 (txn_dt, perip_sys_uuid, rvrs_idr)
142+
);
143+
`)
144+
tk.MustExec(`insert into tbl_miss (id,txn_dt,perip_sys_uuid,rvrs_idr) values (1,"20221201","123","1");`)
145+
tk.MustExec(`create table tbl_src(
146+
txn_dt date default null
147+
,uuid varchar(32) not null
148+
,rvrs_idr char(1)
149+
,expd_inf varchar(5000)
150+
,primary key(uuid,rvrs_idr) nonclustered
151+
);
152+
`)
153+
tk.MustExec(`insert into tbl_src (txn_dt,uuid,rvrs_idr) values ("20221201","123","1");`)
154+
sql := `select /*+ use_index(mis,) inl_join(src) */
155+
*
156+
from tbl_miss mis
157+
,tbl_src src
158+
where src.txn_dt >= str_to_date('20221201', '%Y%m%d')
159+
and mis.id between 1 and 10000
160+
and mis.perip_sys_uuid = src.uuid
161+
and mis.rvrs_idr = src.rvrs_idr
162+
and mis.txn_dt = src.txn_dt
163+
and (
164+
case when isnull(src.expd_inf) = 1 then ''
165+
else
166+
substr(concat_ws('',src.expd_inf,'~~'),
167+
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4,
168+
instr(substr(concat_ws('',src.expd_inf,'~~'),
169+
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4, length(concat_ws('',src.expd_inf,'~~'))),'~~') -1)
170+
end
171+
) != '01';`
172+
rows := [][]interface{}{
173+
{"HashJoin_9"},
174+
{"├─TableReader_12(Build)"},
175+
{"│ └─Selection_11"},
176+
{"│ └─TableRangeScan_10"},
177+
{"└─Selection_13(Probe)"},
178+
{" └─TableReader_16"},
179+
{" └─Selection_15"},
180+
{" └─TableFullScan_14"},
181+
}
182+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
183+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
184+
rows = [][]interface{}{
185+
{"IndexJoin_13"},
186+
{"├─TableReader_25(Build)"},
187+
{"│ └─Selection_24"},
188+
{"│ └─TableRangeScan_23"},
189+
{"└─Selection_12(Probe)"},
190+
{" └─IndexLookUp_11"},
191+
{" ├─IndexRangeScan_8(Build)"},
192+
{" └─Selection_10(Probe)"},
193+
{" └─TableRowIDScan_9"},
194+
}
195+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
196+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
197+
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
198+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
199+
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
200+
}
201+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))

planner/core/exhaust_physical_plans.go

+73
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,35 @@ func (p *LogicalJoin) getIndexJoinByOuterIdx(prop *property.PhysicalProperty, ou
734734
return nil
735735
}
736736
}
737+
<<<<<<< HEAD
738+
=======
739+
case *LogicalProjection:
740+
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
741+
return nil
742+
}
743+
// For now, we only allow proj with all Column expression can be the inner side of index join
744+
for _, expr := range child.Exprs {
745+
if _, ok := expr.(*expression.Column); !ok {
746+
return nil
747+
}
748+
}
749+
wrapper.proj = child
750+
ds, isDataSource := wrapper.proj.Children()[0].(*DataSource)
751+
if !isDataSource {
752+
return nil
753+
}
754+
wrapper.ds = ds
755+
case *LogicalSelection:
756+
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
757+
return nil
758+
}
759+
wrapper.sel = child
760+
ds, isDataSource := wrapper.sel.Children()[0].(*DataSource)
761+
if !isDataSource {
762+
return nil
763+
}
764+
wrapper.ds = ds
765+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
737766
}
738767
var avgInnerRowCnt float64
739768
if outerChild.statsInfo().RowCount > 0 {
@@ -1042,6 +1071,50 @@ func (p *LogicalJoin) constructInnerTableScanTask(
10421071
return t
10431072
}
10441073

1074+
<<<<<<< HEAD
1075+
=======
1076+
func (p *LogicalJoin) constructInnerByWrapper(wrapper *indexJoinInnerChildWrapper, child PhysicalPlan) PhysicalPlan {
1077+
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
1078+
if wrapper.us != nil {
1079+
return p.constructInnerUnionScan(wrapper.us, child)
1080+
}
1081+
return child
1082+
}
1083+
if wrapper.us != nil {
1084+
return p.constructInnerUnionScan(wrapper.us, child)
1085+
} else if wrapper.proj != nil {
1086+
return p.constructInnerProj(wrapper.proj, child)
1087+
} else if wrapper.sel != nil {
1088+
return p.constructInnerSel(wrapper.sel, child)
1089+
}
1090+
return child
1091+
}
1092+
1093+
func (p *LogicalJoin) constructInnerSel(sel *LogicalSelection, child PhysicalPlan) PhysicalPlan {
1094+
if sel == nil {
1095+
return child
1096+
}
1097+
physicalSel := PhysicalSelection{
1098+
Conditions: sel.Conditions,
1099+
}.Init(sel.ctx, sel.stats, sel.blockOffset, nil)
1100+
physicalSel.SetChildren(child)
1101+
return physicalSel
1102+
}
1103+
1104+
func (p *LogicalJoin) constructInnerProj(proj *LogicalProjection, child PhysicalPlan) PhysicalPlan {
1105+
if proj == nil {
1106+
return child
1107+
}
1108+
physicalProj := PhysicalProjection{
1109+
Exprs: proj.Exprs,
1110+
CalculateNoDelay: proj.CalculateNoDelay,
1111+
AvoidColumnEvaluator: proj.AvoidColumnEvaluator,
1112+
}.Init(proj.ctx, proj.stats, proj.blockOffset, nil)
1113+
physicalProj.SetChildren(child)
1114+
return physicalProj
1115+
}
1116+
1117+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
10451118
func (p *LogicalJoin) constructInnerUnionScan(us *LogicalUnionScan, reader PhysicalPlan) PhysicalPlan {
10461119
if us == nil {
10471120
return reader

sessionctx/variable/session.go

+51
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,57 @@ type SessionVars struct {
13191319

13201320
// StoreBatchSize indicates the batch size limit of store batch, set this field to 0 to disable store batch.
13211321
StoreBatchSize int
1322+
<<<<<<< HEAD
1323+
=======
1324+
1325+
// shardRand is used by TxnCtx, for the GetCurrentShard() method.
1326+
shardRand *rand.Rand
1327+
1328+
// Resource group name
1329+
ResourceGroupName string
1330+
1331+
// ProtectedTSList holds a list of timestamps that should delay GC.
1332+
ProtectedTSList protectedTSList
1333+
1334+
// PessimisticTransactionAggressiveLocking controls whether aggressive locking for pessimistic transaction
1335+
// is enabled.
1336+
PessimisticTransactionAggressiveLocking bool
1337+
1338+
// EnableINLJoinInnerMultiPattern indicates whether enable multi pattern for index join inner side
1339+
// For now it is not public to user
1340+
EnableINLJoinInnerMultiPattern bool
1341+
}
1342+
1343+
// planReplayerSessionFinishedTaskKeyLen is used to control the max size for the finished plan replayer task key in session
1344+
// in order to control the used memory
1345+
const planReplayerSessionFinishedTaskKeyLen = 128
1346+
1347+
// AddPlanReplayerFinishedTaskKey record finished task key in session
1348+
func (s *SessionVars) AddPlanReplayerFinishedTaskKey(key replayer.PlanReplayerTaskKey) {
1349+
if len(s.PlanReplayerFinishedTaskKey) >= planReplayerSessionFinishedTaskKeyLen {
1350+
s.initializePlanReplayerFinishedTaskKey()
1351+
}
1352+
s.PlanReplayerFinishedTaskKey[key] = struct{}{}
1353+
}
1354+
1355+
func (s *SessionVars) initializePlanReplayerFinishedTaskKey() {
1356+
s.PlanReplayerFinishedTaskKey = make(map[replayer.PlanReplayerTaskKey]struct{}, planReplayerSessionFinishedTaskKeyLen)
1357+
}
1358+
1359+
// CheckPlanReplayerFinishedTaskKey check whether the key exists
1360+
func (s *SessionVars) CheckPlanReplayerFinishedTaskKey(key replayer.PlanReplayerTaskKey) bool {
1361+
if s.PlanReplayerFinishedTaskKey == nil {
1362+
s.initializePlanReplayerFinishedTaskKey()
1363+
return false
1364+
}
1365+
_, ok := s.PlanReplayerFinishedTaskKey[key]
1366+
return ok
1367+
}
1368+
1369+
// IsPlanReplayerCaptureEnabled indicates whether capture or continues capture enabled
1370+
func (s *SessionVars) IsPlanReplayerCaptureEnabled() bool {
1371+
return s.EnablePlanReplayerCapture || s.EnablePlanReplayedContinuesCapture
1372+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
13221373
}
13231374

13241375
// GetNewChunkWithCapacity Attempt to request memory from the chunk pool

sessionctx/variable/sysvar.go

+30
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,36 @@ var defaultSysVars = []*SysVar{
22372237
return strconv.Itoa(int(TTLDeleteWorkerCount.Load())), nil
22382238
},
22392239
},
2240+
<<<<<<< HEAD
2241+
=======
2242+
{Scope: ScopeGlobal, Name: TiDBEnableResourceControl, Value: BoolToOnOff(DefTiDBEnableResourceControl), Type: TypeBool, SetGlobal: func(ctx context.Context, vars *SessionVars, s string) error {
2243+
if TiDBOptOn(s) != EnableResourceControl.Load() {
2244+
EnableResourceControl.Store(TiDBOptOn(s))
2245+
(*SetGlobalResourceControl.Load())(TiDBOptOn(s))
2246+
logutil.BgLogger().Info("set resource control", zap.Bool("enable", TiDBOptOn(s)))
2247+
}
2248+
return nil
2249+
}, GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
2250+
return BoolToOnOff(EnableResourceControl.Load()), nil
2251+
}},
2252+
{Scope: ScopeGlobal | ScopeSession, Name: TiDBPessimisticTransactionAggressiveLocking, Value: BoolToOnOff(DefTiDBPessimisticTransactionAggressiveLocking), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
2253+
s.PessimisticTransactionAggressiveLocking = TiDBOptOn(val)
2254+
return nil
2255+
}},
2256+
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnablePlanCacheForParamLimit, Value: BoolToOnOff(DefTiDBEnablePlanCacheForParamLimit), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
2257+
s.EnablePlanCacheForParamLimit = TiDBOptOn(val)
2258+
return nil
2259+
}},
2260+
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableINLJoinInnerMultiPattern, Value: BoolToOnOff(false), Type: TypeBool,
2261+
SetSession: func(s *SessionVars, val string) error {
2262+
s.EnableINLJoinInnerMultiPattern = TiDBOptOn(val)
2263+
return nil
2264+
},
2265+
GetSession: func(s *SessionVars) (string, error) {
2266+
return BoolToOnOff(s.EnableINLJoinInnerMultiPattern), nil
2267+
},
2268+
},
2269+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
22402270
}
22412271

22422272
// FeedbackProbability points to the FeedbackProbability in statistics package.

sessionctx/variable/tidb_vars.go

+19
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,25 @@ const (
786786

787787
// TiDBStoreBatchSize indicates the batch size of coprocessor in the same store.
788788
TiDBStoreBatchSize = "tidb_store_batch_size"
789+
<<<<<<< HEAD
790+
=======
791+
792+
// MppExchangeCompressionMode indicates the data compression method in mpp exchange operator
793+
MppExchangeCompressionMode = "mpp_exchange_compression_mode"
794+
795+
// MppVersion indicates the mpp-version used to build mpp plan
796+
MppVersion = "mpp_version"
797+
798+
// TiDBPessimisticTransactionAggressiveLocking controls whether aggressive locking for pessimistic transaction
799+
// is enabled.
800+
TiDBPessimisticTransactionAggressiveLocking = "tidb_pessimistic_txn_aggressive_locking"
801+
802+
// TiDBEnablePlanCacheForParamLimit controls whether prepare statement with parameterized limit can be cached
803+
TiDBEnablePlanCacheForParamLimit = "tidb_enable_plan_cache_for_param_limit"
804+
805+
// TiDBEnableINLJoinInnerMultiPattern indicates whether enable multi pattern for inner side of inl join
806+
TiDBEnableINLJoinInnerMultiPattern = "tidb_enable_inl_join_inner_multi_pattern"
807+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
789808
)
790809

791810
// TiDB vars that have only global scope

0 commit comments

Comments
 (0)