Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lib/ecto/query/planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2207,8 +2207,8 @@ defmodule Ecto.Query.Planner do
{{:ok, {:struct, _}}, {:fragment, _, _}} ->
error!(query, "it is not possible to return a struct subset of a fragment")

{{:ok, {:struct, _}}, %Ecto.SubQuery{}} ->
error!(query, "it is not possible to return a struct subset of a subquery")
{{:ok, {:struct, fields}}, %Ecto.SubQuery{select: select}} ->
subquery_select_fields(select, fields, ix, query)

{{:ok, {_, []}}, {_, _, _}} ->
error!(
Expand Down Expand Up @@ -2280,6 +2280,35 @@ defmodule Ecto.Query.Planner do
end)
end

defp subquery_select_fields(select, requested_fields, ix, query) do
available_fields = subquery_source_fields(select)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not able to check atm but I believe error! prints the query so it might be unnecessary to include this in the message

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I don't think I'm currently adding the query to any error message, but maybe I'm missing it. Could you point out the line?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry I meant that available_fields is only used here:

error!(query, "field `#{field}` in struct/2 is not available in the subquery. " <>
                         "Subquery only returns fields: #{inspect(available_fields)}")

and i think it might be unnecessary because the error! function already prints the entire query. it raises Ecto.QueryError which takes care of it. so you are already told which fields exist in that way.

it's not a big deal either way it was just a tiny cleanup (if my assumptions are right)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. I think I'd prefer this because when the query gets big (like in my current case) or involves multiple subqueries for example, it's not always entirely obvious. Listing the available fields I think will make it much clearer.

requested_fields = List.wrap(requested_fields)

schema =
case select do
{:source, {_, schema}, _, _} when not is_nil(schema) -> schema

_ ->
error!(query, "it is not possible to return a struct subset of a subquery that does not return a schema struct")
end

types =
Enum.map(requested_fields, fn field ->
Comment thread
zachdaniel marked this conversation as resolved.
case subquery_type_for(select, field) do
{:ok, type} ->
{field, type}

:error ->
error!(query, "field `#{field}` in struct/2 is not available in the subquery. " <>
"Subquery only returns fields: #{inspect(available_fields)}")
end
end)

field_exprs = Enum.map(requested_fields, &select_field(&1, ix, :always))

{{:source, {nil, schema}, nil, types}, field_exprs}
end

defp select_field(field, ix, writable) do
{{:., [writable: writable], [{:&, [], [ix]}, field]}, [], []}
end
Expand Down
22 changes: 21 additions & 1 deletion test/ecto/query/subquery_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,28 @@ defmodule Ecto.Query.SubqueryTest do
subquery = from p in Post, select: %{id: p.id, title: p.title}
query = normalize(from(p in subquery(subquery), select: map(p, [:title])))
assert [{{:., _, [{:&, [], [0]}, :title]}, [], []}] = query.select.fields
end

test "struct/2 with subqueries" do
subquery = from p in Post, select: p
query = normalize(from(p in subquery(subquery), select: struct(p, [:id, :title])))
assert query.select.fields == [
{{:., [writable: :always], [{:&, [], [0]}, :id]}, [], []},
{{:., [writable: :always], [{:&, [], [0]}, :title]}, [], []}
]

subquery = from p in Post, select: p
query = normalize(from(c in Comment, join: p in subquery(subquery), on: true, select: struct(p, [:title])))
assert query.select.fields == [{{:., [writable: :always], [{:&, [], [1]}, :title]}, [], []}]

subquery = from p in Post, select: struct(p, [:id, :title, :text])
query = normalize(from(p in subquery(subquery), select: struct(p, [:id, :title])))
assert query.select.fields == [
{{:., [writable: :always], [{:&, [], [0]}, :id]}, [], []},
{{:., [writable: :always], [{:&, [], [0]}, :title]}, [], []}
]

assert_raise Ecto.QueryError, ~r/it is not possible to return a struct subset of a subquery/, fn ->
assert_raise Ecto.QueryError, ~r/it is not possible to return a struct subset of a subquery that does not return a schema struct/, fn ->
subquery = from p in Post, select: %{id: p.id, title: p.title}
normalize(from(p in subquery(subquery), select: struct(p, [:title])))
end
Expand Down
Loading