Skip to content

Commit 3b6a82d

Browse files
authored
Merge pull request #2385 from josephschorr/additional-crdb-index
Add additional forced CRDB index in special case of no object IDs
2 parents 2d1505e + 77727b6 commit 3b6a82d

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

internal/datastore/crdb/schema/indexes.go

+13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ func IndexingHintForQueryShape(schema common.SchemaInformation, qs queryshape.Sh
8888

8989
// IndexForFilter returns the index to use for a given relationships filter or nil if no index is forced.
9090
func IndexForFilter(schema common.SchemaInformation, filter datastore.RelationshipsFilter) *common.IndexDefinition {
91+
// Special case: if the filter specifies the resource type and relation and the subject type and relation, then
92+
// the schema diff index can be used.
93+
if filter.OptionalResourceType != "" &&
94+
filter.OptionalResourceRelation != "" &&
95+
len(filter.OptionalSubjectsSelectors) == 1 &&
96+
filter.OptionalSubjectsSelectors[0].OptionalSubjectType != "" &&
97+
filter.OptionalSubjectsSelectors[0].RelationFilter.NonEllipsisRelation != "" &&
98+
!filter.OptionalSubjectsSelectors[0].RelationFilter.IncludeEllipsisRelation &&
99+
!filter.OptionalSubjectsSelectors[0].RelationFilter.OnlyNonEllipsisRelations {
100+
return &IndexRelationshipBySubjectRelation
101+
}
102+
103+
// Otherwise, determine an index based on whether the filter has a larger match on the resources or subject.
91104
resourceFieldDepth := 0
92105
if filter.OptionalResourceType != "" {
93106
resourceFieldDepth = 1

internal/datastore/crdb/schema/indexes_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,82 @@ func TestIndexForFilter(t *testing.T) {
118118
},
119119
expected: "",
120120
},
121+
{
122+
name: "filter by resource type, relation and subject type and relation",
123+
filter: datastore.RelationshipsFilter{
124+
OptionalResourceType: "foo",
125+
OptionalResourceRelation: "bar",
126+
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
127+
{
128+
OptionalSubjectType: "foo",
129+
RelationFilter: datastore.SubjectRelationFilter{
130+
NonEllipsisRelation: "baz",
131+
},
132+
},
133+
},
134+
},
135+
expected: "ix_relation_tuple_by_subject_relation",
136+
},
137+
{
138+
name: "filter by resource type, relation and subject type",
139+
filter: datastore.RelationshipsFilter{
140+
OptionalResourceType: "foo",
141+
OptionalResourceRelation: "bar",
142+
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
143+
{
144+
OptionalSubjectType: "foo",
145+
},
146+
},
147+
},
148+
expected: "pk_relation_tuple",
149+
},
150+
{
151+
name: "filter by resource type, relation and subject relation",
152+
filter: datastore.RelationshipsFilter{
153+
OptionalResourceType: "foo",
154+
OptionalResourceRelation: "bar",
155+
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
156+
{
157+
RelationFilter: datastore.SubjectRelationFilter{
158+
NonEllipsisRelation: "baz",
159+
},
160+
},
161+
},
162+
},
163+
expected: "pk_relation_tuple",
164+
},
165+
{
166+
name: "filter by resource relation and subject type and relation",
167+
filter: datastore.RelationshipsFilter{
168+
OptionalResourceRelation: "bar",
169+
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
170+
{
171+
OptionalSubjectType: "foo",
172+
RelationFilter: datastore.SubjectRelationFilter{
173+
NonEllipsisRelation: "baz",
174+
},
175+
},
176+
},
177+
},
178+
expected: "",
179+
},
180+
{
181+
name: "filter by resource type, relation and subject type and relation, include ellipsis",
182+
filter: datastore.RelationshipsFilter{
183+
OptionalResourceType: "foo",
184+
OptionalResourceRelation: "bar",
185+
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
186+
{
187+
OptionalSubjectType: "foo",
188+
RelationFilter: datastore.SubjectRelationFilter{
189+
IncludeEllipsisRelation: true,
190+
NonEllipsisRelation: "baz",
191+
},
192+
},
193+
},
194+
},
195+
expected: "pk_relation_tuple",
196+
},
121197
}
122198

123199
schema := Schema(common.ColumnOptimizationOptionNone, false, false)

0 commit comments

Comments
 (0)