anchor.diff:                                                            ]},
                                                  {filter,   []}
                                                 ],
  -                              partition_key = #key_v1{ast = [
  -                                                             #hash_fn_v1{mod = riak_ql_quanta,
  -                                                                         fn = quantum,
  -                                                                         args = [
  -                                                                                 #param_v1{name = [<<"time">>]},
  -                                                                                 15,
  -                                                                                 m
  -                                                                                ],
  -                                                                         type = timestamp}
  -                                                            ]
  -                                                     },
  -                              local_key = #key_v1{ast = [
  -                                                         #param_v1{name = [<<"time">>]},
  -                                                         #param_v1{name = [<<"user">>]}
  -                                                        ]}
  +                              partition_key = get_standard_pk(),
  +                              local_key     = get_standard_lk()
                        }],
  -    gg:format("in simplest~nExpected:~n- ~p~nGot:~n- ~p~n", [Expected, Got]),
  +    %% simplify the test we overwrite a couple of fields
       ?assertEqual(Expected, Got).
   
  +%% got for 3 queries to get partition ordering problems flushed out
   simple_spanning_boundary_test() ->
       DDL = get_standard_ddl(),
  -    Query = "select weather from GeoCheckin where time > 3000 and time < 16000 and user = \"user_1\"",
  +    Query = "select weather from GeoCheckin where time > 3000 and time < 31000 and user = \"user_1\"",
       {ok, Q} = get_query(Query),
       true = is_query_valid(DDL, Q),
       %% get basic query
  -    %% make new Where clause
  -    NewW = [
  -	    {and_, 
  -	     {and_,
  -	      {'>', ?NAME, {int,  1 * ?MIN}},
  -	      {'<', ?NAME, {int, 16 * ?MIN}}
  -	     },
  -	     {'=', "user", {word, "user_1"}}
  -	    }
  -	   ],
  -    Q2 = Q#riak_sql_v1{'WHERE' = NewW},
  -    Got = compile(DDL, Q2),
  -    %% now make the result - expecting 2 queries
  +    Got = compile(DDL, Q),
  +    %% now make the result - expecting 3 queries
       W1 = [
  -	  {startkey, "user_112323213"},
  -	  {endkey,   "user_14345345"}
  +	  {startkey, [{<<"time">>, 3000},  {<<"user">>, <<"user_1">>}]},
  +	  {endkey,   [{<<"time">>, 15000}, {<<"user">>, <<"user_1">>}]},
  +	  {filter, []}
   	 ],
       W2 = [
  -	  {startkey, "user_1dlfkljfkldsjdsa"},
  -	  {endkey,   "user_1wjweqoewoiei"}
  +	  {startkey, [{<<"time">>, 15000}, {<<"user">>, <<"user_1">>}]},
  +	  {endkey,   [{<<"time">>, 30000}, {<<"user">>, <<"user_1">>}]},
  +	  {filter, []}
  +	 ],
  +    W3 = [
  +	  {startkey, [{<<"time">>, 30000}, {<<"user">>, <<"user_1">>}]},
  +	  {endkey,   [{<<"time">>, 31000}, {<<"user">>, <<"user_1">>}]},
  +	  {filter, []}
   	 ],
       Expected = [
   		Q#riak_sql_v1{is_executable = true,
   			      type          = timeseries,
  -			      'WHERE'       = W1},
  +			      'WHERE'       = W1,
  +                              partition_key = get_standard_pk(),
  +                              local_key     = get_standard_lk()},
   		Q#riak_sql_v1{is_executable = true,
   			      type          = timeseries,
  -			      'WHERE'       = W2}
  +			      'WHERE'       = W2,
  +                              partition_key = get_standard_pk(),
  +                              local_key     = get_standard_lk()},
  +		Q#riak_sql_v1{is_executable = true,
  +			      type          = timeseries,
  +			      'WHERE'       = W3,
  +                              partition_key = get_standard_pk(),
  +                              local_key     = get_standard_lk()}
   	       ],
  -    gg:format("in simple spanning~nExpected:~n- ~p~nGot:~n- ~p~n", [Expected, Got]),
       ?assertEqual(Expected, Got).
   
   %%
  
positive.code: -include_lib("eunit/include/eunit.hrl").
  
  %%
  %% Helper Fn for unit tests
  %%
  
  make_ddl(Bucket, Fields) when is_binary(Bucket) ->
  
negative.code: %% adding the local key here is a bodge
  %% should be a helper fun in the generated DDL module but I couldn't
  %% write that up in time
  comp2(#ddl_v1{} = DDL, #riak_sql_v1{is_executable = false,
                     'WHERE'       = W} = Q) ->
      case compile_where(DDL, W) of
          {error, E} -> [{error, E}];
          NewW       -> expand_query(DDL, Q, NewW)
      end.
  
  %% now break out the query on quantum boundaries
  expand_query(#ddl_v1{local_key = LK, partition_key = PK}, Q, NewW) ->
      %% bodge
      NewWs = expand_where(NewW, PK),
      [Q#riak_sql_v1{is_executable = true,
  		   type          = timeseries,
  		   'WHERE'       = X,
  		   local_key     = LK,
  		   partition_key = PK} || X <- NewWs].
  	
  
  expand_where(Where, #key_v1{ast = PAST}) ->
      GetMaxMinFun = fun({startkey, [{_, H} | _T]}, {_S, E}) ->
  			   {H, E};
  		      ({endkey, [{_, H} | _T]}, {S, _E}) ->
  			   {S, H};
  		      (_, {S, E})  ->
  			   {S, E}
  		   end,
      {Min, Max} = lists:foldl(GetMaxMinFun, {"", ""}, Where),
      [{[QField], Q, U}] = [{X, Y, Z} || #hash_fn_v1{mod = riak_ql_quanta,
  						 fn   = quantum,
  						 args = [#param_v1{name = X}, Y, Z]} 
  					 <- PAST],
      {NoSubQueries, Boundaries} = riak_ql_quanta:quanta(Min, Max, Q, U),
      case NoSubQueries of
  	1 -> [Where];
  	_ -> _NewWs = make_wheres(Where, QField, Min, Max, Boundaries)
      end.
  
  make_wheres(Where, QField, Min, Max, Boundaries) ->
      Starts = [Min | Boundaries],
      Ends   = Boundaries ++ [Max],
      make_w2(Starts, Ends, QField, Where, []).
  
  make_w2([], [], _QField, _Where, Acc) ->
      lists:reverse(Acc);
  make_w2([Start | T1], [End | T2], QField, Where, Acc) ->
      Where2 = swap(Where, QField, startkey, Start),
      Where3 = swap(Where2, QField, endkey, End),
      make_w2(T1, T2, QField, Where, [Where3 | Acc]).
  
  swap(Where, QField, Key, Val) ->
      {Key, Fields} = lists:keyfind(Key, 1, Where),
      NewFields = lists:keyreplace(QField, 1, Fields, {QField, Val}),
      _NewWhere = lists:keyreplace(Key, 1, Where, {Key, NewFields}).
  
  %% going forward the compilation and restructuring of the queries will be a big piece of work
  %% for the moment we just brute force assert that the query is a timeseries SQL request
  %% and go with that
  compile_where(DDL, Where) ->
      case check_if_timeseries(DDL, Where) of
          {error, E}   -> {error, E};
          {true, NewW} -> NewW
  end.
  
  check_if_timeseries(#ddl_v1{bucket = _B, partition_key = PK, local_key = LK},
                [{and_, _LHS, _RHS} = W]) ->
      try    #key_v1{ast = PAST} = PK,
             #key_v1{ast = LAST} = LK,
             LocalFields     = [X || #param_v1{name = X} <- LAST],
             PartitionFields = [X || #param_v1{name = X} <- PAST],
             [QField] = [X || #hash_fn_v1{mod = riak_ql_quanta,
  					fn   = quantum,
  					args = [#param_v1{name = X} | _Rest]} 
  				<- PAST],
             StrippedW = strip(W, []),
             {StartW, EndW, Filter} = break_out_timeseries(StrippedW, LocalFields, [QField]),
             StartKey = rewrite(LK, StartW),
             EndKey = rewrite(LK, EndW),
             {true, [
                     {startkey, StartKey},
                     {endkey,   EndKey},
                     {filter,   Filter}
                    ]}
      catch  _:_ -> {error, {where_not_timeseries, W}}
      end;
  check_if_timeseries(_DLL, Where) ->
      {error, {where_not_supported, Where}}.
  
  break_out_timeseries(Ands, LocalFields, QuantumFields) ->
      {NewAnds, [Ends, Starts]} = get_fields(QuantumFields, Ands, []),
  