clickhouse-js-node-rowbinary

作者: clickhouse

生成讀取/解碼以及寫入/編碼 ClickHouse RowBinary 串流的 TypeScript/JavaScript 程式碼,適用於 ClickHouse HTTP 伺服器。每當需要時使用此技能…

npx skills add https://github.com/clickhouse/agent-skills --skill clickhouse-js-node-rowbinary

ClickHouse JS RowBinary Codec Generator for Node.js

This skill generates both directions of the wire format: readers (decode bytes → values) and writers (encode values → bytes, the mirror). A given task normally needs only one side. This file is the shared entry point — the format gate plus the principles common to both directions; the per-direction decisions, guidance, and the per-type reference tables live in two sibling files.

Pick your side — read only the one you need:

  • Decoding a RowBinary* response from ClickHouse into JS values → reader.md. Streaming vs whole-buffer, row-objects vs columnar, fixed vs runtime schema, and the per-type reader reference.
  • Encoding JS values into a RowBinary payload to send to ClickHouse → writer.md. The Sink/writeX building blocks, writeRows streaming, and the per-type writer reference.

The per-type code is real, split by direction under src/readers/ and src/writers/.

First: is RowBinary even the right format?

RowBinary exists for throughput, but it is not automatically the fastest path — match the format to the shape of the data before committing to a bespoke parser.

Prefer a JSON* format (e.g. JSONEachRow) when the result is mostly strings / JSON-like values that you consume wholesale — randomly accessing essentially every field, running string/regexp methods on them, treating values as text. V8's native JSON.parse is heavily optimized C++ and builds JS strings and objects faster than a JS-level RowBinary decoder can; pair it with HTTP response compression (gzip / zstd, which crushes JSON's repetitive keys) and the wire cost shrinks too.

RowBinary clearly wins when the result is dominated by:

  • Wide numericsInt128/Int256/UInt128/UInt256, Decimal128/Decimal256.
  • Binary / fixed-width blobsIPv4, IPv6, UUID, FixedString.
  • High-volume fixed-width numeric columns generally, where each value is a single DataView read.

Prefer the Native format when columnar load and client-side analytics are the main goal (fold/scan/filter columns, feed typed arrays to a Worker or WASM). Native is column-major, so it loads straight into one typed array per column with no transpose.

For help choosing and consuming a JSON* format (or CSV / TSV) instead, use the clickhouse-js-node-coding skill.

Core guidance (both directions)

These principles apply whether you are generating a reader or a writer; the side-specific operational guidance is in reader.md / writer.md.

  • Little-endian only. RowBinary is little-endian; target x86/ARM. Read and write every multi-byte number with DataView accessors passing a literal true for the littleEndian flag.

  • Correct first, then optimize. First emit a correct codec built from the plain per-type API. Only after it's correct (and tested) specialize it. Don't bake performance assumptions in before correctness.

  • Monomorphize generic/composite types. Emit specialized, inlined code per type combination instead of passing functions as arguments where the type is known ahead of time.

  • Inline the leaf ops. The per-type readX/writeX functions are the correct, composable reference; the generated codec should INLINE their bodies, not call them, so the row loop is straight-line with no per-field indirection (and so the fixed-width coalescing can fold the offset arithmetic together).

  • Annotate the type per column. Inlining erases the type structure, so put a short comment above each column's encode/decode block naming the ClickHouse type it handles.

  • Shared scratch is not reentrant. Some hot methods reuse a module-level scratch buffer as a write-then-read pair — correct only because the access is fully synchronous. An async/yield boundary between populating and reading it corrupts the value.

  • TypeScript by default. Generate TypeScript code and helpers unless the user explicitly asks for plain JavaScript.

Worked examples

Six end-to-end examples with real speedup are catalogued in EXAMPLES.md.

Out of scope

  • JSON / CSV / TSV / Parquet parsing → use clickhouse-js-node-coding.
  • Connection errors, hangs, type mismatches → use clickhouse-js-node-troubleshooting.
  • Browser / Web Worker / Edge@clickhouse/client-web.

Still Stuck?

來自 clickhouse 的更多技能

clickhouse-best-practices
clickhouse
我们要求翻译一段文本,目标语言是繁体中文。文本内容是关于ClickHouse最佳实践的规则,包括模式设计、查询优化和数据摄取策略。需要保留名称"clickhouse-best-practices"(但名称不在<text>内,所以不翻译)。注意不要添加额外内容,只翻译<text>内的文字。 翻译时注意专业术语:schema design -> 模式設計,query optimization -> 查詢優化,data ingestion strategy -> 數據攝取策略,primary key -> 主鍵,data type selection -> 數據類型選擇,immutable design decisions -> 不可變設計決策,JOIN -> JOIN(保留),insert batching -> 插入批次處理,mutation avoidance -> 避免突變,columnar storage -> 列式存儲,sparse index mechanics -> 稀疏索引機制,structured review procedures -> 結構化審查程序。 注意繁体中文用词:规则、组织、涵盖、关键、标记、提供等。 文本末尾有"for...",但原文是"for..."后面没有完整
official
clickhouse-js-node-coding
clickhouse
Write idiomatic application code with the ClickHouse Node.js client (`@clickhouse/client`). Use this skill whenever a user is *building* against the Node.js…
official
clickhousectl-cloud-deploy
clickhouse
當用戶想要將ClickHouse部署到雲端、上線生產環境、使用ClickHouse Cloud、託管受管理的ClickHouse服務,或從本地遷移時使用。
official
clickstack-otel-collector
clickhouse
當使用者想要將 OpenTelemetry collector 接入 ClickHouse Cloud 上的 Managed ClickStack 服務時使用,無論是透過部署新的本地 collector…
official
infra-clickhouse
clickhouse
使用clickhousectl CLI設定及管理ClickHouse——安裝並執行本機ClickHouse伺服器以利開發,同時建立受管理的ClickHouse Cloud…
official
infra-postgres
clickhouse
使用 clickhousectl CLI 設定並管理 Postgres — 執行本機 Docker 支援的 Postgres 以進行開發,並建立及操作受管理的 ClickHouse…
official
clickhouse-best-practices
clickhouse
審查 ClickHouse 結構、查詢或配置時必須使用。包含 31 條規則,在提供建議前必須檢查。務必閱讀…
official
setup
clickhouse
引導用戶設定與此插件捆綁的 ClickHouse MCP 伺服器連線。當用戶首次安裝插件或遇到問題時使用…
official