-
Notifications
You must be signed in to change notification settings - Fork 815
/
Copy pathquery.go
377 lines (349 loc) · 10.6 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2019 The Go Cloud Development Kit Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO(jba): figure out how to get filters with uints to work: since they are represented as
// int64s, the sign is wrong.
package gcpfirestore
import (
"context"
"fmt"
"math"
"path"
"reflect"
"strings"
"time"
pb "cloud.google.com/go/firestore/apiv1/firestorepb"
"gocloud.dev/docstore/driver"
"gocloud.dev/internal/gcerr"
"google.golang.org/protobuf/types/known/wrapperspb"
)
func (c *collection) RunGetQuery(ctx context.Context, q *driver.Query) (driver.DocumentIterator, error) {
return c.newDocIterator(ctx, q)
}
func (c *collection) newDocIterator(ctx context.Context, q *driver.Query) (*docIterator, error) {
sq, localFilters, err := c.queryToProto(q)
if err != nil {
return nil, err
}
req := &pb.RunQueryRequest{
Parent: path.Dir(c.collPath),
QueryType: &pb.RunQueryRequest_StructuredQuery{sq},
}
if q.BeforeQuery != nil {
if err := q.BeforeQuery(driver.AsFunc(req)); err != nil {
return nil, err
}
}
ctx, cancel := context.WithCancel(ctx)
sc, err := c.client.RunQuery(withResourceHeader(ctx, c.dbPath), req)
if err != nil {
cancel()
return nil, err
}
return &docIterator{
streamClient: sc,
nameField: c.nameField,
revField: c.opts.RevisionField,
localFilters: localFilters,
cancel: cancel,
}, nil
}
// //////////////////////////////////////////////////////////////
// The code below is adapted from cloud.google.com/go/firestore.
type docIterator struct {
streamClient pb.Firestore_RunQueryClient
nameField, revField string
localFilters []driver.Filter
// We call cancel to make sure the stream client doesn't leak resources.
// We don't need to call it if Recv() returns a non-nil error.
// See https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
cancel func()
}
func (it *docIterator) Next(ctx context.Context, doc driver.Document) error {
res, err := it.nextResponse(ctx)
if err != nil {
return err
}
return decodeDoc(res.Document, doc, it.nameField, it.revField)
}
func (it *docIterator) nextResponse(ctx context.Context) (*pb.RunQueryResponse, error) {
for {
res, err := it.streamClient.Recv()
if err != nil {
return nil, err
}
// No document => partial progress; keep receiving.
if res.Document == nil {
continue
}
match, err := it.evaluateLocalFilters(res.Document)
if err != nil {
return nil, err
}
if match {
return res, nil
}
}
}
// Report whether the filters are true of the document.
func (it *docIterator) evaluateLocalFilters(pdoc *pb.Document) (bool, error) {
if len(it.localFilters) == 0 {
return true, nil
}
// TODO(jba): optimization: evaluate the filter directly on the proto document, without decoding.
m := map[string]interface{}{}
doc, err := driver.NewDocument(m)
if err != nil {
return false, err
}
if err := decodeDoc(pdoc, doc, it.nameField, it.revField); err != nil {
return false, err
}
for _, f := range it.localFilters {
if !evaluateFilter(f, doc) {
return false, nil
}
}
return true, nil
}
func evaluateFilter(f driver.Filter, doc driver.Document) bool {
val, err := doc.Get(f.FieldPath)
if err != nil {
// Treat a missing field as false.
return false
}
// Compare times.
if t1, ok := val.(time.Time); ok {
if t2, ok := f.Value.(time.Time); ok {
return applyComparison(f.Op, driver.CompareTimes(t1, t2))
}
return false
}
lhs := reflect.ValueOf(val)
rhs := reflect.ValueOf(f.Value)
if lhs.Kind() == reflect.String {
if rhs.Kind() != reflect.String {
return false
}
return applyComparison(f.Op, strings.Compare(lhs.String(), rhs.String()))
}
cmp, err := driver.CompareNumbers(lhs, rhs)
if err != nil {
return false
}
return applyComparison(f.Op, cmp)
}
// op is one of the five permitted docstore operators ("=", "<", etc.)
// c is the result of strings.Compare or the like.
func applyComparison(op string, c int) bool {
switch op {
case driver.EqualOp:
return c == 0
case ">":
return c > 0
case "<":
return c < 0
case ">=":
return c >= 0
case "<=":
return c <= 0
default:
panic("bad op")
}
}
func (it *docIterator) Stop() { it.cancel() }
func (it *docIterator) As(i interface{}) bool {
p, ok := i.(*pb.Firestore_RunQueryClient)
if !ok {
return false
}
*p = it.streamClient
return true
}
// Converts the query to a Firestore proto. Also returns filters that need to be
// evaluated on the client.
func (c *collection) queryToProto(q *driver.Query) (*pb.StructuredQuery, []driver.Filter, error) {
// The collection ID is the last component of the collection path.
collID := path.Base(c.collPath)
p := &pb.StructuredQuery{
From: []*pb.StructuredQuery_CollectionSelector{{CollectionId: collID}},
}
if len(q.FieldPaths) > 0 {
p.Select = &pb.StructuredQuery_Projection{}
for _, fp := range q.FieldPaths {
p.Select.Fields = append(p.Select.Fields, fieldRef(fp))
}
}
// Apply offset.
if q.Offset > 0 {
p.Offset = int32(q.Offset)
}
// Apply limit.
if q.Limit > 0 {
p.Limit = &wrapperspb.Int32Value{Value: int32(q.Limit)}
}
// TODO(jba): make sure we retrieve the fields needed for local filters.
sendFilters, localFilters := splitFilters(q.Filters)
if len(localFilters) > 0 && !c.opts.AllowLocalFilters {
return nil, nil, gcerr.Newf(gcerr.InvalidArgument, nil, "query requires local filters; set Options.AllowLocalFilters to true to enable")
}
// If there is only one filter, use it directly. Otherwise, construct
// a CompositeFilter.
var pfs []*pb.StructuredQuery_Filter
for _, f := range sendFilters {
pf, err := c.filterToProto(f)
if err != nil {
return nil, nil, err
}
pfs = append(pfs, pf)
}
if len(pfs) == 1 {
p.Where = pfs[0]
} else if len(pfs) > 1 {
p.Where = &pb.StructuredQuery_Filter{
FilterType: &pb.StructuredQuery_Filter_CompositeFilter{&pb.StructuredQuery_CompositeFilter{
Op: pb.StructuredQuery_CompositeFilter_AND,
Filters: pfs,
}},
}
}
if q.OrderByField != "" {
// TODO(jba): reorder filters so order-by one is first of inequalities?
// TODO(jba): see if it's OK if filter inequality direction differs from sort direction.
fref := []string{q.OrderByField}
if q.OrderByField == c.nameField {
fref[0] = "__name__"
}
var dir pb.StructuredQuery_Direction
if q.OrderAscending {
dir = pb.StructuredQuery_ASCENDING
} else {
dir = pb.StructuredQuery_DESCENDING
}
p.OrderBy = []*pb.StructuredQuery_Order{{Field: fieldRef(fref), Direction: dir}}
}
// TODO(jba): cursors (start/end)
return p, localFilters, nil
}
// splitFilters separates the list of query filters into those we can send to the Firestore service,
// and those we must evaluate here on the client.
func splitFilters(fs []driver.Filter) (sendToFirestore, evaluateLocally []driver.Filter) {
// Enforce that only one field can have an inequality.
var rangeFP []string
for _, f := range fs {
if f.Op == driver.EqualOp {
sendToFirestore = append(sendToFirestore, f)
} else {
if rangeFP == nil || driver.FieldPathsEqual(rangeFP, f.FieldPath) {
// Multiple inequality filters on the same field are OK.
rangeFP = f.FieldPath
sendToFirestore = append(sendToFirestore, f)
} else {
evaluateLocally = append(evaluateLocally, f)
}
}
}
return sendToFirestore, evaluateLocally
}
func (c *collection) filterToProto(f driver.Filter) (*pb.StructuredQuery_Filter, error) {
// Treat filters on the name field specially.
if c.nameField != "" && driver.FieldPathEqualsField(f.FieldPath, c.nameField) {
v := reflect.ValueOf(f.Value)
if v.Kind() != reflect.String {
return nil, gcerr.Newf(gcerr.InvalidArgument, nil,
"name field filter value %v of type %[1]T is not a string", f.Value)
}
return newFieldFilter([]string{"__name__"}, f.Op,
&pb.Value{ValueType: &pb.Value_ReferenceValue{c.collPath + "/" + v.String()}})
}
// "= nil" and "= NaN" are handled specially.
if uop, ok := unaryOpFor(f.Value); ok {
if f.Op != driver.EqualOp {
return nil, fmt.Errorf("firestore: must use '=' when comparing %v", f.Value)
}
return &pb.StructuredQuery_Filter{
FilterType: &pb.StructuredQuery_Filter_UnaryFilter{
UnaryFilter: &pb.StructuredQuery_UnaryFilter{
OperandType: &pb.StructuredQuery_UnaryFilter_Field{
Field: fieldRef(f.FieldPath),
},
Op: uop,
},
},
}, nil
}
pv, err := encodeValue(f.Value)
if err != nil {
return nil, err
}
return newFieldFilter(f.FieldPath, f.Op, pv)
}
func unaryOpFor(value interface{}) (pb.StructuredQuery_UnaryFilter_Operator, bool) {
switch {
case value == nil:
return pb.StructuredQuery_UnaryFilter_IS_NULL, true
case isNaN(value):
return pb.StructuredQuery_UnaryFilter_IS_NAN, true
default:
return pb.StructuredQuery_UnaryFilter_OPERATOR_UNSPECIFIED, false
}
}
func isNaN(x interface{}) bool {
switch x := x.(type) {
case float32:
return math.IsNaN(float64(x))
case float64:
return math.IsNaN(x)
default:
return false
}
}
func fieldRef(fp []string) *pb.StructuredQuery_FieldReference {
return &pb.StructuredQuery_FieldReference{FieldPath: toServiceFieldPath(fp)}
}
func newFieldFilter(fp []string, op string, val *pb.Value) (*pb.StructuredQuery_Filter, error) {
var fop pb.StructuredQuery_FieldFilter_Operator
switch op {
case "<":
fop = pb.StructuredQuery_FieldFilter_LESS_THAN
case "<=":
fop = pb.StructuredQuery_FieldFilter_LESS_THAN_OR_EQUAL
case ">":
fop = pb.StructuredQuery_FieldFilter_GREATER_THAN
case ">=":
fop = pb.StructuredQuery_FieldFilter_GREATER_THAN_OR_EQUAL
case driver.EqualOp:
fop = pb.StructuredQuery_FieldFilter_EQUAL
case "in":
fop = pb.StructuredQuery_FieldFilter_IN
case "not-in":
fop = pb.StructuredQuery_FieldFilter_NOT_IN
// TODO(jba): can we support array-contains portably?
// case "array-contains":
// fop = pb.StructuredQuery_FieldFilter_ARRAY_CONTAINS
default:
return nil, gcerr.Newf(gcerr.InvalidArgument, nil, "invalid operator: %q", op)
}
return &pb.StructuredQuery_Filter{
FilterType: &pb.StructuredQuery_Filter_FieldFilter{
FieldFilter: &pb.StructuredQuery_FieldFilter{
Field: fieldRef(fp),
Op: fop,
Value: val,
},
},
}, nil
}
func (c *collection) QueryPlan(q *driver.Query) (string, error) {
return "unknown", nil
}