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: add support for inequality filters #338

Merged
merged 2 commits into from
Jun 2, 2022
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
5 changes: 5 additions & 0 deletions deploy/datastore/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ indexes:
direction: asc
- name: Timestamps.UpdatedAt
direction: asc

- kind: record
ancestor: yes
properties:
- name: Properties.prop1
21 changes: 17 additions & 4 deletions deploy/terraform/gcp/datastore.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,36 @@
resource "google_datastore_index" "blob_status_updated_at" {
kind = "blob"
properties {
name = "Status"
name = "Status"
direction = "ASCENDING"
}
properties {
name = "Timestamps.UpdatedAt"
name = "Timestamps.UpdatedAt"
direction = "ASCENDING"
}
}

resource "google_datastore_index" "chunk_status_updated_at" {
kind = "chunk"
properties {
name = "Status"
name = "Status"
direction = "ASCENDING"
}
properties {
name = "Timestamps.UpdatedAt"
name = "Timestamps.UpdatedAt"
direction = "ASCENDING"
}
}

resource "google_datastore_index" "default_indexed_properties" {
kind = "record"
ancestor = "ALL_ANCESTORS"
properties {
name = "Properties.prop1"
direction = "ASCENDING"
}
properties {
name = "Properties.prop1"
direction = "DESCENDING"
}
}
70 changes: 69 additions & 1 deletion internal/app/server/open_saves_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ func TestOpenSaves_ExternalBlobSimple(t *testing.T) {
verifyBlob(ctx, t, client, store.Key, record.Key, make([]byte, 0))
}

func TestOpenSaves_QueryRecords_Filter(t *testing.T) {
func TestOpenSaves_QueryRecords_EqualityFilter(t *testing.T) {
ctx := context.Background()
_, listener := getOpenSavesServer(ctx, t, "gcp")
_, client := getTestClient(ctx, t, listener)
Expand Down Expand Up @@ -824,6 +824,74 @@ func TestOpenSaves_QueryRecords_Filter(t *testing.T) {
assert.Equal(t, resp.Records[0].Properties["prop1"].Value, stringVal1)
}

func TestOpenSaves_QueryRecords_InequalityFilter(t *testing.T) {
ctx := context.Background()
_, listener := getOpenSavesServer(ctx, t, "gcp")
_, client := getTestClient(ctx, t, listener)
storeKey := uuid.NewString()
store := &pb.Store{Key: storeKey}
setupTestStore(ctx, t, client, store)

recordKey1 := uuid.NewString()
intVal1 := &pb.Property_IntegerValue{IntegerValue: 10}
setupTestRecord(ctx, t, client, storeKey, &pb.Record{
Key: recordKey1,
Properties: map[string]*pb.Property{
"prop1": {
Type: pb.Property_INTEGER,
Value: intVal1,
},
},
})

recordKey2 := uuid.NewString()
intVal2 := &pb.Property_IntegerValue{IntegerValue: 20}
setupTestRecord(ctx, t, client, storeKey, &pb.Record{
Key: recordKey2,
Properties: map[string]*pb.Property{
"prop1": {
Type: pb.Property_INTEGER,
Value: intVal2,
},
},
})

intVal3 := &pb.Property_IntegerValue{IntegerValue: 0}
queryReq := &pb.QueryRecordsRequest{
StoreKey: storeKey,
Filters: []*pb.QueryFilter{
{
PropertyName: "prop1",
Operator: pb.FilterOperator_GREATER,
Value: &pb.Property{
Type: pb.Property_INTEGER,
Value: intVal3,
},
},
},
}
resp, err := client.QueryRecords(ctx, queryReq)
require.NoError(t, err)

// Both records match the query.
hongalex marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, 2, len(resp.Records))
require.Equal(t, 2, len(resp.StoreKeys))

assert.Equal(t, storeKey, resp.StoreKeys[0])
assert.Equal(t, resp.Records[0].Properties["prop1"].Value, intVal1)
assert.Equal(t, resp.Records[1].Properties["prop1"].Value, intVal2)

// Run a new query that matches only one record.
queryReq.Filters[0].Value.Value = &pb.Property_IntegerValue{IntegerValue: 15}

resp, err = client.QueryRecords(ctx, queryReq)
require.NoError(t, err)

require.Equal(t, 1, len(resp.Records))

assert.Equal(t, resp.Records[0].Properties["prop1"].Value, intVal2)
}

func TestOpenSaves_QueryRecords_Owner(t *testing.T) {
ctx := context.Background()
_, listener := getOpenSavesServer(ctx, t, "gcp")
Expand Down
16 changes: 13 additions & 3 deletions internal/pkg/metadb/metadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,24 @@ func (m *MetaDB) GetChildChunkRefs(ctx context.Context, blobKey uuid.UUID) *chun
return chunkref.NewCursor(m.client.Run(ctx, query))
}

// addPropertyFilter augments a query with the QueryFilter operations.
func addPropertyFilter(q *ds.Query, f *pb.QueryFilter) (*ds.Query, error) {
filter := propertiesField + "." + f.PropertyName
switch f.Operator {
case pb.FilterOperator_EQUAL:
return q.Filter(propertiesField+"."+f.PropertyName+"=", record.ExtractValue(f.Value)), nil
filter += "="
case pb.FilterOperator_GREATER:
filter += ">"
case pb.FilterOperator_LESS:
filter += "<"
case pb.FilterOperator_GREATER_OR_EQUAL:
filter += ">="
case pb.FilterOperator_LESS_OR_EQUAL:
filter += "<="
default:
// TODO(hongalex): implement inequality filters
return nil, status.Errorf(codes.Unimplemented, "only the equality operator is supported currently")
return nil, status.Errorf(codes.Unimplemented, "unknown filter operator detected: %+v", f.Operator)
}
return q.Filter(filter, record.ExtractValue(f.Value)), nil
}

// QueryRecords returns a list of records that match the given filters and their stores.
Expand Down