Skip to content

Commit

Permalink
planner: fix incorrect table name in explain information (pingcap#11780)
Browse files Browse the repository at this point in the history
  • Loading branch information
foreyes committed Sep 16, 2019
1 parent ada3a82 commit 20a9758
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 30 deletions.
36 changes: 18 additions & 18 deletions cmd/explaintest/r/explain_complex.result
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Projection_13 1.00 root test.gad.id, test.dd.id, test.gad.aid, test.gad.cm, test
└─IndexLookUp_33 3.33 root
├─IndexScan_30 3333.33 cop table:gad, index:t, range:(1478143908,+inf], keep order:false, stats:pseudo
└─Selection_32 3.33 cop eq(test.gad.pt, "android"), not(isnull(test.gad.ip))
└─TableScan_31 3333.33 cop table:st, keep order:false, stats:pseudo
└─TableScan_31 3333.33 cop table:gad, keep order:false, stats:pseudo
explain select gad.id as gid,sdk.id as sid,gad.aid as aid,gad.cm as cm,sdk.dic as dic,sdk.ip as ip, sdk.t as t, gad.p1 as p1, gad.p2 as p2, gad.p3 as p3, gad.p4 as p4, gad.p5 as p5, gad.p6_md5 as p6, gad.p7_md5 as p7, gad.ext as ext from st gad join dd sdk on gad.aid = sdk.aid and gad.dic = sdk.mac and gad.t < sdk.t where gad.t > 1477971479 and gad.bm = 0 and gad.pt = 'ios' and gad.dit = 'mac' and sdk.t > 1477971479 and sdk.bm = 0 and sdk.pt = 'ios' limit 3000;
id count task operator info
Projection_10 0.00 root test.gad.id, test.sdk.id, test.gad.aid, test.gad.cm, test.sdk.dic, test.sdk.ip, test.sdk.t, test.gad.p1, test.gad.p2, test.gad.p3, test.gad.p4, test.gad.p5, test.gad.p6_md5, test.gad.p7_md5, test.gad.ext
Expand All @@ -135,11 +135,11 @@ Projection_10 0.00 root test.gad.id, test.sdk.id, test.gad.aid, test.gad.cm, tes
├─IndexLookUp_27 0.00 root
│ ├─IndexScan_24 3333.33 cop table:gad, index:t, range:(1477971479,+inf], keep order:false, stats:pseudo
│ └─Selection_26 0.00 cop eq(test.gad.bm, 0), eq(test.gad.dit, "mac"), eq(test.gad.pt, "ios"), not(isnull(test.gad.dic))
│ └─TableScan_25 3333.33 cop table:st, keep order:false, stats:pseudo
│ └─TableScan_25 3333.33 cop table:gad, keep order:false, stats:pseudo
└─IndexLookUp_17 0.00 root
├─IndexScan_14 10.00 cop table:sdk, index:aid, dic, range: decided by [eq(test.sdk.aid, test.gad.aid)], keep order:false, stats:pseudo
└─Selection_16 0.00 cop eq(test.sdk.bm, 0), eq(test.sdk.pt, "ios"), gt(test.sdk.t, 1477971479), not(isnull(test.sdk.mac)), not(isnull(test.sdk.t))
└─TableScan_15 10.00 cop table:dd, keep order:false, stats:pseudo
└─TableScan_15 10.00 cop table:sdk, keep order:false, stats:pseudo
explain SELECT cm, p1, p2, p3, p4, p5, p6_md5, p7_md5, count(1) as click_pv, count(DISTINCT ip) as click_ip FROM st WHERE (t between 1478188800 and 1478275200) and aid='cn.sbkcq' and pt='android' GROUP BY cm, p1, p2, p3, p4, p5, p6_md5, p7_md5;
id count task operator info
Projection_5 1.00 root test.st.cm, test.st.p1, test.st.p2, test.st.p3, test.st.p4, test.st.p5, test.st.p6_md5, test.st.p7_md5, 3_col_0, 3_col_1
Expand Down Expand Up @@ -244,18 +244,18 @@ explain SELECT d.id, d.ctx, d.name, d.left_value, d.right_value, d.depth, d.lead
id count task operator info
Sort_10 1.00 root test.d.left_value:asc
└─HashAgg_15 1.00 root group by:test.d.id, funcs:firstrow(test.d.id), firstrow(test.d.ctx), firstrow(test.d.name), firstrow(test.d.left_value), firstrow(test.d.right_value), firstrow(test.d.depth), firstrow(test.d.leader_id), firstrow(test.d.status), firstrow(test.d.created_on), firstrow(test.d.updated_on)
└─Selection_20 0.01 root or(eq(test.ep.user_id, 62), or(eq(test.d.id, 20), eq(test.d.id, 20)))
└─HashLeftJoin_21 0.02 root left outer join, inner:TableReader_55, equal:[eq(test.p.id, test.ep.position_id)]
├─IndexJoin_29 0.01 root left outer join, inner:IndexLookUp_28, outer key:test.d.id, inner key:test.p.department_id
│ ├─IndexLookUp_45 0.01 root
│ │ ├─IndexScan_42 10.00 cop table:d, index:ctx, range:[1,1], keep order:false, stats:pseudo
│ │ └─Selection_44 0.01 cop eq(test.d.status, 1000)
│ │ └─TableScan_43 10.00 cop table:org_department, keep order:false, stats:pseudo
│ └─IndexLookUp_28 0.01 root
│ ├─Selection_26 9.99 cop not(isnull(test.p.department_id))
│ │ └─IndexScan_24 10.00 cop table:p, index:department_id, range: decided by [eq(test.p.department_id, test.d.id)], keep order:false, stats:pseudo
│ └─Selection_27 0.01 cop eq(test.p.status, 1000)
│ └─TableScan_25 9.99 cop table:org_position, keep order:false, stats:pseudo
└─TableReader_55 9.99 root data:Selection_54
└─Selection_54 9.99 cop eq(test.ep.status, 1000), not(isnull(test.ep.position_id))
└─TableScan_53 10000.00 cop table:ep, range:[-inf,+inf], keep order:false, stats:pseudo
└─Selection_22 0.01 root or(eq(test.ep.user_id, 62), or(eq(test.d.id, 20), eq(test.d.id, 20)))
└─HashLeftJoin_23 0.02 root left outer join, inner:TableReader_57, equal:[eq(test.p.id, test.ep.position_id)]
├─IndexJoin_32 0.01 root left outer join, inner:IndexLookUp_31, outer key:test.d.id, inner key:test.p.department_id
│ ├─IndexLookUp_47 0.01 root
│ │ ├─IndexScan_44 10.00 cop table:d, index:ctx, range:[1,1], keep order:false, stats:pseudo
│ │ └─Selection_46 0.01 cop eq(test.d.status, 1000)
│ │ └─TableScan_45 10.00 cop table:d, keep order:false, stats:pseudo
│ └─IndexLookUp_31 0.01 root
│ ├─Selection_29 9.99 cop not(isnull(test.p.department_id))
│ │ └─IndexScan_27 10.00 cop table:p, index:department_id, range: decided by [eq(test.p.department_id, test.d.id)], keep order:false, stats:pseudo
│ └─Selection_30 0.01 cop eq(test.p.status, 1000)
│ └─TableScan_28 9.99 cop table:p, keep order:false, stats:pseudo
└─TableReader_57 9.99 root data:Selection_56
└─Selection_56 9.99 cop eq(test.ep.status, 1000), not(isnull(test.ep.position_id))
└─TableScan_55 10000.00 cop table:ep, range:[-inf,+inf], keep order:false, stats:pseudo
2 changes: 1 addition & 1 deletion cmd/explaintest/r/explain_complex_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Projection_10 170.34 root test.gad.id, test.sdk.id, test.gad.aid, test.gad.cm, t
└─IndexLookUp_17 0.25 root
├─IndexScan_14 1.00 cop table:sdk, index:aid, dic, range: decided by [eq(test.sdk.aid, test.gad.aid)], keep order:false
└─Selection_16 0.25 cop eq(test.sdk.bm, 0), eq(test.sdk.pt, "ios"), gt(test.sdk.t, 1477971479), not(isnull(test.sdk.mac)), not(isnull(test.sdk.t))
└─TableScan_15 1.00 cop table:dd, keep order:false
└─TableScan_15 1.00 cop table:sdk, keep order:false
explain SELECT cm, p1, p2, p3, p4, p5, p6_md5, p7_md5, count(1) as click_pv, count(DISTINCT ip) as click_ip FROM st WHERE (t between 1478188800 and 1478275200) and aid='cn.sbkcq' and pt='android' GROUP BY cm, p1, p2, p3, p4, p5, p6_md5, p7_md5;
id count task operator info
Projection_5 39.28 root test.st.cm, test.st.p1, test.st.p2, test.st.p3, test.st.p4, test.st.p5, test.st.p6_md5, test.st.p7_md5, 3_col_0, 3_col_1
Expand Down
4 changes: 2 additions & 2 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ Projection_11 10000.00 root 9_aux_0
├─IndexLookUp_29 9.99 root
│ ├─IndexScan_26 10.00 cop table:s, index:b, range: decided by [eq(test.s.b, test.t.a)], keep order:false, stats:pseudo
│ └─Selection_28 9.99 cop not(isnull(test.s.c))
│ └─TableScan_27 10.00 cop table:t, keep order:false, stats:pseudo
│ └─TableScan_27 10.00 cop table:s, keep order:false, stats:pseudo
└─TableReader_33 1.00 root data:TableScan_32
└─TableScan_32 1.00 cop table:t1, range: decided by [test.s.c], keep order:false, stats:pseudo
insert into t values(1, 1, 1), (2, 2 ,2), (3, 3, 3), (4, 3, 4),(5,3,5);
Expand Down Expand Up @@ -687,7 +687,7 @@ Projection_8 8320.83 root test.t.a, test.t1.a
└─UnionScan_14 6656.67 root not(and(ge(test.t1.a, 1), le(test.t1.a, 2))), not(isnull(test.t1.a))
└─TableReader_17 6656.67 root data:Selection_16
└─Selection_16 6656.67 cop not(isnull(test.t1.a)), or(lt(test.t1.a, 1), gt(test.t1.a, 2))
└─TableScan_15 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
└─TableScan_15 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
rollback;
drop table if exists t;
create table t(a time, b date);
Expand Down
4 changes: 2 additions & 2 deletions cmd/explaintest/r/tpch.result
Original file line number Diff line number Diff line change
Expand Up @@ -1241,11 +1241,11 @@ Projection_25 1.00 root tpch.supplier.s_name, 17_col_0
│ │ └─TableScan_59 1.00 cop table:orders, range: decided by [tpch.l1.l_orderkey], keep order:false
│ └─IndexLookUp_55 1.00 root
│ ├─IndexScan_53 1.00 cop table:l2, index:L_ORDERKEY, L_LINENUMBER, range: decided by [eq(tpch.l2.l_orderkey, tpch.l1.l_orderkey)], keep order:false
│ └─TableScan_54 1.00 cop table:lineitem, keep order:false
│ └─TableScan_54 1.00 cop table:l2, keep order:false
└─IndexLookUp_39 0.80 root
├─IndexScan_36 1.00 cop table:l3, index:L_ORDERKEY, L_LINENUMBER, range: decided by [eq(tpch.l3.l_orderkey, tpch.l1.l_orderkey)], keep order:false
└─Selection_38 0.80 cop gt(tpch.l3.l_receiptdate, tpch.l3.l_commitdate)
└─TableScan_37 1.00 cop table:lineitem, keep order:false
└─TableScan_37 1.00 cop table:l3, keep order:false
/*
Q22 Global Sales Opportunity Query
The Global Sales Opportunity Query identifies geographies where there are customers who may be likely to make a
Expand Down
2 changes: 1 addition & 1 deletion planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (p *LogicalJoin) constructInnerIndexScan(ds *DataSource, idx *model.IndexIn
}
if !isCoveringIndex(ds.schema.Columns, is.Index.Columns, is.Table.PKIsHandle) {
// On this way, it's double read case.
ts := PhysicalTableScan{Columns: ds.Columns, Table: is.Table}.Init(ds.ctx)
ts := PhysicalTableScan{Columns: ds.Columns, Table: is.Table, TableAsName: ds.TableAsName}.Init(ds.ctx)
ts.SetSchema(is.dataSourceSchema)
cop.tablePlan = ts
}
Expand Down
1 change: 1 addition & 0 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ func (ds *DataSource) convertToIndexScan(prop *property.PhysicalProperty, candid
ts := PhysicalTableScan{
Columns: ds.Columns,
Table: is.Table,
TableAsName: ds.TableAsName,
isPartition: ds.isPartition,
physicalTableID: ds.physicalTableID,
}.Init(ds.ctx)
Expand Down
8 changes: 3 additions & 5 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,14 @@ func (b *PlanBuilder) buildResultSetNode(ctx context.Context, node ast.ResultSet
case *ast.UnionStmt:
p, err = b.buildUnion(ctx, v)
case *ast.TableName:
p, err = b.buildDataSource(ctx, v)
p, err = b.buildDataSource(ctx, v, &x.AsName)
default:
err = ErrUnsupportedType.GenWithStackByArgs(v)
}
if err != nil {
return nil, err
}

if v, ok := p.(*DataSource); ok {
v.TableAsName = &x.AsName
}
for _, col := range p.Schema().Columns {
col.OrigTblName = col.TblName
if x.AsName.L != "" {
Expand Down Expand Up @@ -2216,7 +2213,7 @@ func getStatsTable(ctx sessionctx.Context, tblInfo *model.TableInfo, pid int64)
return statsTbl
}

func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName) (LogicalPlan, error) {
func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, asName *model.CIStr) (LogicalPlan, error) {
dbName := tn.Schema
if dbName.L == "" {
dbName = model.NewCIStr(b.ctx.GetSessionVars().CurrentDB)
Expand Down Expand Up @@ -2276,6 +2273,7 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName) (L

ds := DataSource{
DBName: dbName,
TableAsName: asName,
table: tbl,
tableInfo: tableInfo,
statisticTable: statisticTable,
Expand Down
2 changes: 1 addition & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func (b *PlanBuilder) buildPhysicalIndexLookUpReader(ctx context.Context, dbName
}.Init(b.ctx)
is.stats = property.NewSimpleStats(0)
// It's double read case.
ts := PhysicalTableScan{Columns: tblReaderCols, Table: is.Table}.Init(b.ctx)
ts := PhysicalTableScan{Columns: tblReaderCols, Table: is.Table, TableAsName: &tblInfo.Name}.Init(b.ctx)
ts.SetSchema(tblSchema)
cop := &copTask{indexPlan: is, tablePlan: ts}
ts.HandleIdx = pkOffset
Expand Down

0 comments on commit 20a9758

Please sign in to comment.