anchor.diff:              %% timestamp field.
               W1 = lists:keystore(startkey, 1, W, {startkey, EndKey0}),
               W2 = lists:keystore(endkey, 1, W1, {endkey, StartKey0}),
  -            %% Swap the start/end inclusivity flags unless they are the default
  -            %% value.
  +            W1 = lists:keystore(startkey, 1, W, {startkey, EndKey0}),
  +            W2 = lists:keystore(endkey, 1, W1, {endkey, StartKey0}),
               %% start inclusive defaults true, end inclusive defaults false
  -            W3 =
  -            case lists:keyfind(end_inclusive, 1, W) of
  -                {end_inclusive, false} ->
  -                    lists:keystore(start_inclusive, 1, W2, {start_inclusive, false});
  -                _ ->
  -                    W2
  -            end,
  -            case lists:keyfind(start_inclusive, 1, W2) of
  -                {start_inclusive, true} ->
  -                    lists:keystore(end_inclusive, 1, W3, {end_inclusive, true});
  -                _ ->
  -                    W3
  -            end
  +            W3 = lists:keystore(start_inclusive, 1, W2,
  +                                {start_inclusive, proplists:get_value(end_inclusive, W, false)}),
  +            _W4 = lists:keystore(end_inclusive, 1, W3,
  +                                 {end_inclusive, proplists:get_value(start_inclusive, W2, true)})
       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:     Fns2 = [Function | Fns1],
      {{finalise_aggregation, FnName, FinaliserLen + length(Fns2)}, Fns2}.
  
  %% Only change the start order if the query has descending fields, otherwise
  %% the natural order is correct.
  maybe_fix_start_order(false, W, _, _) ->
      W;
  maybe_fix_start_order(true, W, Mod, LK) ->
      fix_start_order(W, Mod, LK).
  
  %%
  fix_start_order(W, Mod, LK) ->
      {startkey, StartKey0} = lists:keyfind(startkey, 1, W),
      {endkey, EndKey0} = lists:keyfind(endkey, 1, W),
  