Skip to content

Commit

Permalink
address eurekaka's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bb7133 committed Jun 30, 2019
1 parent 9b6893e commit 3fd835b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
16 changes: 8 additions & 8 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -194,31 +194,31 @@ HashAgg_18 24000.00 root group by:c1, funcs:firstrow(join_agg_0)
explain select count(1) from (select count(1) from (select * from t1 where c3 = 100) k) k2;
id count task operator info
StreamAgg_13 1.00 root funcs:count(1)
└─StreamAgg_28 1.00 root funcs:count(col_0)
└─StreamAgg_28 1.00 root funcs:firstrow(col_0)
└─TableReader_29 1.00 root data:StreamAgg_17
└─StreamAgg_17 1.00 cop funcs:count(1)
└─StreamAgg_17 1.00 cop funcs:firstrow(1)
└─Selection_27 10.00 cop eq(test.t1.c3, 100)
└─TableScan_26 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
explain select 1 from (select count(c2), count(c3) from t1) k;
id count task operator info
Projection_5 1.00 root 1
└─StreamAgg_17 1.00 root funcs:count(col_0)
└─StreamAgg_17 1.00 root funcs:firstrow(col_0)
└─TableReader_18 1.00 root data:StreamAgg_9
└─StreamAgg_9 1.00 cop funcs:count(1)
└─StreamAgg_9 1.00 cop funcs:firstrow(1)
└─TableScan_16 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
explain select count(1) from (select max(c2), count(c3) as m from t1) k;
id count task operator info
StreamAgg_11 1.00 root funcs:count(1)
└─StreamAgg_23 1.00 root funcs:count(col_0)
└─StreamAgg_23 1.00 root funcs:firstrow(col_0)
└─TableReader_24 1.00 root data:StreamAgg_15
└─StreamAgg_15 1.00 cop funcs:count(1)
└─StreamAgg_15 1.00 cop funcs:firstrow(1)
└─TableScan_22 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
explain select count(1) from (select count(c2) from t1 group by c3) k;
id count task operator info
StreamAgg_11 1.00 root funcs:count(1)
└─HashAgg_23 8000.00 root group by:col_0,
└─HashAgg_23 8000.00 root group by:col_1, funcs:firstrow(col_0)
└─TableReader_24 8000.00 root data:HashAgg_20
└─HashAgg_20 8000.00 cop group by:test.t1.c3,
└─HashAgg_20 8000.00 cop group by:test.t1.c3, funcs:firstrow(1)
└─TableScan_15 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
set @@session.tidb_opt_insubq_to_join_and_agg=0;
explain select sum(t1.c1 in (select c1 from t2)) from t1;
Expand Down
14 changes: 9 additions & 5 deletions planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,19 @@ func (la *LogicalAggregation) PruneColumns(parentUsedCols []*expression.Column)
for _, aggrFunc := range la.AggFuncs {
selfUsedCols = expression.ExtractColumnsFromExpressions(selfUsedCols, aggrFunc.Args, nil)
}
if len(la.GroupByItems) == 0 && len(la.AggFuncs) == 0 {
// If all the aggregate functions are pruned and there is no group-by item, we should add
// an aggregate function to keep the correctness.
one, err := aggregation.NewAggFuncDesc(la.ctx, ast.AggFuncCount, []expression.Expression{expression.One}, false)
if len(la.AggFuncs) == 0 {
// If all the aggregate functions are pruned, we should add an aggregate function to keep the correctness.
one, err := aggregation.NewAggFuncDesc(la.ctx, ast.AggFuncFirstRow, []expression.Expression{expression.One}, false)
if err != nil {
return err
}
la.AggFuncs = []*aggregation.AggFuncDesc{one}
la.schema.Columns = []*expression.Column{{TblName: model.NewCIStr("dummy_cnt"), RetType: types.NewFieldType(mysql.TypeLonglong)}}
col := &expression.Column{
ColName: model.NewCIStr("dummy_agg"),
UniqueID: la.ctx.GetSessionVars().AllocPlanColumnID(),
RetType: types.NewFieldType(mysql.TypeLonglong),
}
la.schema.Columns = []*expression.Column{col}
}

if len(la.GroupByItems) > 0 {
Expand Down

0 comments on commit 3fd835b

Please sign in to comment.