Skip to content

Commit

Permalink
fixed bug in export query
Browse files Browse the repository at this point in the history
see #296
  • Loading branch information
Bernhard B committed Jul 4, 2022
1 parent 4879a30 commit 8e80ca1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,6 @@ func main() {
}

queryParser := parser.NewQueryParser(query)
queryParser.SetVersion(1)
parseResult, err := queryParser.Parse()
if err != nil {
c.JSON(422, gin.H{"error": err.Error()})
Expand Down
74 changes: 45 additions & 29 deletions src/database/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,50 +616,66 @@ func (p *ImageMonkeyDatabase) Export(parseResult parser.ParseResult, annotations
identifier = "a.accessor"
}

q := fmt.Sprintf(`SELECT i.key, CASE WHEN json_agg(q3.annotations)::jsonb = '[null]'::jsonb THEN '[]' ELSE json_agg(q3.annotations)::jsonb END as annotations,
q3.validations, i.width, i.height
FROM image i
JOIN
(
SELECT COALESCE(q.image_id, q1.image_id) as image_id, q.annotations, q1.validations FROM
(
SELECT an.image_id as image_id, (d.annotation || ('{"label":"' || %s || '"}')::jsonb || ('{"type":"' || t.name || '"}')::jsonb)::jsonb as annotations
q := fmt.Sprintf(`WITH image_annotation_refinements as (
SELECT an.image_id as image_id, (d.annotation || ('{"label":"' || %s || '"}')::jsonb || ('{"type":"' || t.name || '"}')::jsonb)::jsonb as annotations, %s as label
FROM image_annotation_refinement r
JOIN annotation_data d ON r.annotation_data_id = d.id
JOIN annotation_type t ON d.annotation_type_id = t.id
JOIN image_annotation an ON d.image_annotation_id = an.id
%s
WHERE ((%s) AND an.auto_generated = false)
UNION
SELECT n.image_id as image_id, (d.annotation || ('{"label":"' || %s || '"}')::jsonb || ('{"type":"' || t.name || '"}')::jsonb)::jsonb as annotations
WHERE an.auto_generated = false
), image_annotations as (
SELECT n.image_id as image_id, (d.annotation || ('{"label":"' || %s || '"}')::jsonb || ('{"type":"' || t.name || '"}')::jsonb)::jsonb as annotations, %s as label
FROM image_annotation n
JOIN annotation_data d ON d.image_annotation_id = n.id
JOIN annotation_type t ON d.annotation_type_id = t.id
%s
WHERE ((%s) AND n.auto_generated = false)
) q
%s (
SELECT i.id as image_id, json_agg(json_build_object('label', %s, 'num_yes', num_of_valid, 'num_no', num_of_invalid))::jsonb as validations
WHERE n.auto_generated = false
), image_validations as (
SELECT i.id as image_id, json_agg(json_build_object('label', %s, 'num_yes', num_of_valid, 'num_no', num_of_invalid))::jsonb as validations, array_agg(%s) as accessors
FROM image i
JOIN image_validation v ON i.id = v.image_id
%s
WHERE (%s)
GROUP BY i.id
) q1
ON q1.image_id = q.image_id
)q3
ON i.id = q3.image_id
WHERE i.unlocked = true
GROUP BY i.key, q3.validations, i.width, i.height`, identifier, q1, parseResult.Query, identifier, q2, parseResult.Query, joinType, identifier, q3, parseResult.Query)
), unlocked_images as (
SELECT i.id as image_id, i.key as image_uuid, i.width as image_width, i.height as image_height
FROM image i
WHERE i.unlocked = true
), filtered_annotations_and_refinements as (
SELECT image_id, annotations, accessors
FROM (
SELECT r.image_id as image_id, r.annotations as annotations, array_agg(label) as accessors
FROM image_annotation_refinements r
GROUP BY r.image_id, r.annotations
UNION
SELECT a.image_id as image_id, a.annotations as annotations, array_agg(label) as accessors
FROM image_annotations a
GROUP BY a.image_id, a.annotations
) q
WHERE %s
), filtered_image_validations as (
SELECT image_id, validations, accessors
FROM image_validations q
WHERE %s
)
SELECT i.image_uuid, CASE WHEN json_agg(q2.annotations)::jsonb = '[null]'::jsonb THEN '[]' ELSE json_agg(q2.annotations)::jsonb END as annotations
FROM unlocked_images i
JOIN
(
SELECT COALESCE(a.image_id, v.image_id) as image_id, a.annotations, v.validations
FROM filtered_annotations_and_refinements a
%s
filtered_image_validations v
ON a.image_id = v.image_id
) q2
ON q2.image_id = i.image_id
GROUP BY i.image_uuid, q2.validations, i.image_width, i.image_height`, identifier, identifier, q1, identifier, identifier, q2, identifier, identifier, q3, parseResult.Query, parseResult.Query, joinType)
rows, err := p.db.Query(context.TODO(), q, parseResult.QueryValues...)
if err != nil {
log.Debug("Couldn't export data: ", err.Error())
log.Error("Export Query: ", q)
log.Error("Couldn't export data: ", err.Error())
raven.CaptureError(err, nil)
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,6 @@ gopkg.in/src-d/go-git.v4 v4.12.0 h1:CKgvBCJCcdfNnyXPYI4Cp8PaDDAmAPEN0CtfEdEAbd8=
gopkg.in/src-d/go-git.v4 v4.12.0/go.mod h1:zjlNnzc1Wjn43v3Mtii7RVxiReNP0fIu9npcXKzuNp4=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7 changes: 7 additions & 0 deletions src/parser/v2/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,10 @@ func TestQueryImageNumAnnotations2(t *testing.T) {
equals(t, len(parseResult.QueryValues), 0)
equals(t, parseResult.Query, "q.image_num_annotations=1")
}

func TestQueryCanContainSlash(t *testing.T) {
queryParser := NewQueryParser("a/b")
parseResult, err := queryParser.Parse()
ok(t, err)
equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[]")
}
1 change: 1 addition & 0 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,6 @@ gopkg.in/src-d/go-git.v4 v4.12.0 h1:CKgvBCJCcdfNnyXPYI4Cp8PaDDAmAPEN0CtfEdEAbd8=
gopkg.in/src-d/go-git.v4 v4.12.0/go.mod h1:zjlNnzc1Wjn43v3Mtii7RVxiReNP0fIu9npcXKzuNp4=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 8e80ca1

Please sign in to comment.