-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(datastore): SUM and AVG aggregations #8307
Changes from 6 commits
386f606
7b8fb9e
6e0bb47
7d77dfb
826280e
874af9b
58e0a80
64523b2
febcdeb
29f0590
d4bc287
d763f65
c9e359f
95da4a8
00ef889
cc9acd6
ba83ddf
71affc5
9d596ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ import ( | |
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
// TODO(djd): Make test entity clean up more robust: some test entities may | ||
|
@@ -471,6 +472,8 @@ func TestIntegration_NilKey(t *testing.T) { | |
type SQChild struct { | ||
I, J int | ||
T, U int64 | ||
V float64 | ||
W string | ||
} | ||
|
||
type SQTestCase struct { | ||
|
@@ -701,17 +704,17 @@ func TestIntegration_AggregationQueries(t *testing.T) { | |
client := newTestClient(ctx, t) | ||
defer client.Close() | ||
|
||
parent := NameKey("SQParent", "TestIntegration_Filters"+suffix, nil) | ||
parent := NameKey("SQParent", "TestIntegration_AggregationQueries"+suffix, nil) | ||
now := timeNow.Truncate(time.Millisecond).Unix() | ||
children := []*SQChild{ | ||
{I: 0, T: now, U: now}, | ||
{I: 1, T: now, U: now}, | ||
{I: 2, T: now, U: now}, | ||
{I: 3, T: now, U: now}, | ||
{I: 4, T: now, U: now}, | ||
{I: 5, T: now, U: now}, | ||
{I: 6, T: now, U: now}, | ||
{I: 7, T: now, U: now}, | ||
{I: 0, T: now, U: now, V: 1.5, W: "str"}, | ||
{I: 1, T: now, U: now, V: 1.5, W: "str"}, | ||
{I: 2, T: now, U: now, V: 1.5, W: "str"}, | ||
{I: 3, T: now, U: now, V: 1.5, W: "str"}, | ||
{I: 4, T: now, U: now, V: 1.5, W: "str"}, | ||
{I: 5, T: now, U: now, V: 1.5, W: "str"}, | ||
{I: 6, T: now, U: now, V: 1.5, W: "str"}, | ||
{I: 7, T: now, U: now, V: 1.5, W: "str"}, | ||
} | ||
|
||
keys := make([]*Key, len(children)) | ||
|
@@ -739,20 +742,75 @@ func TestIntegration_AggregationQueries(t *testing.T) { | |
}{ | ||
{ | ||
desc: "Count Failure - Missing index", | ||
aggQuery: baseQuery.Filter("T>=", now).NewAggregationQuery().WithCount("count"), | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T>=", now).NewAggregationQuery().WithCount("count"), | ||
wantFailure: true, | ||
wantErrMsg: "no matching index found", | ||
wantAggResult: nil, | ||
}, | ||
{ | ||
desc: "Count Success", | ||
aggQuery: baseQuery.Filter("T=", now).Filter("I>=", 3).NewAggregationQuery().WithCount("count"), | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Filter("I>=", 3).NewAggregationQuery().WithCount("count"), | ||
wantFailure: false, | ||
wantErrMsg: "", | ||
wantAggResult: map[string]interface{}{ | ||
"count": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: 5}}, | ||
}, | ||
}, | ||
{ | ||
desc: "Multiple aggregations", | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).NewAggregationQuery().WithSum("I", "i_sum").WithAvg("I", "avg").WithSum("V", "v_sum"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: stacking these chained calls will help readability. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
wantFailure: false, | ||
wantErrMsg: "", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style nit: would it be better to omit this and rely on the zero value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
wantAggResult: map[string]interface{}{ | ||
"i_sum": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: 28}}, | ||
"v_sum": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: 12}}, | ||
"avg": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: 3.5}}, | ||
}, | ||
}, | ||
{ | ||
desc: "Multiple aggregations with limit ", | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Limit(2).NewAggregationQuery().WithSum("I", "sum").WithAvg("I", "avg"), | ||
wantFailure: false, | ||
wantErrMsg: "", | ||
wantAggResult: map[string]interface{}{ | ||
"sum": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: 1}}, | ||
"avg": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: 0.5}}, | ||
}, | ||
}, | ||
{ | ||
desc: "Multiple aggregations on non-numeric field", | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Limit(2).NewAggregationQuery().WithSum("W", "sum").WithAvg("W", "avg"), | ||
wantFailure: false, | ||
wantErrMsg: "", | ||
wantAggResult: map[string]interface{}{ | ||
"sum": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}}, | ||
"avg": &pb.Value{ValueType: &pb.Value_NullValue{NullValue: structpb.NullValue_NULL_VALUE}}, | ||
}, | ||
}, | ||
{ | ||
desc: "Sum aggregation without alias", | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).NewAggregationQuery().WithSum("I", ""), | ||
wantFailure: false, | ||
wantErrMsg: "", | ||
wantAggResult: map[string]interface{}{ | ||
"property_1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: 28}}, | ||
}, | ||
}, | ||
{ | ||
desc: "Average aggregation without alias", | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).NewAggregationQuery().WithAvg("I", ""), | ||
wantFailure: false, | ||
wantErrMsg: "", | ||
wantAggResult: map[string]interface{}{ | ||
"property_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: 3.5}}, | ||
}, | ||
}, | ||
{ | ||
desc: "Aggregation with invalid field name", | ||
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).NewAggregationQuery().WithSum("I2", "").WithAvg("I2", ""), | ||
wantFailure: true, | ||
wantErrMsg: "", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,17 @@ indexes: | |
properties: | ||
- name: T | ||
- name: J | ||
- name: U | ||
- name: U | ||
|
||
- kind: SQChild | ||
ancestor: yes | ||
properties: | ||
- name: T | ||
- name: I | ||
- name: V | ||
|
||
- kind: SQChild | ||
ancestor: yes | ||
properties: | ||
- name: T | ||
- name: W | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit missing newline at end of file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name is a little unclear here as it looks like
TestIntegration_Filters
are being removed - should we separate them out into two keys?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No testcases are being removed here. The data for this testcase TestIntegration_AggregationQueries was copied from Filters testcase but the name wasn't modified by mistake. So, modified now.