Repository: elixir-ecto/ecto (elixir)

Issue:
Ecto.Query.API.map broken on single nil column with join
### Environment

* Elixir version: Elixir 1.7.0-dev (4175ddebd) (compiled with OTP 20)
* Database and version: psql (PostgreSQL) 9.6.5
* Ecto version (mix deps): master
* Database adapter and version (mix deps): postgrex 0.14.0-dev
* Operating system: macOS Sierra

### Current behavior

```elixir
# works on simple query
iex> TestRepo.one(from p in Post, where: p.title == "1", select: map(p, [:counter]))
%{counter: nil}

# doesn't work with join
iex> TestRepo.one(from p in Post, join: c in assoc(p, :comments), where: p.title == "1", select: map(p, [:counter]))
nil

# when selecting more columns it works again
iex> TestRepo.one(from p in Post, join: c in assoc(p, :comments), where: p.title == "1", select: map(p, [:title, :counter]))
%{counter: nil, title: "1"}
```

### Expected behavior

```
iex> TestRepo.one(from(p in Post, join: c in assoc(p, :comments), where: p.title() == "1", select: [:counter]))
%{counter: nil}
```

here's a test case!

```
diff --git a/integration_test/cases/repo.exs b/integration_test/cases/repo.exs
index aac73cf0..18d62030 100644
--- a/integration_test/cases/repo.exs
+++ b/integration_test/cases/repo.exs
@@ -1091,6 +1091,13 @@ defmodule Ecto.Integration.RepoTest do
              TestRepo.one(from p in Post, where: p.title == "1", select: [:counter])
     end
 
+    test "take with join assocs and single nil column" do
+      %{id: pid} = TestRepo.insert!(%Post{title: "1"}, counter: nil)
+      TestRepo.insert!(%Comment{post_id: pid, text: "comment"})
+      assert %{counter: nil} =
+             TestRepo.one(from p in Post, join: c in assoc(p, :comments), where: p.title == "1", select: [:counter])
+    end
+
     test "field source" do
       TestRepo.insert!(%Permalink{url: "url"})
       assert ["url"] = Permalink |> select([p], p.url) |> TestRepo.all()

```

```
  1) test query select take with join assocs and single nil column (Ecto.Integration.RepoTest)
     integration_test/cases/repo.exs:1094
     match (=) failed
     code:  assert %{counter: nil} = TestRepo.one(from(p in Post, join: c in assoc(p, :comments), where: p.title() == "1", select: [:counter]))
     right: nil
     stacktrace:
       integration_test/cases/repo.exs:1097: (test)
```

Pull request:
Fix Ecto.Query.API.map for single nil column with join
Fixes #2380

Not sure if this is the best approach, but as far as I could test it solves the issue.

Used the test case provided by @wojtekmach in the issue as a regression test (thanks!)



Patch:
diff --git a/lib/ecto/repo/queryable.ex b/lib/ecto/repo/queryable.ex
index 59a5aaf0..44579eeb 100644
--- a/lib/ecto/repo/queryable.ex
+++ b/lib/ecto/repo/queryable.ex
@@ -184,11 +184,9 @@ defmodule Ecto.Repo.Queryable do
     end
   end
 
-  defp preprocessor({_, {:source, source_schema, fields}}, preprocess, sources, prefix, adapter) do
-    all_nil? = tuple_size(sources) != 1
-
+  defp preprocessor({_, {:source, source_schema, fields}}, preprocess, _sources, prefix, adapter) do
     fn row ->
-      {entry, rest} = process_source(source_schema, fields, row, all_nil?, prefix, adapter)
+      {entry, rest} = process_source(source_schema, fields, row, false, prefix, adapter)
       preprocess(rest, preprocess, entry, prefix, adapter)
     end
   end


Test patch:
diff --git a/integration_test/cases/joins.exs b/integration_test/cases/joins.exs
index 54911225..808464b2 100644
--- a/integration_test/cases/joins.exs
+++ b/integration_test/cases/joins.exs
@@ -112,7 +112,7 @@ defmodule Ecto.Integration.JoinsTest do
 
     query = from(p in Post, right_join: c in assoc(p, :comments),
                  preload: :permalink, order_by: c.id)
-    assert [p1, p2, nil] = TestRepo.all(query)
+    assert [p1, p2, _] = TestRepo.all(query)
     assert p1.id == pid1
     assert p2.id == pid2
 
diff --git a/integration_test/cases/repo.exs b/integration_test/cases/repo.exs
index cbaca6ad..e7899221 100644
--- a/integration_test/cases/repo.exs
+++ b/integration_test/cases/repo.exs
@@ -1093,6 +1093,13 @@ defmodule Ecto.Integration.RepoTest do
              TestRepo.one(from p in Post, where: p.title == "1", select: [:counter])
     end
 
+    test "take with join assocs and single nil column" do
+      %{id: post_id} = TestRepo.insert!(%Post{title: "1"}, counter: nil)
+      TestRepo.insert!(%Comment{post_id: post_id, text: "comment"})
+      assert %{counter: nil} ==
+              TestRepo.one(from p in Post, join: c in assoc(p, :comments), where: p.title == "1", select:  map(p, [:counter]))
+    end
+
     test "field source" do
       TestRepo.insert!(%Permalink{url: "url"})
       assert ["url"] = Permalink |> select([p], p.url) |> TestRepo.all()