anchor.diff:  %% 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{local_key = LK, partition_key = PK} = DDL,
  -      #riak_sql_v1{is_executable = false,
  +comp2(#ddl_v1{} = DDL, #riak_sql_v1{is_executable = false,
                      'WHERE'       = W} = Q) ->
       case compile_where(DDL, W) of
           {error, E} -> [{error, E}];
  -        NewWs      -> [Q#riak_sql_v1{is_executable = true,
  -                                     type          = timeseries,
  -                                     'WHERE'       = X,
  -                                     local_key     = LK,
  -                                     partition_key = PK} || X <- NewWs]
  +        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 is_timeseries(DDL, Where) of
  -        false         -> {error, {where_not_supported, Where}};
  -        {true, NewWs} -> NewWs
  +    case check_if_timeseries(DDL, Where) of
  +        {error, E}   -> {error, E};
  +        {true, NewW} -> NewW
   end.
   
  -is_timeseries(#ddl_v1{bucket = _B, partition_key = PK, local_key = LK},
  +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],
  -           QuantumFields   = [X || #hash_fn_v1{mod = riak_ql_quanta,
  -                                               fn   = quantum,
  -                                               args = [#param_v1{name = X} | _Rest]} <- 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, QuantumFields),
  +           {StartW, EndW, Filter} = break_out_timeseries(StrippedW, LocalFields, [QField]),
              StartKey = rewrite(LK, StartW),
              EndKey = rewrite(LK, EndW),
  -           {true, [[
  +           {true, [
                      {startkey, StartKey},
                      {endkey,   EndKey},
                      {filter,   Filter}
  -                  ]]}
  +                  ]}
       catch  _:_ -> {error, {where_not_timeseries, W}}
       end;
  -is_timeseries(_DLL, _Where) ->
  -    false.
  +check_if_timeseries(_DLL, Where) ->
  +    {error, {where_not_supported, Where}}.
   
   break_out_timeseries(Ands, LocalFields, QuantumFields) ->
       {NewAnds, [Ends, Starts]} = get_fields(QuantumFields, Ands, []),
  
positive.code: -include_lib("eunit/include/eunit.hrl").
  
  %%
  %% Helper Fn for unit tests
  %%
  
  make_ddl(Bucket, Fields) when is_binary(Bucket) ->
  
negative.code:         "time timestamp not null, " ++
          "weather varchar not null, " ++
          "temperature varchar, " ++
          "PRIMARY KEY((quantum(time, 15, s)), time, user))",
      Lexed = riak_ql_lexer:get_tokens(SQL),
      {ok, DDL} = riak_ql_parser:parse(Lexed),
      {module, _Module} = riak_ql_ddl_compiler:make_helper_mod(DDL),
      DDL.
  
  get_standard_pk() ->
      #key_v1{ast = [
  		   #hash_fn_v1{mod = riak_ql_quanta,
  			       fn = quantum,
  			       args = [
  				       #param_v1{name = [<<"time">>]},
  				       15,
  				       s
  				      ],
  			       type = timestamp}
  		  ]
  	   }.
  
  get_standard_lk() ->
      #key_v1{ast = [
  		   #param_v1{name = [<<"time">>]},
  		   #param_v1{name = [<<"user">>]}
  		  ]}.
  
  %%
  %% Unit tests
  