Clast.Sketches 0.1.0

dotnet add package Clast.Sketches --version 0.1.0
                    
NuGet\Install-Package Clast.Sketches -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Clast.Sketches" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Clast.Sketches" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Clast.Sketches" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Clast.Sketches --version 0.1.0
                    
#r "nuget: Clast.Sketches, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Clast.Sketches@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Clast.Sketches&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Clast.Sketches&version=0.1.0
                    
Install as a Cake Tool

Clast.Sketches

Streaming sketches for .NET, binary-compatible with Apache DataSketches.

NuGet CI License: Apache 2.0

Overview

Clast.Sketches is a from-scratch C# implementation of the Apache DataSketches algorithms and, critically, of their serialized forms. A sketch written by datasketches-java, datasketches-cpp, Spark, or Trino reads here, and one written here reads there. That portability is the point: sketches are usually produced by one engine and consumed by another.

The initial target is the sketches that show up in Apache Iceberg Puffin files — Theta for the apache-datasketches-theta-v1 blob, and HLL for engines that store NDV that way.

Status

First release. Theta and HLL are complete and compatibility-tested against the reference implementations. The public API may still shift before 1.0, which is why the version is 0.x — under 0.x semver the minor is the breaking slot.

Component State
MurmurHash3 x64 128 Done — matches the Java reference bit for bit
Compact Theta sketch: read, estimate, serialize Done — round-trips the TCK snapshots byte for byte
Theta update sketch, QuickSelect Done — reproduces the TCK snapshots byte for byte from scratch
Theta update sketch, Alpha (required by Puffin) Done
Theta union, intersection, A-not-B Done
Theta error bounds Done — matches the reference to 1e-15 across ~38M evaluations
HLL sketch (HLL_4 / HLL_6 / HLL_8) Done — reproduces all 24 TCK snapshots byte for byte
HLL union Done
Delta-compressed Theta (serialization version 4) Done — reproduces the TCK snapshots byte for byte

Compatibility is tested against apache/datasketches-tck, the project's own cross-language serialization snapshots — the same images the Java, C++, and Go implementations validate against.

Example

using Clast.Sketches.Theta;

// Count distinct values.
var sketch = UpdateThetaSketch.Builder().Build();
foreach (var value in values)
    sketch.Update(value);

Console.WriteLine(sketch.Estimate);

// Serialize to the format Iceberg Puffin stores in an
// `apache-datasketches-theta-v1` blob.
byte[] blob = sketch.Compact().ToByteArray();

// Read one back — written by us, by Spark, by Trino, by anything.
var loaded = CompactThetaSketch.Deserialize(blob);
Console.WriteLine(loaded.Estimate);

Every sketch can report how much to trust its estimate:

// ~95% confidence by default; pass 1 or 3 for ~68% or ~99.7%.
Console.WriteLine($"{sketch.GetLowerBound()} .. {sketch.GetUpperBound()}");

Sketches built independently merge exactly — which is the reason to use Theta over a plain counter. Counting distinct values across a hundred Iceberg partitions becomes a hundred cheap merges instead of a rescan:

var union = new ThetaUnion(nominalEntries: 4096);
foreach (var blob in puffinBlobs)
    union.UnionCompactImage(blob);

Console.WriteLine(union.GetResult().Estimate);

Theta sketches also have a delta-compressed serialization, typically 30-40% smaller. Ordered hashes sit fairly evenly below theta, so the gaps between them need far fewer bits than the hashes themselves:

byte[] smaller = sketch.Compact().ToByteArrayCompressed();

// Deserialize reads either form — the image says which it is.
var loaded = CompactThetaSketch.Deserialize(smaller);

Intersection and set difference work too — and unlike a union, they cannot be computed from the estimates alone, only from the sketches:

var shared  = ThetaIntersection.Of(monday, tuesday);   // seen on both days
var newToday = ThetaAnotB.Of(tuesday, monday);         // seen only on Tuesday

Their results carry wider relative error than their operands, since a small intersection is recovered from two large sketches. Check the bounds before trusting a near-empty result.

Puffin specifies the Alpha family, which is more accurate standalone:

var sketch = UpdateThetaSketch.Builder()
    .SetFamily(SketchFamily.Alpha)   // requires >= 512 nominal entries
    .SetNominalEntries(4096)
    .Build();

HLL

When all you need is a distinct count, HLL is markedly more compact than Theta for the same accuracy — Theta earns its extra space by supporting intersection and set difference, which HLL cannot do.

using Clast.Sketches.Hll;

var sketch = new HllSketch(lgConfigK: 12, TgtHllType.Hll4);
foreach (var value in values)
    sketch.Update(value);

Console.WriteLine($"{sketch.Estimate} ({sketch.GetLowerBound()}..{sketch.GetUpperBound()})");

byte[] blob = sketch.ToCompactByteArray();
var loaded = HllSketch.Deserialize(blob);

HLL sketches merge too, and because registers hold a maximum the result is exactly the sketch you would have built over the union of the inputs — no error accumulates across merges. Sketches with different k or different register widths can be mixed freely:

var union = new HllUnion(lgMaxK: 12);
foreach (var blob in blobs)
    union.Update(blob);

HllSketch merged = union.GetResult();

Merging HLL_8 sketches is AVX2- and ARM NEON-accelerated, with a portable fallback; the register array is a byte per register, so a merge is an element-wise maximum.

This is the DataSketches HLL — what Spark's hll_sketch_agg produces — not the HyperLogLog++ of the Google paper, which is a different algorithm with a different wire format.

Target frameworks

  • netstandard2.0
  • net8.0
  • net10.0

Relationship to Apache DataSketches

This is an independent implementation, not an ASF project and not affiliated with or endorsed by the Apache Software Foundation. Algorithms, constants, and wire formats are ported from the Apache-2.0 licensed DataSketches sources; see NOTICE for attribution.

License

Licensed under the Apache License, Version 2.0.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 110 8/3/2026