Skip to content

Add support for cross lateral joins. #458

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
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
2 changes: 2 additions & 0 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ if Code.ensure_loaded?(MyXQL) do
end

defp join_on(:cross, true, _sources, _query), do: []
defp join_on(:cross_lateral, true, _sources, _query), do: []
defp join_on(_qual, expr, sources, query), do: [" ON " | expr(expr, sources, query)]

defp join_qual(:inner, _), do: " INNER JOIN "
Expand All @@ -397,6 +398,7 @@ if Code.ensure_loaded?(MyXQL) do
defp join_qual(:right, _), do: " RIGHT OUTER JOIN "
defp join_qual(:full, _), do: " FULL OUTER JOIN "
defp join_qual(:cross, _), do: " CROSS JOIN "
defp join_qual(:cross_lateral, _), do: " CROSS JOIN LATERAL "

defp where(%{wheres: wheres} = query, sources) do
boolean(" WHERE ", wheres, sources, query)
Expand Down
2 changes: 2 additions & 0 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ if Code.ensure_loaded?(Postgrex) do
end

defp join_on(:cross, true, _sources, _query), do: []
defp join_on(:cross_lateral, true, _sources, _query), do: []
defp join_on(_qual, expr, sources, query), do: [" ON " | expr(expr, sources, query)]

defp join_qual(:inner), do: "INNER JOIN "
Expand All @@ -519,6 +520,7 @@ if Code.ensure_loaded?(Postgrex) do
defp join_qual(:right), do: "RIGHT OUTER JOIN "
defp join_qual(:full), do: "FULL OUTER JOIN "
defp join_qual(:cross), do: "CROSS JOIN "
defp join_qual(:cross_lateral), do: "CROSS JOIN LATERAL "

defp where(%{wheres: wheres} = query, sources) do
boolean(" WHERE ", wheres, sources, query)
Expand Down
1 change: 1 addition & 0 deletions lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ if Code.ensure_loaded?(Tds) do
defp join_qual(:cross), do: "CROSS JOIN "
defp join_qual(:inner_lateral), do: "CROSS APPLY "
defp join_qual(:left_lateral), do: "OUTER APPLY "
defp join_qual(:cross_lateral), do: error!(nil, "cross lateral joins are not supported in the Tds Adapter")

defp where(%Query{wheres: wheres} = query, sources) do
boolean(" WHERE ", wheres, sources, query)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule EctoSQL.MixProject do
if path = System.get_env("ECTO_PATH") do
{:ecto, path: path}
else
{:ecto, "~> 3.9.0"}
{:ecto, "~> 3.10.0-dev", github: "elixir-ecto/ecto"}
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"deep_merge": {:hex, :deep_merge, "0.2.0", "c1050fa2edf4848b9f556fba1b75afc66608a4219659e3311d9c9427b5b680b3", [:mix], [], "hexpm", "e3bf435a54ed27b0ba3a01eb117ae017988804e136edcbe8a6a14c310daa966e"},
"earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"},
"ecto": {:hex, :ecto, "3.9.0", "7c74fc0d950a700eb7019057ff32d047ed7f19b57c1b2ca260cf0e565829101d", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fed5ebc5831378b916afd0b5852a0c5bb3e7390665cc2b0ec8ab0c712495b73d"},
"ecto": {:git, "https://github.com/elixir-ecto/ecto.git", "96d4920339d5d645dedaed1ef2ce7428683f529d", []},
"ex_doc": {:hex, :ex_doc, "0.29.0", "4a1cb903ce746aceef9c1f9ae8a6c12b742a5461e6959b9d3b24d813ffbea146", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "f096adb8bbca677d35d278223361c7792d496b3fc0d0224c9d4bc2f651af5db1"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
Expand Down
12 changes: 12 additions & 0 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,18 @@ defmodule Ecto.Adapters.MyXQLTest do
~s{WHERE ((s0.`id` > 0) AND (s0.`id` < ?))}
end

test "cross lateral join with fragment" do
query = Schema
|> join(:cross_lateral, [p], q in fragment("SELECT * FROM schema2 AS s2 WHERE s2.id = ? AND s2.field = ?", p.x, ^10))
|> select([p, q], {p.id, q.z})
|> where([p], p.id > 0 and p.id < ^100)
|> plan()
assert all(query) ==
~s{SELECT s0.`id`, f1.`z` FROM `schema` AS s0 CROSS JOIN LATERAL } <>
~s{(SELECT * FROM schema2 AS s2 WHERE s2.id = s0.`x` AND s2.field = ?) AS f1 } <>
~s{WHERE ((s0.`id` > 0) AND (s0.`id` < ?))}
end

test "cross join" do
query = from(p in Schema, cross_join: c in Schema2, select: {p.id, c.id}) |> plan()
assert all(query) ==
Expand Down
12 changes: 12 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,18 @@ defmodule Ecto.Adapters.PostgresTest do
~s{WHERE ((s0."id" > 0) AND (s0."id" < $2))}
end

test "cross lateral join with fragment" do
query = Schema
|> join(:cross_lateral, [p], q in fragment("SELECT * FROM schema2 AS s2 WHERE s2.id = ? AND s2.field = ?", p.x, ^10))
|> select([p, q], {p.id, q.z})
|> where([p], p.id > 0 and p.id < ^100)
|> plan()
assert all(query) ==
~s{SELECT s0."id", f1."z" FROM "schema" AS s0 CROSS JOIN LATERAL } <>
~s{(SELECT * FROM schema2 AS s2 WHERE s2.id = s0."x" AND s2.field = $1) AS f1 } <>
~s{WHERE ((s0."id" > 0) AND (s0."id" < $2))}
end

test "cross join" do
query = from(p in Schema, cross_join: c in Schema2, select: {p.id, c.id}) |> plan()
assert all(query) ==
Expand Down