Select_merge with dynamic fields on a left outer join

We have a query that does a left outer join on a child table and then uses select_merge to merge dynamically selected fields from the child row into the parent. However, when there is no child row, the call to map fails with cannot merge because the right side is not a map, got: nil. Is there a way to get the call to map to use an empty map for merging when there isn’t a row to merge?

As a simplified example (assume that Product exists and has virtual fields for the translated fields in
Product.Translation), we have the function below where the translated_fields are provided contextually (for a product list, you might just want the name, but on a product display page, you want the description as well):

def product_with_translations(sku_code, locale, translated_fields \\ ~w(name)a) do
  |> from(as: :product)
  |> where(sku_code: ^sku_code)
  |> join(
    :left, [product: p],
    t in Product.Translation,
    on: t.product_id == and t.locale == ^locale,
    as: :translation
  |> select([product: p], p)
  |> select_merge([translation: t], map(t, ^translated_fields))

As long as the associated Product.Translation exists, this works beautifully. However, if we were to ask for locale de-DE and don’t have it we receive an ArgumentError:

iex>"123", "fr-CA", ~w(name description)a))
** (ArgumentError) cannot merge because the right side is not a map, got: nil

Nominally, it seems that this should work, but how can we make map produce a default empty map on a left outer join? Or is this an Ecto bug or missing feature?

Are translated_fields really dynamic?
If no, then I’d recommend to use simpler select expression:

|> select([product: p, translation: t], %{p | name: coalesce(,})

actually, multiple select_merge can be composed:

|> Enum.reduce(query, fn
  # it would be great to have something like this:
  # field, query ->
  #   query |> select_merge([product: p, translation:t], %{^field => coalesce(field(t, ^field), field(p, ^field))})
  # but dynamic atoms are not supported as a field key
  :name, query ->
    |> select_merge([product: p, translation: t], %{name: coalesce(,})
  :description, query ->
    |> select_merge([product: p, translation: t], %{description: coalesce(t.description, p.description)})

Yes, the fields are dynamic. While the example I provided does use a single table, we are using this with multiple tables that all have a translations child relationship. We are using a function to get both the child schema and the fields that need to be mapped with select_merge.

I have opened this as a bug in the Ecto tracker #3218 as the more I think about this, map(t, ^fields) should probably act the same as Map.take(t || %{}, fields) would in pure Elixir.