anchor.diff:  %% refactored to explicitly understand key order at some future point.
   fix_subquery_order_compare(Qa, Qb) ->
       {startkey, Astartkey} = lists:keyfind(startkey, 1, Qa?SQL_SELECT.'WHERE'),
  -    {endkey, Aendkey} = lists:keyfind(endkey, 1, Qa?SQL_SELECT.'WHERE'),
  +    {endkey, Aendkey}     = lists:keyfind(endkey, 1, Qa?SQL_SELECT.'WHERE'),
       {startkey, Bstartkey} = lists:keyfind(startkey, 1, Qb?SQL_SELECT.'WHERE'),
  -    case Astartkey < Aendkey of
  +    {endkey, Bendkey}     = lists:keyfind(endkey, 1, Qb?SQL_SELECT.'WHERE'),
  +
  +    fix_subquery_order_compare(Astartkey, Aendkey, Bstartkey, Bendkey).
  +
  +%%
  +fix_subquery_order_compare(Astartkey, Aendkey, Bstartkey, Bendkey) ->
  +    if
  +        (Astartkey == Aendkey) ->
  +            (Astartkey =< Bstartkey orelse Aendkey =< Bendkey);
  +        (Bstartkey == Bendkey) ->
  +            not (Astartkey =< Bstartkey orelse Aendkey =< Bendkey);
  +        (Astartkey =< Aendkey) ->
  +            Astartkey =< Bstartkey;
           true ->
  -            Astartkey =< Bstartkey;
  -        false ->
               Bstartkey =< Astartkey
       end.
   
  
positive.code:     EndKey = riak_ql_ddl:make_key(Mod, LK,  EndVals),
      (StartKey < EndKey).
  
  fix_subquery_order(Qs) ->
      lists:sort(fun fix_subquery_order_compare/2, Qs).
  
  %% Make the subqueries appear in the same order as the keys.  The qry worker
  %% returns the results to the client in the order of the subqueries, so
  
negative.code:             {error, E};
          Where2 ->
              Mod = riak_ql_ddl:make_module_name(Table),
              IsDescending = lists:member(descending, Mod:field_orders()),
              SubQueries1 =
                  [Q1?SQL_SELECT{
                         is_executable = true,
                         type          = timeseries,
                         'WHERE'       = maybe_fix_start_order(IsDescending, X, Mod, LK),
                         local_key     = LK,
                         partition_key = PK} || X <- Where2],
              SubQueries2 =
                  case IsDescending of
                      true ->
                          fix_subquery_order(SubQueries1);
                      false ->
                          SubQueries1
                  end,
              {ok, SubQueries2}
      end.
  
  