This is an automated email from the ASF dual-hosted git repository.

garren pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/master by this push:
     new d98fd88  Added more info for a mango sort error (#1970)
d98fd88 is described below

commit d98fd880bfa97f799c1bc6a05f2c888160616c53
Author: garren smith <[email protected]>
AuthorDate: Thu Mar 7 15:46:18 2019 +0200

    Added more info for a mango sort error (#1970)
    
    Added more info for a mango sort error
    
    The mango sort error can be a bit confusing with using a partitioned
    database this gives a little more detail on why mango could not find
    a specific index when a sort is used.
---
 src/mango/src/mango_error.erl             | 16 +++++++++++++++-
 src/mango/src/mango_idx.erl               | 27 +++++++++++++++++++++++----
 test/elixir/test/partition_mango_test.exs | 27 +++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/src/mango/src/mango_error.erl b/src/mango/src/mango_error.erl
index dcf4b9a..2f22552 100644
--- a/src/mango/src/mango_error.erl
+++ b/src/mango/src/mango_error.erl
@@ -25,7 +25,21 @@ info(mango_idx, {no_usable_index, missing_sort_index}) ->
     {
         400,
         <<"no_usable_index">>,
-        <<"No index exists for this sort, try indexing by the sort fields.">>
+        <<"No index exists for this sort, "
+            "try indexing by the sort fields.">>
+    };
+info(mango_idx, {no_usable_index, missing_sort_index_partitioned}) ->
+    {
+        400,
+        <<"no_usable_index">>,
+        <<"No partitioned index exists for this sort, "
+            "try indexing by the sort fields.">>
+    };
+info(mango_idx, {no_usable_index, missing_sort_index_global}) ->
+    {
+        400,
+        <<"no_usable_index">>,
+        <<"No global index exists for this sort, try indexing by the sort 
fields.">>
     };
 info(mango_json_bookmark, {invalid_bookmark, BadBookmark}) ->
     {
diff --git a/src/mango/src/mango_idx.erl b/src/mango/src/mango_idx.erl
index 6e2abca..c2c2695 100644
--- a/src/mango/src/mango_idx.erl
+++ b/src/mango/src/mango_idx.erl
@@ -72,12 +72,23 @@ get_usable_indexes(Db, Selector, Opts) ->
 
     case lists:filter(UsableFilter, UsableIndexes1) of
         [] ->
-            ?MANGO_ERROR({no_usable_index, missing_sort_index});
+            mango_sort_error(Db, Opts);
         UsableIndexes ->
             UsableIndexes
     end.
 
 
+mango_sort_error(Db, Opts) ->
+    case {fabric_util:is_partitioned(Db), is_opts_partitioned(Opts)} of
+        {false, _} ->
+            ?MANGO_ERROR({no_usable_index, missing_sort_index});
+        {true, true} ->
+            ?MANGO_ERROR({no_usable_index, missing_sort_index_partitioned});
+        {true, false} ->
+            ?MANGO_ERROR({no_usable_index, missing_sort_index_global})
+    end.
+
+
 recover(Db) ->
     {ok, DDocs0} = mango_util:open_ddocs(Db),
     Pred = fun({Props}) ->
@@ -410,12 +421,20 @@ get_idx_partitioned(Db, DDocProps) ->
             Default
     end.
 
+is_opts_partitioned(Opts) ->
+    case couch_util:get_value(partition, Opts, <<>>) of
+        <<>> ->
+            false;
+        Partition when is_binary(Partition) ->
+            true
+    end.
+
 
 filter_partition_indexes(Indexes, Opts) ->
-    PFilt = case couch_util:get_value(partition, Opts) of
-        <<>> ->
+    PFilt = case is_opts_partitioned(Opts) of
+        false ->
             fun(#idx{partitioned = P}) -> not P end;
-        Partition when is_binary(Partition) ->
+        true ->
             fun(#idx{partitioned = P}) -> P end
     end,
     Filt = fun(Idx) -> type(Idx) == <<"special">> orelse PFilt(Idx) end,
diff --git a/test/elixir/test/partition_mango_test.exs 
b/test/elixir/test/partition_mango_test.exs
index 5a59789..3fd38d5 100644
--- a/test/elixir/test/partition_mango_test.exs
+++ b/test/elixir/test/partition_mango_test.exs
@@ -633,4 +633,31 @@ defmodule PartitionMangoTest do
     %{:body => %{"reason" => reason}} = resp
     assert Regex.match?(~r/Partition must not start/, reason)
   end
+
+  @tag :with_partitioned_db
+  test "partitioned query sends correct errors for sort errors", context do
+    db_name = context[:db_name]
+    create_partition_docs(db_name)
+
+    url = "/#{db_name}/_partition/foo/_find"
+
+    selector = %{
+      selector: %{
+        some: "field"
+      },
+      sort: ["some"],
+      limit: 50
+    }
+
+    resp = Couch.post(url, body: selector)
+    assert resp.status_code == 400
+    %{:body => %{"reason" => reason}} = resp
+    assert Regex.match?(~r/No partitioned index exists for this sort/, reason)
+
+    url = "/#{db_name}/_find"
+    resp = Couch.post(url, body: selector)
+    assert resp.status_code == 400
+    %{:body => %{"reason" => reason}} = resp
+    assert Regex.match?(~r/No global index exists for this sort/, reason)
+  end
 end

Reply via email to