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
16 changes: 12 additions & 4 deletions lib/ecto/query/planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1282,8 +1282,11 @@ defmodule Ecto.Query.Planner do
error!(query, "queries that do not have a schema need to explicitly pass a :select clause")
end

def ensure_select(%{select: nil, from: %{source: {:fragment, _, _}}} = query, true) do
error!(query, "queries from a fragment need to explicitly pass a :select clause")
def ensure_select(%{select: nil, from: %{source: {:fragment, [], _}}} = query, true) do
error!(
query,
"queries from a fragment need to explicitly pass a :select clause or use the `:columns` option"
)
end

def ensure_select(%{select: nil} = query, true) do
Expand Down Expand Up @@ -2248,8 +2251,13 @@ defmodule Ecto.Query.Planner do
{{:map, Enum.map(fields, &{&1, {:value, :any}})},
Enum.map(fields, &select_field(&1, ix, :always))}

{:error, {:fragment, _, _}} ->
{{:value, :map}, [{:&, [], [ix]}]}
{:error, {:fragment, meta, _}} ->
if columns = meta[:column_names] do
{{:map, Enum.map(columns, &{&1, {:value, :any}})},
Enum.map(columns, &select_field(&1, ix, :always))}
Comment on lines +2255 to +2257

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@josevalim Sorry while making your change I actually noticed we already had a way to handle map creation without adding a new {:source, :fragment, ...} construct. Just letting you take a look again before mering

else
{{:value, :map}, [{:&, [], [ix]}]}
end

{:error, {:values, _, [types, _]}} ->
fields = Keyword.keys(types)
Expand Down
6 changes: 6 additions & 0 deletions test/ecto/query/planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,12 @@ defmodule Ecto.Query.PlannerTest do
end
end

test "normalize: select source on fragment with columns" do
query = from f in fragment("select 1", columns: [:x])
{_, _, _, select} = normalize_with_params(query)
assert %{from: {_, {:map, [x: {:value, :any}]}}} = select
end

test "normalize: select with map/2" do
query = Post |> select([p], map(p, [:id, :title])) |> normalize()
assert query.select.expr == {:&, [], [0]}
Expand Down
2 changes: 1 addition & 1 deletion test/ecto/query/subquery_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ defmodule Ecto.Query.SubqueryTest do

test "raises on fragment source without :select" do
query = from f in fragment("select 1 as x")
assert_raise Ecto.SubQueryError, ~r/queries from a fragment need to explicitly pass a :select clause in query/, fn ->
assert_raise Ecto.SubQueryError, ~r/queries from a fragment need to explicitly pass a :select clause/, fn ->
plan(from(subquery(query), []))
end
end
Expand Down
Loading