encode

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 14 Imported by: 0 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 encode is the encoders of the lightweight encodings: bit-packing, frame of reference, zigzag, run-length, run-end, dictionary, sequence, constant and sparse.

An encoder takes a typed slice and the nulls of its rows (true for a null row, nil for none) and returns the Node tree that the array writer serializes: the identifier, the metadata, the buffers and the children of each array, as the reference writes them. For the deterministic encoders the bytes are the reference's, which testdata/vectors checks.

Every encoder belongs to an Encoder, which owns all the memory of the nodes it returns: they are valid until the next Reset. A running Encoder allocates nothing.

Each encoder has an Estimate twin that returns, from a sample of the rows, the size in bytes of the buffers of the encoded array, without building it.

Index

Constants

View Source
const (
	IDPrimitive  = "vortex.primitive"
	IDBool       = "vortex.bool"
	IDBitPacked  = "fastlanes.bitpacked"
	IDFoR        = "fastlanes.for"
	IDRLE        = "fastlanes.rle"
	IDZigZag     = "vortex.zigzag"
	IDRunEnd     = "vortex.runend"
	IDDict       = "vortex.dict"
	IDSequence   = "vortex.sequence"
	IDConstant   = "vortex.constant"
	IDMasked     = "vortex.masked"
	IDSparse     = "vortex.sparse"
	IDVarBin     = "vortex.varbin"
	IDVarBinView = "vortex.varbinview"
	IDByteBool   = "vortex.bytebool"
	IDChunked    = "vortex.chunked"

	IDALP           = "vortex.alp"
	IDALPRD         = "vortex.alprd"
	IDFSST          = "vortex.fsst"
	IDOnPair        = "vortex.onpair"
	IDDecimalBytes  = "vortex.decimal_byte_parts"
	IDDateTimeParts = "vortex.datetimeparts"
	IDZstd          = "vortex.zstd"
	IDPco           = "vortex.pco"
)

The identifiers of the arrays the encoders make.

View Source
const (
	TicksSecond = 1
	TicksMilli  = 1_000
	TicksMicro  = 1_000_000
	TicksNano   = 1_000_000_000
)

The ticks of a second of the units of a timestamp.

View Source
const (

	// OnPairSampleFraction is the share of the bytes the training reads on the sample of a
	// compressor (SAMPLE_TRAINING_FRACTION of vortex-btrblocks): 15% of a sample of about a
	// thousand rows trains a dictionary too poor to rank OnPair against FSST.
	OnPairSampleFraction = 0.5
)

Variables

View Source
var ErrNegative = errors.New("encode: cannot bit-pack a negative value")

ErrNegative reports a negative value given to the bit-packing, which packs unsigned values.

Functions

func BestWidth

func BestWidth(h []int, valueBytes int) int

BestWidth is the bit width that makes the smallest array, from a histogram of the bit lengths of the values and the bytes of a value: an exception costs a value and an index. It is the choice of the reference.

func ByteCounts added in v0.2.1

func ByteCounts(v []uint8) (c [256]int)

ByteCounts counts the bytes of v by their value. Four tables are filled in turn, so that two bytes of the same value that follow each other do not wait for one another: it is four times as fast as one increment per byte, for the bytes of a uuid as for the codes of a dictionary.

func Encodings

func Encodings() []string

Encodings lists the identifiers of the arrays the encoders make.

func EstimateALP

func EstimateALP[F Float](e *Encoder, sample []F, nulls []bool, rows int) int

EstimateALP is the size of ALP.

func EstimateALPRD

func EstimateALPRD[F Float](e *Encoder, sample []F, nulls []bool, rows int) int

EstimateALPRD is the size of ALPRD.

func EstimateBitPack

func EstimateBitPack[T Int](sample []T, nulls []bool, rows, width int) int

EstimateBitPack is the size of BitPack at the given width.

func EstimateConstant

func EstimateConstant[T Number](sample []T, nulls []bool) int

EstimateConstant is the size of Constant: the ScalarValue of the first value of the sample.

func EstimateDateTimeParts

func EstimateDateTimeParts(v []int64, nulls []bool, rows int, ticks int64) int

EstimateDateTimeParts is the size of DateTimeParts.

func EstimateDecimalBytes

func EstimateDecimalBytes(raw []byte, width int, nulls []bool, rows int) int

EstimateDecimalBytes is the size of DecimalBytes: the width of the narrowest integer that holds the sample.

func EstimateDict

func EstimateDict[T Number](e *Encoder, sample []T, nulls []bool, rows int) int

EstimateDict is the size of Dict.

func EstimateDictBytes

func EstimateDictBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int

EstimateDictBytes is the size of DictBytes.

func EstimateDictStrings

func EstimateDictStrings(e *Encoder, sample []string, nulls []bool, rows int) int

EstimateDictStrings is the size of DictStrings.

func EstimateFSSTBytes

func EstimateFSSTBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int

EstimateFSSTBytes is the size of FSSTBytes.

func EstimateFSSTStrings

func EstimateFSSTStrings(e *Encoder, sample []string, nulls []bool, rows int) int

EstimateFSSTStrings is the size of FSSTStrings.

func EstimateFoR

func EstimateFoR[T Int](nulls []bool, rows int) int

EstimateFoR is the size of FoR.

func EstimateOnPairBytes

func EstimateOnPairBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int

EstimateOnPairBytes is the size of OnPairBytes, from the sample of rows rows.

func EstimatePco

func EstimatePco[T Number](e *Encoder, sample []T, nulls []bool, rows int) int

EstimatePco is the size of Pco: the sample is coded, and its pages scaled to the rows.

func EstimateRLE

func EstimateRLE[T Number](sample []T, nulls []bool, rows int) int

EstimateRLE is the size of RLE.

func EstimateRunEnd

func EstimateRunEnd[T Number](sample []T, nulls []bool, rows int) int

EstimateRunEnd is the size of RunEnd.

func EstimateSequence

func EstimateSequence() int

EstimateSequence is the size of Sequence, which has no buffer.

func EstimateSparse

func EstimateSparse[T Number](e *Encoder, sample []T, nulls []bool, rows int) int

EstimateSparse is the size of Sparse.

func EstimateVarBinBytes

func EstimateVarBinBytes(sample [][]byte, nulls []bool, rows int) int

EstimateVarBinBytes is the size of VarBinBytes, its offsets not compressed.

func EstimateZigZag

func EstimateZigZag[T Signed](nulls []bool, rows int) int

EstimateZigZag is the size of ZigZag.

func EstimateZstd

func EstimateZstd[T Number](e *Encoder, sample []T, nulls []bool, rows int) int

EstimateZstd is the size of Zstd: the sample is compressed and what comes out scaled.

func EstimateZstdBytes

func EstimateZstdBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int

EstimateZstdBytes is the size of ZstdBytes.

func EstimateZstdStrings

func EstimateZstdStrings(e *Encoder, sample []string, nulls []bool, rows int) int

EstimateZstdStrings is the size of ZstdStrings.

func Histogram

func Histogram[T Int](v []T, nulls []bool, h []int)

Histogram counts the valid values of v by their number of bits: h[b] values have b bits. A null counts as a value of no bit. h has the bits of T plus one entries, and is overwritten.

func NarrowOnPair added in v0.2.0

func NarrowOnPair(e *Encoder, n *Node)

NarrowOnPair narrows the four integer children of n, an array of OnPairBytes, each to the smallest unsigned integer that holds its values, and writes their types in the metadata, as the OnPair scheme of the reference does before it compresses them.

Types

type Buf

type Buf struct {
	Data  []byte
	Align int
}

Buf is a buffer of an array and the alignment it asks for.

type Encoder

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

Encoder holds the memory of the nodes that the encoders make, and what they need to work. The zero value is not usable: call New.

func New

func New() *Encoder

New returns an Encoder.

func (*Encoder) Measure added in v0.1.1

func (e *Encoder) Measure()

Measure makes the arrays of e ones that are only measured, never read: bit-packing leaves the bytes of its values and of its patches unwritten, in buffers of the right sizes. The compressor measures its candidates on a sample that way.

func (*Encoder) Reset

func (e *Encoder) Reset()

Reset frees the nodes and buffers of the earlier calls, which must not be used any more. The blocks of the arena are kept: the same calls fit them next time, with no new memory.

type Exponents

type Exponents struct{ E, F uint8 }

Exponents is the pair of powers of ten ALP scales a value by: it encodes round(v * 10^E * 10^-F), and decodes n * 10^F * 10^-E.

func BestExponents

func BestExponents[F Float](e *Encoder, v []F) Exponents

BestExponents is the pair of exponents that makes the smallest encoding, from a sample of v, as the alp crate picks it: an estimate of the bits of the integers, once the range of the ones that decode back is taken off, and of the patches, over each pair; of two as good, the one that scales least.

type Float

type Float interface{ ~float32 | ~float64 }

Float is the float types the encoders of floats take.

type Int

type Int interface {
	~int8 | ~int16 | ~int32 | ~int64 | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Int is the integer types the encoders take.

type Node

type Node struct {
	ID   string
	Meta []byte
	Bufs []Buf
	Kids []*Node
	// contains filtered or unexported fields
}

Node is an encoded array.

func ALP

func ALP[F Float](e *Encoder, v []F, nulls []bool) *Node

ALP is the ALP encoding of the floats of v, at the exponents BestExponents finds: the integers round(v * 10^e * 10^-f) in a child, and in patches (a position, a value and the number of patches before each chunk of 1024 rows) the valid values that do not decode back to the same bits: a NaN, an infinity, a -0 or a value that needs more digits. The integer of a patch is the first one that is not, so that it does not widen the range of the others.

func ALPRD

func ALPRD[F Float](e *Encoder, v []F, nulls []bool) (*Node, bool)

ALPRD is the ALP-RD encoding of the floats of v, for the ones that have no short decimal form: the cut of each float into its left part, a code of a dictionary of at most 8 entries (bit-packed in 1 to 3 bits), and its right part (bit-packed); a left part that is not in the dictionary has the code 0 and a patch with its bits. It returns false for no row.

func ALPRDWith

func ALPRDWith[F Float](e *Encoder, v []F, nulls []bool, rightBits int, dict []uint16) (*Node, bool)

ALPRDWith is ALPRD with the cut and the dictionary given: the right part is rightBits wide, at most the bits of F less 1. It returns false for more than 8 entries, or no row.

func ALPWith

func ALPWith[F Float](e *Encoder, v []F, nulls []bool, x Exponents) *Node

ALPWith is ALP at the given exponents, at most 10 for floats of 32 bits and 18 for floats of 64: it returns nil for more.

func BitPack

func BitPack[T Int](e *Encoder, v []T, nulls []bool, width int) (*Node, error)

BitPack packs the integers of v in width bits each, in FastLanes blocks of 1024. A value that does not fit goes to the patches: its index and its value, with the number of patches before each block. The slots of null rows are packed as they are. The width must be under the bits of T and the valid values not negative.

func BitPackBest

func BitPackBest[T Int](e *Encoder, v []T, nulls []bool) (*Node, error)

BitPackBest is BitPack at the width BestWidth picks from the histogram of the values. It fails when that width is the bits of T: the values are not worth packing.

func Bools

func Bools(e *Encoder, v []bool, nulls []bool) *Node

Bools is the plain vortex.bool array of the booleans v: a bit per row, and the validity.

func ByteBool

func ByteBool(e *Encoder, v []bool, nulls []bool) *Node

ByteBool is the vortex.bytebool array of the booleans v: a byte per row, 0 or 1 (0 for a null row), and the validity as its child. The compressor never picks it, as vortex.bool holds the same values in a bit each: a test forces it, to read it back with the reference.

func Chunked

func Chunked(e *Encoder, parts []*Node, rows []int) *Node

Chunked is the vortex.chunked array of parts, which hold rows[i] rows each: the offsets of the parts as u64 (parts + 1) are child 0, then the parts. It has no validity of its own: the parts have theirs.

func Constant

func Constant[T Number](e *Encoder, v []T, nulls []bool) (*Node, bool)

Constant is the array of v when every row has the same value, a float compared by its bits, or when every row is null: no metadata, and the value as a ScalarValue in the one buffer. It returns false for an empty array or any other array, a null among values included.

func ConstantBools

func ConstantBools(e *Encoder, v []bool, nulls []bool) (*Node, bool)

ConstantBools is Constant for booleans.

func ConstantBoolsMasked

func ConstantBoolsMasked(e *Encoder, v []bool, nulls []bool) (*Node, bool)

ConstantBoolsMasked is ConstantMasked for booleans.

func ConstantBytes

func ConstantBytes(e *Encoder, v [][]byte, nulls []bool, utf8 bool) (*Node, bool)

ConstantBytes is Constant for byte slices: the value is a string when utf8 is set, else bytes.

func ConstantBytesMasked

func ConstantBytesMasked(e *Encoder, v [][]byte, nulls []bool, utf8 bool) (*Node, bool)

ConstantBytesMasked is ConstantMasked for byte slices.

func ConstantMasked

func ConstantMasked[T Number](e *Encoder, v []T, nulls []bool) (*Node, bool)

ConstantMasked is Constant, and also the array of a chunk whose valid values are all equal and some rows are null: the constant of that value under the validity. It returns false for an empty array or one with two distinct valid values.

func DateTimeParts

func DateTimeParts(e *Encoder, v []int64, nulls []bool, ticks int64) (*Node, bool)

DateTimeParts is the datetime parts encoding of the timestamps of v, counts of ticks (one of the Ticks constants) of a second since the epoch: the days, the seconds of the day and the subseconds, each the remainder of a division towards zero, so that those of a time before the epoch are negative. The days are the narrowest signed integer that holds the valid ones, and carry the nulls; the seconds and the subseconds are i32, the subseconds a constant 0 for a timestamp in seconds. It returns false for other ticks.

func DecimalBytes

func DecimalBytes(e *Encoder, raw []byte, width int, nulls []bool) (*Node, bool)

DecimalBytes is the decimal byte parts encoding of decimals: raw holds the unscaled values, width bytes each (1, 2, 4, 8, 16 or 32), two's complement, little-endian. A decimal whose valid values all fit an i64 is written as the narrowest signed integer that holds them: the most significant part, with no lower part, and it carries the nulls. It returns false for a value that does not fit an i64, which would need lower parts, or a width that is not one.

func Dict

func Dict[T Number](e *Encoder, v []T, nulls []bool) *Node

Dict is the dictionary encoding of the numbers of v: the codes, in the smallest unsigned type that holds them, and the distinct values in the order of their first appearance. A null has a value of its own, null in the validity of the values, so that no code is null. Numbers are compared by their bits.

func DictBytes

func DictBytes(e *Encoder, v [][]byte, nulls []bool) *Node

DictBytes is Dict for byte slices: the values are a varbinview.

func DictStrings

func DictStrings(e *Encoder, v []string, nulls []bool) *Node

DictStrings is Dict for strings: the values are a varbinview.

func FSSTBytes

func FSSTBytes(e *Encoder, vals [][]byte, nulls []bool) *Node

FSSTBytes is the FSST encoding of the byte slices of vals: the table trained on the valid ones, 3 buffers (the symbols, their lengths, the codes) and the children that say, for each row, the length of the string and where its codes end, then the validity.

func FSSTBytesWith

func FSSTBytesWith(e *Encoder, vals [][]byte, nulls []bool, syms []uint64, lens []uint8) (*Node, bool)

FSSTBytesWith is FSSTBytes with the table given, the symbols by code and their lengths (those of the crate's compressor): it returns false for a table that is not one.

func FSSTStrings

func FSSTStrings(e *Encoder, vals []string, nulls []bool) *Node

FSSTStrings is FSSTBytes for strings.

func FSSTStringsWith

func FSSTStringsWith(e *Encoder, vals []string, nulls []bool, syms []uint64, lens []uint8) (*Node, bool)

FSSTStringsWith is FSSTBytesWith for strings.

func FoR

func FoR[T Int](e *Encoder, v []T, nulls []bool) *Node

FoR is the frame of reference of the integers of v: the reference is the smallest valid value, and the child holds each value minus the reference (wrapping), zero for a null row. A column with no valid value has the reference 0.

func Masked

func Masked(e *Encoder, values *Node, nulls []bool, rows int) *Node

Masked is the array of values under the validity of nulls: child 0 the values, child 1 the validity, no metadata and no buffer. values is of the non-nullable type. The callers only mask a chunk with a null row and a valid row.

func OnPairBytes

func OnPairBytes(e *Encoder, vals [][]byte, nulls []bool) *Node

OnPairBytes is the OnPair encoding of the byte slices of vals: the dictionary trained on the valid ones, in a buffer, and the children that give the offsets of the tokens, the codes, where the codes of each string end and its length, then the validity. A column where every row is null is a null constant.

func OnPairBytesTrained added in v0.2.0

func OnPairBytesTrained(e *Encoder, vals [][]byte, nulls []bool, fraction float64) *Node

OnPairBytesTrained is OnPairBytes with a training that reads the given share of the bytes, OnPairSampleFraction for the sample of a compressor.

func OnPairBytesWith

func OnPairBytesWith(e *Encoder, vals [][]byte, nulls []bool, dict []byte, offs []uint32) (*Node, bool)

OnPairBytesWith is OnPairBytes with the dictionary given, the bytes of the tokens and their offsets (the ones of the reference's output): it returns false for a dictionary that is not one, which is sorted, has the 256 bytes and at most 4096 tokens of 1 to 16 bytes.

func Pco

func Pco[T Number](e *Encoder, v []T, nulls []bool) *Node

Pco is the Pco encoding of the numbers of v: the valid ones, in chunks of 2^18 and pages of about 8192, as the reference writes them. It returns nil for 8-bit numbers, which Pco does not code.

func Primitive

func Primitive(e *Encoder, raw []byte, width int, nulls []bool, rows int) *Node

Primitive is the plain vortex.primitive array of the rows numbers of width bytes in raw, and the validity.

func RLE

func RLE[T Number](e *Encoder, v []T, nulls []bool) *Node

RLE is the FastLanes run-length encoding of v, in blocks of 1024 values: the children are the values of the runs of all the blocks, one u16 index per row (the position of its run in its block), and, per block, the number of values before it. The last block is padded with its last value. A null row takes the value before it in its block, the first of a block zero, so that it does not start a run; floats are compared by their bits.

func RunEnd

func RunEnd[T Number](e *Encoder, v []T, nulls []bool) *Node

RunEnd is the run-end encoding of v: the children are the end of each run, in the smallest unsigned type that holds the number of rows, and the value of the run (the last of its rows). A run is rows equal to the one before: two nulls are equal, a null and a value are not, and the numbers are compared as numbers, so a NaN is a run of its own and 0 and -0 are one.

func Sequence

func Sequence[T Int](e *Encoder, v []T, nulls []bool) (*Node, bool)

Sequence is the array of v when every value is the one before plus the same step, which is not zero and does not make a value overflow: the metadata holds the first value and the step (a signed integer), and there is no buffer. One value is a sequence of step 0. It returns false for an empty array, a null, or any other array.

func Sparse

func Sparse[T Number](e *Encoder, v []T, nulls []bool) (*Node, bool)

Sparse is the sparse encoding of the numbers of v: a fill value and the patches, the rows that differ from it. The fill is null when more than nine tenths of the rows are null, the patches being the valid rows; else it is the most frequent value, the first of them when several are, and the patches are the rows that are not equal to it, the null ones included. The fill is in the one buffer, as a ScalarValue, and a null patch keeps its value. An array that is all equal to its fill is the constant of Constant, as the reference does. It returns false when the array is empty or all null.

func VarBinBytes

func VarBinBytes(e *Encoder, v [][]byte, nulls []bool) *Node

VarBinBytes is the vortex.varbin array of the byte slices of v: the values end to end in one buffer, the rows + 1 offsets (the narrowest unsigned type that holds the last) and the validity. A null row has no byte.

func ZigZag

func ZigZag[T Signed](e *Encoder, v []T, nulls []bool) *Node

ZigZag maps the signed integers of v to unsigned ones that are small when the values are close to zero: 0, -1, 1, -2 become 0, 1, 2, 3. The slot of a null row is mapped too.

func Zstd

func Zstd[T Number](e *Encoder, v []T, nulls []bool) *Node

Zstd is the zstd encoding of the numbers of v.

func ZstdBytes

func ZstdBytes(e *Encoder, vals [][]byte, nulls []bool) *Node

ZstdBytes is the zstd encoding of the byte slices of vals.

func ZstdStrings

func ZstdStrings(e *Encoder, vals []string, nulls []bool) *Node

ZstdStrings is the zstd encoding of the strings of vals.

func (*Node) Size

func (n *Node) Size() int

Size is the bytes of the buffers of the tree.

type Number

type Number = unsafex.Number

Number is the numeric types the encoders take: integers and floats.

type Signed

type Signed interface {
	~int8 | ~int16 | ~int32 | ~int64
}

Signed is the signed integer types, the ones ZigZag takes.

Jump to

Keyboard shortcuts

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