Skip to content

Include array length byte when encoding empty Dynamic array - #474

Open
travisim wants to merge 1 commit into
plausible:masterfrom
travisim:fix/dynamic-empty-array-length
Open

travisim wants to merge 1 commit into
plausible:masterfrom
travisim:fix/dynamic-empty-array-length

Conversation

@travisim

Copy link
Copy Markdown

Fixes #454

Root cause

Ch.RowBinary.encode(:dynamic, []) at lib/ch/row_binary.ex:478 emitted only the Array(Nothing) type descriptor (0x1E, 0x00) and omitted the varint array-length byte. The Dynamic wire format is <type descriptor><value>, so an empty array must be <<0x1E, 0x00, 0x00>>. Without the trailing zero the driver cannot decode its own output, and ClickHouse rejects the insert with ATTEMPT_TO_READ_AFTER_EOF (or, as observed on 26.11, Serialization is not implemented for type Nothing).

       %NaiveDateTime{} -> [0x11 | encode(:datetime, value)]
-      [] -> [0x1E, 0x00]
+      [] -> [0x1E, 0x00, 0]
     end

Tests

  • test/ch/row_binary_test.exs:511 asserts the exact three-byte wire format, round-trips, and that a truncated two-byte payload still reports incomplete data.
  • test/ch/row_binary_test.exs:517 covers an empty Dynamic array adjacent to a following column, so the missing length cannot swallow the next field's bytes.
  • The existing dynamic byte-by-byte test now includes empty arrays (and two of them in one row), which exercises every split through decode_rows_continue/3.
  • test/ch/dynamic_test.exs:37 restores the previously commented-out live insert assertion for [].

Evidence (local, ClickHouse 26.11.4.11, Elixir 1.20.5 / OTP 29)

Before the fix, with the new tests applied to master:

Result: 86/91 passed (9/9 doctests, 2/2 properties, 75/80 tests)
Failed: 5 tests
  - Dynamic empty array includes the array length byte
    left:  <<30, 0>>
    right: <<30, 0, 0>>
  - Dynamic empty array before another column does not consume its bytes
  - Dynamic truncated two-byte empty array payload reports incomplete data
  - test it works (x2 parameterizations): Ch.Error Code 48 ...

After the fix:

$ MIX_ENV=test mix test test/ch/row_binary_test.exs test/ch/dynamic_test.exs
Result: 91 passed (9 doctests, 2 properties, 80 tests)

Full suite, compared against a clean master checkout:

passed failed
clean master 677/683 6
this branch 680/686 6

The 6 failures are identical and pre-existing in this environment (4 × Ch.HTTPTest user-agent assertions, 2 × Ch.ConnectionTest "select with params"; the local ClickHouse reports timezone Asia/Singapore). mix format --check-formatted and mix compile --warnings-as-errors pass.

Dynamic values encode a type descriptor followed by the value. An empty
list was encoded as Array(Nothing) without the zero array-length byte, so
the driver could not decode its own output and ClickHouse rejected the
insert with ATTEMPT_TO_READ_AFTER_EOF.

Emit the varint zero length and cover the wire format, adjacent columns,
truncated payloads, byte-by-byte incremental decoding, and a live insert.

Fixes plausible#454
Copilot AI balanced review requested due to automatic review settings November 27, 2026 22:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Comment thread lib/ch/row_binary.ex
%Date{} -> [0x0F | encode(:date, value)]
%NaiveDateTime{} -> [0x11 | encode(:datetime, value)]
[] -> [0x1E, 0x00]
[] -> [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

@ruslandoga

Copy link
Copy Markdown
Collaborator

👋 @travisim

Thank you for the fix!

@ruslandoga

ruslandoga commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Could you also please update the changelog and add this PR to the Unreleased section 3c0c4c3?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P2] Dynamic empty arrays omit the RowBinary array-length byte

4 participants