willholley closed pull request #1014: Fix index validation for nested $and
URL: https://github.com/apache/couchdb/pull/1014
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/mango/src/mango_selector.erl b/src/mango/src/mango_selector.erl
index 4ff36945a9..2a546c9baa 100644
--- a/src/mango/src/mango_selector.erl
+++ b/src/mango/src/mango_selector.erl
@@ -578,36 +578,46 @@ match({[_, _ | _] = _Props} = Sel, _Value, _Cmp) ->
 % until we match then all or run out of selector to
 % match against.
 
+has_required_fields(Selector, RequiredFields) ->
+    Remainder = has_required_fields_int(Selector, RequiredFields),
+    Remainder == [].
+
 % Empty selector
-has_required_fields({[]}, _) ->
-    false;
+has_required_fields_int({[]}, Remainder) ->
+    Remainder;
 
 % No more required fields
-has_required_fields(_, []) ->
-    true;
+has_required_fields_int(_, []) ->
+    [];
 
 % No more selector
-has_required_fields([], _) ->
-    false;
+has_required_fields_int([], Remainder) ->
+    Remainder;
 
-has_required_fields(Selector, RequiredFields) when not is_list(Selector) ->
-    has_required_fields([Selector], RequiredFields);
+has_required_fields_int(Selector, RequiredFields) when not is_list(Selector) ->
+    has_required_fields_int([Selector], RequiredFields);
 
 % We can "see" through $and operator. We ignore other
 % combination operators because they can't be used to restrict
 % an index.
-has_required_fields([{[{<<"$and">>, Args}]}], RequiredFields) 
+has_required_fields_int([{[{<<"$and">>, Args}]}], RequiredFields) 
+        when is_list(Args) ->
+    has_required_fields_int(Args, RequiredFields);
+
+% Handle $and operator where it has peers
+has_required_fields_int([{[{<<"$and">>, Args}]} | Rest], RequiredFields) 
         when is_list(Args) ->
-    has_required_fields(Args, RequiredFields);
+    Remainder = has_required_fields_int(Args, RequiredFields),
+    has_required_fields_int(Rest, Remainder);
 
-has_required_fields([{[{Field, Cond}]} | Rest], RequiredFields) ->
+has_required_fields_int([{[{Field, Cond}]} | Rest], RequiredFields) ->
     case Cond of
         % $exists:false is a special case - this is the only operator
         % that explicitly does not require a field to exist
         {[{<<"$exists">>, false}]} ->
-            has_required_fields(Rest, RequiredFields);
+            has_required_fields_int(Rest, RequiredFields);
         _ ->
-            has_required_fields(Rest, lists:delete(Field, RequiredFields))
+            has_required_fields_int(Rest, lists:delete(Field, RequiredFields))
     end.
 
 
@@ -651,6 +661,27 @@ has_required_fields_and_true_test() ->
     Normalized = normalize(Selector),
     ?assertEqual(true, has_required_fields(Normalized, RequiredFields)).
 
+has_required_fields_nested_and_true_test() ->
+    RequiredFields = [<<"A">>, <<"B">>],
+    Selector1 = {[{<<"$and">>,
+          [
+              {[{<<"A">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector2 = {[{<<"$and">>,
+          [
+              {[{<<"B">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector = {[{<<"$and">>,
+          [
+              Selector1,
+              Selector2
+          ]
+    }]},
+
+    ?assertEqual(true, has_required_fields(Selector, RequiredFields)).
+
 has_required_fields_and_false_test() ->
     RequiredFields = [<<"A">>, <<"C">>],
     Selector = {[{<<"$and">>,
diff --git a/src/mango/test/05-index-selection-test.py 
b/src/mango/test/05-index-selection-test.py
index fe36257e38..f8cc825768 100644
--- a/src/mango/test/05-index-selection-test.py
+++ b/src/mango/test/05-index-selection-test.py
@@ -28,6 +28,16 @@ def test_with_and(self):
             }, explain=True)
         self.assertEqual(resp["index"]["type"], "json")
 
+    def test_with_nested_and(self):
+        resp = self.db.find({
+                "name.first": {
+                    "$gt": "a",
+                    "$lt": "z"
+                },
+                "name.last": "Foo"
+            }, explain=True)
+        self.assertEqual(resp["index"]["type"], "json")
+
     def test_use_most_columns(self):
         # ddoc id for the age index
         ddocid = "_design/ad3d537c03cd7c6a43cf8dff66ef70ea54c2b40f"


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to