kernel

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package kernel provides type-specialized vectorized operations for the query engine. Generic functions are monomorphized at compile time and dispatch is resolved once at query init time (not per-row), eliminating type-switch overhead from hot loops.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareDecimalAt

func CompareDecimalAt(a *batch.Vector, ai int, b *batch.Vector, bi int) int

CompareDecimalAt orders two DECIMAL values by NUMERIC value, which is what PostgreSQL's `numeric` ordering means and what every other comparator in this file already does for its type. Before this arm existed, DECIMAL fell through the three resolvers' defaults to a comparator that reports every row equal, so `ORDER BY dec_col` was a stable no-op that returned input order, and a sort-merge join on a DECIMAL key matched every row against every row. The other path in the tree — compareAny over Vector.GetValue — compares the FORMATTED string instead, where "10.001" sorts before "2.0002". Same query, three different sequences depending on which path answered (#394).

The comparison is EXACT at every scale. Equal scales compare the unscaled Int128s directly — that is every sort over one column, every sorted run and every k-way merge over runs. Unequal scales, reachable where two separately declared DECIMAL columns meet, rescale the smaller-scale operand by 10^(delta) and compare the unscaled integers; if that product overflows Int128 the two are compared as big.Int rather than approximated.

Exactness is not a nicety here: SortMergeJoin uses this comparator for key EQUALITY (sort_merge_join.go), so an approximate answer is a spurious JOIN MATCH. The float64 rescale this replaced held to 2^53 unscaled units and then started reporting 9007199254740993 and 9007199254740992.0 — which differ by one unscaled unit at the common scale — as the same key.

func DecimalRowCompare

func DecimalRowCompare(vec *batch.Vector, row int, literal string, op CompareOp) bool

DecimalRowCompare answers `vec[row] <op> literal` for a DECIMAL column, for the row-at-a-time predicate path. It shares decimalLiteralAt with the vectorized kernel deliberately: two comparison rules for the same predicate is exactly the shape #394 was filed for.

func UUIDLiteralToRaw

func UUIDLiteralToRaw(s string) string

UUIDLiteralToRaw is parseUUIDToRawString for the row-at-a-time predicate in package exec, so the two comparison paths convert the literal identically.

Types

type Accumulator

type Accumulator struct {
	SumI64    int64
	SumF64    float64
	SumDec    batch.Int128
	Count     int64
	MinI64    int64
	MaxI64    int64
	MinF64    float64
	MaxF64    float64
	MinDec    batch.Int128
	MaxDec    batch.Int128
	MinStr    string
	MaxStr    string
	HasMin    bool
	HasMax    bool
	IsFloat   bool // true when the source column is a float type (or AVG over int64, which accumulates in float64 to avoid int64 sum wraparound)
	IsDecimal bool // true when the source column is DECIMAL
	IsString  bool // true when the source column is a string/bytes type (MIN/MAX)
	DecScale  int  // scale for DECIMAL columns
}

Accumulator holds aggregate state with typed precision. Int64 sums stay int64 (no float64 precision loss); float sums use float64. Decimal sums use Int128 for exact fixed-point arithmetic.

func (*Accumulator) FinalAvg

func (a *Accumulator) FinalAvg() any

FinalAvg returns the accumulated average.

func (*Accumulator) FinalMax

func (a *Accumulator) FinalMax() any

FinalMax returns the accumulated maximum.

func (*Accumulator) FinalMin

func (a *Accumulator) FinalMin() any

FinalMin returns the accumulated minimum.

func (*Accumulator) FinalSum

func (a *Accumulator) FinalSum() any

FinalSum returns the accumulated sum as the appropriate type.

func (*Accumulator) Merge

func (a *Accumulator) Merge(other *Accumulator)

Merge combines another accumulator's state into this one. Used for parallel aggregation: each worker builds partial state, then merges.

type BatchAggKernel

type BatchAggKernel func(acc *Accumulator, vec *batch.Vector, sel []uint32, vecLen int)

BatchAggKernel processes an entire column (or selection) into an accumulator. Used for non-grouped aggregation or pre-aggregated groups.

func ResolveBatchAvg

func ResolveBatchAvg(typ batch.TypeID) BatchAggKernel

ResolveBatchAvg returns a batch-level kernel for AVG. Differs from ResolveBatchSum only for int64-class inputs (float64 accumulation).

func ResolveBatchCount

func ResolveBatchCount() BatchAggKernel

ResolveBatchCount returns a batch-level count kernel.

func ResolveBatchMax

func ResolveBatchMax(typ batch.TypeID) BatchAggKernel

ResolveBatchMax returns a batch-level max kernel for the given column type.

func ResolveBatchMin

func ResolveBatchMin(typ batch.TypeID) BatchAggKernel

ResolveBatchMin returns a batch-level min kernel for the given column type.

func ResolveBatchSum

func ResolveBatchSum(typ batch.TypeID) BatchAggKernel

ResolveBatchSum returns a batch-level sum kernel for the given column type.

type ColColFilterKernel

type ColColFilterKernel func(left, right *batch.Vector, sel []uint32, vecLen int, outSel []uint32) []uint32

ColColFilterKernel compares two columns element-wise, returning matching row indices.

func ResolveColColFilterKernel

func ResolveColColFilterKernel(typ batch.TypeID, op CompareOp) ColColFilterKernel

ResolveColColFilterKernel creates a ColColFilterKernel for comparing two columns of the given type. Returns nil if the type is not supported.

type CompareOp

type CompareOp int

CompareOp represents a comparison operation.

const (
	OpEq CompareOp = iota
	OpNe
	OpLt
	OpLe
	OpGt
	OpGe
)

type FilterKernel

type FilterKernel func(vec *batch.Vector, sel []uint32, vecLen int, outSel []uint32) []uint32

FilterKernel evaluates a column against a pre-resolved constant for all rows, returning the indices of matching rows.

func ResolveFilterKernel

func ResolveFilterKernel(typ batch.TypeID, op CompareOp, value any) FilterKernel

ResolveFilterKernel creates a FilterKernel for comparing a column of the given type against a constant value. The type dispatch happens once here; the returned function has no type switches in its inner loop.

func ResolveInFilterKernel

func ResolveInFilterKernel(typ batch.TypeID, values []any, negate bool) FilterKernel

ResolveInFilterKernel creates a FilterKernel that checks set membership. The set is built once; the inner loop does a hash lookup per element.

func ResolveLikeFilterKernel

func ResolveLikeFilterKernel(pattern string, negate bool) FilterKernel

ResolveLikeFilterKernel creates a FilterKernel for SQL LIKE pattern matching. Converts SQL LIKE patterns (% and _) to optimized matching functions.

type Numeric

type Numeric interface {
	~int32 | ~int64 | ~float32 | ~float64
}

Numeric constrains types that support arithmetic operations.

type Ordered

type Ordered interface {
	~int32 | ~int64 | ~float32 | ~float64 | ~string
}

Ordered constrains types that support comparison.

type RowAggUpdater

type RowAggUpdater func(acc *Accumulator, vec *batch.Vector, row int)

RowAggUpdater updates an accumulator for a single row (used in grouped aggregation). The type dispatch is resolved once; the function body has no type switches.

func ResolveRowAvg

func ResolveRowAvg(typ batch.TypeID) RowAggUpdater

ResolveRowAvg returns a row-level updater for AVG. Differs from ResolveRowSum only for int64-class inputs (float64 accumulation).

func ResolveRowAvgNoNulls

func ResolveRowAvgNoNulls(typ batch.TypeID) RowAggUpdater

ResolveRowAvgNoNulls is the no-null-check variant of ResolveRowAvg.

func ResolveRowCount

func ResolveRowCount(countStar bool) RowAggUpdater

ResolveRowCount returns a row-level count updater. If countStar is true, counts all rows (including nulls).

func ResolveRowMax

func ResolveRowMax(typ batch.TypeID) RowAggUpdater

ResolveRowMax returns a row-level max updater for the given column type.

func ResolveRowMaxNoNulls

func ResolveRowMaxNoNulls(typ batch.TypeID) RowAggUpdater

ResolveRowMaxNoNulls returns a no-null-check max updater.

func ResolveRowMin

func ResolveRowMin(typ batch.TypeID) RowAggUpdater

ResolveRowMin returns a row-level min updater for the given column type.

func ResolveRowMinNoNulls

func ResolveRowMinNoNulls(typ batch.TypeID) RowAggUpdater

ResolveRowMinNoNulls returns a no-null-check min updater.

func ResolveRowSum

func ResolveRowSum(typ batch.TypeID) RowAggUpdater

ResolveRowSum returns a row-level sum updater for the given column type.

func ResolveRowSumNoNulls

func ResolveRowSumNoNulls(typ batch.TypeID) RowAggUpdater

ResolveRowSumNoNulls returns a no-null-check sum updater.

type SortCompareKernel

type SortCompareKernel func(a *batch.Vector, ai int, b *batch.Vector, bi int) int

SortCompareKernel compares one row from vector a against one row from vector b. Returns -1, 0, or 1. Null handling is included.

func ResolveSortCompare

func ResolveSortCompare(typ batch.TypeID) SortCompareKernel

ResolveSortCompare returns a comparison function for the given column type. The returned function has no type switch — the type is baked into the closure.

func ResolveSortCompareNoNulls

func ResolveSortCompareNoNulls(typ batch.TypeID) SortCompareKernel

ResolveSortCompareNoNulls returns a sort compare function that skips null checks.

func ResolveSortCompareNullsLast

func ResolveSortCompareNullsLast(typ batch.TypeID) SortCompareKernel

ResolveSortCompareNullsLast returns a sort compare function with NULLS LAST ordering.

Jump to

Keyboard shortcuts

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