Skip to content

Commit a214e91

Browse files
authored
implement support for count(_, :distinct) (#171)
1 parent 455bee6 commit a214e91

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

integration_test/test_helper.exs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ excludes = [
124124
:selected_as_with_order_by_expression,
125125
:selected_as_with_having,
126126

127-
# Distinct with options not supported
128-
:distinct_count,
129-
130127
# SQLite does not support anything except a single column in DISTINCT
131128
:multicolumn_distinct,
132129

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do
14371437

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

1440+
defp expr({:count, _, [expr, :distinct]}, sources, query) do
1441+
["count(distinct ", expr(expr, sources, query), ")"]
1442+
end
1443+
14401444
defp expr({:count, _, [{:&, _, [_]}]}, _sources, query) do
14411445
raise Ecto.QueryError,
14421446
query: query,

test/ecto/adapters/sqlite3/connection/aggregates_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ defmodule Ecto.Adapters.SQLite3.Connection.AggregatesTest do
2828
end
2929
end
3030

31-
test "raises when trying to distinct count" do
32-
assert_raise Ecto.QueryError, fn ->
31+
test "distinct counts" do
32+
query =
3333
Schema
3434
|> select([r], count(r.x, :distinct))
3535
|> plan()
36-
|> all()
37-
end
36+
37+
assert ~s{SELECT count(distinct s0."x") FROM "schema" AS s0} == all(query)
3838
end
3939

4040
test "allows naked count" do

0 commit comments

Comments
 (0)