Skip to content

Commit 48e7db4

Browse files
authored
fix Array(Array(Nothing)) (#263)
* add failing test * add another failing test * tests pass * changelog
1 parent 9a517fe commit 48e7db4

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Fix invalid `Array(Array(Nothing))` param type for params like `[[],[],[1]]` https://github.com/plausible/ecto_ch/pull/263
6+
37
## 0.8.4 (2026-01-13)
48

59
- Allow `Ch` [v0.7.x](https://github.com/plausible/ch/blob/master/CHANGELOG.md#070-2026-01-13) which handles disconnects better

lib/ecto/adapters/clickhouse/connection.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,10 +1229,18 @@ defmodule Ecto.Adapters.ClickHouse.Connection do
12291229
["Decimal64(", Integer.to_string(scale), ?)]
12301230
end
12311231

1232+
# context: https://github.com/plausible/analytics/pull/6049#issuecomment-3850062609
12321233
defp param_type([]), do: "Array(Nothing)"
12331234

1234-
# TODO check whole list
1235-
defp param_type([v | _]), do: ["Array(", param_type(v), ?)]
1235+
defp param_type([v | vs]) do
1236+
param_type = param_type(v)
1237+
1238+
if param_type == "Array(Nothing)" do
1239+
param_type(vs)
1240+
else
1241+
["Array(", param_type, ?)]
1242+
end
1243+
end
12361244

12371245
defp param_type(%s{}) do
12381246
raise ArgumentError, "struct #{inspect(s)} is not supported in params"

test/ecto/adapters/clickhouse/connection_test.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,6 +3233,39 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do
32333233
assert all(query) == ~s{SELECT [1,2,3] FROM "schema" AS s0}
32343234
end
32353235

3236+
# https://github.com/plausible/ecto_ch/issues/262
3237+
test "empty array params" do
3238+
assert all(
3239+
from p in Post,
3240+
where: fragment("array(1,2,3)") in ^[[]],
3241+
select: p.id
3242+
) ==
3243+
"""
3244+
SELECT p0."id" FROM "posts" AS p0 \
3245+
WHERE (array(1,2,3) IN ({$0:Array(Nothing)}))\
3246+
"""
3247+
3248+
assert all(
3249+
from p in Post,
3250+
where: fragment("array(1,2,3)") in ^[[], [], [1, 2, 3]],
3251+
select: p.id
3252+
) ==
3253+
"""
3254+
SELECT p0."id" FROM "posts" AS p0 \
3255+
WHERE (array(1,2,3) IN ({$0:Array(Nothing)},{$1:Array(Nothing)},{$2:Array(Int64)}))\
3256+
"""
3257+
3258+
assert all(
3259+
from p in Post,
3260+
where: fragment("? in ?", [1, 2, 3], ^[[], [], [1, 2, 3]]),
3261+
select: p.id
3262+
) ==
3263+
"""
3264+
SELECT p0."id" FROM "posts" AS p0 \
3265+
WHERE ([1,2,3] in {$0:Array(Array(Int64))})\
3266+
"""
3267+
end
3268+
32363269
test "maps" do
32373270
assert all(select(Schema, [], fragment("?[site_id]", ^%{}))) ==
32383271
~s|SELECT {$0:Map(Nothing,Nothing)}[site_id] FROM "schema" AS s0|

test/ecto/integration/type_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,23 @@ defmodule Ecto.Integration.TypeTest do
377377
assert tag.ints == [1, 0, 3]
378378
end
379379

380+
test "array of arrays" do
381+
assert TestRepo.all(
382+
from n in fragment("numbers(1)"),
383+
select: fragment("array(?)", n.number) in ^[[]]
384+
) == [0]
385+
386+
assert TestRepo.all(
387+
from n in fragment("numbers(1)"),
388+
select: fragment("array(?)", n.number) in ^[[], [0]]
389+
) == [1]
390+
391+
assert TestRepo.all(
392+
from n in fragment("numbers(1)"),
393+
select: fragment("array(?) in ?", n.number, ^[[], [0]])
394+
) == [1]
395+
end
396+
380397
test "empty untyped map" do
381398
timezones = %{}
382399

0 commit comments

Comments
 (0)