Skip to content

Walk the schema for the minimum encoded record size without recursion - #41

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:iterative-min-encoded-bytes-walk
Oct 6, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:iterative-min-encoded-bytes-walk

Conversation

@groeneai

@groeneai groeneai commented Oct 5, 2026 •

Copy link
Copy Markdown

readHeader runs MinEncodedBytesWalker for every file. It recursed through each record field and union branch, so a schema that chains named types deeply overflowed the thread stack: A_k { x: A_(k-1) } for k up to 100000, each A_k defined inside an array field (shallow as text, and not descended by the walk), reached by a last field ["null", "A_n"]. Reported in ClickHouse/ClickHouse#123998.

The walk now keeps its own stack of frames on the heap. It visits leaves in the same order and keeps the memo and the cycle cut, so it returns the same bound for every schema.

Checked with a temporary build computing both walks in readHeader: no difference over 854 reads of 428 generated schemas (DAGs, self, mutual and required cycles, unions, fixed, every codec) and 944 reads in ClickHouse's Avro and Iceberg stateless tests. ClickHouse PR: ClickHouse/ClickHouse#124053.

`readHeader` computes a lower bound on a record's encoded size with
`MinEncodedBytesWalker`, which recursed through every record field and
union branch, following named references. A schema can chain named types
to any depth while staying shallow as text: define each `A_k { x: A_{k-1} }`
inside an array field, which the walk does not descend, and reach the whole
chain from a last field `["null", "A_n"]`. Neither the memo nor the cycle
cut bounds that depth, so 100000 such types overflowed the thread stack
while the header was parsed.

The walk now keeps its own stack of frames on the heap. It visits leaves in
the same order, memoises the same nodes and cuts the same cycles, so the
bound is the same for every schema.

Reported in ClickHouse/ClickHouse#123998.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PedroTadim

Copy link
Copy Markdown
Member

I have to accept this

@PedroTadim
PedroTadim merged commit d8ff6d9 into ClickHouse:master Oct 6, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 6, 2026
ClickHouse/avro#41 was merged as d8ff6d9c9fe4 on the avro master branch.
Its tree is identical to the previously pinned PR head fcc115b5c42e, so
the compiled code does not change; the pin now points at a commit on the
avro master branch instead of the fork branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants