Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: use ANY to avoid concats (#17856)

p0nyec6bf336b3c1
diff --git a/backend/store/principal_filter.go b/backend/store/principal_filter.go
index e34262d307..17c4f5e1c3 100644
--- a/backend/store/principal_filter.go
+++ b/backend/store/principal_filter.go
@@ -1,7 +1,6 @@
package store
import (
- "fmt"
"strings"
"github.com/google/cel-go/cel"
@@ -99,7 +98,7 @@ func GetListUserFilter(filter string) (*GetListUserFilterResult, error) {
}
}
- parseToUserTypeSQL := func(expr celast.Expr, relation string) (*qb.Query, error) {
+ parseToUserTypeSQL := func(expr celast.Expr) (*qb.Query, error) {
variable, value := getVariableAndValueFromExpr(expr)
if variable != "user_type" {
return nil, errors.Errorf(`only "user_type" support "user_type in [xx]"/"!(user_type in [xx])" operator`)
@@ -126,9 +125,7 @@ func GetListUserFilter(filter string) (*GetListUserFilterResult, error) {
userTypeList = append(userTypeList, principalType.String())
}
- placeholders := strings.Repeat("?,", len(userTypeList))
- placeholders = placeholders[:len(placeholders)-1]
- return qb.Q().Space(fmt.Sprintf("principal.type %s (%s)", relation, placeholders), userTypeList...), nil
+ return qb.Q().Space("principal.type = ANY(?)", userTypeList), nil
}
getFilter = func(expr celast.Expr) (*qb.Query, error) {
@@ -174,13 +171,17 @@ func GetListUserFilter(filter string) (*GetListUserFilterResult, error) {
}
return qb.Q().Space("LOWER(principal."+variable+") LIKE ?", "%"+strings.ToLower(strValue)+"%"), nil
case celoperators.In:
- return parseToUserTypeSQL(expr, "IN")
+ return parseToUserTypeSQL(expr)
case celoperators.LogicalNot:
args := expr.AsCall().Args()
if len(args) != 1 {
return nil, errors.Errorf(`only support !(user_type in ["{type1}", "{type2}"]) format`)
}
- return parseToUserTypeSQL(args[0], "NOT IN")
+ qq, err := getFilter(args[0])
+ if err != nil {
+ return nil, err
+ }
+ return q.Space("(NOT (?))", qq), nil
default:
return nil, errors.Errorf("unexpected function %v", functionName)
}
diff --git a/backend/store/principal_filter_test.go b/backend/store/principal_filter_test.go
index ab6a2b5604..152aa00d77 100644
--- a/backend/store/principal_filter_test.go
+++ b/backend/store/principal_filter_test.go
@@ -70,15 +70,15 @@ func TestGetListUserFilter(t *testing.T) {
{
name: "user_type in list",
filter: `user_type in ["SERVICE_ACCOUNT", "USER"]`,
- wantSQL: "(principal.type IN ($1,$2))",
- wantArgs: []any{storepb.PrincipalType_SERVICE_ACCOUNT.String(), storepb.PrincipalType_END_USER.String()},
+ wantSQL: "(principal.type = ANY($1))",
+ wantArgs: []any{[]any{storepb.PrincipalType_SERVICE_ACCOUNT.String(), storepb.PrincipalType_END_USER.String()}},
wantErr: false,
},
{
name: "user_type not in list",
filter: `!(user_type in ["SERVICE_ACCOUNT"])`,
- wantSQL: "(principal.type NOT IN ($1))",
- wantArgs: []any{storepb.PrincipalType_SERVICE_ACCOUNT.String()},
+ wantSQL: "((NOT (principal.type = ANY($1))))",
+ wantArgs: []any{[]any{storepb.PrincipalType_SERVICE_ACCOUNT.String()}},
wantErr: false,
},
{