Skip to content
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): Support aggregation query in transaction #8439

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 36 additions & 8 deletions datastore/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ func TestIntegration_AggregationQueries(t *testing.T) {
for i := range keys {
keys[i] = IncompleteKey("SQChild", parent)
}
beforeCreate := time.Now()
keys, err := client.PutMulti(ctx, keys, children)
if err != nil {
t.Fatalf("client.PutMulti: %v", err)
Expand All @@ -729,30 +730,57 @@ func TestIntegration_AggregationQueries(t *testing.T) {
}
}()

baseQuery := NewQuery("SQChild").Ancestor(parent)
// Create transaction with read before create
txBeforeCreate, err := client.NewTransaction(ctx, []TransactionOption{ReadOnly, WithReadTime(beforeCreate)}...)
if err != nil {
t.Fatalf("client.NewTransaction: %v", err)
}

// Create transaction with read after create
txAfterCreate, err := client.NewTransaction(ctx, []TransactionOption{ReadOnly, WithReadTime(time.Now())}...)
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("client.NewTransaction: %v", err)
}

testCases := []struct {
desc string
aggQuery *AggregationQuery
wantFailure bool
wantErrMsg string
wantAggResult AggregationResult
desc string
aggQuery *AggregationQuery
transactionOpts []TransactionOption
wantFailure bool
wantErrMsg string
wantAggResult AggregationResult
}{

{
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: "Count in transaction before creating entities",
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Transaction(txBeforeCreate).NewAggregationQuery().WithCount("count"),
wantAggResult: map[string]interface{}{
"count": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: 0}},
},
},
{
desc: "Count in transaction after creating entities",
aggQuery: NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Transaction(txAfterCreate).NewAggregationQuery().WithCount("count"),
wantAggResult: map[string]interface{}{
"count": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
},
},
}

for _, testCase := range testCases {
Expand Down
1 change: 0 additions & 1 deletion datastore/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,6 @@ func DecodeCursor(s string) (Cursor, error) {
// NewAggregationQuery returns an AggregationQuery with this query as its
// base query.
func (q *Query) NewAggregationQuery() *AggregationQuery {
q.eventual = true
return &AggregationQuery{
query: q,
aggregationQueries: make([]*pb.AggregationQuery_Aggregation, 0),
Expand Down
5 changes: 0 additions & 5 deletions datastore/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,6 @@ func fakeRunAggregationQuery(req *pb.RunAggregationQueryRequest) (*pb.RunAggrega
},
},
},
ReadOptions: &pb.ReadOptions{
ConsistencyType: &pb.ReadOptions_ReadConsistency_{
ReadConsistency: pb.ReadOptions_EVENTUAL,
},
},
}
if !proto.Equal(req, expectedIn) {
return nil, fmt.Errorf("unsupported argument: got %v want %v", req, expectedIn)
Expand Down