Skip to content

Commit 2ee7262

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

File tree

5 files changed

+745
-0
lines changed

5 files changed

+745
-0
lines changed

executor/index_advise_test.go

+134
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,137 @@ func TestIndexAdvise(t *testing.T) {
6969
require.Equal(t, uint64(4), ia.MaxIndexNum.PerTable)
7070
require.Equal(t, uint64(5), ia.MaxIndexNum.PerDB)
7171
}
72+
<<<<<<< HEAD
73+
=======
74+
75+
func TestIndexJoinProjPattern(t *testing.T) {
76+
store := testkit.CreateMockStore(t)
77+
tk := testkit.NewTestKit(t, store)
78+
tk.MustExec("use test")
79+
tk.MustExec(`create table t1(
80+
pnbrn_cnaps varchar(5) not null,
81+
new_accno varchar(18) not null,
82+
primary key(pnbrn_cnaps,new_accno) nonclustered
83+
);`)
84+
tk.MustExec(`create table t2(
85+
pnbrn_cnaps varchar(5) not null,
86+
txn_accno varchar(18) not null,
87+
txn_dt date not null,
88+
yn_frz varchar(1) default null
89+
);`)
90+
tk.MustExec(`insert into t1(pnbrn_cnaps,new_accno) values ("40001","123")`)
91+
tk.MustExec(`insert into t2(pnbrn_cnaps, txn_accno, txn_dt, yn_frz) values ("40001","123","20221201","0");`)
92+
93+
sql := `update
94+
/*+ inl_join(a) */
95+
t2 b,
96+
(
97+
select t1.pnbrn_cnaps,
98+
t1.new_accno
99+
from t1
100+
where t1.pnbrn_cnaps = '40001'
101+
) a
102+
set b.yn_frz = '1'
103+
where b.txn_dt = str_to_date('20221201', '%Y%m%d')
104+
and b.pnbrn_cnaps = a.pnbrn_cnaps
105+
and b.txn_accno = a.new_accno;`
106+
rows := [][]interface{}{
107+
{"Update_8"},
108+
{"└─IndexJoin_14"},
109+
{" ├─TableReader_25(Build)"},
110+
{" │ └─Selection_24"},
111+
{" │ └─TableFullScan_23"},
112+
{" └─IndexReader_12(Probe)"},
113+
{" └─Selection_11"},
114+
{" └─IndexRangeScan_10"},
115+
}
116+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
117+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
118+
rows = [][]interface{}{
119+
{"Update_8"},
120+
{"└─HashJoin_10"},
121+
{" ├─IndexReader_17(Build)"},
122+
{" │ └─IndexRangeScan_16"},
123+
{" └─TableReader_14(Probe)"},
124+
{" └─Selection_13"},
125+
{" └─TableFullScan_12"},
126+
}
127+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
128+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
129+
130+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
131+
tk.MustExec(sql)
132+
tk.MustQuery("select yn_frz from t2").Check(testkit.Rows("1"))
133+
}
134+
135+
func TestIndexJoinSelPattern(t *testing.T) {
136+
store := testkit.CreateMockStore(t)
137+
tk := testkit.NewTestKit(t, store)
138+
tk.MustExec("use test")
139+
tk.MustExec(` create table tbl_miss(
140+
id bigint(20) unsigned not null
141+
,txn_dt date default null
142+
,perip_sys_uuid varchar(32) not null
143+
,rvrs_idr varchar(1) not null
144+
,primary key(id) clustered
145+
,key idx1 (txn_dt, perip_sys_uuid, rvrs_idr)
146+
);
147+
`)
148+
tk.MustExec(`insert into tbl_miss (id,txn_dt,perip_sys_uuid,rvrs_idr) values (1,"20221201","123","1");`)
149+
tk.MustExec(`create table tbl_src(
150+
txn_dt date default null
151+
,uuid varchar(32) not null
152+
,rvrs_idr char(1)
153+
,expd_inf varchar(5000)
154+
,primary key(uuid,rvrs_idr) nonclustered
155+
);
156+
`)
157+
tk.MustExec(`insert into tbl_src (txn_dt,uuid,rvrs_idr) values ("20221201","123","1");`)
158+
sql := `select /*+ use_index(mis,) inl_join(src) */
159+
*
160+
from tbl_miss mis
161+
,tbl_src src
162+
where src.txn_dt >= str_to_date('20221201', '%Y%m%d')
163+
and mis.id between 1 and 10000
164+
and mis.perip_sys_uuid = src.uuid
165+
and mis.rvrs_idr = src.rvrs_idr
166+
and mis.txn_dt = src.txn_dt
167+
and (
168+
case when isnull(src.expd_inf) = 1 then ''
169+
else
170+
substr(concat_ws('',src.expd_inf,'~~'),
171+
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4,
172+
instr(substr(concat_ws('',src.expd_inf,'~~'),
173+
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4, length(concat_ws('',src.expd_inf,'~~'))),'~~') -1)
174+
end
175+
) != '01';`
176+
rows := [][]interface{}{
177+
{"HashJoin_9"},
178+
{"├─TableReader_12(Build)"},
179+
{"│ └─Selection_11"},
180+
{"│ └─TableRangeScan_10"},
181+
{"└─Selection_13(Probe)"},
182+
{" └─TableReader_16"},
183+
{" └─Selection_15"},
184+
{" └─TableFullScan_14"},
185+
}
186+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
187+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
188+
rows = [][]interface{}{
189+
{"IndexJoin_13"},
190+
{"├─TableReader_25(Build)"},
191+
{"│ └─Selection_24"},
192+
{"│ └─TableRangeScan_23"},
193+
{"└─Selection_12(Probe)"},
194+
{" └─IndexLookUp_11"},
195+
{" ├─IndexRangeScan_8(Build)"},
196+
{" └─Selection_10(Probe)"},
197+
{" └─TableRowIDScan_9"},
198+
}
199+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
200+
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
201+
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
202+
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
203+
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
204+
}
205+
>>>>>>> 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
@@ -702,6 +702,35 @@ func (p *LogicalJoin) getIndexJoinByOuterIdx(prop *property.PhysicalProperty, ou
702702
return nil
703703
}
704704
}
705+
<<<<<<< HEAD
706+
=======
707+
case *LogicalProjection:
708+
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
709+
return nil
710+
}
711+
// For now, we only allow proj with all Column expression can be the inner side of index join
712+
for _, expr := range child.Exprs {
713+
if _, ok := expr.(*expression.Column); !ok {
714+
return nil
715+
}
716+
}
717+
wrapper.proj = child
718+
ds, isDataSource := wrapper.proj.Children()[0].(*DataSource)
719+
if !isDataSource {
720+
return nil
721+
}
722+
wrapper.ds = ds
723+
case *LogicalSelection:
724+
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
725+
return nil
726+
}
727+
wrapper.sel = child
728+
ds, isDataSource := wrapper.sel.Children()[0].(*DataSource)
729+
if !isDataSource {
730+
return nil
731+
}
732+
wrapper.ds = ds
733+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
705734
}
706735
var avgInnerRowCnt float64
707736
if outerChild.statsInfo().RowCount > 0 {
@@ -1001,6 +1030,50 @@ func (p *LogicalJoin) constructInnerTableScanTask(
10011030
return t
10021031
}
10031032

1033+
<<<<<<< HEAD
1034+
=======
1035+
func (p *LogicalJoin) constructInnerByWrapper(wrapper *indexJoinInnerChildWrapper, child PhysicalPlan) PhysicalPlan {
1036+
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
1037+
if wrapper.us != nil {
1038+
return p.constructInnerUnionScan(wrapper.us, child)
1039+
}
1040+
return child
1041+
}
1042+
if wrapper.us != nil {
1043+
return p.constructInnerUnionScan(wrapper.us, child)
1044+
} else if wrapper.proj != nil {
1045+
return p.constructInnerProj(wrapper.proj, child)
1046+
} else if wrapper.sel != nil {
1047+
return p.constructInnerSel(wrapper.sel, child)
1048+
}
1049+
return child
1050+
}
1051+
1052+
func (p *LogicalJoin) constructInnerSel(sel *LogicalSelection, child PhysicalPlan) PhysicalPlan {
1053+
if sel == nil {
1054+
return child
1055+
}
1056+
physicalSel := PhysicalSelection{
1057+
Conditions: sel.Conditions,
1058+
}.Init(sel.ctx, sel.stats, sel.blockOffset, nil)
1059+
physicalSel.SetChildren(child)
1060+
return physicalSel
1061+
}
1062+
1063+
func (p *LogicalJoin) constructInnerProj(proj *LogicalProjection, child PhysicalPlan) PhysicalPlan {
1064+
if proj == nil {
1065+
return child
1066+
}
1067+
physicalProj := PhysicalProjection{
1068+
Exprs: proj.Exprs,
1069+
CalculateNoDelay: proj.CalculateNoDelay,
1070+
AvoidColumnEvaluator: proj.AvoidColumnEvaluator,
1071+
}.Init(proj.ctx, proj.stats, proj.blockOffset, nil)
1072+
physicalProj.SetChildren(child)
1073+
return physicalProj
1074+
}
1075+
1076+
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
10041077
func (p *LogicalJoin) constructInnerUnionScan(us *LogicalUnionScan, reader PhysicalPlan) PhysicalPlan {
10051078
if us == nil {
10061079
return reader

0 commit comments

Comments
 (0)