diff --git a/CHANGELOG.md b/CHANGELOG.md index d9f852f8..53452801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ch/row_binary.ex b/lib/ch/row_binary.ex index 48ac7e02..fad3af88 100644 --- a/lib/ch/row_binary.ex +++ b/lib/ch/row_binary.ex @@ -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 . `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] end end diff --git a/test/ch/dynamic_test.exs b/test/ch/dynamic_test.exs index e925fcd8..7dd08d5e 100644 --- a/test/ch/dynamic_test.exs +++ b/test/ch/dynamic_test.exs @@ -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"] diff --git a/test/ch/row_binary_test.exs b/test/ch/row_binary_test.exs index d67ebd4c..cad4d5a3 100644 --- a/test/ch/row_binary_test.exs +++ b/test/ch/row_binary_test.exs @@ -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)"]) @@ -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