Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CREATE TABLE `t2`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`t1_id` int unsigned NOT NULL,
`col` int unsigned,
PRIMARY KEY (`id`)
) ENGINE InnoDB,
CHARSET utf8mb4,
Expand All @@ -31,10 +32,10 @@ values (1, 'A'),
(3, 'C'),
(4, 'D');

insert into t2 (id, t1_id)
values (1, 1),
(2, 2),
(3, 3);
insert into t2 (id, t1_id, col)
values (1, 1, 1),
(2, 2, 2),
(3, 3, 3);

insert into t3 (id, name)
values (1, 'A'),
Expand Down Expand Up @@ -64,4 +65,10 @@ from (select id, count(*) as num_segments from t1 group by 1 order by 2 desc lim
join t2 u on u.id = t.id;

select name
from (select name from t1 group by name having count(t1.id) > 1) t1;
from (select name from t1 group by name having count(t1.id) > 1) t1;

select t1_id
from (select t1_id, col
from t2
group by 1, 2) t
group by 1;
1 change: 1 addition & 0 deletions go/vt/vtgate/engine/ordered_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (oa *OrderedAggregate) TryExecute(ctx context.Context, vcursor VCursor, bin
return qr.Truncate(oa.TruncateColumnCount), nil
}

// executeGroupBy is used when the plan contains grouping but not aggregations
func (oa *OrderedAggregate) executeGroupBy(result *sqltypes.Result) (*sqltypes.Result, error) {
if len(result.Rows) < 1 {
return result, nil
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func isDerived(op Operator) bool {
}

func (a *Aggregator) GetColumns(ctx *plancontext.PlanningContext) (res []*sqlparser.AliasedExpr) {
if isDerived(a.Source) {
if isDerived(a.Source) && len(a.Aggregations) > 0 {
return truncate(a, a.Columns)
}

Expand Down
38 changes: 38 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -7406,5 +7406,43 @@
"user.user_extra"
]
}
},
{
"comment": "Group-only Aggregation on top of a derived table should return the correct number of columns",
"query": "select col from (select col, intcol from user group by 1, 2) t group by 1",
"plan": {
"QueryType": "SELECT",
"Original": "select col from (select col, intcol from user group by 1, 2) t group by 1",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "0",
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "0, 1",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select col, intcol from `user` where 1 != 1 group by col, intcol",
"OrderBy": "0 ASC, 1 ASC",
"Query": "select col, intcol from `user` group by col, intcol order by col asc, intcol asc",
"Table": "`user`"
}
]
}
]
},
"TablesUsed": [
"user.user"
]
}
}
]
Loading