Skip to content

Commit

Permalink
don't wrap "top level" subqueries in parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed May 25, 2024
1 parent 494496e commit 0ba8221
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 18 additions & 3 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ if Code.ensure_loaded?(MyXQL) do
[
" GROUP BY "
| Enum.map_intersperse(group_bys, ", ", fn %ByExpr{expr: expr} ->
Enum.map_intersperse(expr, ", ", &expr(&1, sources, query))
Enum.map_intersperse(expr, ", ", &expr({:top_level, &1}, sources, query))
end)
]
end
Expand Down Expand Up @@ -556,7 +556,7 @@ if Code.ensure_loaded?(MyXQL) do
end

defp order_by_expr({dir, expr}, sources, query) do
str = expr(expr, sources, query)
str = expr({:top_level, expr}, sources, query)

case dir do
:asc -> str
Expand Down Expand Up @@ -687,6 +687,17 @@ if Code.ensure_loaded?(MyXQL) do
error!(query, "MySQL adapter does not support aggregate filters")
end

defp expr({:top_level, %Ecto.SubQuery{query: query}}, sources, parent_query) do
combinations =
Enum.map(query.combinations, fn {type, combination_query} ->
{type, put_in(combination_query.aliases[@parent_as], {parent_query, sources})}
end)

query = put_in(query.combinations, combinations)
query = put_in(query.aliases[@parent_as], {parent_query, sources})
[all(query, subquery_as_prefix(sources))]
end

defp expr(%Ecto.SubQuery{query: query}, sources, parent_query) do
combinations =
Enum.map(query.combinations, fn {type, combination_query} ->
Expand Down Expand Up @@ -790,10 +801,14 @@ if Code.ensure_loaded?(MyXQL) do
[op_to_binary(left, sources, query), op | op_to_binary(right, sources, query)]

{:fun, fun} ->
[fun, ?(, modifier, Enum.map_intersperse(args, ", ", &expr(&1, sources, query)), ?)]
[fun, ?(, modifier, Enum.map_intersperse(args, ", ", &expr({:top_level, &1}, sources, query)), ?)]
end
end

defp expr({:top_level, other}, sources, parent_query) do
expr(other, sources, parent_query)
end

defp expr(list, _sources, query) when is_list(list) do
error!(query, "Array type is not supported by MySQL")
end
Expand Down
4 changes: 2 additions & 2 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan()

assert all(query) ==
~s{SELECT s0.`x` FROM `schema` AS s0 ORDER BY (SELECT exists(SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`)))}
~s{SELECT s0.`x` FROM `schema` AS s0 ORDER BY exists(SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`))}

end

Expand Down Expand Up @@ -882,7 +882,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan()

assert all(query) ==
~s{SELECT s0.`x` FROM `schema` AS s0 GROUP BY (SELECT exists(SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`)))}
~s{SELECT s0.`x` FROM `schema` AS s0 GROUP BY exists(SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`))}
end

test "interpolated values" do
Expand Down

0 comments on commit 0ba8221

Please sign in to comment.