Skip to content

Commit 83e39b6

Browse files
committed
mango: correct behavior of fields on _explain
The `fields` attribute on `_explain` might have the string value `"all_fields"` when the respective query parameter (`fields`) is not set by the user. This is to express that no projection of fields would happen on the returned documents. The current behavior contradicts with the current contract, because by documentation, `fields` is an array of strings instead to provide information on the projected fields. The discrepancy here makes it hard to formalize this in OpenAPI which leads to problems in the development of SDKs, among others. Change `_explain` to return `[]` for `fields` when it was not set through the query parameters. This is thought to be semantically equivalent to `"all_fields"` therefore this would not cause problems while preserving the promised type. Thanks @ricellis for reporting this problem!
1 parent f79d951 commit 83e39b6

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/docs/src/api/database/find.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,9 @@ it easier to take advantage of future improvements to query planning
14011401
:>json object mrargs: Arguments passed to the underlying view.
14021402
:>json number limit: Limit parameter used.
14031403
:>json number skip: Skip parameter used.
1404-
:>json array fields: Fields to be returned by the query.
1404+
:>json array fields: Fields to be returned by the query. The `[]`
1405+
value here means all the fields, since there is no projection
1406+
happening in that case.
14051407
:>json boolean partitioned: The database is partitioned or not.
14061408
:>json array index_candidates: The list of all indexes that were
14071409
found but not selected for serving the query. See :ref:`the

src/mango/src/mango_cursor.erl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ extract_selector_hints(Selector) ->
294294
| {opts, ejson()}
295295
| {limit, integer()}
296296
| {skip, integer()}
297-
| {fields, fields()}
297+
| {fields, [field()]}
298298
| {index_candidates, [candidate_index()]}
299299
| {selector_hints, selector_hints()}
300300
| {atom(), any()}.
@@ -309,7 +309,7 @@ explain(#cursor{} = Cursor) ->
309309
opts = Opts0,
310310
limit = Limit,
311311
skip = Skip,
312-
fields = Fields
312+
fields = Fields0
313313
} = Cursor,
314314
DbName = couch_db:name(Db),
315315
Partitioned = fabric_util:is_partitioned(Db),
@@ -332,6 +332,11 @@ explain(#cursor{} = Cursor) ->
332332
false ->
333333
Opts1
334334
end,
335+
Fields =
336+
case Fields0 of
337+
all_fields -> [];
338+
Value -> Value
339+
end,
335340
CandidateIndexes = extract_candidate_indexes(Cursor),
336341
SelectorHints = extract_selector_hints(Selector),
337342
{
@@ -1091,7 +1096,6 @@ explain_test_() ->
10911096
t_explain_empty(_) ->
10921097
Selector = {[]},
10931098
Indexes = sets:new(),
1094-
Fields = all_fields,
10951099
Trace =
10961100
#{
10971101
all_indexes => Indexes,
@@ -1110,7 +1114,7 @@ t_explain_empty(_) ->
11101114
opts = [{user_ctx, user_ctx}],
11111115
limit = limit,
11121116
skip = skip,
1113-
fields = Fields,
1117+
fields = all_fields,
11141118
trace = Trace
11151119
},
11161120
Hints = [{[{type, json}, {indexable_fields, []}, {unindexable_fields, []}]}],
@@ -1123,7 +1127,7 @@ t_explain_empty(_) ->
11231127
{opts, {[]}},
11241128
{limit, limit},
11251129
{skip, skip},
1126-
{fields, Fields},
1130+
{fields, []},
11271131
{index_candidates, []},
11281132
{selector_hints, Hints}
11291133
]},
@@ -1140,7 +1144,7 @@ t_explain_regular(_) ->
11401144
},
11411145
Selector = {[]},
11421146
Indexes = sets:from_list([Index]),
1143-
Fields = all_fields,
1147+
Fields = some_fields,
11441148
Trace =
11451149
#{
11461150
all_indexes => Indexes,

0 commit comments

Comments
 (0)