Skip to content

implement support for count(_, :distinct) #171

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
merged 2 commits into from
Jun 20, 2025
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
3 changes: 0 additions & 3 deletions integration_test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ excludes = [
:selected_as_with_order_by_expression,
:selected_as_with_having,

# Distinct with options not supported
:distinct_count,

# SQLite does not support anything except a single column in DISTINCT
:multicolumn_distinct,

Expand Down
4 changes: 4 additions & 0 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do

defp expr({:count, _, []}, _sources, _query), do: "count(*)"

defp expr({:count, _, [expr, :distinct]}, sources, query) do
["count(distinct ", expr(expr, sources, query), ")"]
end

defp expr({:count, _, [{:&, _, [_]}]}, _sources, query) do
raise Ecto.QueryError,
query: query,
Expand Down
8 changes: 4 additions & 4 deletions test/ecto/adapters/sqlite3/connection/aggregates_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ defmodule Ecto.Adapters.SQLite3.Connection.AggregatesTest do
end
end

test "raises when trying to distinct count" do
assert_raise Ecto.QueryError, fn ->
test "distinct counts" do
query =
Schema
|> select([r], count(r.x, :distinct))
|> plan()
|> all()
end

assert ~s{SELECT count(distinct s0."x") FROM "schema" AS s0} == all(query)
end

test "allows naked count" do
Expand Down
Loading