Skip to content

Commit

Permalink
This is an automated cherry-pick of #41319
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
Yisaer authored and ti-chi-bot committed Feb 13, 2023
1 parent ac6560f commit 2ee7262
Show file tree
Hide file tree
Showing 5 changed files with 745 additions and 0 deletions.
134 changes: 134 additions & 0 deletions executor/index_advise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,137 @@ func TestIndexAdvise(t *testing.T) {
require.Equal(t, uint64(4), ia.MaxIndexNum.PerTable)
require.Equal(t, uint64(5), ia.MaxIndexNum.PerDB)
}
<<<<<<< HEAD
=======

func TestIndexJoinProjPattern(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t1(
pnbrn_cnaps varchar(5) not null,
new_accno varchar(18) not null,
primary key(pnbrn_cnaps,new_accno) nonclustered
);`)
tk.MustExec(`create table t2(
pnbrn_cnaps varchar(5) not null,
txn_accno varchar(18) not null,
txn_dt date not null,
yn_frz varchar(1) default null
);`)
tk.MustExec(`insert into t1(pnbrn_cnaps,new_accno) values ("40001","123")`)
tk.MustExec(`insert into t2(pnbrn_cnaps, txn_accno, txn_dt, yn_frz) values ("40001","123","20221201","0");`)

sql := `update
/*+ inl_join(a) */
t2 b,
(
select t1.pnbrn_cnaps,
t1.new_accno
from t1
where t1.pnbrn_cnaps = '40001'
) a
set b.yn_frz = '1'
where b.txn_dt = str_to_date('20221201', '%Y%m%d')
and b.pnbrn_cnaps = a.pnbrn_cnaps
and b.txn_accno = a.new_accno;`
rows := [][]interface{}{
{"Update_8"},
{"└─IndexJoin_14"},
{" ├─TableReader_25(Build)"},
{" │ └─Selection_24"},
{" │ └─TableFullScan_23"},
{" └─IndexReader_12(Probe)"},
{" └─Selection_11"},
{" └─IndexRangeScan_10"},
}
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
rows = [][]interface{}{
{"Update_8"},
{"└─HashJoin_10"},
{" ├─IndexReader_17(Build)"},
{" │ └─IndexRangeScan_16"},
{" └─TableReader_14(Probe)"},
{" └─Selection_13"},
{" └─TableFullScan_12"},
}
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)

tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
tk.MustExec(sql)
tk.MustQuery("select yn_frz from t2").Check(testkit.Rows("1"))
}

func TestIndexJoinSelPattern(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(` create table tbl_miss(
id bigint(20) unsigned not null
,txn_dt date default null
,perip_sys_uuid varchar(32) not null
,rvrs_idr varchar(1) not null
,primary key(id) clustered
,key idx1 (txn_dt, perip_sys_uuid, rvrs_idr)
);
`)
tk.MustExec(`insert into tbl_miss (id,txn_dt,perip_sys_uuid,rvrs_idr) values (1,"20221201","123","1");`)
tk.MustExec(`create table tbl_src(
txn_dt date default null
,uuid varchar(32) not null
,rvrs_idr char(1)
,expd_inf varchar(5000)
,primary key(uuid,rvrs_idr) nonclustered
);
`)
tk.MustExec(`insert into tbl_src (txn_dt,uuid,rvrs_idr) values ("20221201","123","1");`)
sql := `select /*+ use_index(mis,) inl_join(src) */
*
from tbl_miss mis
,tbl_src src
where src.txn_dt >= str_to_date('20221201', '%Y%m%d')
and mis.id between 1 and 10000
and mis.perip_sys_uuid = src.uuid
and mis.rvrs_idr = src.rvrs_idr
and mis.txn_dt = src.txn_dt
and (
case when isnull(src.expd_inf) = 1 then ''
else
substr(concat_ws('',src.expd_inf,'~~'),
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4,
instr(substr(concat_ws('',src.expd_inf,'~~'),
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4, length(concat_ws('',src.expd_inf,'~~'))),'~~') -1)
end
) != '01';`
rows := [][]interface{}{
{"HashJoin_9"},
{"├─TableReader_12(Build)"},
{"│ └─Selection_11"},
{"│ └─TableRangeScan_10"},
{"└─Selection_13(Probe)"},
{" └─TableReader_16"},
{" └─Selection_15"},
{" └─TableFullScan_14"},
}
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
rows = [][]interface{}{
{"IndexJoin_13"},
{"├─TableReader_25(Build)"},
{"│ └─Selection_24"},
{"│ └─TableRangeScan_23"},
{"└─Selection_12(Probe)"},
{" └─IndexLookUp_11"},
{" ├─IndexRangeScan_8(Build)"},
{" └─Selection_10(Probe)"},
{" └─TableRowIDScan_9"},
}
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='ON'")
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
tk.MustExec("set @@session.tidb_enable_inl_join_inner_multi_pattern='OFF'")
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
}
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
73 changes: 73 additions & 0 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,35 @@ func (p *LogicalJoin) getIndexJoinByOuterIdx(prop *property.PhysicalProperty, ou
return nil
}
}
<<<<<<< HEAD
=======
case *LogicalProjection:
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
return nil
}
// For now, we only allow proj with all Column expression can be the inner side of index join
for _, expr := range child.Exprs {
if _, ok := expr.(*expression.Column); !ok {
return nil
}
}
wrapper.proj = child
ds, isDataSource := wrapper.proj.Children()[0].(*DataSource)
if !isDataSource {
return nil
}
wrapper.ds = ds
case *LogicalSelection:
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
return nil
}
wrapper.sel = child
ds, isDataSource := wrapper.sel.Children()[0].(*DataSource)
if !isDataSource {
return nil
}
wrapper.ds = ds
>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
}
var avgInnerRowCnt float64
if outerChild.statsInfo().RowCount > 0 {
Expand Down Expand Up @@ -1001,6 +1030,50 @@ func (p *LogicalJoin) constructInnerTableScanTask(
return t
}

<<<<<<< HEAD
=======
func (p *LogicalJoin) constructInnerByWrapper(wrapper *indexJoinInnerChildWrapper, child PhysicalPlan) PhysicalPlan {
if !p.ctx.GetSessionVars().EnableINLJoinInnerMultiPattern {
if wrapper.us != nil {
return p.constructInnerUnionScan(wrapper.us, child)
}
return child
}
if wrapper.us != nil {
return p.constructInnerUnionScan(wrapper.us, child)
} else if wrapper.proj != nil {
return p.constructInnerProj(wrapper.proj, child)
} else if wrapper.sel != nil {
return p.constructInnerSel(wrapper.sel, child)
}
return child
}

func (p *LogicalJoin) constructInnerSel(sel *LogicalSelection, child PhysicalPlan) PhysicalPlan {
if sel == nil {
return child
}
physicalSel := PhysicalSelection{
Conditions: sel.Conditions,
}.Init(sel.ctx, sel.stats, sel.blockOffset, nil)
physicalSel.SetChildren(child)
return physicalSel
}

func (p *LogicalJoin) constructInnerProj(proj *LogicalProjection, child PhysicalPlan) PhysicalPlan {
if proj == nil {
return child
}
physicalProj := PhysicalProjection{
Exprs: proj.Exprs,
CalculateNoDelay: proj.CalculateNoDelay,
AvoidColumnEvaluator: proj.AvoidColumnEvaluator,
}.Init(proj.ctx, proj.stats, proj.blockOffset, nil)
physicalProj.SetChildren(child)
return physicalProj
}

>>>>>>> 982a6163a1 (sysvar: introduce variable tidb_enable_inl_join_inner_multi_pattern (#41319))
func (p *LogicalJoin) constructInnerUnionScan(us *LogicalUnionScan, reader PhysicalPlan) PhysicalPlan {
if us == nil {
return reader
Expand Down
Loading

0 comments on commit 2ee7262

Please sign in to comment.