Skip to content

Commit

Permalink
support subqueries in order_bys
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed May 16, 2024
1 parent 942f8ac commit e239dda
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
12 changes: 6 additions & 6 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if Code.ensure_loaded?(MyXQL) do
## Query

@parent_as __MODULE__
alias Ecto.Query.{BooleanExpr, JoinExpr, QueryExpr, WithExpr}
alias Ecto.Query.{BooleanExpr, ByExpr, JoinExpr, QueryExpr, WithExpr}

@impl true
def all(query, as_prefix \\ []) do
Expand Down Expand Up @@ -319,10 +319,10 @@ if Code.ensure_loaded?(MyXQL) do
end

defp distinct(nil, _sources, _query), do: []
defp distinct(%QueryExpr{expr: true}, _sources, _query), do: "DISTINCT "
defp distinct(%QueryExpr{expr: false}, _sources, _query), do: []
defp distinct(%ByExpr{expr: true}, _sources, _query), do: "DISTINCT "
defp distinct(%ByExpr{expr: false}, _sources, _query), do: []

defp distinct(%QueryExpr{expr: exprs}, _sources, query) when is_list(exprs) do
defp distinct(%ByExpr{expr: exprs}, _sources, query) when is_list(exprs) do
error!(query, "DISTINCT with multiple columns is not supported by MySQL")
end

Expand Down Expand Up @@ -511,7 +511,7 @@ if Code.ensure_loaded?(MyXQL) do
defp group_by(%{group_bys: group_bys} = query, sources) do
[
" GROUP BY "
| Enum.map_intersperse(group_bys, ", ", fn %QueryExpr{expr: expr} ->
| Enum.map_intersperse(group_bys, ", ", fn %ByExpr{expr: expr} ->
Enum.map_intersperse(expr, ", ", &expr(&1, sources, query))
end)
]
Expand Down Expand Up @@ -549,7 +549,7 @@ if Code.ensure_loaded?(MyXQL) do
defp order_by(%{order_bys: order_bys} = query, sources) do
[
" ORDER BY "
| Enum.map_intersperse(order_bys, ", ", fn %QueryExpr{expr: expr} ->
| Enum.map_intersperse(order_bys, ", ", fn %ByExpr{expr: expr} ->
Enum.map_intersperse(expr, ", ", &order_by_expr(&1, sources, query))
end)
]
Expand Down
12 changes: 6 additions & 6 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ if Code.ensure_loaded?(Postgrex) do
end

@parent_as __MODULE__
alias Ecto.Query.{BooleanExpr, JoinExpr, QueryExpr, WithExpr}
alias Ecto.Query.{BooleanExpr, ByExpr, JoinExpr, QueryExpr, WithExpr}

@impl true
def all(query, as_prefix \\ []) do
Expand Down Expand Up @@ -551,11 +551,11 @@ if Code.ensure_loaded?(Postgrex) do
end

defp distinct(nil, _, _), do: {[], []}
defp distinct(%QueryExpr{expr: []}, _, _), do: {[], []}
defp distinct(%QueryExpr{expr: true}, _, _), do: {" DISTINCT", []}
defp distinct(%QueryExpr{expr: false}, _, _), do: {[], []}
defp distinct(%ByExpr{expr: []}, _, _), do: {[], []}
defp distinct(%ByExpr{expr: true}, _, _), do: {" DISTINCT", []}
defp distinct(%ByExpr{expr: false}, _, _), do: {[], []}

defp distinct(%QueryExpr{expr: exprs}, sources, query) do
defp distinct(%ByExpr{expr: exprs}, sources, query) do
{[
" DISTINCT ON (",
Enum.map_intersperse(exprs, ", ", fn {_, expr} -> expr(expr, sources, query) end),
Expand Down Expand Up @@ -772,7 +772,7 @@ if Code.ensure_loaded?(Postgrex) do
[
" GROUP BY "
| Enum.map_intersperse(group_bys, ", ", fn
%QueryExpr{expr: expr} ->
%ByExpr{expr: expr} ->
Enum.map_intersperse(expr, ", ", &expr(&1, sources, query))
end)
]
Expand Down
12 changes: 6 additions & 6 deletions lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ if Code.ensure_loaded?(Tds) do

@parent_as __MODULE__
alias Ecto.Query
alias Ecto.Query.{BooleanExpr, JoinExpr, QueryExpr, WithExpr}
alias Ecto.Query.{BooleanExpr, ByExpr, JoinExpr, QueryExpr, WithExpr}

@impl true
def all(query, as_prefix \\ []) do
Expand Down Expand Up @@ -390,10 +390,10 @@ if Code.ensure_loaded?(Tds) do
end

defp distinct(nil, _sources, _query), do: []
defp distinct(%QueryExpr{expr: true}, _sources, _query), do: "DISTINCT "
defp distinct(%QueryExpr{expr: false}, _sources, _query), do: []
defp distinct(%ByExpr{expr: true}, _sources, _query), do: "DISTINCT "
defp distinct(%ByExpr{expr: false}, _sources, _query), do: []

defp distinct(%QueryExpr{expr: exprs}, _sources, query) when is_list(exprs) do
defp distinct(%ByExpr{expr: exprs}, _sources, query) when is_list(exprs) do
error!(
query,
"DISTINCT with multiple columns is not supported by MsSQL. " <>
Expand Down Expand Up @@ -584,7 +584,7 @@ if Code.ensure_loaded?(Tds) do
defp group_by(%{group_bys: group_bys} = query, sources) do
[
" GROUP BY "
| Enum.map_intersperse(group_bys, ", ", fn %QueryExpr{expr: expr} ->
| Enum.map_intersperse(group_bys, ", ", fn %ByExpr{expr: expr} ->
Enum.map_intersperse(expr, ", ", &expr(&1, sources, query))
end)
]
Expand All @@ -595,7 +595,7 @@ if Code.ensure_loaded?(Tds) do
defp order_by(%{order_bys: order_bys} = query, sources) do
[
" ORDER BY "
| Enum.map_intersperse(order_bys, ", ", fn %QueryExpr{expr: expr} ->
| Enum.map_intersperse(order_bys, ", ", fn %ByExpr{expr: expr} ->
Enum.map_intersperse(expr, ", ", &order_by_expr(&1, sources, query))
end)
]
Expand Down
11 changes: 11 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ defmodule Ecto.Adapters.PostgresTest do
assert all(query) ==
~s{SELECT s0."x" FROM "schema" AS s0 ORDER BY s0."x" ASC NULLS FIRST, s0."y" DESC NULLS FIRST}

order_by =
[asc: Ecto.Query.dynamic([], exists(from other_schema in "schema", where: other_schema.x == parent_as(:r).x, select: [other_schema.x]))]

query =
(from row in Schema, as: :r)
|> order_by(^order_by)
|> select([r], r.x)
|> plan()

assert all(query) == ~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")))}

query =
Schema
|> order_by([r], asc_nulls_last: r.x, desc_nulls_last: r.y)
Expand Down

0 comments on commit e239dda

Please sign in to comment.