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:        {error, invalid_coverage_context_checksum},
         compile(get_ddl("create table t (a timestamp not null, primary key ((quantum(a,1,d)), a))"), Q, 100)).
  
  times_have_gap_test_() ->
      BadRanges = [
                   {
                     {'>=', 100},
                     {'<=', 99},
                     lower_bound_must_be_less_than_upper_bound
                   },
                   {
                     {'>', 100},
                     {'<=', 99},
                     lower_bound_must_be_less_than_upper_bound
                   },
                   {
                     {'>=', 100},
                     {'<', 99},
                     lower_bound_must_be_less_than_upper_bound
                   },
                   {
                     {'>', 100},
                     {'<', 99},
                     lower_bound_must_be_less_than_upper_bound
                   },
                   {
                     {'>', 99},
                     {'<', 99},
                     lower_and_upper_bounds_are_equal_when_no_equals_operator
                   },
                   {
                     {'>=', 99},
                     {'<', 99},
                     lower_and_upper_bounds_are_equal_when_no_equals_operator
                   },
                   {
                     {'>', 99},
                     {'<=', 99},
                     lower_and_upper_bounds_are_equal_when_no_equals_operator
                   },
                   {
                     {'>', 99},
                     {'<', 100},
                     lower_and_upper_bounds_are_equal_when_no_equals_operator
                   }
                  ],
      GoodRanges = [
                    {
                      {'>=', 99},
                      {'<', 100}
                    },
                    {
                      {'>', 99},
                      {'<=', 100}
                    },
                    {
                      {'>', 99},
                      {'<', 101}
                    },
                    {
                      {'>=', 99},
                      {'<', 101}
                    },
                    {
                      {'>=', 99},
                      {'<=', 101}
                    }
                   ],
  
      lists:map(
        fun({{Op1, Val1}, {Op2, Val2}, Error}) ->
                ?_assertError(
                   {Error, _},
                   break_out_timeseries(
                     [{Op1, <<"qfield">>, {integer, Val1}},
                      {Op2, <<"qfield">>, {integer, Val2}}],
                     [], <<"qfield">>))
        end, BadRanges)
          ++
      lists:map(
        fun({{Op1, Val1}, {Op2, Val2}}) ->
                ?_assertMatch(
                   {_, _, _},
                   break_out_timeseries(
                     [{Op1, <<"qfield">>, {integer, Val1}},
                      {Op2, <<"qfield">>, {integer, Val2}}],
                     [], <<"qfield">>))
        end, GoodRanges).
  
  -endif.
  
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.
  
  