Skip to content

Commit

Permalink
Merge pull request #1263 from Permify/query-optimization
Browse files Browse the repository at this point in the history
refactor(query-optimization): optimize entity_id condition for single…
  • Loading branch information
tolgaOzen committed May 21, 2024
2 parents c7992f5 + 95c467e commit 4a1fe94
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/api-reference/apidocs.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Permify API",
"description": "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.",
"version": "v0.8.6",
"version": "v0.8.7",
"contact": {
"name": "API Support",
"url": "https://github.com/Permify/permify/issues",
Expand Down
2 changes: 1 addition & 1 deletion internal/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Identifier = ""
*/
const (
// Version is the last release of the Permify (e.g. v0.1.0)
Version = "v0.8.6"
Version = "v0.8.7"
)

// Function to create a single line of the ASCII art with centered content and color
Expand Down
100 changes: 78 additions & 22 deletions internal/storage/postgres/utils/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ func TuplesFilterQueryForSelectBuilder(sl squirrel.SelectBuilder, filter *base.T
eq["entity_type"] = filter.GetEntity().GetType()
}

if len(filter.GetEntity().GetIds()) > 0 {
eq["entity_id"] = filter.GetEntity().GetIds()
entityIds := filter.GetEntity().GetIds()
if len(entityIds) > 0 {
if len(entityIds) == 1 {
// If there is only one ID, use the = operator
eq["entity_id"] = entityIds[0]
} else {
// If there are multiple IDs, use the IN operator
eq["entity_id"] = entityIds
}
}

if filter.GetRelation() != "" {
Expand All @@ -26,16 +33,23 @@ func TuplesFilterQueryForSelectBuilder(sl squirrel.SelectBuilder, filter *base.T
eq["subject_type"] = filter.GetSubject().GetType()
}

if len(filter.GetSubject().GetIds()) > 0 {
eq["subject_id"] = filter.GetSubject().GetIds()
subjectIds := filter.GetSubject().GetIds()
if len(subjectIds) > 0 {
if len(subjectIds) == 1 {
// If there is only one ID, use the = operator
eq["subject_id"] = subjectIds[0]
} else {
// If there are multiple IDs, use the IN operator
eq["subject_id"] = subjectIds
}
}

if filter.GetSubject().GetRelation() != "" {
eq["subject_relation"] = filter.GetSubject().GetRelation()
}

// If eq is empty, return the original squirrel.UpdateBuilder without attaching a WHERE clause.
// This ensures we don't accidentally update every row in the table.
// If eq is empty, return the original squirrel.SelectBuilder without attaching a WHERE clause.
// This ensures we don't accidentally select every row in the table.
if len(eq) == 0 {
return sl
}
Expand All @@ -51,16 +65,30 @@ func AttributesFilterQueryForSelectBuilder(sl squirrel.SelectBuilder, filter *ba
eq["entity_type"] = filter.GetEntity().GetType()
}

if len(filter.GetEntity().GetIds()) > 0 {
eq["entity_id"] = filter.GetEntity().GetIds()
entityIds := filter.GetEntity().GetIds()
if len(entityIds) > 0 {
if len(entityIds) == 1 {
// If there is only one ID, use the = operator
eq["entity_id"] = entityIds[0]
} else {
// If there are multiple IDs, use the IN operator
eq["entity_id"] = entityIds
}
}

if len(filter.GetAttributes()) > 0 {
eq["attribute"] = filter.GetAttributes()
attributes := filter.GetAttributes()
if len(attributes) > 0 {
if len(attributes) == 1 {
// If there is only one attribute, use the = operator
eq["attribute"] = attributes[0]
} else {
// If there are multiple attributes, use the IN operator
eq["attribute"] = attributes
}
}

// If eq is empty, return the original squirrel.UpdateBuilder without attaching a WHERE clause.
// This ensures we don't accidentally update every row in the table.
// If eq is empty, return the original squirrel.SelectBuilder without attaching a WHERE clause.
// This ensures we don't accidentally select every row in the table.
if len(eq) == 0 {
return sl
}
Expand All @@ -76,8 +104,15 @@ func TuplesFilterQueryForUpdateBuilder(sl squirrel.UpdateBuilder, filter *base.T
eq["entity_type"] = filter.GetEntity().GetType()
}

if len(filter.GetEntity().GetIds()) > 0 {
eq["entity_id"] = filter.GetEntity().GetIds()
entityIds := filter.GetEntity().GetIds()
if len(entityIds) > 0 {
if len(entityIds) == 1 {
// If there is only one ID, use the = operator
eq["entity_id"] = entityIds[0]
} else {
// If there are multiple IDs, use the IN operator
eq["entity_id"] = entityIds
}
}

if filter.GetRelation() != "" {
Expand All @@ -88,8 +123,15 @@ func TuplesFilterQueryForUpdateBuilder(sl squirrel.UpdateBuilder, filter *base.T
eq["subject_type"] = filter.GetSubject().GetType()
}

if len(filter.GetSubject().GetIds()) > 0 {
eq["subject_id"] = filter.GetSubject().GetIds()
subjectIds := filter.GetSubject().GetIds()
if len(subjectIds) > 0 {
if len(subjectIds) == 1 {
// If there is only one ID, use the = operator
eq["subject_id"] = subjectIds[0]
} else {
// If there are multiple IDs, use the IN operator
eq["subject_id"] = subjectIds
}
}

if filter.GetSubject().GetRelation() != "" {
Expand All @@ -113,12 +155,26 @@ func AttributesFilterQueryForUpdateBuilder(sl squirrel.UpdateBuilder, filter *ba
eq["entity_type"] = filter.GetEntity().GetType()
}

if len(filter.GetEntity().GetIds()) > 0 {
eq["entity_id"] = filter.GetEntity().GetIds()
}

if len(filter.GetAttributes()) > 0 {
eq["attribute"] = filter.GetAttributes()
entityIds := filter.GetEntity().GetIds()
if len(entityIds) > 0 {
if len(entityIds) == 1 {
// If there is only one ID, use the = operator
eq["entity_id"] = entityIds[0]
} else {
// If there are multiple IDs, use the IN operator
eq["entity_id"] = entityIds
}
}

attributes := filter.GetAttributes()
if len(attributes) > 0 {
if len(attributes) == 1 {
// If there is only one attribute, use the = operator
eq["attribute"] = attributes[0]
} else {
// If there are multiple attributes, use the IN operator
eq["attribute"] = attributes
}
}

// If eq is empty, return the original squirrel.UpdateBuilder without attaching a WHERE clause.
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strings"
"time"

"github.com/exaring/otelpgx"

"github.com/cenkalti/backoff/v4"

"github.com/exaring/otelpgx"

"golang.org/x/exp/slog"

"github.com/jackc/pgx/v5"
Expand Down
2 changes: 1 addition & 1 deletion pkg/pb/base/v1/openapi.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/base/v1/openapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
info: {
title: "Permify API";
description: "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.";
version: "v0.8.6";
version: "v0.8.7";
contact: {
name: "API Support";
url: "https://github.com/Permify/permify/issues";
Expand Down

0 comments on commit 4a1fe94

Please sign in to comment.