Skip to content

Commit

Permalink
Fixed rendering NOT-EXISTS queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpromislow committed Nov 30, 2024
1 parent 3414e22 commit 2e39614
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 8 additions & 5 deletions pkg/cache/sql/informer/listoption_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (l *ListOptionIndexer) constructQuery(lo ListOptions, partitions []partitio
// 2- Filtering: WHERE clauses (from lo.Filters)
whereClauses := []string{}
for _, orFilters := range lo.Filters {
orClause, orParams, err := l.buildORClauseFromFilters(orFilters)
orClause, orParams, err := l.buildORClauseFromFilters(orFilters, dbName)
if err != nil {
return queryInfo, err
}
Expand Down Expand Up @@ -539,7 +539,7 @@ func (l *ListOptionIndexer) validateColumn(column string) error {
}

// buildORClause creates an SQLite compatible query that ORs conditions built from passed filters
func (l *ListOptionIndexer) buildORClauseFromFilters(orFilters OrFilter) (string, []any, error) {
func (l *ListOptionIndexer) buildORClauseFromFilters(orFilters OrFilter, dbName string) (string, []any, error) {
var params []any
clauses := make([]string, 0, len(orFilters.Filters))
var newParams []any
Expand All @@ -548,7 +548,7 @@ func (l *ListOptionIndexer) buildORClauseFromFilters(orFilters OrFilter) (string

for _, filter := range orFilters.Filters {
if isLabelFilter(&filter) {
newClause, newParams, err = l.getLabelFilter(filter)
newClause, newParams, err = l.getLabelFilter(filter, dbName)
} else {
newClause, newParams, err = l.getFieldFilter(filter)
}
Expand Down Expand Up @@ -630,7 +630,7 @@ func (l *ListOptionIndexer) getFieldFilter(filter Filter) (string, []any, error)
return "", nil, fmt.Errorf("unrecognized operator: %s", opString)
}

func (l *ListOptionIndexer) getLabelFilter(filter Filter) (string, []any, error) {
func (l *ListOptionIndexer) getLabelFilter(filter Filter, dbName string) (string, []any, error) {
opString := ""
escapeString := ""
if len(filter.Field) < 3 || filter.Field[0] != "metadata" || filter.Field[1] != "labels" {
Expand Down Expand Up @@ -666,7 +666,10 @@ func (l *ListOptionIndexer) getLabelFilter(filter Filter) (string, []any, error)
return clause, []any{labelName}, nil

case NotExists:
clause := fmt.Sprintf(`lt.label != ?`)
clause := fmt.Sprintf(`NOT EXISTS (SELECT 1 FROM "%s" o1
JOIN "%s_fields" f1 ON o1.key = f1.key
JOIN "%s_labels" lt1 ON o1.key = lt1.key
WHERE label = ?)`, dbName, dbName, dbName)
return clause, []any{labelName}, nil

case In:
Expand Down
5 changes: 4 additions & 1 deletion pkg/cache/sql/informer/listoption_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,10 @@ func TestConstructQuery(t *testing.T) {
JOIN "something_fields" f ON o.key = f.key
JOIN "something_labels" lt ON o.key = lt.key
WHERE
(lt.label != ?) AND
(NOT EXISTS (SELECT 1 FROM "something" o1
JOIN "something_fields" f1 ON o1.key = f1.key
JOIN "something_labels" lt1 ON o1.key = lt1.key
WHERE label = ?)) AND
(FALSE)
ORDER BY f."metadata.name" ASC `,
expectedStmtArgs: []any{"labelNOTEXISTS"},
Expand Down

0 comments on commit 2e39614

Please sign in to comment.