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 ¶
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 ¶
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 ¶
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) NDV ¶
NDV returns Estimate truncated to an int64, matching how Iceberg-Java records the "ndv" blob property (String.valueOf((long) sketch.getEstimate())).
func (*Sketch) Serialize ¶
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) AddBytes ¶
AddBytes decodes a serialized sketch (as produced by Sketch.Serialize or by another Iceberg implementation) and merges it into the union.