-
Notifications
You must be signed in to change notification settings - Fork 285
/
sql.go
599 lines (502 loc) · 18.8 KB
/
sql.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package common
import (
"context"
"math"
"strings"
sq "github.com/Masterminds/squirrel"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/jzelinskie/stringz"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/options"
"github.com/authzed/spicedb/pkg/spiceerrors"
)
var (
// CaveatNameKey is a tracing attribute representing a caveat name
CaveatNameKey = attribute.Key("authzed.com/spicedb/sql/caveatName")
// ObjNamespaceNameKey is a tracing attribute representing the resource
// object type.
ObjNamespaceNameKey = attribute.Key("authzed.com/spicedb/sql/objNamespaceName")
// ObjRelationNameKey is a tracing attribute representing the resource
// relation.
ObjRelationNameKey = attribute.Key("authzed.com/spicedb/sql/objRelationName")
// ObjIDKey is a tracing attribute representing the resource object ID.
ObjIDKey = attribute.Key("authzed.com/spicedb/sql/objId")
// SubNamespaceNameKey is a tracing attribute representing the subject object
// type.
SubNamespaceNameKey = attribute.Key("authzed.com/spicedb/sql/subNamespaceName")
// SubRelationNameKey is a tracing attribute representing the subject
// relation.
SubRelationNameKey = attribute.Key("authzed.com/spicedb/sql/subRelationName")
// SubObjectIDKey is a tracing attribute representing the the subject object
// ID.
SubObjectIDKey = attribute.Key("authzed.com/spicedb/sql/subObjectId")
tracer = otel.Tracer("spicedb/internal/datastore/common")
)
// PaginationFilterType is an enumerator
type PaginationFilterType uint8
const (
// TupleComparison uses a comparison with a compound key,
// e.g. (namespace, object_id, relation) > ('ns', '123', 'viewer')
// which is not compatible with all datastores.
TupleComparison PaginationFilterType = iota
// ExpandedLogicComparison comparison uses a nested tree of ANDs and ORs to properly
// filter out already received relationships. Useful for databases that do not support
// tuple comparison, or do not execute it efficiently
ExpandedLogicComparison
)
// SchemaInformation holds the schema information from the SQL datastore implementation.
type SchemaInformation struct {
colNamespace string
colObjectID string
colRelation string
colUsersetNamespace string
colUsersetObjectID string
colUsersetRelation string
colCaveatName string
colExpiration string
paginationFilterType PaginationFilterType
nowFunction string
}
func NewSchemaInformation(
colNamespace,
colObjectID,
colRelation,
colUsersetNamespace,
colUsersetObjectID,
colUsersetRelation,
colCaveatName string,
colExpiration string,
paginationFilterType PaginationFilterType,
nowFunction string,
) SchemaInformation {
return SchemaInformation{
colNamespace,
colObjectID,
colRelation,
colUsersetNamespace,
colUsersetObjectID,
colUsersetRelation,
colCaveatName,
colExpiration,
paginationFilterType,
nowFunction,
}
}
// SchemaQueryFilterer wraps a SchemaInformation and SelectBuilder to give an opinionated
// way to build query objects.
type SchemaQueryFilterer struct {
schema SchemaInformation
queryBuilder sq.SelectBuilder
filteringColumnCounts map[string]int
filterMaximumIDCount uint16
}
// NewSchemaQueryFilterer creates a new SchemaQueryFilterer object.
func NewSchemaQueryFilterer(schema SchemaInformation, initialQuery sq.SelectBuilder, filterMaximumIDCount uint16) SchemaQueryFilterer {
if filterMaximumIDCount == 0 {
filterMaximumIDCount = 100
log.Warn().Msg("SchemaQueryFilterer: filterMaximumIDCount not set, defaulting to 100")
}
// Filter out any expired relationships.
initialQuery = initialQuery.Where(sq.Or{
sq.Eq{schema.colExpiration: nil},
sq.Expr(schema.colExpiration + " > " + schema.nowFunction + "()"),
})
return SchemaQueryFilterer{
schema: schema,
queryBuilder: initialQuery,
filteringColumnCounts: map[string]int{},
filterMaximumIDCount: filterMaximumIDCount,
}
}
func (sqf SchemaQueryFilterer) UnderlyingQueryBuilder() sq.SelectBuilder {
return sqf.queryBuilder
}
func (sqf SchemaQueryFilterer) TupleOrder(order options.SortOrder) SchemaQueryFilterer {
switch order {
case options.ByResource:
sqf.queryBuilder = sqf.queryBuilder.OrderBy(
sqf.schema.colNamespace,
sqf.schema.colObjectID,
sqf.schema.colRelation,
sqf.schema.colUsersetNamespace,
sqf.schema.colUsersetObjectID,
sqf.schema.colUsersetRelation,
)
case options.BySubject:
sqf.queryBuilder = sqf.queryBuilder.OrderBy(
sqf.schema.colUsersetNamespace,
sqf.schema.colUsersetObjectID,
sqf.schema.colUsersetRelation,
sqf.schema.colNamespace,
sqf.schema.colObjectID,
sqf.schema.colRelation,
)
}
return sqf
}
type nameAndValue struct {
name string
value string
}
func (sqf SchemaQueryFilterer) After(cursor options.Cursor, order options.SortOrder) SchemaQueryFilterer {
spiceerrors.DebugAssertNotNil(cursor, "cursor cannot be nil")
// NOTE: The ordering of these columns can affect query performance, be aware when changing.
columnsAndValues := map[options.SortOrder][]nameAndValue{
options.ByResource: {
{
sqf.schema.colNamespace, cursor.Resource.ObjectType,
},
{
sqf.schema.colObjectID, cursor.Resource.ObjectID,
},
{
sqf.schema.colRelation, cursor.Resource.Relation,
},
{
sqf.schema.colUsersetNamespace, cursor.Subject.ObjectType,
},
{
sqf.schema.colUsersetObjectID, cursor.Subject.ObjectID,
},
{
sqf.schema.colUsersetRelation, cursor.Subject.Relation,
},
},
options.BySubject: {
{
sqf.schema.colUsersetNamespace, cursor.Subject.ObjectType,
},
{
sqf.schema.colUsersetObjectID, cursor.Subject.ObjectID,
},
{
sqf.schema.colNamespace, cursor.Resource.ObjectType,
},
{
sqf.schema.colObjectID, cursor.Resource.ObjectID,
},
{
sqf.schema.colRelation, cursor.Resource.Relation,
},
{
sqf.schema.colUsersetRelation, cursor.Subject.Relation,
},
},
}[order]
switch sqf.schema.paginationFilterType {
case TupleComparison:
// For performance reasons, remove any column names that have static values in the query.
columnNames := make([]string, 0, len(columnsAndValues))
valueSlots := make([]any, 0, len(columnsAndValues))
comparisonSlotCount := 0
for _, cav := range columnsAndValues {
if sqf.filteringColumnCounts[cav.name] != 1 {
columnNames = append(columnNames, cav.name)
valueSlots = append(valueSlots, cav.value)
comparisonSlotCount++
}
}
if comparisonSlotCount > 0 {
comparisonTuple := "(" + strings.Join(columnNames, ",") + ") > (" + strings.Repeat(",?", comparisonSlotCount)[1:] + ")"
sqf.queryBuilder = sqf.queryBuilder.Where(
comparisonTuple,
valueSlots...,
)
}
case ExpandedLogicComparison:
// For performance reasons, remove any column names that have static values in the query.
orClause := sq.Or{}
for index, cav := range columnsAndValues {
if sqf.filteringColumnCounts[cav.name] != 1 {
andClause := sq.And{}
for _, previous := range columnsAndValues[0:index] {
if sqf.filteringColumnCounts[previous.name] != 1 {
andClause = append(andClause, sq.Eq{previous.name: previous.value})
}
}
andClause = append(andClause, sq.Gt{cav.name: cav.value})
orClause = append(orClause, andClause)
}
}
if len(orClause) > 0 {
sqf.queryBuilder = sqf.queryBuilder.Where(orClause)
}
}
return sqf
}
// FilterToResourceType returns a new SchemaQueryFilterer that is limited to resources of the
// specified type.
func (sqf SchemaQueryFilterer) FilterToResourceType(resourceType string) SchemaQueryFilterer {
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Eq{sqf.schema.colNamespace: resourceType})
sqf.recordColumnValue(sqf.schema.colNamespace)
return sqf
}
func (sqf SchemaQueryFilterer) recordColumnValue(colName string) {
if value, ok := sqf.filteringColumnCounts[colName]; ok {
sqf.filteringColumnCounts[colName] = value + 1
return
}
sqf.filteringColumnCounts[colName] = 1
}
// FilterToResourceID returns a new SchemaQueryFilterer that is limited to resources with the
// specified ID.
func (sqf SchemaQueryFilterer) FilterToResourceID(objectID string) SchemaQueryFilterer {
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Eq{sqf.schema.colObjectID: objectID})
sqf.recordColumnValue(sqf.schema.colObjectID)
return sqf
}
func (sqf SchemaQueryFilterer) MustFilterToResourceIDs(resourceIds []string) SchemaQueryFilterer {
updated, err := sqf.FilterToResourceIDs(resourceIds)
if err != nil {
panic(err)
}
return updated
}
// FilterWithResourceIDPrefix returns new SchemaQueryFilterer that is limited to resources whose ID
// starts with the specified prefix.
func (sqf SchemaQueryFilterer) FilterWithResourceIDPrefix(prefix string) (SchemaQueryFilterer, error) {
if strings.Contains(prefix, "%") {
return sqf, spiceerrors.MustBugf("prefix cannot contain the percent sign")
}
if prefix == "" {
return sqf, spiceerrors.MustBugf("prefix cannot be empty")
}
prefix = strings.ReplaceAll(prefix, `\`, `\\`)
prefix = strings.ReplaceAll(prefix, "_", `\_`)
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Like{sqf.schema.colObjectID: prefix + "%"})
// NOTE: we do *not* record the use of the resource ID column here, because it is not used
// statically and thus is necessary for sorting operations.
return sqf, nil
}
func (sqf SchemaQueryFilterer) MustFilterWithResourceIDPrefix(prefix string) SchemaQueryFilterer {
updated, err := sqf.FilterWithResourceIDPrefix(prefix)
if err != nil {
panic(err)
}
return updated
}
// FilterToResourceIDs returns a new SchemaQueryFilterer that is limited to resources with any of the
// specified IDs.
func (sqf SchemaQueryFilterer) FilterToResourceIDs(resourceIds []string) (SchemaQueryFilterer, error) {
spiceerrors.DebugAssert(func() bool {
return len(resourceIds) <= int(sqf.filterMaximumIDCount)
}, "cannot have more than %d resource IDs in a single filter", sqf.filterMaximumIDCount)
var builder strings.Builder
builder.WriteString(sqf.schema.colObjectID)
builder.WriteString(" IN (")
args := make([]any, 0, len(resourceIds))
for _, resourceID := range resourceIds {
if len(resourceID) == 0 {
return sqf, spiceerrors.MustBugf("got empty resource ID")
}
args = append(args, resourceID)
sqf.recordColumnValue(sqf.schema.colObjectID)
}
builder.WriteString("?")
if len(resourceIds) > 1 {
builder.WriteString(strings.Repeat(",?", len(resourceIds)-1))
}
builder.WriteString(")")
sqf.queryBuilder = sqf.queryBuilder.Where(builder.String(), args...)
return sqf, nil
}
// FilterToRelation returns a new SchemaQueryFilterer that is limited to resources with the
// specified relation.
func (sqf SchemaQueryFilterer) FilterToRelation(relation string) SchemaQueryFilterer {
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Eq{sqf.schema.colRelation: relation})
sqf.recordColumnValue(sqf.schema.colRelation)
return sqf
}
// MustFilterWithRelationshipsFilter returns a new SchemaQueryFilterer that is limited to resources with
// resources that match the specified filter.
func (sqf SchemaQueryFilterer) MustFilterWithRelationshipsFilter(filter datastore.RelationshipsFilter) SchemaQueryFilterer {
updated, err := sqf.FilterWithRelationshipsFilter(filter)
if err != nil {
panic(err)
}
return updated
}
func (sqf SchemaQueryFilterer) FilterWithRelationshipsFilter(filter datastore.RelationshipsFilter) (SchemaQueryFilterer, error) {
csqf := sqf
if filter.OptionalResourceType != "" {
csqf = csqf.FilterToResourceType(filter.OptionalResourceType)
}
if filter.OptionalResourceRelation != "" {
csqf = csqf.FilterToRelation(filter.OptionalResourceRelation)
}
if len(filter.OptionalResourceIds) > 0 && filter.OptionalResourceIDPrefix != "" {
return csqf, spiceerrors.MustBugf("cannot filter by both resource IDs and ID prefix")
}
if len(filter.OptionalResourceIds) > 0 {
usqf, err := csqf.FilterToResourceIDs(filter.OptionalResourceIds)
if err != nil {
return csqf, err
}
csqf = usqf
}
if len(filter.OptionalResourceIDPrefix) > 0 {
usqf, err := csqf.FilterWithResourceIDPrefix(filter.OptionalResourceIDPrefix)
if err != nil {
return csqf, err
}
csqf = usqf
}
if len(filter.OptionalSubjectsSelectors) > 0 {
usqf, err := csqf.FilterWithSubjectsSelectors(filter.OptionalSubjectsSelectors...)
if err != nil {
return csqf, err
}
csqf = usqf
}
if filter.OptionalCaveatName != "" {
csqf = csqf.FilterWithCaveatName(filter.OptionalCaveatName)
}
if filter.OptionalExpirationOption == datastore.ExpirationFilterOptionHasExpiration {
csqf.queryBuilder = csqf.queryBuilder.Where(sq.NotEq{csqf.schema.colExpiration: nil})
} else if filter.OptionalExpirationOption == datastore.ExpirationFilterOptionNoExpiration {
csqf.queryBuilder = csqf.queryBuilder.Where(sq.Eq{csqf.schema.colExpiration: nil})
}
return csqf, nil
}
// MustFilterWithSubjectsSelectors returns a new SchemaQueryFilterer that is limited to resources with
// subjects that match the specified selector(s).
func (sqf SchemaQueryFilterer) MustFilterWithSubjectsSelectors(selectors ...datastore.SubjectsSelector) SchemaQueryFilterer {
usqf, err := sqf.FilterWithSubjectsSelectors(selectors...)
if err != nil {
panic(err)
}
return usqf
}
// FilterWithSubjectsSelectors returns a new SchemaQueryFilterer that is limited to resources with
// subjects that match the specified selector(s).
func (sqf SchemaQueryFilterer) FilterWithSubjectsSelectors(selectors ...datastore.SubjectsSelector) (SchemaQueryFilterer, error) {
selectorsOrClause := sq.Or{}
for _, selector := range selectors {
selectorClause := sq.And{}
if len(selector.OptionalSubjectType) > 0 {
selectorClause = append(selectorClause, sq.Eq{sqf.schema.colUsersetNamespace: selector.OptionalSubjectType})
sqf.recordColumnValue(sqf.schema.colUsersetNamespace)
}
if len(selector.OptionalSubjectIds) > 0 {
spiceerrors.DebugAssert(func() bool {
return len(selector.OptionalSubjectIds) <= int(sqf.filterMaximumIDCount)
}, "cannot have more than %d subject IDs in a single filter", sqf.filterMaximumIDCount)
var builder strings.Builder
builder.WriteString(sqf.schema.colUsersetObjectID)
builder.WriteString(" IN (")
args := make([]any, 0, len(selector.OptionalSubjectIds))
for _, subjectID := range selector.OptionalSubjectIds {
if len(subjectID) == 0 {
return sqf, spiceerrors.MustBugf("got empty subject ID")
}
args = append(args, subjectID)
sqf.recordColumnValue(sqf.schema.colUsersetObjectID)
}
builder.WriteString("?")
if len(selector.OptionalSubjectIds) > 1 {
builder.WriteString(strings.Repeat(",?", len(selector.OptionalSubjectIds)-1))
}
builder.WriteString(")")
selectorClause = append(selectorClause, sq.Expr(builder.String(), args...))
}
if !selector.RelationFilter.IsEmpty() {
if selector.RelationFilter.OnlyNonEllipsisRelations {
selectorClause = append(selectorClause, sq.NotEq{sqf.schema.colUsersetRelation: datastore.Ellipsis})
sqf.recordColumnValue(sqf.schema.colUsersetRelation)
} else {
relations := make([]string, 0, 2)
if selector.RelationFilter.IncludeEllipsisRelation {
relations = append(relations, datastore.Ellipsis)
}
if selector.RelationFilter.NonEllipsisRelation != "" {
relations = append(relations, selector.RelationFilter.NonEllipsisRelation)
}
if len(relations) == 1 {
relName := relations[0]
selectorClause = append(selectorClause, sq.Eq{sqf.schema.colUsersetRelation: relName})
sqf.recordColumnValue(sqf.schema.colUsersetRelation)
} else {
orClause := sq.Or{}
for _, relationName := range relations {
dsRelationName := stringz.DefaultEmpty(relationName, datastore.Ellipsis)
orClause = append(orClause, sq.Eq{sqf.schema.colUsersetRelation: dsRelationName})
sqf.recordColumnValue(sqf.schema.colUsersetRelation)
}
selectorClause = append(selectorClause, orClause)
}
}
}
selectorsOrClause = append(selectorsOrClause, selectorClause)
}
sqf.queryBuilder = sqf.queryBuilder.Where(selectorsOrClause)
return sqf, nil
}
// FilterToSubjectFilter returns a new SchemaQueryFilterer that is limited to resources with
// subjects that match the specified filter.
func (sqf SchemaQueryFilterer) FilterToSubjectFilter(filter *v1.SubjectFilter) SchemaQueryFilterer {
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Eq{sqf.schema.colUsersetNamespace: filter.SubjectType})
sqf.recordColumnValue(sqf.schema.colUsersetNamespace)
if filter.OptionalSubjectId != "" {
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Eq{sqf.schema.colUsersetObjectID: filter.OptionalSubjectId})
sqf.recordColumnValue(sqf.schema.colUsersetObjectID)
}
if filter.OptionalRelation != nil {
dsRelationName := stringz.DefaultEmpty(filter.OptionalRelation.Relation, datastore.Ellipsis)
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Eq{sqf.schema.colUsersetRelation: dsRelationName})
sqf.recordColumnValue(sqf.schema.colUsersetRelation)
}
return sqf
}
func (sqf SchemaQueryFilterer) FilterWithCaveatName(caveatName string) SchemaQueryFilterer {
sqf.queryBuilder = sqf.queryBuilder.Where(sq.Eq{sqf.schema.colCaveatName: caveatName})
sqf.recordColumnValue(sqf.schema.colCaveatName)
return sqf
}
// Limit returns a new SchemaQueryFilterer which is limited to the specified number of results.
func (sqf SchemaQueryFilterer) limit(limit uint64) SchemaQueryFilterer {
sqf.queryBuilder = sqf.queryBuilder.Limit(limit)
return sqf
}
// QueryExecutor is a tuple query runner shared by SQL implementations of the datastore.
type QueryExecutor struct {
Executor ExecuteQueryFunc
}
// ExecuteQuery executes the query.
func (tqs QueryExecutor) ExecuteQuery(
ctx context.Context,
query SchemaQueryFilterer,
opts ...options.QueryOptionsOption,
) (datastore.RelationshipIterator, error) {
queryOpts := options.NewQueryOptionsWithOptions(opts...)
query = query.TupleOrder(queryOpts.Sort)
if queryOpts.After != nil {
if queryOpts.Sort == options.Unsorted {
return nil, datastore.ErrCursorsWithoutSorting
}
query = query.After(queryOpts.After, queryOpts.Sort)
}
var limit uint64
// NOTE: we use a uint here because it lines up with the
// assignments in this function, but we set it to MaxInt64
// because that's the biggest value that postgres and friends
// treat as valid.
limit = math.MaxInt64
if queryOpts.Limit != nil {
limit = *queryOpts.Limit
}
toExecute := query.limit(limit)
// Run the query.
sql, args, err := toExecute.queryBuilder.ToSql()
if err != nil {
return nil, err
}
return tqs.Executor(ctx, sql, args)
}
// ExecuteQueryFunc is a function that can be used to execute a single rendered SQL query.
type ExecuteQueryFunc func(ctx context.Context, sql string, args []any) (datastore.RelationshipIterator, error)
// TxCleanupFunc is a function that should be executed when the caller of
// TransactionFactory is done with the transaction.
type TxCleanupFunc func(context.Context)