summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTony Sun <tony.sun427@gmail.com>2020-04-21 14:44:23 -0700
committerTony Sun <tony.sun427@gmail.com>2020-04-21 14:51:59 -0700
commit5e795c6c755a9b228129349adb081ab0d29415c6 (patch)
tree9fb283a1b686eddd6f317fb941d854e65c86bdf8
parent2e9f4785d76ddc0b7a3da5b7a31e2b63ff67c0e7 (diff)
downloadcouchdb-cherry-pick-mango-fix-3.0.x-fauxton.tar.gz
fix operator issue with empty arrays (#2805)cherry-pick-mango-fix-3.0.x-fauxton
Previously, in https://github.com/apache/couchdb/pull/1783, the logic was wrong in relation to how certain operators interacted with empty arrays. We modify this logic to make it such that: {"foo":"bar", "bar":{"$in":[]}} and {"foo":"bar", "bar":{"$all":[]}} should return 0 results.
-rw-r--r--src/mango/src/mango_selector.erl4
-rw-r--r--src/mango/test/21-empty-selector-tests.py24
2 files changed, 25 insertions, 3 deletions
diff --git a/src/mango/src/mango_selector.erl b/src/mango/src/mango_selector.erl
index 3ea83c220..e884dc55c 100644
--- a/src/mango/src/mango_selector.erl
+++ b/src/mango/src/mango_selector.erl
@@ -421,7 +421,7 @@ match({[{<<"$not">>, Arg}]}, Value, Cmp) ->
not match(Arg, Value, Cmp);
match({[{<<"$all">>, []}]}, _, _) ->
- true;
+ false;
% All of the values in Args must exist in Values or
% Values == hd(Args) if Args is a single element list
% that contains a list.
@@ -506,7 +506,7 @@ match({[{<<"$gt">>, Arg}]}, Value, Cmp) ->
Cmp(Value, Arg) > 0;
match({[{<<"$in">>, []}]}, _, _) ->
- true;
+ false;
match({[{<<"$in">>, Args}]}, Values, Cmp) when is_list(Values)->
Pred = fun(Arg) ->
lists:foldl(fun(Value,Match) ->
diff --git a/src/mango/test/21-empty-selector-tests.py b/src/mango/test/21-empty-selector-tests.py
index beb222c85..31ad8e645 100644
--- a/src/mango/test/21-empty-selector-tests.py
+++ b/src/mango/test/21-empty-selector-tests.py
@@ -35,14 +35,36 @@ def make_empty_selector_suite(klass):
docs = self.db.find({"age": 22, "$or": []})
assert len(docs) == 1
+ def test_empty_array_in_with_age(self):
+ resp = self.db.find({"age": 22, "company": {"$in": []}}, explain=True)
+ self.assertEqual(resp["index"]["type"], klass.INDEX_TYPE)
+ docs = self.db.find({"age": 22, "company": {"$in": []}})
+ assert len(docs) == 0
+
def test_empty_array_and_with_age(self):
resp = self.db.find(
- {"age": 22, "$and": [{"b": {"$all": []}}]}, explain=True
+ {"age": 22, "$and": []}, explain=True
)
self.assertEqual(resp["index"]["type"], klass.INDEX_TYPE)
docs = self.db.find({"age": 22, "$and": []})
assert len(docs) == 1
+ def test_empty_array_all_age(self):
+ resp = self.db.find(
+ {"age": 22, "company": {"$all": []}}, explain=True
+ )
+ self.assertEqual(resp["index"]["type"], klass.INDEX_TYPE)
+ docs = self.db.find({"age": 22, "company": {"$all": []}})
+ assert len(docs) == 0
+
+ def test_empty_array_nested_all_with_age(self):
+ resp = self.db.find(
+ {"age": 22, "$and": [{"company": {"$all": []}}]}, explain=True
+ )
+ self.assertEqual(resp["index"]["type"], klass.INDEX_TYPE)
+ docs = self.db.find( {"age": 22, "$and": [{"company": {"$all": []}}]})
+ assert len(docs) == 0
+
def test_empty_arrays_complex(self):
resp = self.db.find({"$or": [], "a": {"$in": []}}, explain=True)
self.assertEqual(resp["index"]["type"], klass.INDEX_TYPE)