riak_ecto icon indicating copy to clipboard operation
riak_ecto copied to clipboard

Select columns when query

Open developerworks opened this issue 9 years ago • 4 comments

def get_user_authorizations_order_by(user_id, limit \\ 5) do
  q = from(
    auth in __MODULE__,
    order_by: [asc: auth.date_created],
    where: auth.user_id == ^user_id,
    select: %{date_created: auth.date_created, ip: auth.ip},
    limit: ^limit
  )
  Logger.debug "q = #{inspect q}"
  q |> Repo.all
end

When i run it in IEx, error riased:

iex(9)> Authorization.get_user_authorizations_order_by 796486
2016-04-27 16:32:46.601 [debug] q = #Ecto.Query<from a in TelegramRiak.Model.Authorization, where: a.user_id == ^796486, order_by: [asc: a.date_created], limit: ^5, select: %{date_created: a.date_created, ip: a.ip}>
** (Ecto.QueryError) 1) Invalid expression for Riak adapter in select clause in query:

from a in TelegramRiak.Model.Authorization,
  where: a.user_id == ^...,
  order_by: [asc: a.date_created],
  limit: ^...,
  select: %{date_created: a.date_created, ip: a.ip}

    (riak_ecto) lib/riak_ecto/normalized_query.ex:383: Riak.Ecto.NormalizedQuery.error/2
    (riak_ecto) lib/riak_ecto/normalized_query.ex:161: Riak.Ecto.NormalizedQuery.projection/6
    (riak_ecto) lib/riak_ecto/normalized_query.ex:52: Riak.Ecto.NormalizedQuery.all/2
    (riak_ecto) lib/riak_ecto.ex:146: Riak.Ecto.execute/6
         (ecto) lib/ecto/repo/queryable.ex:95: Ecto.Repo.Queryable.execute/5
         (ecto) lib/ecto/repo/queryable.ex:15: Ecto.Repo.Queryable.all/4

developerworks avatar Apr 27 '16 08:04 developerworks

I guess that riak is unstructured and object(map data type) as a whole to save, it's difficult to select individual columns from query, right?

developerworks avatar Apr 27 '16 08:04 developerworks

@developerworks It's certainly possible to implement this in the adapter.

When we're querying by anything other than the ID, we're using the Solr interface instead of the KV interface. And Solr does support specifying the fields we want to retrieve. This is supported in riak_pb_socket.search/6 search option fl (field list):

  • http://basho.github.io/riak-erlang-client/riakc_pb_socket.html#type-search_option
  • https://cwiki.apache.org/confluence/display/solr/Common+Query+Parameters#CommonQueryParameters-Thefl(FieldList)Parameter

When querying via the KV interface, we always get the complete map from the database. But we can still respect the select to filter the fields to consider when building the Ecto.Schema struct.

pma avatar Apr 27 '16 11:04 pma

And Solr does support specifying the fields we want to retrieve

You means, if i want to specifying the fields we want to retrieve, i must use the :riakc_pb_socket.search/6 functions ?

developerworks avatar Apr 28 '16 03:04 developerworks

@developerworks The call to is done here: https://github.com/pma/riak_ecto/blob/master/lib/riak/connection.ex#L74

To support specifying a subset of fields to be retrieved, we would have to properly handle Ecto.Query in Riak.Ecto.NormalizedQuery.all/2 and make sure it's passed down to the point where we pull the data from Riak.

pma avatar May 01 '16 11:05 pma