Use subquery for common third party filter
Replace the two-step ID-materializing pattern (fetch IDs in Go, pass as ANY(@ids)) with an IN-subquery that keeps the filtering entirely in the database and eliminates an extra round trip. Remove the now unused LoadIDsByCommonThirdPartyID and its service wrapper. Update the coredata rule to clarify that subqueries for filtering are OK. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -2746,40 +2746,6 @@ func (s *Service) GetCommonTrackerPatternsByIDs(
|
||||
return patterns, nil
|
||||
}
|
||||
|
||||
// LoadCommonTrackerPatternIDsByCommonThirdPartyID returns the IDs of
|
||||
// every common tracker pattern referencing the given common third party.
|
||||
// Used by the trackers list filter to translate a CommonThirdParty GID
|
||||
// into a `common_tracker_pattern_id = ANY(...)` constraint without
|
||||
// JOINing across entity tables in coredata.
|
||||
func (s *Service) LoadCommonTrackerPatternIDsByCommonThirdPartyID(
|
||||
ctx context.Context,
|
||||
commonThirdPartyID gid.GID,
|
||||
) ([]gid.GID, error) {
|
||||
var ids []gid.GID
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
var (
|
||||
patterns coredata.CommonTrackerPatterns
|
||||
err error
|
||||
)
|
||||
|
||||
ids, err = patterns.LoadIDsByCommonThirdPartyID(ctx, conn, commonThirdPartyID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load common tracker pattern ids: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// LoadDistinctThirdPartyIDsByCookieBannerID returns the distinct
|
||||
// org-scoped third-party IDs referenced by tracker patterns of the
|
||||
// banner. The companion
|
||||
|
||||
@@ -483,36 +483,3 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadIDsByCommonThirdPartyID returns just the IDs of the common tracker
|
||||
// patterns linked to the given common third party. Callers use it to feed
|
||||
// a `common_tracker_pattern_id = ANY(...)` filter on tracker_patterns
|
||||
// without crossing the entity boundary.
|
||||
func (ps *CommonTrackerPatterns) LoadIDsByCommonThirdPartyID(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
commonThirdPartyID gid.GID,
|
||||
) ([]gid.GID, error) {
|
||||
q := `
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
common_tracker_patterns
|
||||
WHERE
|
||||
common_third_party_id = @common_third_party_id
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{"common_third_party_id": commonThirdPartyID}
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot query common tracker pattern ids: %w", err)
|
||||
}
|
||||
|
||||
ids, err := pgx.CollectRows(rows, pgx.RowTo[gid.GID])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot collect common tracker pattern ids: %w", err)
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
@@ -20,14 +20,14 @@ import (
|
||||
)
|
||||
|
||||
type TrackerPatternFilter struct {
|
||||
matchType *TrackerPatternMatchType
|
||||
cookieCategoryID *gid.GID
|
||||
excluded *bool
|
||||
query *string
|
||||
source *CookieSource
|
||||
trackerType *TrackerType
|
||||
thirdPartyID *gid.GID
|
||||
commonTrackerPatternIDs []gid.GID
|
||||
matchType *TrackerPatternMatchType
|
||||
cookieCategoryID *gid.GID
|
||||
excluded *bool
|
||||
query *string
|
||||
source *CookieSource
|
||||
trackerType *TrackerType
|
||||
thirdPartyID *gid.GID
|
||||
commonThirdPartyID *gid.GID
|
||||
}
|
||||
|
||||
func NewTrackerPatternFilter(
|
||||
@@ -62,14 +62,8 @@ func (f *TrackerPatternFilter) WithThirdPartyID(thirdPartyID *gid.GID) *TrackerP
|
||||
return f
|
||||
}
|
||||
|
||||
// WithCommonTrackerPatternIDs constrains the result to tracker patterns
|
||||
// whose `common_tracker_pattern_id` is in the given set. Callers
|
||||
// pre-resolve this list (typically via
|
||||
// CommonTrackerPatterns.LoadIDsByCommonThirdPartyID) so the filter stays
|
||||
// inside the tracker_patterns table. Passing an empty (non-nil) slice
|
||||
// yields no rows.
|
||||
func (f *TrackerPatternFilter) WithCommonTrackerPatternIDs(ids []gid.GID) *TrackerPatternFilter {
|
||||
f.commonTrackerPatternIDs = ids
|
||||
func (f *TrackerPatternFilter) WithCommonThirdPartyID(id *gid.GID) *TrackerPatternFilter {
|
||||
f.commonThirdPartyID = id
|
||||
return f
|
||||
}
|
||||
|
||||
@@ -130,9 +124,12 @@ func (f *TrackerPatternFilter) SQLFragment() string {
|
||||
END
|
||||
AND
|
||||
CASE
|
||||
WHEN @has_common_tracker_pattern_ids_filter::boolean = false THEN TRUE
|
||||
WHEN @has_common_tracker_pattern_ids_filter::boolean = true THEN
|
||||
common_tracker_pattern_id = ANY(@filter_common_tracker_pattern_ids::text[])
|
||||
WHEN @has_common_third_party_id_filter::boolean = false THEN TRUE
|
||||
WHEN @has_common_third_party_id_filter::boolean = true THEN
|
||||
common_tracker_pattern_id IN (
|
||||
SELECT id FROM common_tracker_patterns
|
||||
WHERE common_third_party_id = @filter_common_third_party_id::text
|
||||
)
|
||||
ELSE TRUE
|
||||
END
|
||||
)`
|
||||
@@ -144,21 +141,21 @@ func (f *TrackerPatternFilter) SQLArguments() pgx.StrictNamedArgs {
|
||||
}
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"has_match_type_filter": false,
|
||||
"filter_match_type": nil,
|
||||
"has_cookie_category_id_filter": false,
|
||||
"filter_cookie_category_id": nil,
|
||||
"has_excluded_filter": false,
|
||||
"filter_excluded": nil,
|
||||
"filter_query": nil,
|
||||
"has_source_filter": false,
|
||||
"filter_source": nil,
|
||||
"has_tracker_type_filter": false,
|
||||
"filter_tracker_type": nil,
|
||||
"has_third_party_id_filter": false,
|
||||
"filter_third_party_id": nil,
|
||||
"has_common_tracker_pattern_ids_filter": false,
|
||||
"filter_common_tracker_pattern_ids": []gid.GID{},
|
||||
"has_match_type_filter": false,
|
||||
"filter_match_type": nil,
|
||||
"has_cookie_category_id_filter": false,
|
||||
"filter_cookie_category_id": nil,
|
||||
"has_excluded_filter": false,
|
||||
"filter_excluded": nil,
|
||||
"filter_query": nil,
|
||||
"has_source_filter": false,
|
||||
"filter_source": nil,
|
||||
"has_tracker_type_filter": false,
|
||||
"filter_tracker_type": nil,
|
||||
"has_third_party_id_filter": false,
|
||||
"filter_third_party_id": nil,
|
||||
"has_common_third_party_id_filter": false,
|
||||
"filter_common_third_party_id": nil,
|
||||
}
|
||||
|
||||
if f.matchType != nil {
|
||||
@@ -195,9 +192,9 @@ func (f *TrackerPatternFilter) SQLArguments() pgx.StrictNamedArgs {
|
||||
args["filter_third_party_id"] = *f.thirdPartyID
|
||||
}
|
||||
|
||||
if f.commonTrackerPatternIDs != nil {
|
||||
args["has_common_tracker_pattern_ids_filter"] = true
|
||||
args["filter_common_tracker_pattern_ids"] = f.commonTrackerPatternIDs
|
||||
if f.commonThirdPartyID != nil {
|
||||
args["has_common_third_party_id_filter"] = true
|
||||
args["filter_common_third_party_id"] = *f.commonThirdPartyID
|
||||
}
|
||||
|
||||
return args
|
||||
|
||||
@@ -217,17 +217,7 @@ func (r *cookieBannerResolver) TrackerPatterns(ctx context.Context, obj *types.C
|
||||
case coredata.ThirdPartyEntityType:
|
||||
coredataFilter = coredataFilter.WithThirdPartyID(filter.ThirdPartyID)
|
||||
case coredata.CommonThirdPartyEntityType:
|
||||
ids, err := r.cookieBanner.LoadCommonTrackerPatternIDsByCommonThirdPartyID(ctx, *filter.ThirdPartyID)
|
||||
if err != nil {
|
||||
r.logger.ErrorCtx(ctx, "cannot resolve common third party tracker patterns", log.Error(err))
|
||||
return nil, gqlutils.Internal(ctx)
|
||||
}
|
||||
|
||||
if ids == nil {
|
||||
ids = []gid.GID{}
|
||||
}
|
||||
|
||||
coredataFilter = coredataFilter.WithCommonTrackerPatternIDs(ids)
|
||||
coredataFilter = coredataFilter.WithCommonThirdPartyID(filter.ThirdPartyID)
|
||||
default:
|
||||
return nil, gqlutils.Invalidf(ctx, "thirdPartyId must reference a ThirdParty or CommonThirdParty")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user