rowbinary: typeStringToReader keeps Variant members in text order, so Variant(Int64, Float64) reads 7 as 3.5e-323
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
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):7is read as the Float643.5e-323, and2.5as the Int644612811918334230528n.Variant(UInt64, String):7is read as a String of 7 NUL characters, andreadRows()returns 1 of the 2 rows.- The same occurs inside
ArrayandMap, 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
npm i @clickhouse/[email protected].- Run the program below with
CLICKHOUSE_URLset to the server. It makes a table with the columnsv Variant(Int64, Float64)andw Variant(UInt64, String), inserts two rows, and reads each column three times: asFORMAT RowBinarywith the type of theCREATE TABLEstatement, asFORMAT RowBinarywith the members in sorted order, and asRowBinaryWithNamesAndTypeswith 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/rowbinary0.2.0 with@clickhouse/datatype-parser0.1.3 (the samecompile.tsasmainat 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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ClickHouse/clickhouse-js
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/clickhouse-js#1292 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/clickhouse-js#1290 ·
Maintainers usually reply within 1 day
-
bug data-types documentation
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-js#1282 ·
Maintainers usually reply within 1 day
-
Core SQL test harness: the shim takes the next setting as the value of --allow_repeated_settingsPossibly taken @polyglotAI-bot claimed this 3 days ago. Opentests
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/clickhouse-js#1266 · 1 assignee ·
Maintainers usually reply within 1 day
-
Node.js 24/26: an insert from a raw-format stream of non-ASCII strings stops and times outPossibly taken @gedaliakliger claimed this 5 days ago. Openbug node queries
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-js#1246 ·
Maintainers usually reply within 1 day
All issues in ClickHouse/clickhouse-js
Similar issues
-
area: middleware enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
agent/quality testing
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Maintainers usually reply within 1 day
-
GitHub registry install via `gh` credentials rewrites `\u0000`–`\u001f` escapes in source files to `^@`–`^_`Possibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
juspay/agent-distro#203 ·
Maintainers usually reply within 1 day
-
area/docs framework/jekyll language/markdown
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
meshery/schemas#1456 · 1 comment ·
Maintainers usually reply within 1 day