Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select columns when query #10

Open
developerworks opened this issue Apr 27, 2016 · 4 comments
Open

Select columns when query #10

developerworks opened this issue Apr 27, 2016 · 4 comments

Comments

@developerworks
Copy link

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
Copy link
Author

developerworks commented Apr 27, 2016

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?

@pma
Copy link
Owner

pma commented Apr 27, 2016

@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):

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.

@developerworks
Copy link
Author

developerworks commented Apr 28, 2016

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 ?

@pma
Copy link
Owner

pma commented May 1, 2016

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants