Skip to content

Commit ff36901

Browse files
fix rollup with having (#21834)
fix rollup with having Approved by: @heni02, @ouyuanning, @aressu1985, @aunjgr
1 parent 4a68f8d commit ff36901

File tree

8 files changed

+825
-749
lines changed

8 files changed

+825
-749
lines changed

pkg/pb/plan/plan.pb.go

Lines changed: 769 additions & 732 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/parsers/tree/select.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,9 @@ func (node *SelectClause) GetQueryType() string { return QueryTypeDQL }
390390

391391
// WHERE or HAVING clause.
392392
type Where struct {
393-
Type string
394-
Expr Expr
393+
Type string
394+
RollupHaving bool
395+
Expr Expr
395396
}
396397

397398
func (node *Where) Format(ctx *FmtCtx) {

pkg/sql/plan/pushdown.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,20 @@ func (builder *QueryBuilder) pushdownFilters(nodeID int32, filters []*plan.Expr,
127127

128128
case plan.Node_FILTER:
129129
canPushdown = filters
130-
for _, filter := range node.FilterList {
131-
canPushdown = append(canPushdown, splitPlanConjunction(applyDistributivity(builder.GetContext(), filter))...)
130+
if !node.RollupFilter {
131+
for _, filter := range node.FilterList {
132+
canPushdown = append(canPushdown, splitPlanConjunction(applyDistributivity(builder.GetContext(), filter))...)
133+
}
132134
}
133135

134136
childID, cantPushdownChild := builder.pushdownFilters(node.Children[0], canPushdown, separateNonEquiConds)
135137

136-
if len(cantPushdownChild) > 0 {
138+
if node.RollupFilter {
139+
if len(cantPushdownChild) > 0 {
140+
node.Children[0] = childID
141+
node.FilterList = append(node.FilterList, cantPushdownChild...)
142+
}
143+
} else if len(cantPushdownChild) > 0 {
137144
node.Children[0] = childID
138145
node.FilterList = cantPushdownChild
139146
} else {

pkg/sql/plan/query_builder.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,6 +2638,7 @@ func (builder *QueryBuilder) bindSelect(stmt *tree.Select, ctx *BindContext, isR
26382638
var notCacheable bool
26392639
var helpFunc *helpFunc
26402640
var boundTimeWindowGroupBy *plan.Expr
2641+
var rollupFilter bool
26412642

26422643
astOrderBy := stmt.OrderBy
26432644
astLimit := stmt.Limit
@@ -2731,6 +2732,9 @@ func (builder *QueryBuilder) bindSelect(stmt *tree.Select, ctx *BindContext, isR
27312732
selectStmts := make([]*tree.SelectClause, groupingCount)
27322733
if groupingCount > 1 {
27332734
for i, list := range selectClause.GroupBy.GroupByExprsList {
2735+
if selectClause.Having != nil {
2736+
selectClause.Having.RollupHaving = true
2737+
}
27342738
selectStmts[i] = &tree.SelectClause{
27352739
Distinct: selectClause.Distinct,
27362740
Exprs: selectClause.Exprs,
@@ -2769,6 +2773,9 @@ func (builder *QueryBuilder) bindSelect(stmt *tree.Select, ctx *BindContext, isR
27692773
); err != nil {
27702774
return
27712775
}
2776+
if selectClause.Having != nil {
2777+
rollupFilter = selectClause.Having.RollupHaving
2778+
}
27722779
case *tree.UnionClause:
27732780
return builder.buildUnion(selectClause, astOrderBy, astLimit, ctx, isRoot)
27742781
case *tree.ValuesClause:
@@ -2874,7 +2881,7 @@ func (builder *QueryBuilder) bindSelect(stmt *tree.Select, ctx *BindContext, isR
28742881
return
28752882
}
28762883
} else if len(ctx.groups) > 0 || len(ctx.aggregates) > 0 {
2877-
if nodeID, err = builder.appendAggNode(ctx, nodeID, boundHavingList); err != nil {
2884+
if nodeID, err = builder.appendAggNode(ctx, nodeID, boundHavingList, rollupFilter); err != nil {
28782885
return
28792886
}
28802887
}
@@ -3545,6 +3552,7 @@ func (builder *QueryBuilder) appendAggNode(
35453552
ctx *BindContext,
35463553
nodeID int32,
35473554
boundHavingList []*plan.Expr,
3555+
rollupFilter bool,
35483556
) (newNodeID int32, err error) {
35493557
if ctx.bindingRecurStmt() {
35503558
err = moerr.NewInternalError(builder.GetContext(), "not support aggregate function recursive cte")
@@ -3579,9 +3587,10 @@ func (builder *QueryBuilder) appendAggNode(
35793587
}
35803588

35813589
nodeID = builder.appendNode(&plan.Node{
3582-
NodeType: plan.Node_FILTER,
3583-
Children: []int32{nodeID},
3584-
FilterList: newFilterList,
3590+
NodeType: plan.Node_FILTER,
3591+
Children: []int32{nodeID},
3592+
FilterList: newFilterList,
3593+
RollupFilter: rollupFilter,
35853594
}, ctx)
35863595
}
35873596

pkg/sql/plan/query_builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func TestQueryBuilder_appendAggNode(t *testing.T) {
456456
boundHavingList, err := builder.bindHaving(bindCtx, selectClause.Having, NewHavingBinder(builder, bindCtx))
457457
require.NoError(t, err)
458458

459-
nodeID, err = builder.appendAggNode(bindCtx, nodeID, boundHavingList)
459+
nodeID, err = builder.appendAggNode(bindCtx, nodeID, boundHavingList, false)
460460
require.NoError(t, err)
461461
require.Equal(t, int32(2), nodeID)
462462
require.Equal(t, 3, len(builder.qry.Nodes))

proto/plan.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ message Node {
853853
OnDuplicateAction on_duplicate_action = 66;
854854
string dedup_col_name = 67;
855855
repeated Type dedup_col_types = 68 [(gogoproto.nullable) = false];
856+
bool rollup_filter = 69;
856857
}
857858

858859
// Snapshot Represents a snapshot of the database

test/distributed/cases/window/rollup.result

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,18 @@ product with rollup
151151
having
152152
(product is not null and quarter is not null) or
153153
(product is null and quarter is null);
154-
[unknown result because it is related to issue#19984]
154+
year quarter product total_sales
155+
null null null 22000.00
156+
2022 null null 7000.00
157+
2023 null null 15000.00
158+
2022 Q1 Product A 1000.00
159+
2022 Q1 Product B 1500.00
160+
2022 Q2 Product A 2000.00
161+
2022 Q2 Product C 2500.00
162+
2023 Q1 Product A 3000.00
163+
2023 Q1 Product B 3500.00
164+
2023 Q2 Product B 4000.00
165+
2023 Q2 Product C 4500.00
155166
select
156167
year,
157168
quarter,
@@ -165,7 +176,13 @@ quarter,
165176
product with rollup
166177
having
167178
(product = 'Product A') or (product = 'Product B');
168-
[unknown result because it is related to issue#19984]
179+
year quarter product total_sales
180+
2022 Q1 Product A 1000.00
181+
2022 Q1 Product B 1500.00
182+
2022 Q2 Product A 2000.00
183+
2023 Q1 Product A 3000.00
184+
2023 Q1 Product B 3500.00
185+
2023 Q2 Product B 4000.00
169186
drop table sales;
170187
drop table if exists orders;
171188
drop table if exists order_items;
@@ -367,7 +384,15 @@ group by
367384
j, i with rollup
368385
having
369386
(i is not null and j is not null);
370-
[unknown result because it is related to issue#19984]
387+
i j total
388+
18446744073709551615 2147483647 123214
389+
4294967295 2147483647 2
390+
18446744073709551615 1 2
391+
2147483647 23289483 123214
392+
13289392 2 2
393+
18446744073709551615 23289483 1
394+
3824 13289392 123215
395+
2438294 1 2
371396
drop table uint_64;
372397
drop table if exists sales;
373398
create table sales (

test/distributed/cases/window/rollup.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ group by
8484
product with rollup;
8585
-- @bvt:issue
8686

87-
-- @bvt:issue#19984
8887
select
8988
year,
9089
quarter,
@@ -113,7 +112,6 @@ group by
113112
product with rollup
114113
having
115114
(product = 'Product A') or (product = 'Product B');
116-
-- @bvt:issue
117115
drop table sales;
118116

119117

@@ -301,7 +299,6 @@ group by
301299
j, i with rollup;
302300
-- @bvt:issue
303301

304-
-- @bvt:issue#19984
305302
select
306303
i, j, sum(k) as total
307304
from
@@ -310,7 +307,6 @@ group by
310307
j, i with rollup
311308
having
312309
(i is not null and j is not null);
313-
-- @bvt:issue
314310
drop table uint_64;
315311

316312

0 commit comments

Comments
 (0)