Skip to content

Add further filtering of forced indexes on CRDB to discount other shapes #2437

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

Merged
merged 1 commit into from
Jun 10, 2025
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
18 changes: 16 additions & 2 deletions internal/datastore/crdb/schema/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"github.com/authzed/spicedb/internal/datastore/common"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/queryshape"
"github.com/authzed/spicedb/pkg/genutil/mapz"
"github.com/authzed/spicedb/pkg/spiceerrors"
)

Expand Down Expand Up @@ -103,12 +104,16 @@
if len(filter.OptionalResourceIds) > 0 || filter.OptionalResourceIDPrefix != "" {
resourceFieldDepth = 2
if filter.OptionalResourceRelation != "" {
if filter.OptionalResourceIDPrefix != "" {
return nil // Cannot use an index with a prefix and a relation.
}

Check warning on line 109 in internal/datastore/crdb/schema/indexes.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/crdb/schema/indexes.go#L107-L109

Added lines #L107 - L109 were not covered by tests

resourceFieldDepth = 3
}
}
}

subjectFieldDepth := 0
subjectFieldDepths := mapz.NewSet[int]()
Comment on lines -111 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely clear on the intent of using a map here - I can see that the semantic is that there should only be a single field depth, but I'm not clear on when there could be multiple, or why that would indicate that an index shouldn't be used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a set: we need to make sure all filters have the same depth

for _, subjectSelector := range filter.OptionalSubjectsSelectors {
sfd := 0
if len(subjectSelector.OptionalSubjectIds) > 0 {
Expand All @@ -120,7 +125,16 @@
}
}
}
subjectFieldDepth = max(subjectFieldDepth, sfd)
subjectFieldDepths.Add(sfd)

Check warning on line 128 in internal/datastore/crdb/schema/indexes.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/crdb/schema/indexes.go#L128

Added line #L128 was not covered by tests
}

if subjectFieldDepths.Len() > 1 {
return nil
}

Check warning on line 133 in internal/datastore/crdb/schema/indexes.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/crdb/schema/indexes.go#L132-L133

Added lines #L132 - L133 were not covered by tests

subjectFieldDepth := 0
if !subjectFieldDepths.IsEmpty() {
subjectFieldDepth = subjectFieldDepths.AsSlice()[0]

Check warning on line 137 in internal/datastore/crdb/schema/indexes.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/crdb/schema/indexes.go#L137

Added line #L137 was not covered by tests
}

if resourceFieldDepth == 0 && subjectFieldDepth == 0 {
Expand Down
210 changes: 177 additions & 33 deletions internal/datastore/crdb/schema/indexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@ import (

func TestIndexForFilter(t *testing.T) {
tests := []struct {
name string
filter datastore.RelationshipsFilter
expected string
name string
filter datastore.RelationshipsFilter
expectedWithoutIntegrity string
expectedWithIntegrity string
}{
{
name: "no filter",
filter: datastore.RelationshipsFilter{},
expected: "",
name: "no filter",
filter: datastore.RelationshipsFilter{},
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
{
name: "filter by resource type",
filter: datastore.RelationshipsFilter{OptionalResourceType: "foo"},
expected: "pk_relation_tuple",
name: "filter by resource type",
filter: datastore.RelationshipsFilter{OptionalResourceType: "foo"},
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by resource type and relation",
filter: datastore.RelationshipsFilter{
OptionalResourceType: "foo",
OptionalResourceRelation: "bar",
},
expected: "pk_relation_tuple",
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by resource type, resource ID and relation",
Expand All @@ -40,7 +44,8 @@ func TestIndexForFilter(t *testing.T) {
OptionalResourceIds: []string{"baz"},
OptionalResourceRelation: "bar",
},
expected: "pk_relation_tuple",
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by subject type, subject ID and relation",
Expand All @@ -55,7 +60,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "ix_relation_tuple_by_subject",
expectedWithoutIntegrity: "ix_relation_tuple_by_subject",
expectedWithIntegrity: "",
},
{
name: "filter by subject type, subject ID",
Expand All @@ -67,7 +73,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "ix_relation_tuple_by_subject",
expectedWithoutIntegrity: "ix_relation_tuple_by_subject",
expectedWithIntegrity: "",
},
{
name: "filter by subject relation, subject ID",
Expand All @@ -81,7 +88,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "ix_relation_tuple_by_subject",
expectedWithoutIntegrity: "ix_relation_tuple_by_subject",
expectedWithIntegrity: "",
},
{
name: "filter by subject type",
Expand All @@ -92,7 +100,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "",
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
{
name: "filter by resource type and subject type",
Expand All @@ -104,7 +113,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "pk_relation_tuple",
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by resource type and subject object ID",
Expand All @@ -116,7 +126,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "",
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
{
name: "filter by resource type, relation and subject type and relation",
Expand All @@ -132,7 +143,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "ix_relation_tuple_by_subject_relation",
expectedWithoutIntegrity: "ix_relation_tuple_by_subject_relation",
expectedWithIntegrity: "ix_relation_tuple_by_subject_relation",
},
{
name: "filter by resource type, relation and subject type",
Expand All @@ -145,7 +157,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "pk_relation_tuple",
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by resource type, relation and subject relation",
Expand All @@ -160,7 +173,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "pk_relation_tuple",
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by resource relation and subject type and relation",
Expand All @@ -175,7 +189,8 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "",
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
{
name: "filter by resource type, relation and subject type and relation, include ellipsis",
Expand All @@ -192,20 +207,149 @@ func TestIndexForFilter(t *testing.T) {
},
},
},
expected: "pk_relation_tuple",
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by resource type and ID prefix",
filter: datastore.RelationshipsFilter{
OptionalResourceType: "foo",
OptionalResourceIDPrefix: "prefix",
},
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "filter by resource type, ID prefix and relation",
filter: datastore.RelationshipsFilter{
OptionalResourceType: "foo",
OptionalResourceIDPrefix: "prefix",
OptionalResourceRelation: "bar",
},
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
{
name: "multiple subject selectors with different depths",
filter: datastore.RelationshipsFilter{
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
{
OptionalSubjectType: "foo",
},
{
OptionalSubjectType: "bar",
OptionalSubjectIds: []string{"id1"},
RelationFilter: datastore.SubjectRelationFilter{
NonEllipsisRelation: "baz",
},
},
},
},
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
{
name: "multiple subject selectors with same depth",
filter: datastore.RelationshipsFilter{
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
{
OptionalSubjectType: "foo",
OptionalSubjectIds: []string{"id1"},
},
{
OptionalSubjectType: "bar",
OptionalSubjectIds: []string{"id2"},
},
},
},
expectedWithoutIntegrity: "ix_relation_tuple_by_subject",
expectedWithIntegrity: "",
},
{
name: "multiple subject selectors with resource filter",
filter: datastore.RelationshipsFilter{
OptionalResourceType: "resource_type",
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
{
OptionalSubjectType: "foo",
},
{
OptionalSubjectType: "bar",
OptionalSubjectIds: []string{"id1"},
},
},
},
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
{
name: "subject IDs without subject type",
filter: datastore.RelationshipsFilter{
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
{
OptionalSubjectIds: []string{"id1"},
RelationFilter: datastore.SubjectRelationFilter{
NonEllipsisRelation: "relation",
},
},
},
},
expectedWithoutIntegrity: "ix_relation_tuple_by_subject",
expectedWithIntegrity: "",
},
{
name: "schema diff index with only non-ellipsis relations",
filter: datastore.RelationshipsFilter{
OptionalResourceType: "foo",
OptionalResourceRelation: "bar",
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
{
OptionalSubjectType: "baz",
RelationFilter: datastore.SubjectRelationFilter{
NonEllipsisRelation: "qux",
OnlyNonEllipsisRelations: true,
IncludeEllipsisRelation: false,
},
},
},
},
expectedWithoutIntegrity: "pk_relation_tuple",
expectedWithIntegrity: "pk_relation_tuple",
},
{
name: "empty subject selector",
filter: datastore.RelationshipsFilter{
OptionalSubjectsSelectors: []datastore.SubjectsSelector{
{},
},
},
expectedWithoutIntegrity: "",
expectedWithIntegrity: "",
},
}

schema := Schema(common.ColumnOptimizationOptionNone, false, false)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
index := IndexForFilter(*schema, test.filter)
if test.expected == "" {
require.Nil(t, index)
} else {
require.NotNil(t, index)
require.Equal(t, test.expected, index.Name)
}
})
for _, withIntegrity := range []bool{false, true} {
integritySuffix := ""
if withIntegrity {
integritySuffix = " with integrity"
}

schema := Schema(common.ColumnOptimizationOptionNone, withIntegrity, false)
for _, test := range tests {
t.Run(test.name+integritySuffix, func(t *testing.T) {
index := IndexForFilter(*schema, test.filter)
expected := test.expectedWithoutIntegrity
if withIntegrity {
expected = test.expectedWithIntegrity
}

if expected == "" {
require.Nil(t, index)
} else {
require.NotNil(t, index)
require.Equal(t, expected, index.Name)
}
})
}
}
}
Loading