Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 6 min read How-to mediumAdept

Semantic Versioning UDF in ClickHouse

A ClickHouse SQL UDF sorts app version strings by semantic version rather than lexicographically, so that 2.1.2 sorts before 2.1.10. It extracts the dotted numeric portion with a regex, splits it on periods, and maps each part to UInt32 with toUInt32OrZero, producing integer arrays that ClickHouse compares element-wise. Known limitations are that pre-release suffixes like -prod or -stg are dropped, so those versions tie, and that oddly shaped strings such as hash-based snapshots parse incorrectly. Embrace reports this is good enough in practice because customers tend to stick to one versioning scheme.

Assumes: SQL basicsregular expressions

Related posts

  • Type Constraints in 65 lines of SQL

    Supabase mediumAdept

    A Postgres composite type plus a domain with check constraints can model Semantic Versioning so invalid versions are rejected on insert or cast. A semver_components composite stores major, minor, patch, pre-release and build metadata as queryable fields, and a semver domain adds non-null and non-negative checks and regex validation of array elements through a helper SQL function. An explicit semver_to_text function, optionally persisted as a stored generated column, gives the familiar 2.0.0-rc.1 rendering. The approach suits types whose integrity is critical and whose spec is stable, and it can be extended with custom equality operators, parsers and aggregates.

  • ClickHouse Release 24.1

    ClickHouse mediumAdept

    ClickHouse 24.1 ships 26 features, 22 performance optimisations and 47 bug fixes. Highlights include an experimental Variant type, a discriminated union of nested columns that lets maps, arrays and CSV schema inference hold mixed types, read back per type with variantElement. New Damerau-Levenshtein, Jaro and Jaro-Winkler similarity functions join the existing Levenshtein distance, demonstrated by finding typosquatting domains in the 7M-row Tranco list. The release also adds a vertical algorithm that speeds up FINAL queries on ReplacingMergeTree tables.

  • ClickHouse Over the Years with Benchmarks

    ClickHouse mediumAdept

    Every ClickHouse release from 1.1.54378 (April 2018) onward was installed on one AWS m5.8xlarge and run through the Star Schema Benchmark, the Brown University benchmark and ClickHouse's own hits benchmark to see how performance changed over time. The setup pulls 328 versions from the old Yandex and new packages.clickhouse.com apt repositories, then exports the hits dataset to CSV with clickhouse-local. Getting the data into old versions meant workarounds for missing features: dropping index_granularity_bytes, replacing LowCardinality with String and IPv4 with String, and splitting DateTime64 into DateTime plus a milliseconds column. Star Schema Benchmark results come with a caveat, since its random value distribution does not resemble real datasets.

  • Building a Paste Service With ClickHouse

    ClickHouse mediumAdept

    pastila.nl is a minimal paste service whose browser JavaScript sends INSERT and SELECT queries directly to an internet-facing ClickHouse instance. Content is addressed by a 128-bit SipHash plus a 32-bit locality-sensitive fingerprint built from the minimum hash of word shingles. Ordering the primary key by fingerprint keeps similar edits adjacent, so storing every keypress compresses to almost nothing. The schema relies on EPHEMERAL and MATERIALIZED columns and CHECK constraints for hash verification, size limits and entropy-based garbage rejection, and runs on a Replicated database with two-node ClickHouse Keeper across Frankfurt and Ohio behind Route53 geo DNS. The setup deliberately embraces anti-patterns, such as a two-node consensus cluster that loses write availability if either node fails.

  • User-defined functions in ClickHouse Cloud

    ClickHouse mediumAdept

    ClickHouse SQL user-defined functions, now available in ClickHouse Cloud, can reconstruct a file's full commit history across renames in git-import data. ClickHouse lacks recursive CTEs and UDFs cannot recurse, so the workaround chains six UDFs that each call the next. Each one looks up the file's previous path from Rename/Add rows and appends it to an array, which caps the rename depth at five. The resulting array then filters the history with `path IN file_path_history(...)`. Fixing the data at insert time with a stable file ID is noted as the cleaner alternative.

The quest for software engineering knowledge.

Look:
Layout: