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
20 changes: 12 additions & 8 deletions lib/ecto/query/planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2258,8 +2258,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, fields}}, %Ecto.SubQuery{select: select}} ->
subquery_select_fields(select, fields, ix, query)
{{:ok, {kind, fields}}, %Ecto.SubQuery{select: select}} ->
subquery_select_fields(kind, select, fields, ix, query)

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

defp subquery_select_fields(select, requested_fields, ix, query) do
defp subquery_select_fields(kind, select, requested_fields, ix, query) do
available_fields = subquery_source_fields(select)
requested_fields = List.wrap(requested_fields)

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

_ ->
error!(query, "it is not possible to return a struct subset of a subquery that does not return a schema struct")
{kind, _} when kind != :struct ->
nil

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

types =
Expand All @@ -2355,7 +2359,7 @@ defmodule Ecto.Query.Planner do
{field, type}

:error ->
error!(query, "field `#{field}` in struct/2 is not available in the subquery. " <>
error!(query, "field `#{field}` is not available in the subquery. " <>
"Subquery only returns fields: #{inspect(available_fields)}")
end
end)
Expand Down
14 changes: 14 additions & 0 deletions test/ecto/query/planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,20 @@ defmodule Ecto.Query.PlannerTest do
] = query.select.fields
end

test "normalze: select list of fields from subquery source" do
{_, _, _, select} = subquery(Post) |> select([p], [:title]) |> normalize_with_params()
%{from: {_, {:source, {_, postprocess_schema}, _, types}}} = select
assert postprocess_schema == Post
assert types == [title: :string]
end

test "normalze: select map/2 from subquery source" do
{_, _, _, select} = subquery(Post) |> select([p], map(p, [:title])) |> normalize_with_params()
%{from: {_, {:source, {_, postprocess_schema}, _, types}}} = select
assert postprocess_schema == nil
assert types == [title: :string]
end

test "normalize: select with :%{}" do
query = Post |> select([p], %{p | title: "foo"}) |> normalize()
assert query.select.expr == {:%{}, [], [{:|, [], [{:&, [], [0]}, [title: "foo"]]}]}
Expand Down
Loading