Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix RowBinary encoding of empty `Dynamic` arrays, which omitted the array-length byte and made ClickHouse reject the value (`Serialization is not implemented for type Nothing`). https://github.com/plausible/ch/pull/474

## 0.9.3 (2026-10-15)

- Preserve fractional seconds when Ecto casts `Time64` values; RowBinary encoding applies the declared precision. https://github.com/plausible/ch/pull/469
Expand Down
5 changes: 4 additions & 1 deletion lib/ch/row_binary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,10 @@ defmodule Ch.RowBinary do
_ when is_float(value) -> [0x0E | encode(:f64, value)]
%Date{} -> [0x0F | encode(:date, value)]
%NaiveDateTime{} -> [0x11 | encode(:datetime, value)]
[] -> [0x1E, 0x00]
# A Dynamic value is <type descriptor><value>. `0x1E 0x00` is the
# Array(Nothing) descriptor; the trailing `0` is the varint array length
# for an empty array, which the wire format still requires.
[] -> [0x1E, 0x00, 0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[] -> [0x1E, 0x00, 0]
[] -> [0x1E, 0, 0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and maybe we could add a comment what these two zeroes mean

end
end

Expand Down
2 changes: 1 addition & 1 deletion test/ch/dynamic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Ch.DynamicTest do

# Nothing 0x00
assert select.("[]::Array(Nothing)") == [[], "Array(Nothing)"]
# assert insert.([]) == []
assert insert.([]) == []

# UInt8 0x01
assert select.("0::UInt8") == [0, "UInt8"]
Expand Down
21 changes: 20 additions & 1 deletion test/ch/row_binary_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,23 @@ defmodule Ch.RowBinaryTest do
assert decode_rows(<<1, 2>>, [:u8, :u8]) == [[1, 2]]
end

test "Dynamic empty array includes the array length byte" do
encoded = encode_rows([[[]]], ["Dynamic"]) |> IO.iodata_to_binary()
assert encoded == <<0x1E, 0x00, 0x00>>
assert decode_rows(encoded, ["Dynamic"]) == [[[]]]
end

test "Dynamic empty array before another column does not consume its bytes" do
encoded = encode_rows([[[], "x"]], ["Dynamic", "String"]) |> IO.iodata_to_binary()
assert decode_rows(encoded, ["Dynamic", "String"]) == [[[], "x"]]
end

test "Dynamic truncated two-byte empty array payload reports incomplete data" do
assert_raise ArgumentError, ~r/incomplete RowBinary data/, fn ->
decode_rows(<<0x1E, 0x00>>, ["Dynamic"])
end
end

test "rejects invalid Variant type indexes" do
assert_raise ArgumentError, "invalid Variant type index: 2", fn ->
decode_rows(<<2>>, ["Variant(String, UInt32)"])
Expand Down Expand Up @@ -994,7 +1011,9 @@ defmodule Ch.RowBinaryTest do

rows = [
["hello", 1],
[3.14, Date.utc_today()]
[3.14, Date.utc_today()],
[[], "hello"],
[[], []]
]

assert rows |> encode_rows(types) |> byte_by_byte(types) == rows
Expand Down