rowbinary: typeStringToReader keeps Variant members in text order, so Variant(Int64, Float64) reads 7 as 3.5e-323

Open Beginner friendly
#1,293 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
nodejs, typescript
Domain
data

Research direction

Start with skills/clickhouse-js-node-rowbinary/src/readers/compile.ts around variantReader, then inspect skills/clickhouse-js-node-rowbinary/src/readers/composite.ts around readVariant to understand how the discriminant indexes member readers. Run the issue's reproduction program against a ClickHouse server using both Variant(Int64, Float64) and Variant(UInt64, String). Done means these readers return the same values as the sorted-order and RowBinaryWithNamesAndTypes paths, including correct nested Array and Map behavior and no dropped rows.

Written by the indexing model from the issue text.

Description

bug data-types
Describe the bug

typeStringToReader() of @clickhouse/rowbinary reads a Variant value as a different member of the Variant when the members in the type name are not in sorted order. Members of the same width give wrong values with no error. Members of other widths make the cursor lose its synchronization, and readRows() returns fewer rows with no error.

  • Variant(Int64, Float64): 7 is read as the Float64 3.5e-323, and 2.5 as the Int64 4612811918334230528n.
  • Variant(UInt64, String): 7 is read as a String of 7 NUL characters, and readRows() returns 1 of the 2 rows.
  • The same occurs inside Array and Map, and with composite members: Variant(Array(Int64), Array(Float64)) reads [7] as [3.5e-323].

astToReader() and createTypeReaderCache() have the same defect. compileRowBinaryWithNamesAndTypes() gives the correct values, because the server sends the header type with its members in sorted order. So the defect occurs for type names that the application writes, for example to read FORMAT RowBinary, which has no header. The README gives such type names as an input of the readers ("as returned by RowBinaryWithNamesAndTypes, or supplied by the user"), and a type that the application copies from its CREATE TABLE statement can have the members in any order.

The same defect was reported for clickhouse-cs in https://github.com/ClickHouse/clickhouse-cs/issues/819.

Steps to reproduce
  1. npm i @clickhouse/[email protected].
  2. Run the program below with CLICKHOUSE_URL set to the server. It makes a table with the columns v Variant(Int64, Float64) and w Variant(UInt64, String), inserts two rows, and reads each column three times: as FORMAT RowBinary with the type of the CREATE TABLE statement, as FORMAT RowBinary with the members in sorted order, and as RowBinaryWithNamesAndTypes with the header type.
Expected behaviour

Each reader returns [7n, 2.5] for v and [7n, 'hello'] for w, as the header path does. The RowBinary docs (https://clickhouse.com/docs/reference/formats/RowBinary/RowBinary#variant): "While for the end user Variant(T1, T2) means exactly the same as Variant(T2, T1), the order of types in the definition matters for the wire format: the types in the definition are always sorted alphabetically, and this is important, since the exact variant is encoded by a “discriminant” - the data type index in the definition." The server gives the sorted order for the table:

SELECT name, type FROM system.columns WHERE table = 't'
id  UInt8
v   Variant(Float64, Int64)
w   Variant(String, UInt64)
Code example
Reproduction program (JavaScript, 16 lines)
// Reads the same RowBinary rows of two Variant columns with three readers.
import { Cursor, readRows, typeStringToReader, compileRowBinaryWithNamesAndTypes } from "@clickhouse/rowbinary";

const url = process.env.CLICKHOUSE_URL ?? "http://localhost:8123";
const post = async (sql) => Buffer.from(await (await fetch(url, { method: "POST", body: sql })).arrayBuffer());
await post("CREATE OR REPLACE TABLE t (id UInt8, v Variant(Int64, Float64), w Variant(UInt64, String)) ENGINE = Memory");
await post("INSERT INTO t VALUES (1, 7::Int64, 7::UInt64), (2, 2.5::Float64, 'hello')");
const cases = [["v", "Variant(Int64, Float64)", "Variant(Float64, Int64)"], ["w", "Variant(UInt64, String)", "Variant(String, UInt64)"]];
for (const [col, ...types] of cases) {
  const body = await post(`SELECT ${col} FROM t ORDER BY id FORMAT RowBinary`);
  for (const type of types) console.log(`${col}: typeStringToReader("${type}")`.padEnd(46), readRows(typeStringToReader(type))(new Cursor(body)));
  const c = new Cursor(await post(`SELECT ${col} FROM t ORDER BY id FORMAT RowBinaryWithNamesAndTypes`));
  const h = compileRowBinaryWithNamesAndTypes(c);
  console.log(`${col}: header type "${h.types[0]}"`.padEnd(46), h.readRows(c).map((r) => r[col]));
}
Error log

No error. Output:

v: typeStringToReader("Variant(Int64, Float64)") [ 3.5e-323, 4612811918334230528n ]
v: typeStringToReader("Variant(Float64, Int64)") [ 7n, 2.5 ]
v: header type "Variant(Float64, Int64)"       [ 7n, 2.5 ]
w: typeStringToReader("Variant(UInt64, String)") [ '\x00\x00\x00\x00\x00\x00\x00' ]
w: typeStringToReader("Variant(String, UInt64)") [ 7n, 'hello' ]
w: header type "Variant(String, UInt64)"       [ 7n, 'hello' ]
Root cause

variantReader builds the member readers in the order of the type name (skills/clickhouse-js-node-rowbinary/src/readers/compile.ts:232), and readVariant uses the discriminant as an index into that list (src/readers/composite.ts:171), whose doc comment requires the sorted order. The comment at compile.ts:228-229 says that a hand-written Variant(...) string "would need sorting first", but typeStringToReader() does not sort, and neither its doc comment nor the README tells the caller to sort.

The fix is in variantReader: put the members in the order of the server. A caller can avoid the defect if it writes the members in the order that system.columns or DESCRIBE TABLE gives. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Configuration
Environment
  • Client version: @clickhouse/rowbinary 0.2.0 with @clickhouse/datatype-parser 0.1.3 (the same compile.ts as main at 0873ef2)
  • Language version: Node.js 22.23.5
  • OS: Linux
ClickHouse server
  • ClickHouse Server version: 26.11.7.10

Classification: impact: Data integrity · area: Data types & formats (Variant, RowBinary)


Found by automated analysis (Polyglot AI) while checking clickhouse-cs#819 against the other client repos. Verified against a live server.

Dominant language
TypeScript
Stars
332
Forks
94
Avg merge
19h 15m
Merged PRs (30d)
43

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/clickhouse-js

All issues in ClickHouse/clickhouse-js

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.