columnar

package
v3.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package columnar provides utilities for working with columnar in-memory arrays.

Columnar types are Arrow-compatible. The columnar package exists to provide a Loki-optimized APIs for columnar data that is based on memory.Allocator for memory management rather than arrow-go's reference counting.

Package columnar is EXPERIMENTAL and currently only intended to be used by github.com/grafana/loki/v3/pkg/dataobj.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array interface {
	Datum

	// Len returns the total number of elements in the array.
	Len() int

	// Nulls returns the number of null elements in the array. The number of
	// non-null elements can be calculated from Len() - Nulls().
	Nulls() int

	// IsNull returns true if the element at index i is null.
	IsNull(i int) bool

	// Size returns the total consumed size of the array's buffers in bytes.
	//
	// It doesn't account for the size of the Array type itself, or any unused
	// bytes (such as padding past the length up to the capacity of buffers).
	Size() int

	// Slice returns a slice of the Array from index i to j. Slice panics if j <
	// i or if the slice is outside the valid range of the Array. The returned
	// slice has a length of j-i and shares memory with the original Array.
	//
	// Slice panics if the following invariant is not met: 0 <= i <= j <= arr.Len()
	Slice(i, j int) Array

	// Validity returns the validity bitmap of the array. The returned bitmap
	// may be of length 0 if there are no nulls.
	//
	// A value of 1 in the Validity bitmap indicates that the corresponding
	// element at that position is valid (not null).
	Validity() memory.Bitmap
	// contains filtered or unexported methods
}

An Array is a sequence of elements of the same data type.

func Concat

func Concat(alloc *memory.Allocator, in []Array) (Array, error)

Concat concatenates the input arrays into a single, combined array using the provided allocator. Concat returns an error if not all arrays are of the same kind.

type Bool

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

Bool is an Array of bit-packed boolean values.

func NewBool

func NewBool(values, validity memory.Bitmap) *Bool

NewBool creates a new Bool array from the given values and optional validity bitmap.

Bool arrays made from memory owned by a memory.Allocator are invalidated when the allocator reclaims memory.

If validity is of length zero, all elements are considered valid. Otherwise, NewBool panics if the number of elements does not match the length of validity.

func (*Bool) Get

func (arr *Bool) Get(i int) bool

Get returns the value at index i. If the element at index i is null, Get returns an undefined value.

Get panics if i is out of range.

func (*Bool) IsNull

func (arr *Bool) IsNull(i int) bool

IsNull returns true if the element at index i is null.

func (*Bool) Kind

func (arr *Bool) Kind() Kind

Kind returns the kind of Array being represented.

func (*Bool) Len

func (arr *Bool) Len() int

Len returns the total number of elements in the array.

func (*Bool) Nulls

func (arr *Bool) Nulls() int

Nulls returns the number of null elements in the array. The number of non-null elements can be calculated from Len() - Nulls().

func (*Bool) Size

func (arr *Bool) Size() int

Size returns the size in bytes of arr's buffers.

func (*Bool) Slice

func (arr *Bool) Slice(i, j int) Array

Slice returns a slice of arr from i to j.

func (*Bool) Validity

func (arr *Bool) Validity() memory.Bitmap

Validity returns the validity bitmap of the array. The returned bitmap may be of length 0 if there are no nulls.

A value of 1 in the Validity bitmap indicates that the corresponding element at that position is valid (not null).

func (*Bool) Values

func (arr *Bool) Values() memory.Bitmap

Values returns the underlying values bitmap.

type BoolBuilder

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

A BoolBuilder assists with constructing a Bool array. A BoolBuilder must be constructed by calling NewBoolBuilder.

func NewBoolBuilder

func NewBoolBuilder(alloc *memory.Allocator) *BoolBuilder

NewBoolBuilder creates a new BoolBuilder for constructing a Bool array.

func (*BoolBuilder) AppendNull

func (b *BoolBuilder) AppendNull()

AppendNull adds a new null element to b.

func (*BoolBuilder) AppendNulls

func (b *BoolBuilder) AppendNulls(count int)

AppendNulls appends the given number of null elements to b.

func (*BoolBuilder) AppendValue

func (b *BoolBuilder) AppendValue(v bool)

AppendValue adds a new element to b.

func (*BoolBuilder) AppendValueCount

func (b *BoolBuilder) AppendValueCount(v bool, count int)

AppendValue adds a new element to b.

func (*BoolBuilder) Build

func (b *BoolBuilder) Build() *Bool

Build returns the constructed Bool array. After calling Build, the builder is reset to an initial state.

func (*BoolBuilder) BuildArray

func (b *BoolBuilder) BuildArray() Array

Build returns the constructed array. After calling Build, the builder is reset to an initial state.

func (*BoolBuilder) Grow

func (b *BoolBuilder) Grow(n int)

Grow increases b's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to b without another allocation. If n is negative or too large to allocate the memory, Grow panics.

type BoolScalar

type BoolScalar struct {
	Value bool // Value of the scalar.
	Null  bool // True if the scalar is null.
}

BoolScalar is a Scalar representing a boolean value.

func (*BoolScalar) IsNull

func (s *BoolScalar) IsNull() bool

IsNull implements Scalar and returns bs.Null.

func (*BoolScalar) Kind

func (s *BoolScalar) Kind() Kind

Kind implements Datum and returns KindBool.

type Builder

type Builder interface {
	// AppendNull adds a new null element to the Builder.
	AppendNull()

	// AppendNulls appends the given number of null elements to the Builder.
	AppendNulls(count int)

	// Build returns the constructed Array. After calling Build, the builder is
	// reset to an initial state and can be reused.
	BuildArray() Array
}

A Builder assists with constructing arrays. Builder implementations typically have more methods.

type Column

type Column struct {
	Name string // Name of the column.
}

A Column describes a single column in a RecordBatch.

type Datum

type Datum interface {

	// Kind returns the Kind of value represented by the Datum.
	Kind() Kind
	// contains filtered or unexported methods
}

A Datum is a piece of data which either represents an Array or a Scalar.

type Kind

type Kind uint8

A Kind represents the specific type of an Array. The zero Kind is a null value.

const (
	KindNull   Kind = iota // KindNull represents a null value.
	KindBool               // KindBool represents a boolean value.
	KindInt64              // KindInt64 represents a 64-bit integer value.
	KindUint64             // KindUint64 represents a 64-bit unsigned integer value.
	KindUTF8               // KindUTF8 represents a UTF-8 encoded string value.
)

func (Kind) String

func (k Kind) String() string

String returns the string representation of k.

type Null

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

Null is an Array of null values.

func NewNull

func NewNull(validity memory.Bitmap) *Null

NewNull creates a new Null array with the given validity bitmap. NewNull panics if validity contains any bit set to true.

func (*Null) IsNull

func (arr *Null) IsNull(_ int) bool

IsNull returns true for all values in the array.

func (*Null) Kind

func (arr *Null) Kind() Kind

Kind returns KindNull.

func (*Null) Len

func (arr *Null) Len() int

Len returns the number of values in the array.

func (*Null) Nulls

func (arr *Null) Nulls() int

Nulls returns the number of values in the array.

func (*Null) Size

func (arr *Null) Size() int

Size returns the size in bytes of the array's buffers.

func (*Null) Slice

func (arr *Null) Slice(i, j int) Array

Slice returns a slice of arr from i to j.

func (*Null) Validity

func (arr *Null) Validity() memory.Bitmap

Validity returns arr's validity bitmap.

type NullBuilder

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

A NullBuilder assists with constructing a Null array. A NullBuilder must be constructed by calling NewNullBuilder.

func NewNullBuilder

func NewNullBuilder(alloc *memory.Allocator) *NullBuilder

NewNullBuilder creates a new NullBuilder for constructing a Null array.

func (*NullBuilder) AppendNull

func (b *NullBuilder) AppendNull()

AppendNull adds a new null element to b.

func (*NullBuilder) AppendNulls

func (b *NullBuilder) AppendNulls(count int)

AppendNulls appends the given number of null elements to b.

func (*NullBuilder) Build

func (b *NullBuilder) Build() *Null

Build returns the constructed Null array. After calling Build, the builder is reset to an initial state.

func (*NullBuilder) BuildArray

func (b *NullBuilder) BuildArray() Array

Build returns the constructed array. After calling Build, the builder is reset to an initial state.

func (*NullBuilder) Grow

func (b *NullBuilder) Grow(n int)

Grow increases b's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to b without another allocation. If n is negative or too large to allocate the memory, Grow panics.

type NullScalar

type NullScalar struct{}

NullScalar is a Scalar representing an untyped null value.

func (*NullScalar) IsNull

func (s *NullScalar) IsNull() bool

IsNull implements Scalar and returns true.

func (*NullScalar) Kind

func (s *NullScalar) Kind() Kind

Kind implements Datum and returns KindNull.

type Number

type Number[T Numeric] struct {
	// contains filtered or unexported fields
}

Number is an Array of 64-bit unsigned Numeric values.

func NewNumber

func NewNumber[T Numeric](values []T, validity memory.Bitmap) *Number[T]

NewNumber creates a new Number array from the given values and optional validity bitmap.

Number arrays made from memory owned by a memory.Allocator are invalidated when the allocator reclaims memory.

If validity is of length zero, all elements are considered valid. Otherwise, NewNumber panics if the number of elements does not match the length of validity.

func (*Number[T]) Get

func (arr *Number[T]) Get(i int) T

Get returns the value at index i. If the element at index i is null, Get returns an undefined value.

Get panics if i is out of range.

func (*Number[T]) IsNull

func (arr *Number[T]) IsNull(i int) bool

IsNull returns true if the element at index i is null.

func (*Number[T]) Kind

func (arr *Number[T]) Kind() Kind

Kind returns the kind of Array being represented.

func (*Number[T]) Len

func (arr *Number[T]) Len() int

Len returns the total number of elements in the array.

func (*Number[T]) Nulls

func (arr *Number[T]) Nulls() int

Nulls returns the number of null elements in the array. The number of non-null elements can be calculated from Len() - Nulls().

func (*Number[T]) Size

func (arr *Number[T]) Size() int

Size returns the size in bytes of the array's buffers.

func (*Number[T]) Slice

func (arr *Number[T]) Slice(i, j int) Array

Slice returns a slice of arr from i to j.

func (*Number[T]) Validity

func (arr *Number[T]) Validity() memory.Bitmap

Validity returns the validity bitmap of the array. The returned bitmap may be of length 0 if there are no nulls.

A value of 1 in the Validity bitmap indicates that the corresponding element at that position is valid (not null).

func (*Number[T]) Values

func (arr *Number[T]) Values() []T

Values returns the underlying array of values.

type NumberBuilder

type NumberBuilder[T Numeric] struct {
	// contains filtered or unexported fields
}

A NumberBuilder assists with constructing a Number array. A NumberBuilder must be constructed by calling NewNumberBuilder.

func NewNumberBuilder

func NewNumberBuilder[T Numeric](alloc *memory.Allocator) *NumberBuilder[T]

NewNumberBuilder creates a new NumberBuilder for constructing a Number array.

func (*NumberBuilder[T]) AppendNull

func (b *NumberBuilder[T]) AppendNull()

AppendNull adds a new null element to b.

func (*NumberBuilder[T]) AppendNulls

func (b *NumberBuilder[T]) AppendNulls(count int)

AppendNulls appends the given number of null elements to b.

func (*NumberBuilder[T]) AppendValue

func (b *NumberBuilder[T]) AppendValue(v T)

AppendValue adds a new element to b.

func (*NumberBuilder[T]) AppendValueCount

func (b *NumberBuilder[T]) AppendValueCount(v T, count int)

AppendValue adds a new element to b.

func (*NumberBuilder[T]) Build

func (b *NumberBuilder[T]) Build() *Number[T]

Build returns the constructed Number array. After calling Build, the builder is reset to an initial state.

func (*NumberBuilder[T]) BuildArray

func (b *NumberBuilder[T]) BuildArray() Array

BuildArray returns the constructed array. After calling Build, the builder is reset to an initial state.

func (*NumberBuilder[T]) Grow

func (b *NumberBuilder[T]) Grow(n int)

Grow increases b's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to b without another allocation. If n is negative or too large to allocate the memory, Grow panics.

type NumberScalar

type NumberScalar[T Numeric] struct {
	Value T    // Value of the scalar.
	Null  bool // True if the scalar is null.
	// contains filtered or unexported fields
}

NumberScalar is a Scalar representing a Numeric value.

func (*NumberScalar[T]) IsNull

func (s *NumberScalar[T]) IsNull() bool

IsNull implements Scalar and returns s.Null.

func (*NumberScalar[T]) Kind

func (s *NumberScalar[T]) Kind() Kind

Kind implements Datum and returns the kind matching T.

type Numeric

type Numeric interface{ int64 | uint64 }

Numeric is a constraint for recognized numeric types.

type RecordBatch

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

RecordBatch is a collection of equal-length arrays. This corresponds to the RecordBatch concept in the Arrow specification.

func NewRecordBatch

func NewRecordBatch(schema *Schema, nrows int64, arrs []Array) *RecordBatch

NewRecordBatch returns a new RecordBatch created from the provided arrays. nrows specifies the total number of rows in the batch.

func (*RecordBatch) Column

func (rb *RecordBatch) Column(i int64) Array

Column returns the column array at index i.

func (*RecordBatch) NumCols

func (rb *RecordBatch) NumCols() int64

NumCols returns the number of columns in the batch.

func (*RecordBatch) NumRows

func (rb *RecordBatch) NumRows() int64

NumRows returns the number of rows in the batch.

func (*RecordBatch) Schema

func (rb *RecordBatch) Schema() *Schema

Schema returns the schema of the record batch.

func (*RecordBatch) Slice

func (rb *RecordBatch) Slice(i, j int) *RecordBatch

Slice returns a slice of rb from index i to j. The returned slice has a length of j-i and shares memory with rb.

Slice panics if the following invariant is not met: 0 <= i <= j <= rb.NumRows()

type Scalar

type Scalar interface {
	Datum

	// IsNull returns true if the scalar is a null value. IsNull can return true
	// for non-null data types.
	IsNull() bool
	// contains filtered or unexported methods
}

A Scalar is a single element of a specific data type.

type Schema

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

A Schema describes the set of columns in a RecordBatch.

func NewSchema

func NewSchema(columns []Column) *Schema

NewSchema creates a new schema from a list of columns. Column names must be unique. If column names are not unique, NewSchema panics.

func (*Schema) Column

func (s *Schema) Column(i int) Column

Column returns the column at index i. Column panics if i is out of bounds.

func (*Schema) ColumnIndex

func (s *Schema) ColumnIndex(name string) (Column, int)

ColumnIndex returns the column with the given name, along with its index. ColumnIndex returns the Column{}, -1 if the column doesn't exist.

func (*Schema) NumColumns

func (s *Schema) NumColumns() int

NumColumns returns the number of columns in the schema.

type Set

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

Set represents a unique set of values of a given kind. A Set must be constructed using the New functions in order to ensure that the set is properly initialized and all values in the set are of the correct Kind.

func NewNumberSet

func NewNumberSet[T Numeric](values ...T) *Set

NewNumberSet creates a new Set of a numeric Kind T initialized with the provided values. The Numeric type T will map to the following Kinds: - int64 -> KindInt64 - uint64 -> KindUint64

func NewUTF8Set

func NewUTF8Set(values ...string) *Set

NewUTF8Set creates a new Set of Kind UTF8 initialized with the provided strings.

func (*Set) Has

func (s *Set) Has(value any) bool

Has returns true if the set contains the given value. The value must be in the original form provided to the New functions e.g. a string rather than a []byte.

func (*Set) Kind

func (s *Set) Kind() Kind

Kind returns the kind of the values stored in the set.

type UTF8

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

UTF8 is an Array of UTF-8 encoded strings.

func NewUTF8

func NewUTF8(data []byte, offsets []int32, validity memory.Bitmap) *UTF8

NewUTF8 creates a new UTF8 array from the given data, offsets, and optional validity bitmap.

UTF8 arrays made from memory owned by a memory.Allocator are invalidated when the allocator reclaims memory.

Each UTF-8 string goes from data[offsets[i] : offsets[i+1]]. Offsets must be monotonically increasing, even for null values. The offsets slice is not validated for correctness.

If validity is of length zero, all elements are considered valid. Otherwise, NewUTF8 panics if the number of elements does not match the length of validity.

func (*UTF8) Data

func (arr *UTF8) Data() []byte

Data returns the underlying packed UTF8 bytes. If arr is a slice, Data may include bytes beyond the offset ranges from the original array.

func (*UTF8) DataLen

func (arr *UTF8) DataLen() int

DataLen returns the total length of the data in the array.

func (*UTF8) Get

func (arr *UTF8) Get(i int) []byte

Get returns the value at index i. If the element at index i is null, Get returns an empty string.

Get panics if i is out of range.

func (*UTF8) IsNull

func (arr *UTF8) IsNull(i int) bool

IsNull returns true if the element at index i is null.

func (*UTF8) Kind

func (arr *UTF8) Kind() Kind

Kind returns the kind of Array being represented.

func (*UTF8) Len

func (arr *UTF8) Len() int

Len returns the total number of elements in the array.

func (*UTF8) Nulls

func (arr *UTF8) Nulls() int

Nulls returns the number of null elements in the array. The number of non-null elements can be calculated from Len() - Nulls().

func (*UTF8) Offsets

func (arr *UTF8) Offsets() []int32

Offsets returns the underlying offsets array.

func (*UTF8) Size

func (arr *UTF8) Size() int

Size returns the size in bytes of the array's buffers.

func (*UTF8) Slice

func (arr *UTF8) Slice(i, j int) Array

Slice returns a slice of arr from i to j.

A sliced UTF8 array creates a slice of the offsets and validity buffers, but not the data buffer, permitting the offsets to continue to be valid without needing to normalize them to start at 0.

It is recommended to normalize the offsets and remove unused memory in the data buffer before serializing UTF8 for network communication.

func (*UTF8) Validity

func (arr *UTF8) Validity() memory.Bitmap

Validity returns the validity bitmap of the array. The returned bitmap may be of length 0 if there are no nulls.

A value of 1 in the Validity bitmap indicates that the corresponding element at that position is valid (not null).

type UTF8Builder

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

A UTF8Builder assists with constructing a UTF8 array. A UTF8Builder must be constructed by calling NewUTF8Builder.

func NewUTF8Builder

func NewUTF8Builder(alloc *memory.Allocator) *UTF8Builder

NewUTF8Builder creates a new UTF8Builder for constructing a UTF8 array.

func (*UTF8Builder) AppendNull

func (b *UTF8Builder) AppendNull()

AppendNull adds a new null element to b.

func (*UTF8Builder) AppendNulls

func (b *UTF8Builder) AppendNulls(count int)

AppendNulls appends the given number of null elements to b.

func (*UTF8Builder) AppendValue

func (b *UTF8Builder) AppendValue(v []byte)

AppendValue adds a new non-null element to b.

func (*UTF8Builder) Build

func (b *UTF8Builder) Build() *UTF8

Build returns the constructed UTF8 array. After calling Build, the builder is reset to an initial state.

func (*UTF8Builder) BuildArray

func (b *UTF8Builder) BuildArray() Array

BuildArray returns the constructed array. After calling Build, the builder is reset to an initial state.

func (*UTF8Builder) Grow

func (b *UTF8Builder) Grow(n int)

Grow increases b's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to b without another allocation. If n is negative or too large to allocate the memory, Grow panics.

func (*UTF8Builder) GrowData

func (b *UTF8Builder) GrowData(n int)

GrowData increases b's bytes capacity, if necessary, to guarantee space for another n bytes of data. After GrowData(n), at least n bytes can be appended to b (across all UTF8 values) without another allocation. If n is negative or too large to allocate the memory, GrowData panics.

GrowData only impacts capacity of string data. Use UTF8Builder.Grow to reserve space for more elements.

type UTF8Scalar

type UTF8Scalar struct {
	Value []byte // Value of the scalar.
	Null  bool   // True if the scalar is null.
}

UTF8Scalar is a Scalar representing a UTF8 value.

func (*UTF8Scalar) IsNull

func (s *UTF8Scalar) IsNull() bool

IsNull implements Scalar and returns s.Null.

func (*UTF8Scalar) Kind

func (s *UTF8Scalar) Kind() Kind

Kind implements Datum and returns KindUTF8.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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