Skip to content

Commit

Permalink
cherry pick pingcap#21431 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
dyzsr authored and ti-srebot committed Dec 18, 2020
1 parent 8af4c30 commit f5a939b
Show file tree
Hide file tree
Showing 15 changed files with 787 additions and 50 deletions.
68 changes: 68 additions & 0 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,15 @@ drop table if exists t;
create table t(a int, b int, c int);
explain select * from (select * from t order by (select 2)) t order by a, b;
id estRows task access object operator info
<<<<<<< HEAD
Sort_12 10000.00 root test.t.a:asc, test.t.b:asc
└─TableReader_18 10000.00 root data:TableFullScan_17
└─TableFullScan_17 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
=======
Sort_13 10000.00 root test.t.a, test.t.b
└─TableReader_19 10000.00 root data:TableFullScan_18
└─TableFullScan_18 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
>>>>>>> f687ebd91... planner: fix correlated aggregates which should be evaluated in outer query (#21431)
explain select * from (select * from t order by c) t order by a, b;
id estRows task access object operator info
Sort_6 10000.00 root test.t.a:asc, test.t.b:asc
Expand Down Expand Up @@ -784,3 +790,65 @@ Update_4 N/A root N/A
├─IndexRangeScan_9(Build) 0.10 cop[tikv] table:t, index:a(a, b) range:["[250 52 225 9 60 180 40 72 87 52 227 145 127 0 0 0]" "xb","[250 52 225 9 60 180 40 72 87 52 227 145 127 0 0 0]" "xb"], keep order:false, stats:pseudo
└─TableRowIDScan_10(Probe) 0.10 cop[tikv] table:t keep order:false, stats:pseudo
drop table if exists t;
create table t(a int, b int);
explain select (select count(n.a) from t) from t n;
id estRows task access object operator info
Projection_9 1.00 root Column#8
└─Apply_11 1.00 root CARTESIAN left outer join
├─StreamAgg_23(Build) 1.00 root funcs:count(Column#13)->Column#7
│ └─TableReader_24 1.00 root data:StreamAgg_15
│ └─StreamAgg_15 1.00 cop[tikv] funcs:count(test.t.a)->Column#13
│ └─TableFullScan_22 10000.00 cop[tikv] table:n keep order:false, stats:pseudo
└─MaxOneRow_27(Probe) 1.00 root
└─Projection_28 2.00 root Column#7
└─TableReader_30 2.00 root data:TableFullScan_29
└─TableFullScan_29 2.00 cop[tikv] table:t keep order:false, stats:pseudo
explain select (select sum((select count(a)))) from t;
id estRows task access object operator info
Projection_23 1.00 root Column#7
└─Apply_25 1.00 root CARTESIAN left outer join
├─StreamAgg_37(Build) 1.00 root funcs:count(Column#15)->Column#5
│ └─TableReader_38 1.00 root data:StreamAgg_29
│ └─StreamAgg_29 1.00 cop[tikv] funcs:count(test.t.a)->Column#15
│ └─TableFullScan_36 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
└─HashAgg_43(Probe) 1.00 root funcs:sum(Column#12)->Column#7
└─HashJoin_44 1.00 root CARTESIAN left outer join
├─HashAgg_49(Build) 1.00 root group by:1, funcs:sum(Column#16)->Column#12
│ └─Projection_54 1.00 root cast(Column#6, decimal(42,0) BINARY)->Column#16
│ └─MaxOneRow_50 1.00 root
│ └─Projection_51 1.00 root Column#5
│ └─TableDual_52 1.00 root rows:1
└─TableDual_46(Probe) 1.00 root rows:1
explain select count(a) from t group by b order by (select count(a));
id estRows task access object operator info
Sort_12 8000.00 root Column#4
└─HashJoin_14 8000.00 root CARTESIAN left outer join
├─TableDual_24(Build) 1.00 root rows:1
└─HashAgg_20(Probe) 8000.00 root group by:test.t.b, funcs:count(Column#8)->Column#4
└─TableReader_21 8000.00 root data:HashAgg_16
└─HashAgg_16 8000.00 cop[tikv] group by:test.t.b, funcs:count(test.t.a)->Column#8
└─TableFullScan_19 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
explain select (select sum(count(a))) from t;
id estRows task access object operator info
Projection_11 1.00 root Column#5
└─Apply_13 1.00 root CARTESIAN left outer join
├─StreamAgg_25(Build) 1.00 root funcs:count(Column#8)->Column#4
│ └─TableReader_26 1.00 root data:StreamAgg_17
│ └─StreamAgg_17 1.00 cop[tikv] funcs:count(test.t.a)->Column#8
│ └─TableFullScan_24 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
└─StreamAgg_32(Probe) 1.00 root funcs:sum(Column#9)->Column#5
└─Projection_39 1.00 root cast(Column#4, decimal(42,0) BINARY)->Column#9
└─TableDual_37 1.00 root rows:1
explain select sum(a), (select sum(a)), count(a) from t group by b order by (select count(a));
id estRows task access object operator info
Projection_16 8000.00 root Column#4, Column#4, Column#5
└─Sort_17 8000.00 root Column#5
└─HashJoin_19 8000.00 root CARTESIAN left outer join
├─TableDual_33(Build) 1.00 root rows:1
└─HashJoin_21(Probe) 8000.00 root CARTESIAN left outer join
├─TableDual_31(Build) 1.00 root rows:1
└─HashAgg_27(Probe) 8000.00 root group by:test.t.b, funcs:sum(Column#13)->Column#4, funcs:count(Column#14)->Column#5
└─TableReader_28 8000.00 root data:HashAgg_23
└─HashAgg_23 8000.00 cop[tikv] group by:test.t.b, funcs:sum(test.t.a)->Column#13, funcs:count(test.t.a)->Column#14
└─TableFullScan_26 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
drop table if exists t;
17 changes: 17 additions & 0 deletions cmd/explaintest/r/tpch.result
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ and n_name = 'MOZAMBIQUE'
order by
value desc;
id estRows task access object operator info
<<<<<<< HEAD
Projection_52 1304801.67 root tpch.partsupp.ps_partkey, Column#18
└─Sort_53 1304801.67 root Column#18:desc
└─Selection_55 1304801.67 root gt(Column#18, NULL)
Expand All @@ -725,6 +726,22 @@ Projection_52 1304801.67 root tpch.partsupp.ps_partkey, Column#18
│ └─TableFullScan_71 500000.00 cop[tikv] table:supplier keep order:false
└─TableReader_77(Probe) 40000000.00 root data:TableFullScan_76
└─TableFullScan_76 40000000.00 cop[tikv] table:partsupp keep order:false
=======
Projection_57 1304801.67 root tpch.partsupp.ps_partkey, Column#35
└─Sort_58 1304801.67 root Column#35:desc
└─Selection_60 1304801.67 root gt(Column#35, NULL)
└─HashAgg_63 1631002.09 root group by:Column#44, funcs:sum(Column#42)->Column#35, funcs:firstrow(Column#43)->tpch.partsupp.ps_partkey
└─Projection_89 1631002.09 root mul(tpch.partsupp.ps_supplycost, cast(tpch.partsupp.ps_availqty, decimal(20,0) BINARY))->Column#42, tpch.partsupp.ps_partkey, tpch.partsupp.ps_partkey
└─HashJoin_67 1631002.09 root inner join, equal:[eq(tpch.supplier.s_suppkey, tpch.partsupp.ps_suppkey)]
├─HashJoin_80(Build) 20000.00 root inner join, equal:[eq(tpch.nation.n_nationkey, tpch.supplier.s_nationkey)]
│ ├─TableReader_85(Build) 1.00 root data:Selection_84
│ │ └─Selection_84 1.00 cop[tikv] eq(tpch.nation.n_name, "MOZAMBIQUE")
│ │ └─TableFullScan_83 25.00 cop[tikv] table:nation keep order:false
│ └─TableReader_82(Probe) 500000.00 root data:TableFullScan_81
│ └─TableFullScan_81 500000.00 cop[tikv] table:supplier keep order:false
└─TableReader_87(Probe) 40000000.00 root data:TableFullScan_86
└─TableFullScan_86 40000000.00 cop[tikv] table:partsupp keep order:false
>>>>>>> f687ebd91... planner: fix correlated aggregates which should be evaluated in outer query (#21431)
/*
Q12 Shipping Modes and Order Priority Query
This query determines whether selecting less expensive modes of shipping is negatively affecting the critical-priority
Expand Down
8 changes: 8 additions & 0 deletions cmd/explaintest/t/explain_easy.test
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,11 @@ create table t(a binary(16) not null, b varchar(2) default null, c varchar(100)
explain select * from t where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
explain update t set c = 'ssss' where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
drop table if exists t;

create table t(a int, b int);
explain select (select count(n.a) from t) from t n;
explain select (select sum((select count(a)))) from t;
explain select count(a) from t group by b order by (select count(a));
explain select (select sum(count(a))) from t;
explain select sum(a), (select sum(a)), count(a) from t group by b order by (select count(a));
drop table if exists t;
8 changes: 4 additions & 4 deletions executor/testdata/agg_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@
"Name": "TestIssue12759HashAggCalledByApply",
"Cases": [
[
"Projection_28 1.00 root Column#3, Column#6, Column#9, Column#12",
"Projection_28 1.00 root Column#9, Column#10, Column#11, Column#12",
"└─Apply_30 1.00 root CARTESIAN left outer join",
" ├─Apply_32(Build) 1.00 root CARTESIAN left outer join",
" │ ├─Apply_34(Build) 1.00 root CARTESIAN left outer join",
" │ │ ├─HashAgg_39(Build) 1.00 root funcs:sum(Column#22)->Column#3, funcs:firstrow(Column#23)->test.test.a",
" │ │ ├─HashAgg_39(Build) 1.00 root funcs:sum(Column#22)->Column#9, funcs:firstrow(Column#23)->test.test.a",
" │ │ │ └─TableReader_40 1.00 root data:HashAgg_35",
" │ │ │ └─HashAgg_35 1.00 cop[tikv] funcs:sum(test.test.a)->Column#22, funcs:firstrow(test.test.a)->Column#23",
" │ │ │ └─TableFullScan_38 10000.00 cop[tikv] table:tt keep order:false, stats:pseudo",
" │ │ └─Projection_43(Probe) 1.00 root <nil>->Column#6",
" │ │ └─Projection_43(Probe) 1.00 root <nil>->Column#10",
" │ │ └─Limit_44 1.00 root offset:0, count:1",
" │ │ └─TableReader_50 1.00 root data:Limit_49",
" │ │ └─Limit_49 1.00 cop[tikv] offset:0, count:1",
" │ │ └─Selection_48 1.00 cop[tikv] eq(test.test.a, test.test.a)",
" │ │ └─TableFullScan_47 1000.00 cop[tikv] table:test keep order:false, stats:pseudo",
" │ └─Projection_54(Probe) 1.00 root <nil>->Column#9",
" │ └─Projection_54(Probe) 1.00 root <nil>->Column#11",
" │ └─Limit_55 1.00 root offset:0, count:1",
" │ └─TableReader_61 1.00 root data:Limit_60",
" │ └─Limit_60 1.00 cop[tikv] offset:0, count:1",
Expand Down
2 changes: 1 addition & 1 deletion planner/cascades/stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ func (s *testStringerSuite) TestGroupStringer(c *C) {
output[i].SQL = sql
output[i].Result = ToString(group)
})
c.Assert(ToString(group), DeepEquals, output[i].Result)
c.Assert(ToString(group), DeepEquals, output[i].Result, Commentf("case:%v, sql:%s", i, sql))
}
}
22 changes: 22 additions & 0 deletions planner/cascades/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@
{
"SQL": "select sum(a), (select t1.a from t1 where t1.a = t2.a limit 1), (select t1.b from t1 where t1.b = t2.b limit 1) from t2",
"Plan": [
<<<<<<< HEAD
"Projection_28 1.00 root Column#3, test.t1.a, test.t1.b",
"└─Apply_30 1.00 root CARTESIAN left outer join",
" ├─Apply_32(Build) 1.00 root CARTESIAN left outer join",
Expand All @@ -1038,6 +1039,27 @@
" └─Limit_49 1.00 cop[tikv] offset:0, count:1",
" └─Selection_50 1.00 cop[tikv] eq(test.t1.b, test.t2.b)",
" └─TableFullScan_51 1.00 cop[tikv] table:t1 keep order:false, stats:pseudo"
=======
"Projection_30 1.00 root Column#7, test.t1.a, test.t1.b",
"└─Apply_32 1.00 root CARTESIAN left outer join",
" ├─Apply_34(Build) 1.00 root CARTESIAN left outer join",
" │ ├─HashAgg_39(Build) 1.00 root funcs:sum(Column#8)->Column#7, funcs:firstrow(Column#9)->test.t2.a, funcs:firstrow(Column#10)->test.t2.b",
" │ │ └─TableReader_40 1.00 root data:HashAgg_41",
" │ │ └─HashAgg_41 1.00 cop[tikv] funcs:sum(test.t2.a)->Column#8, funcs:firstrow(test.t2.a)->Column#9, funcs:firstrow(test.t2.b)->Column#10",
" │ │ └─TableFullScan_38 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
" │ └─MaxOneRow_42(Probe) 1.00 root ",
" │ └─Limit_43 1.00 root offset:0, count:1",
" │ └─TableReader_44 1.00 root data:Limit_45",
" │ └─Limit_45 1.00 cop[tikv] offset:0, count:1",
" │ └─Selection_46 1.00 cop[tikv] eq(test.t1.a, test.t2.a)",
" │ └─TableFullScan_47 1.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
" └─MaxOneRow_48(Probe) 1.00 root ",
" └─Limit_49 1.00 root offset:0, count:1",
" └─TableReader_50 1.00 root data:Limit_51",
" └─Limit_51 1.00 cop[tikv] offset:0, count:1",
" └─Selection_52 1.00 cop[tikv] eq(test.t1.b, test.t2.b)",
" └─TableFullScan_53 1.00 cop[tikv] table:t1 keep order:false, stats:pseudo"
>>>>>>> f687ebd91... planner: fix correlated aggregates which should be evaluated in outer query (#21431)
],
"Result": [
"6 1 11"
Expand Down
2 changes: 1 addition & 1 deletion planner/cascades/testdata/stringer_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
"SQL": "select a = (select a from t t2 where t1.b = t2.b order by a limit 1) from t t1",
"Result": [
"Group#0 Schema:[Column#25]",
" Projection_2 input:[Group#1], eq(test.t.a, test.t.a)->Column#25",
" Projection_3 input:[Group#1], eq(test.t.a, test.t.a)->Column#25",
"Group#1 Schema:[test.t.a,test.t.b,test.t.a]",
" Apply_9 input:[Group#2,Group#3], left outer join",
"Group#2 Schema:[test.t.a,test.t.b], UniqueKey:[test.t.a]",
Expand Down
19 changes: 16 additions & 3 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,24 @@ func (er *expressionRewriter) Enter(inNode ast.Node) (ast.Node, bool) {
if er.aggrMap != nil {
index, ok = er.aggrMap[v]
}
if !ok {
er.err = ErrInvalidGroupFuncUse
if ok {
// index < 0 indicates this is a correlated aggregate belonging to outer query,
// for which a correlated column will be created later, so we append a null constant
// as a temporary result expression.
if index < 0 {
er.ctxStackAppend(expression.NewNull(), types.EmptyName)
} else {
// index >= 0 indicates this is a regular aggregate column
er.ctxStackAppend(er.schema.Columns[index], er.names[index])
}
return inNode, true
}
er.ctxStackAppend(er.schema.Columns[index], er.names[index])
// replace correlated aggregate in sub-query with its corresponding correlated column
if col, ok := er.b.correlatedAggMapper[v]; ok {
er.ctxStackAppend(col, types.EmptyName)
return inNode, true
}
er.err = ErrInvalidGroupFuncUse
return inNode, true
case *ast.ColumnNameExpr:
if index, ok := er.b.colMapper[v]; ok {
Expand Down
Loading

0 comments on commit f5a939b

Please sign in to comment.