Skip to content

Commit ecb21cb

Browse files
committed
mango: fix derivation of upper bound for the range of $beginsWith
1 parent e9d703c commit ecb21cb

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/mango/src/mango_idx_view.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ range(_, _, LCmp, Low, HCmp, High) ->
417417
% beginsWith requires both a high and low bound
418418
range({[{<<"$beginsWith">>, Arg}]}, LCmp, Low, HCmp, High) ->
419419
{LCmp0, Low0, HCmp0, High0} = range({[{<<"$gte">>, Arg}]}, LCmp, Low, HCmp, High),
420-
range({[{<<"$lte">>, <<Arg/binary, 16#10FFFF>>}]}, LCmp0, Low0, HCmp0, High0);
420+
range({[{<<"$lte">>, <<Arg/binary, 16#FFFF/utf8>>}]}, LCmp0, Low0, HCmp0, High0);
421421
range({[{<<"$lt">>, Arg}]}, LCmp, Low, HCmp, High) ->
422422
case range_pos(Low, Arg, High) of
423423
min ->

src/mango/src/mango_selector.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ norm_ops({[{<<"$text">>, Arg}]}) when
136136
norm_ops({[{<<"$text">>, Arg}]}) ->
137137
?MANGO_ERROR({bad_arg, '$text', Arg});
138138
norm_ops({[{<<"$beginsWith">>, Arg}]} = Cond) when is_binary(Arg) ->
139-
Cond;
139+
case couch_util:validate_utf8(Arg) of
140+
true -> Cond;
141+
false -> ?MANGO_ERROR({bad_arg, '$beginsWith', Arg})
142+
end;
140143
% Not technically an operator but we pass it through here
141144
% so that this function accepts its own output. This exists
142145
% so that $text can have a field name value which simplifies

src/mango/test/25-beginswith-test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ def test_json_range(self):
5454

5555
self.assertEqual(mrargs["start_key"], ["A"])
5656
end_key_bytes = to_utf8_bytes(mrargs["end_key"])
57-
self.assertEqual(end_key_bytes, [b"A\xef\xbf\xbd", b"<MAX>"])
57+
self.assertEqual(end_key_bytes, [b"A\xf4\x8f\xbf\xbf", b"<MAX>"])
5858

5959
def test_compound_key(self):
6060
selector = {"name": "Eddie", "location": {"$beginsWith": "A"}}
6161
mrargs = self.get_mrargs(selector)
6262

6363
self.assertEqual(mrargs["start_key"], ["Eddie", "A"])
6464
end_key_bytes = to_utf8_bytes(mrargs["end_key"])
65-
self.assertEqual(end_key_bytes, [b"Eddie", b"A\xef\xbf\xbd", b"<MAX>"])
65+
self.assertEqual(end_key_bytes, [b"Eddie", b"A\xf4\x8f\xbf\xbf", b"<MAX>"])
6666

6767
docs = self.db.find(selector)
6868
self.assertEqual(len(docs), 1)
@@ -74,12 +74,12 @@ def test_sort(self):
7474
{
7575
"sort": ["location"],
7676
"start_key": [b"A"],
77-
"end_key": [b"A\xef\xbf\xbd", b"<MAX>"],
77+
"end_key": [b"A\xf4\x8f\xbf\xbf", b"<MAX>"],
7878
"direction": "fwd",
7979
},
8080
{
8181
"sort": [{"location": "desc"}],
82-
"start_key": [b"A\xef\xbf\xbd", b"<MAX>"],
82+
"start_key": [b"A\xf4\x8f\xbf\xbf", b"<MAX>"],
8383
"end_key": [b"A"],
8484
"direction": "rev",
8585
},
@@ -97,7 +97,7 @@ def test_all_docs_range(self):
9797

9898
self.assertEqual(mrargs["start_key"], "a")
9999
end_key_bytes = to_utf8_bytes(mrargs["end_key"])
100-
self.assertEqual(end_key_bytes, [b"a", b"\xef\xbf\xbd"])
100+
self.assertEqual(end_key_bytes, [b"a", b"\xf4\x8f\xbf\xbf"])
101101

102102
def test_no_index(self):
103103
selector = {"foo": {"$beginsWith": "a"}}

0 commit comments

Comments
 (0)