anchor.diff:              {SKey, SInc1}
       end.
   
  -rewrite_end_key_with_filters([], _, _, EKey, EInc) ->
  +rewrite_end_key_with_filters([], _, _, _, EKey, EInc) ->
       {EKey, EInc};
  -rewrite_end_key_with_filters([AddFieldName|Tail], ToKeyTypeFn, Filter, EKey, EInc1) when is_binary(AddFieldName) ->
  +rewrite_end_key_with_filters([AddFieldName|Tail], [Order|TailOrder], ToKeyTypeFn, Filter, EKey, EInc1) when is_binary(AddFieldName) ->
       case proplists:get_value(AddFieldName, Filter) of
  -        {Op,_,_} = F  when Op == '='; Op == '<'; Op == '<=' ->
  +        {Op,_,_} = F  when Op == '=' orelse (Order == ascending andalso  (Op == '<' orelse Op == '<='))
  +                                     orelse (Order == descending andalso  (Op == '>' orelse Op == '>=')) ->
               EKeyElem = to_key_elem(ToKeyTypeFn, F),
               %% if the quantum was inclusive, make sure we stay inclusive or
               %% the quantum boundary is modified later on
               EInc2 = ((EInc1 /= false) or is_inclusive_op(Op)),
               rewrite_end_key_with_filters(
  -                Tail, ToKeyTypeFn, Filter, EKey++[EKeyElem], EInc2);
  +                Tail, TailOrder, ToKeyTypeFn, Filter, EKey++[EKeyElem], EInc2);
           _ ->
               %% there is no filter for the next additional field so give up! The
               %% filters must be consecutive, we can't have a '_' inbetween
  
positive.code: %% For equality filters, it is not safe to remove the filter, because {3,7} is
  %% also in the range but should not be returned because only rows where b = 5
  %% are correct for this query.
  rewrite_where_with_additional_filters([], _, WhereProps) ->
      WhereProps;
  rewrite_where_with_additional_filters(AdditionalFields, ToKeyTypeFn, WhereProps) when is_list(WhereProps) ->
      SKey1 = proplists:get_value(startkey, WhereProps),
      EKey1 = proplists:get_value(endkey, WhereProps),
      SInc1 = proplists:get_value(start_inclusive, WhereProps),
  
negative.code: 
  key_length(#key_v1{ast = AST}) ->
      length(AST).
  
  local_key_field_orders(FieldOrders, PK) ->
      lists:nthtail(key_length(PK), FieldOrders).
  
  %% Calulate the final result for an aggregate.
  -spec finalise_aggregate(#riak_sel_clause_v1{}, [any()]) -> [any()].
  