stats

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package stats computes Apache Iceberg table statistics.

This file provides the number-of-distinct-values (NDV) primitive: a thin wrapper over an Apache DataSketches Theta sketch that produces apache-datasketches-theta-v1 statistics blobs mutually readable and mergeable with the ones written by Iceberg-Java's Spark ComputeTableStats action. Interoperability rests on settings this package pins to match Iceberg-Java: the hash seed (9001), the log2 nominal entries (lgK = 12, i.e. 4096 entries), and the serialized form (an uncompressed, ordered compact sketch).

Iceberg-Java builds one ALPHA-family update sketch per Spark partition and merges the partials through a Theta union, so the sketch it stores is always a union result trimmed to 4096 entries. datasketches-go provides only the QuickSelect update family, so this package mirrors the merge instead: Sketch and Union both produce their estimate and serialization through a union with the same settings. The decoded sketch (theta and retained hashes) is therefore identical to Iceberg-Java's whenever each per-partition sketch holds at most 4096 distinct values, including when the merged result holds more and is trimmed. Once a single sketch goes past 4096 distinct values, the ALPHA and QuickSelect families keep different samples, so the estimates agree only within the sketch's error bounds.

Callers must feed values using Iceberg's single-value serialization (the same byte encoding used for manifest bounds: little-endian integers and floats, UTF-8 strings with no length prefix, etc.); the sketch hashes those exact bytes, so matching the encoding is what makes NDV estimates agree across engines. Null values must not be added.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateFromBytes

func EstimateFromBytes(serialized []byte) (float64, error)

EstimateFromBytes decodes a serialized Theta sketch and returns its estimated number of distinct values. It reads sketches written by any Iceberg implementation, including the compressed serialization versions produced by newer DataSketches releases.

func NDVFromBytes

func NDVFromBytes(serialized []byte) (int64, error)

NDVFromBytes decodes a serialized Theta sketch and returns its estimate truncated to an int64.

Types

type Sketch

type Sketch struct {
	// contains filtered or unexported fields
}

Sketch accumulates values into a Theta sketch to estimate the number of distinct values (NDV) of a column. It is not safe for concurrent use.

Estimate, NDV and Serialize each build a new union over the retained entries, so call them once after all values are added rather than per value.

func NewSketch

func NewSketch() *Sketch

NewSketch returns an empty Sketch configured to match Iceberg-Java's NDV sketches (seed 9001, lgK 12).

func (*Sketch) Add

func (s *Sketch) Add(value []byte)

Add feeds one non-null value into the sketch. value must be the Iceberg single-value serialization of the column value; the sketch hashes these bytes directly.

An empty value (such as the empty string or a zero-length binary) is ignored and not counted as a distinct value. This matches Iceberg-Java, whose DataSketches UpdateSketch.update(ByteBuffer) rejects empty input.

func (*Sketch) Estimate

func (s *Sketch) Estimate() float64

Estimate returns the estimated number of distinct values added so far.

func (*Sketch) NDV

func (s *Sketch) NDV() int64

NDV returns Estimate truncated to an int64, matching how Iceberg-Java records the "ndv" blob property (String.valueOf((long) sketch.getEstimate())).

func (*Sketch) Serialize

func (s *Sketch) Serialize() []byte

Serialize returns the compact, ordered, uncompressed serialization of the sketch: the byte form stored inside an apache-datasketches-theta-v1 Puffin blob (before any Puffin-level compression). The bytes are not identical to Iceberg-Java's (DataSketches-Java sets a few preamble fields differently), but each implementation decodes the other's to the same sketch.

type Union

type Union struct {
	// contains filtered or unexported fields
}

Union merges Theta sketches into a single sketch, used to combine per-file or per-partition sketches into one column-level NDV estimate. Merging requires the same seed on every input, which this package guarantees. Union is not safe for concurrent use.

func NewUnion

func NewUnion() *Union

NewUnion returns an empty Union configured to match Iceberg-Java's NDV union (seed 9001, lgK 12).

func (*Union) Add

func (u *Union) Add(s *Sketch)

Add merges s into the union.

func (*Union) AddBytes

func (u *Union) AddBytes(serialized []byte) error

AddBytes decodes a serialized sketch (as produced by Sketch.Serialize or by another Iceberg implementation) and merges it into the union.

func (*Union) Estimate

func (u *Union) Estimate() float64

Estimate returns the estimated number of distinct values across all merged sketches.

func (*Union) NDV

func (u *Union) NDV() int64

NDV returns Estimate truncated to an int64.

func (*Union) Serialize

func (u *Union) Serialize() []byte

Serialize returns the compact, ordered, uncompressed serialization of the merged sketch, in the same byte form as Sketch.Serialize.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL