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
19 changes: 13 additions & 6 deletions go/vt/vtgate/engine/ordered_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,21 @@ func (oa *OrderedAggregate) StreamExecute(vcursor VCursor, bindVars map[string]*
return nil
}

func (oa *OrderedAggregate) convertFields(fields []*querypb.Field) (newFields []*querypb.Field) {
func (oa *OrderedAggregate) convertFields(fields []*querypb.Field) []*querypb.Field {
if !oa.HasDistinct {
return fields
}
newFields = append(newFields, fields...)

for _, aggr := range oa.Aggregates {
if !aggr.isDistinct() {
continue
}
newFields[aggr.Col] = &querypb.Field{
fields[aggr.Col] = &querypb.Field{
Name: aggr.Alias,
Type: opcodeType[aggr.Opcode],
}
}
return newFields
return fields
}

func (oa *OrderedAggregate) convertRow(row []sqltypes.Value) (newRow []sqltypes.Value, curDistinct sqltypes.Value) {
Expand Down Expand Up @@ -375,10 +375,17 @@ func (oa *OrderedAggregate) createEmptyRow() ([]sqltypes.Value, error) {

func createEmptyValueFor(opcode AggregateOpcode) (sqltypes.Value, error) {
switch opcode {
case AggregateCountDistinct:
case
AggregateCountDistinct,
AggregateCount:
return countZero, nil
case AggregateSumDistinct:
case
AggregateSumDistinct,
AggregateSum,
AggregateMin,
AggregateMax:
return sqltypes.NULL, nil

}
return sqltypes.NULL, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "unknown aggregation %v", opcode)
}
111 changes: 74 additions & 37 deletions go/vt/vtgate/engine/ordered_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,42 +666,79 @@ func TestMerge(t *testing.T) {
assert.Equal(want, merged)
}

func TestNoInputAndNoGroupingKeys(t *testing.T) {
assert := assert.New(t)
fp := &fakePrimitive{
results: []*sqltypes.Result{sqltypes.MakeTestResult(
sqltypes.MakeTestFields(
"col1|col2",
"int64|int64",
),
// Empty input table
)},
func TestNoInputAndNoGroupingKeys(outer *testing.T) {
testCases := []struct {
name string
opcode AggregateOpcode
expectedVal string
expectedTyp string
}{{
"count(distinct col1)",
AggregateCountDistinct,
"0",
"int64",
}, {
"col1",
AggregateCount,
"0",
"int64",
}, {
"sum(distinct col1)",
AggregateSumDistinct,
"null",
"decimal",
}, {
"col1",
AggregateSum,
"null",
"int64",
}, {
"col1",
AggregateMax,
"null",
"int64",
}, {
"col1",
AggregateMin,
"null",
"int64",
}}

for _, test := range testCases {
outer.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
fp := &fakePrimitive{
results: []*sqltypes.Result{sqltypes.MakeTestResult(
sqltypes.MakeTestFields(
"col1",
"int64",
),
// Empty input table
)},
}

oa := &OrderedAggregate{
HasDistinct: true,
Aggregates: []AggregateParams{{
Opcode: test.opcode,
Col: 0,
Alias: test.name,
}},
Keys: []int{},
Input: fp,
}

result, err := oa.Execute(nil, nil, false)
assert.NoError(err)

wantResult := sqltypes.MakeTestResult(
sqltypes.MakeTestFields(
test.name,
test.expectedTyp,
),
test.expectedVal,
)
assert.Equal(wantResult, result)
})
}

oa := &OrderedAggregate{
HasDistinct: true,
Aggregates: []AggregateParams{{
Opcode: AggregateCountDistinct,
Col: 0,
Alias: "count(distinct col2)",
}, {
Opcode: AggregateSumDistinct,
Col: 1,
Alias: "sum(distinct col2)",
}},
Keys: []int{},
Input: fp,
}

result, err := oa.Execute(nil, nil, false)
assert.NoError(err)

wantResult := sqltypes.MakeTestResult(
sqltypes.MakeTestFields(
"count(distinct col2)|sum(distinct col2)",
"int64|decimal",
),
"0|null",
)
assert.Equal(wantResult, result)
}