Skip to content

Commit

Permalink
Merge pull request #994 from go-kivik/readReduceCache
Browse files Browse the repository at this point in the history
Disallow certain options for reduce
  • Loading branch information
flimzy authored Jun 4, 2024
2 parents 1540d8a + a39438d commit ea3d422
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
9 changes: 8 additions & 1 deletion x/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ func (d *db) performQuery(
}

if meta.reducible && (vopts.reduce == nil || *vopts.reduce) {
if vopts.includeDocs {
_ = results.Close() //nolint:sqlclosecheck // invalid option specified for reduce, so abort the query
return nil, &internal.Error{Status: http.StatusBadRequest, Message: "include_docs is invalid for reduce"}
}
if vopts.conflicts {
_ = results.Close() //nolint:sqlclosecheck // invalid option specified for reduce, so abort the query
return nil, &internal.Error{Status: http.StatusBadRequest, Message: "conflicts is invalid for reduce"}
}
return d.reduce(ctx, meta.lastSeq, ddoc, view, rev.String(), results, meta.reduceFuncJS, vopts.reduceGroupLevel())
}

Expand Down Expand Up @@ -403,7 +411,6 @@ func (d *db) reduce(ctx context.Context, seq int, ddoc, name, rev string, result
if row.Value != nil {
value, _ = json.Marshal(row.Value)
}
fmt.Printf("INSERTING: %v, %v, %v, %v, %v, %v, %v\n", seq, depth, string(key), row.First, string(key), row.Last, string(value))
if _, err = stmt.ExecContext(ctx, seq, depth, key, row.First, key, row.Last, value); err != nil {
d.logger.Printf("Failed to insert reduce result [%v, %v, %v, %v, %v, %v, %v]: %s",
seq, depth, key, row.First, key, row.Last, value,
Expand Down
102 changes: 98 additions & 4 deletions x/sqlite/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,104 @@ func TestDBQuery(t *testing.T) {
},
}
})
tests.Add("include_docs is invalid for reduce=true", func(t *testing.T) interface{} {
d := newDB(t)
_ = d.tPut("_design/foo", map[string]interface{}{
"views": map[string]interface{}{
"bar": map[string]string{
"map": `function(doc) {
emit(doc._id, [1]);
}`,
"reduce": `_count`,
},
},
})

return test{
db: d,
ddoc: "_design/foo",
view: "_view/bar",
options: kivik.Params(map[string]interface{}{
"reduce": true,
"include_docs": true,
}),
wantErr: "include_docs is invalid for reduce",
wantStatus: http.StatusBadRequest,
}
})
tests.Add("include_docs is invalid for implicit reduce", func(t *testing.T) interface{} {
d := newDB(t)
_ = d.tPut("_design/foo", map[string]interface{}{
"views": map[string]interface{}{
"bar": map[string]string{
"map": `function(doc) {
emit(doc._id, [1]);
}`,
"reduce": `_count`,
},
},
})

return test{
db: d,
ddoc: "_design/foo",
view: "_view/bar",
options: kivik.Params(map[string]interface{}{
"include_docs": true,
}),
wantErr: "include_docs is invalid for reduce",
wantStatus: http.StatusBadRequest,
}
})
tests.Add("conflicts is invalid for reduce=true", func(t *testing.T) interface{} {
d := newDB(t)
_ = d.tPut("_design/foo", map[string]interface{}{
"views": map[string]interface{}{
"bar": map[string]string{
"map": `function(doc) {
emit(doc._id, [1]);
}`,
"reduce": `_count`,
},
},
})

return test{
db: d,
ddoc: "_design/foo",
view: "_view/bar",
options: kivik.Params(map[string]interface{}{
"reduce": true,
"conflicts": true,
}),
wantErr: "conflicts is invalid for reduce",
wantStatus: http.StatusBadRequest,
}
})
tests.Add("conflicts is invalid for implicit reduce", func(t *testing.T) interface{} {
d := newDB(t)
_ = d.tPut("_design/foo", map[string]interface{}{
"views": map[string]interface{}{
"bar": map[string]string{
"map": `function(doc) {
emit(doc._id, [1]);
}`,
"reduce": `_count`,
},
},
})

return test{
db: d,
ddoc: "_design/foo",
view: "_view/bar",
options: kivik.Params(map[string]interface{}{
"conflicts": true,
}),
wantErr: "conflicts is invalid for reduce",
wantStatus: http.StatusBadRequest,
}
})

/*
TODO:
Expand All @@ -1826,10 +1924,6 @@ func TestDBQuery(t *testing.T) {
- skip
- endkey
- startkey
- conflicts
- include_docs
- attachments
- att_encoding_info
- key
- keys
- update_seq
Expand Down

0 comments on commit ea3d422

Please sign in to comment.