Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomtaylor/0f6f18225b518bb2c60b2f10666681cf to your computer and use it in GitHub Desktop.
Save tomtaylor/0f6f18225b518bb2c60b2f10666681cf to your computer and use it in GitHub Desktop.
From c515297e3003ad2471df788f241a487d5b77ac40 Mon Sep 17 00:00:00 2001
From: Tom Taylor <tom@tomtaylor.co.uk>
Date: Fri, 2 Oct 2020 10:05:55 +0100
Subject: [PATCH] Add integration test to trap left lateral join bug
---
integration_test/cases/joins.exs | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/integration_test/cases/joins.exs b/integration_test/cases/joins.exs
index d7d1e5e5..377f2d23 100644
--- a/integration_test/cases/joins.exs
+++ b/integration_test/cases/joins.exs
@@ -152,6 +152,30 @@ defmodule Ecto.Integration.JoinsTest do
assert p2.permalink.id == plid1
end
+ @tag :lateral_left_join
+ test "lateral left joins with missing entries" do
+ %Post{id: pid1} = TestRepo.insert!(%Post{title: "1"})
+ %Post{id: pid2} = TestRepo.insert!(%Post{title: "2"})
+
+ %Permalink{id: plid1} = TestRepo.insert!(%Permalink{url: "1", post_id: pid2})
+
+ %User{id: uid1} = TestRepo.insert!(%User{})
+
+ TestRepo.insert!(%Comment{text: "1", post_id: pid1, author_id: uid1})
+ TestRepo.insert!(%Comment{text: "2", post_id: nil, author_id: uid1})
+
+ comment_sq = from(c in Comment, where: parent_as(:posts).id == c.post_id)
+
+ query =
+ Post
+ |> from(as: :posts)
+ |> join(:left_lateral, [p], subquery(comment_sq))
+ |> join(:left, [p, c], u in User, on: c.author_id == u.id)
+ |> select([p, c, u], {p, c, u})
+
+ assert [{p1, c1, u1}, {p2, nil, nil}] = TestRepo.all(query)
+ end
+
## Associations joins
test "has_many association join" do
--
2.28.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment