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
- Variables
- func BestWidth(h []int, valueBytes int) int
- func Encodings() []string
- func EstimateALP[F Float](e *Encoder, sample []F, nulls []bool, rows int) int
- func EstimateALPRD[F Float](e *Encoder, sample []F, nulls []bool, rows int) int
- func EstimateBitPack[T Int](sample []T, nulls []bool, rows, width int) int
- func EstimateConstant[T Number](sample []T, nulls []bool) int
- func EstimateDateTimeParts(v []int64, nulls []bool, rows int, ticks int64) int
- func EstimateDecimalBytes(raw []byte, width int, nulls []bool, rows int) int
- func EstimateDict[T Number](e *Encoder, sample []T, nulls []bool, rows int) int
- func EstimateDictBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int
- func EstimateDictStrings(e *Encoder, sample []string, nulls []bool, rows int) int
- func EstimateFSSTBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int
- func EstimateFSSTStrings(e *Encoder, sample []string, nulls []bool, rows int) int
- func EstimateFoR[T Int](nulls []bool, rows int) int
- func EstimateOnPairBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int
- func EstimatePco[T Number](e *Encoder, sample []T, nulls []bool, rows int) int
- func EstimateRLE[T Number](sample []T, nulls []bool, rows int) int
- func EstimateRunEnd[T Number](sample []T, nulls []bool, rows int) int
- func EstimateSequence() int
- func EstimateSparse[T Number](e *Encoder, sample []T, nulls []bool, rows int) int
- func EstimateVarBinBytes(sample [][]byte, nulls []bool, rows int) int
- func EstimateZigZag[T Signed](nulls []bool, rows int) int
- func EstimateZstd[T Number](e *Encoder, sample []T, nulls []bool, rows int) int
- func EstimateZstdBytes(e *Encoder, sample [][]byte, nulls []bool, rows int) int
- func EstimateZstdStrings(e *Encoder, sample []string, nulls []bool, rows int) int
- func Histogram[T Int](v []T, nulls []bool, h []int)
- func NarrowOnPair(e *Encoder, n *Node)
- type Buf
- type Encoder
- type Exponents
- type Float
- type Int
- type Node
- func ALP[F Float](e *Encoder, v []F, nulls []bool) *Node
- func ALPRD[F Float](e *Encoder, v []F, nulls []bool) (*Node, bool)
- func ALPRDWith[F Float](e *Encoder, v []F, nulls []bool, rightBits int, dict []uint16) (*Node, bool)
- func ALPWith[F Float](e *Encoder, v []F, nulls []bool, x Exponents) *Node
- func BitPack[T Int](e *Encoder, v []T, nulls []bool, width int) (*Node, error)
- func BitPackBest[T Int](e *Encoder, v []T, nulls []bool) (*Node, error)
- func Bools(e *Encoder, v []bool, nulls []bool) *Node
- func ByteBool(e *Encoder, v []bool, nulls []bool) *Node
- func Chunked(e *Encoder, parts []*Node, rows []int) *Node
- func Constant[T Number](e *Encoder, v []T, nulls []bool) (*Node, bool)
- func ConstantBools(e *Encoder, v []bool, nulls []bool) (*Node, bool)
- func ConstantBoolsMasked(e *Encoder, v []bool, nulls []bool) (*Node, bool)
- func ConstantBytes(e *Encoder, v [][]byte, nulls []bool, utf8 bool) (*Node, bool)
- func ConstantBytesMasked(e *Encoder, v [][]byte, nulls []bool, utf8 bool) (*Node, bool)
- func ConstantMasked[T Number](e *Encoder, v []T, nulls []bool) (*Node, bool)
- func DateTimeParts(e *Encoder, v []int64, nulls []bool, ticks int64) (*Node, bool)
- func DecimalBytes(e *Encoder, raw []byte, width int, nulls []bool) (*Node, bool)
- func Dict[T Number](e *Encoder, v []T, nulls []bool) *Node
- func DictBytes(e *Encoder, v [][]byte, nulls []bool) *Node
- func DictStrings(e *Encoder, v []string, nulls []bool) *Node
- func FSSTBytes(e *Encoder, vals [][]byte, nulls []bool) *Node
- func FSSTBytesWith(e *Encoder, vals [][]byte, nulls []bool, syms []uint64, lens []uint8) (*Node, bool)
- func FSSTStrings(e *Encoder, vals []string, nulls []bool) *Node
- func FSSTStringsWith(e *Encoder, vals []string, nulls []bool, syms []uint64, lens []uint8) (*Node, bool)
- func FoR[T Int](e *Encoder, v []T, nulls []bool) *Node
- func Masked(e *Encoder, values *Node, nulls []bool, rows int) *Node
- func OnPairBytes(e *Encoder, vals [][]byte, nulls []bool) *Node
- func OnPairBytesTrained(e *Encoder, vals [][]byte, nulls []bool, fraction float64) *Node
- func OnPairBytesWith(e *Encoder, vals [][]byte, nulls []bool, dict []byte, offs []uint32) (*Node, bool)
- func Pco[T Number](e *Encoder, v []T, nulls []bool) *Node
- func Primitive(e *Encoder, raw []byte, width int, nulls []bool, rows int) *Node
- func RLE[T Number](e *Encoder, v []T, nulls []bool) *Node
- func RunEnd[T Number](e *Encoder, v []T, nulls []bool) *Node
- func Sequence[T Int](e *Encoder, v []T, nulls []bool) (*Node, bool)
- func Sparse[T Number](e *Encoder, v []T, nulls []bool) (*Node, bool)
- func VarBinBytes(e *Encoder, v [][]byte, nulls []bool) *Node
- func ZigZag[T Signed](e *Encoder, v []T, nulls []bool) *Node
- func Zstd[T Number](e *Encoder, v []T, nulls []bool) *Node
- func ZstdBytes(e *Encoder, vals [][]byte, nulls []bool) *Node
- func ZstdStrings(e *Encoder, vals []string, nulls []bool) *Node
- type Number
- type Signed
Constants ¶
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.
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.
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 ¶
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 ¶
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 Encodings ¶
func Encodings() []string
Encodings lists the identifiers of the arrays the encoders make.
func EstimateALP ¶
EstimateALP is the size of ALP.
func EstimateALPRD ¶
EstimateALPRD is the size of ALPRD.
func EstimateBitPack ¶
EstimateBitPack is the size of BitPack at the given width.
func EstimateConstant ¶
EstimateConstant is the size of Constant: the ScalarValue of the first value of the sample.
func EstimateDateTimeParts ¶
EstimateDateTimeParts is the size of DateTimeParts.
func EstimateDecimalBytes ¶
EstimateDecimalBytes is the size of DecimalBytes: the width of the narrowest integer that holds the sample.
func EstimateDict ¶
EstimateDict is the size of Dict.
func EstimateDictBytes ¶
EstimateDictBytes is the size of DictBytes.
func EstimateDictStrings ¶
EstimateDictStrings is the size of DictStrings.
func EstimateFSSTBytes ¶
EstimateFSSTBytes is the size of FSSTBytes.
func EstimateFSSTStrings ¶
EstimateFSSTStrings is the size of FSSTStrings.
func EstimateFoR ¶
EstimateFoR is the size of FoR.
func EstimateOnPairBytes ¶
EstimateOnPairBytes is the size of OnPairBytes, from the sample of rows rows.
func EstimatePco ¶
EstimatePco is the size of Pco: the sample is coded, and its pages scaled to the rows.
func EstimateRLE ¶
EstimateRLE is the size of RLE.
func EstimateRunEnd ¶
EstimateRunEnd is the size of RunEnd.
func EstimateSequence ¶
func EstimateSequence() int
EstimateSequence is the size of Sequence, which has no buffer.
func EstimateSparse ¶
EstimateSparse is the size of Sparse.
func EstimateVarBinBytes ¶
EstimateVarBinBytes is the size of VarBinBytes, its offsets not compressed.
func EstimateZigZag ¶
EstimateZigZag is the size of ZigZag.
func EstimateZstd ¶
EstimateZstd is the size of Zstd: the sample is compressed and what comes out scaled.
func EstimateZstdBytes ¶
EstimateZstdBytes is the size of ZstdBytes.
func EstimateZstdStrings ¶
EstimateZstdStrings is the size of ZstdStrings.
func Histogram ¶
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
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 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 (*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.
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 ¶
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 Node ¶
type Node struct {
ID string
Meta []byte
Bufs []Buf
Kids []*Node
// contains filtered or unexported fields
}
Node is an encoded array.
func ALP ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Bools is the plain vortex.bool array of the booleans v: a bit per row, and the validity.
func ByteBool ¶
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 ¶
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 ¶
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 ¶
ConstantBools is Constant for booleans.
func ConstantBoolsMasked ¶
ConstantBoolsMasked is ConstantMasked for booleans.
func ConstantBytes ¶
ConstantBytes is Constant for byte slices: the value is a string when utf8 is set, else bytes.
func ConstantBytesMasked ¶
ConstantBytesMasked is ConstantMasked for byte slices.
func ConstantMasked ¶
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 ¶
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 ¶
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 ¶
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 DictStrings ¶
DictStrings is Dict for strings: the values are a varbinview.
func FSSTBytes ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
Primitive is the plain vortex.primitive array of the rows numbers of width bytes in raw, and the validity.
func RLE ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ZstdStrings ¶
ZstdStrings is the zstd encoding of the strings of vals.