Skip to content

Fix nil map merges #4601

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

Merged
merged 3 commits into from
Apr 7, 2025
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
6 changes: 3 additions & 3 deletions integration_test/cases/repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,14 +1538,14 @@ defmodule Ecto.Integration.RepoTest do
|> select_merge([_l, p], map(p, ^~w(title posted)a))
|> TestRepo.all()

# left join record is not present
assert [%{url: "Q", title: "Z", posted: nil}] =
# left join record is not present, we consider it the same as being present with nils
assert [%{url: "Q", title: nil, posted: nil}] =
Permalink
|> join(:left, [l], p in Post, on: l.post_id == p.id and p.public == true)
|> select([l, p], merge(l, map(p, ^~w(title posted)a)))
|> TestRepo.all()

assert [%{url: "Q", title: "Z", posted: nil}] =
assert [%{url: "Q", title: nil, posted: nil}] =
Permalink
|> join(:left, [l], p in Post, on: l.post_id == p.id and p.public == true)
|> select_merge([_l, p], map(p, ^~w(title posted)a))
Expand Down
11 changes: 10 additions & 1 deletion lib/ecto/repo/queryable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ defmodule Ecto.Repo.Queryable do

defp process(row, {:merge, left, right}, from, adapter) do
{left, row} = process(row, left, from, adapter)
{right, row} = process(row, right, from, adapter)
{right, row} = process_merge(row, right, from, adapter)

data =
case {left, right} do
Expand Down Expand Up @@ -427,6 +427,15 @@ defmodule Ecto.Repo.Queryable do
end)
end

defp process_merge(row, {:source, {source, schema}, prefix, types}, _from, adapter) do
struct = Ecto.Schema.Loader.load_struct(schema, prefix, source)
struct_load!(types, row, [], false, struct, adapter)
end

defp process_merge(row, process, from, adapter) do
process(row, process, from, adapter)
end

@compile {:inline, load!: 5}
defp load!(type, value, field, struct, adapter) do
case Ecto.Type.adapter_load(adapter, type, value) do
Expand Down
Loading