npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@clickhouse/datatype-parser

v0.1.3

Published

Standalone ClickHouse data-type string parser — a TypeScript port of the chdt C++ library.

Downloads

1,481

Readme

@clickhouse/datatype-parser

A small, self-contained TypeScript library that parses a ClickHouse data-type string (the kind sent in the types row of RowBinaryWithNamesAndTypes, e.g. Array(Nullable(UInt64)), Tuple(a UInt8, b String), Enum8('a' = 1), Decimal(10, 2)) into a JSON AST.

It is a faithful port of the C++ chdt library (see ./reference-cpp-extracted-parser), which is itself extracted from the server's ParserDataType (src/Parsers/ParserDataType.cpp). It has no runtime dependencies — only the Node.js standard library. The JSON it emits mirrors the data-type subtree of the frozen EXPLAIN AST json = 1 document (format version 2), so its output is a drop-in match for what the server produces — and is byte-identical to the C++ parser's output across the full test corpus.

Install & build

npm install @clickhouse/datatype-parser

Usage

Library:

import { parseDataType, toJSON } from "@clickhouse/datatype-parser";

const r = parseDataType("Tuple(a UInt8, b String)");
if (r.ok()) {
  console.log(toJSON(r.ast!)); // pretty (2-space) JSON
  console.log(toJSON(r.ast!, -1)); // compact JSON
} else {
  console.error(r.error!.message, r.error!.position);
}

AST

Tuple(
  id UInt64,
  name LowCardinality(String),
  price Decimal(18, 4),
  ts DateTime64(9, 'UTC'),
  tags Array(LowCardinality(Nullable(String))),
  attrs Map(String, Array(Nullable(Int32))),
  status Enum8('active' = 1, 'closed' = -2),
  coords Array(Tuple(Float64, Float64)),
  meta Nested(k String, v UInt32),
  fixed FixedString(16),
  dyn Dynamic(max_types = 8),
  variant Variant(UInt64, String, Array(UInt8)),
  raw Object('json')
)
{
  "type": "TupleDataType",
  "name": "Tuple",
  "arguments": [
    {
      "type": "DataType",
      "name": "UInt64"
    },
    {
      "type": "DataType",
      "name": "LowCardinality",
      "arguments": [
        {
          "type": "DataType",
          "name": "String"
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Decimal",
      "arguments": [
        {
          "type": "Literal",
          "value_type": "UInt64",
          "value": "18"
        },
        {
          "type": "Literal",
          "value_type": "UInt64",
          "value": "4"
        }
      ]
    },
    {
      "type": "DataType",
      "name": "DateTime64",
      "arguments": [
        {
          "type": "Literal",
          "value_type": "UInt64",
          "value": "9"
        },
        {
          "type": "Literal",
          "value_type": "String",
          "value": "UTC"
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Array",
      "arguments": [
        {
          "type": "DataType",
          "name": "LowCardinality",
          "arguments": [
            {
              "type": "DataType",
              "name": "Nullable",
              "arguments": [
                {
                  "type": "DataType",
                  "name": "String"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Map",
      "arguments": [
        {
          "type": "DataType",
          "name": "String"
        },
        {
          "type": "DataType",
          "name": "Array",
          "arguments": [
            {
              "type": "DataType",
              "name": "Nullable",
              "arguments": [
                {
                  "type": "DataType",
                  "name": "Int32"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "type": "EnumDataType",
      "name": "Enum8",
      "values": [
        {
          "name": "active",
          "value": 1
        },
        {
          "name": "closed",
          "value": -2
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Array",
      "arguments": [
        {
          "type": "TupleDataType",
          "name": "Tuple",
          "arguments": [
            {
              "type": "DataType",
              "name": "Float64"
            },
            {
              "type": "DataType",
              "name": "Float64"
            }
          ]
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Nested",
      "arguments": [
        {
          "type": "NameTypePair",
          "name": "k",
          "data_type": {
            "type": "DataType",
            "name": "String"
          }
        },
        {
          "type": "NameTypePair",
          "name": "v",
          "data_type": {
            "type": "DataType",
            "name": "UInt32"
          }
        }
      ]
    },
    {
      "type": "DataType",
      "name": "FixedString",
      "arguments": [
        {
          "type": "Literal",
          "value_type": "UInt64",
          "value": "16"
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Dynamic",
      "arguments": [
        {
          "type": "Function",
          "name": "equals",
          "is_operator": true,
          "arguments": [
            {
              "type": "Identifier",
              "name": "max_types"
            },
            {
              "type": "Literal",
              "value_type": "UInt64",
              "value": "8"
            }
          ]
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Variant",
      "arguments": [
        {
          "type": "DataType",
          "name": "UInt64"
        },
        {
          "type": "DataType",
          "name": "String"
        },
        {
          "type": "DataType",
          "name": "Array",
          "arguments": [
            {
              "type": "DataType",
              "name": "UInt8"
            }
          ]
        }
      ]
    },
    {
      "type": "DataType",
      "name": "Object",
      "arguments": [
        {
          "type": "Literal",
          "value_type": "String",
          "value": "json"
        }
      ]
    }
  ],
  "element_names": [
    "id",
    "name",
    "price",
    "ts",
    "tags",
    "attrs",
    "status",
    "coords",
    "meta",
    "fixed",
    "dyn",
    "variant",
    "raw"
  ]
}

Layout

The module structure tracks the C++ sources one-to-one:

| TypeScript | ported from (C++) | role | | --------------- | ----------------------------- | --------------------------------------- | | src/ast.ts | include/chdt/ast.h | the AST node shape + makeNode factory | | src/lexer.ts | src/lexer.{h,cpp} | the purpose-built tokenizer | | src/parser.ts | src/parser.cpp + parser.h | the ParserDataType::parseImpl port | | src/json.ts | src/json.cpp | the byte-faithful JSON serializer | | src/index.ts | — | public barrel |

The lexer and parser deliberately preserve the original control flow, branch ordering, helper names, and pos save/restore points. A few signatures changed where C++ used out-parameters (std::string &): parseIdentifier and decodeQuoted return small result objects instead.

Development

npm install
npm run build      # emits dist/ (JS + .d.ts)
npm run typecheck  # tsc --noEmit

Output shape

Node types and slots match the server (format v2):

| type | slots | | --------------- | -------------------------------------------------------- | | DataType | name, arguments? (present iff the type had (...)) | | EnumDataType | name, values (array of { name, value }) | | TupleDataType | name, arguments?, element_names? (named tuples) | | NameTypePair | name, data_type (a Nested(...) element) | | Literal | value_type, value (64-bit ints as JSON strings) | | Function | name, is_operator?, arguments (e.g. max_types=5) | | Identifier | name, name_parts? |

Coverage

Supported: scalars, parametric types with literal args (Decimal, FixedString, DateTime64, …), nested type args (Array, Map, Nullable, LowCardinality, Variant, …), enums (explicit → EnumDataType; auto-assigned → generic DataType), named/unnamed/mixed tuples, Nested, Dynamic(max_types = N), the legacy Object('json'), and the SQL-standard multi-word aliases (DOUBLE PRECISION, CHAR VARYING, INT SIGNED, …).

Deliberately not supported yet (the parser returns a clear error):

  • AggregateFunction / SimpleAggregateFunction — needs the function-expression parser the server reaches for here.
  • the new JSON(...) object-argument syntax (JSON(a.b UInt32, SKIP x)). Bare JSON and legacy Object('json') parse fine.

Tests

npm test                  # node:test: unit suite + snapshot corpus — NO clickhouse needed
npm run test:unsupported  # asserts the deferred types are rejected

npm test requires no clickhouse binary — it runs entirely against checked-in fixtures:

  • unit (test/parser.test.ts) — pins representative AST shapes and all the deliberate rejections.
  • snapshot (test/snapshot.test.ts) — for every type in test/cases.txt (356 and counting), compares the parser's JSON against a checked-in static snapshot of the real server's data_type subtree, in test/snapshots/ (one <sha1>.json per query). Because a snapshot is only written when the server accepted the type and the parser matched it, "parser == snapshot" means "parser == server".

Regenerating / extending the snapshot corpus

The snapshots are captured from a real server by update_snapshots.ts, which needs a clickhouse binary built from https://github.com/peter-leonov-ch/ClickHouse/pull/1 (the AST-format changes this parser mirrors live in that PR; a stock build will not match):

npm run snapshot:update -- --clickhouse /path/to/clickhouse

It validates every type in test/cases.txt plus any in test/candidates.txt (a seed list of additional types), keeps only those the server accepts and the parser matches, appends new keepers to cases.txt, writes a snapshot per kept query, and prunes orphans. Types the server rejects or where the parser diverges are dropped and listed in test/snapshots_report.txt (never silently added).

There is also a live comparison that skips the snapshots and queries the server directly, useful while iterating:

npm run test:oracle -- --clickhouse /path/to/clickhouse

Confirming the corpus is real (no invented types)

The oracle compares against the server's parser (ParserDataType), which is what this library mirrors. To additionally confirm that every type in the corpus is a real ClickHouse type — not just syntactically well-formed — there is a check that instantiates each type against any stock running server (no AST-JSON support needed; over the HTTP interface):

npm run validate:live -- --url http://localhost:8124/

It runs CREATE TEMPORARY TABLE _probe (c <TYPE>) (session-scoped, nothing persisted) with the relevant experimental settings enabled, so an unknown type family fails with UNKNOWN_TYPE. As of the latest run, 347/356 instantiate and 0 are unexpected.

The remaining 9 are listed in test/non_instantiable.txt: types the parser accepts (and whose AST matches the server's parser) but that the server's type factory later rejects — e.g. partial tuple naming (Tuple(a UInt8, String)), Nullable inside Variant, Nullable(Tuple(...)), Dynamic(max_types = 255), the BINARY alias without a size, and the legacy Object('json') (removed in recent servers). These are deliberate parser test inputs — this is a type-string parser, not a type validator — so they are allowlisted, and validate:live exits non-zero only on an unexpected failure.