Skip to content

Commit

Permalink
Filter out null values when grouping by annotation (#3521)
Browse files Browse the repository at this point in the history
* Add a regression test

Signed-off-by: Noah Held <noahheld17@gmail.com>

* Fix the bug

Signed-off-by: Noah Held <noahheld17@gmail.com>

* Formatting

Signed-off-by: Noah Held <noahheld17@gmail.com>

---------

Signed-off-by: Noah Held <noahheld17@gmail.com>
  • Loading branch information
zuqq committed Apr 16, 2024
1 parent 380d59d commit e013f7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/lookoutv2/repository/groupjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,13 @@ func TestGroupByAnnotation(t *testing.T) {
"test-annotation-1": "test-value-3",
},
}, converter, store)
manyJobs(3, &createJobsOpts{
queue: queue,
jobSet: jobSet,
annotations: map[string]string{
// Note the absence of the key "test-annotation-1".
},
}, converter, store)

result, err := repo.GroupBy(
armadacontext.TODO(),
Expand Down
11 changes: 11 additions & 0 deletions internal/lookoutv2/repository/querybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ func (qb *QueryBuilder) GroupByJsonb(
return nil, err
}

if groupedField.IsAnnotation {
// scanGroup expects values in the grouped-by field not to be null. In
// the case where we are grouping by an annotation key, we end up with
// a `GROUP BY` clause like:
//
// GROUP BY annotations->>'host_instance_id'
//
// This evaluates to null if the annotations object does not contain
// the key in question, so we need to filter out such rows.
filters = append(filters, &model.Filter{Field: groupedField.Field, Match: model.MatchExists, IsAnnotation: true})
}
where, err := qb.makeWhereJsonb(filters)
if err != nil {
return nil, err
Expand Down

0 comments on commit e013f7e

Please sign in to comment.