helper

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package aggrt holds the aggregator-runtime types and helpers shared between the internal Aggregator wrapper, the window sub-package, and per-aggregate function implementations. It is a leaf package: it depends on internal/value but is depended upon by internal/ and by internal/function/window/.

Index

Constants

This section is empty.

Variables

View Source
var QuarterStartMonths = []time.Month{time.January, time.April, time.July, time.October}
View Source
var WeekPartToOffset = map[string]int{
	"WEEK":           0,
	"WEEK_MONDAY":    1,
	"WEEK_TUESDAY":   2,
	"WEEK_WEDNESDAY": 3,
	"WEEK_THURSDAY":  4,
	"WEEK_FRIDAY":    5,
	"WEEK_SATURDAY":  6,
}

Functions

func AddMonth

func AddMonth(t time.Time, m int) time.Time

func AddYear

func AddYear(t time.Time, y int) time.Time

func BucketFloor

func BucketFloor(target, origin time.Time, iv *value.IntervalValue) (time.Time, error)

BucketFloor computes the inclusive lower bound of the fixed-width bucket — anchored at origin — that contains target. Used by DATE_BUCKET / DATETIME_BUCKET / TIMESTAMP_BUCKET.

The interval must be a single-part value (any of YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, NANOSECOND, or a single calendar- week of 7 days). Mixed month + sub-month parts are rejected because month width is calendar-dependent and would change per-bucket.

func DISTINCT

func DISTINCT() (value.Value, error)

DISTINCT, IGNORE_NULLS, LIMIT, ORDER_BY emit the encoded marker values that the analyzer-rewritten SQL passes alongside the real aggregate arguments.

func ExistsNull

func ExistsNull(args []value.Value) bool

ExistsNull reports whether any of the args is the runtime NULL (a nil value.Value). Per-spec helpers that handle their own arity can use this to short-circuit NULL propagation.

func FormatTime

func FormatTime(formatStr string, t *time.Time, typ TimeFormatType) (string, error)

func IGNORE_NULLS

func IGNORE_NULLS() (value.Value, error)

func LIMIT

func LIMIT(limit int64) (value.Value, error)

func ORDER_BY

func ORDER_BY(v value.Value, isAsc bool) (value.Value, error)

func ParseTimeFormat

func ParseTimeFormat(formatStr, targetStr string, typ TimeFormatType) (*time.Time, error)

func SafeByte added in v0.2.0

func SafeByte(v int64) (byte, error)

SafeByte converts a 64-bit integer to byte, returning an error when v falls outside the [0, 255] range.

func SafeInt added in v0.2.0

func SafeInt(v int64) (int, error)

SafeInt converts a 64-bit integer to int. It returns an error when v falls outside the 32-bit range, which is the minimum width the Go specification guarantees for int. Callers use the result as a slice index, length, count, or a date/time component; none of those can legitimately exceed that range, so an out-of-range value is a genuine error rather than a silently truncated conversion.

func SafeInt32 added in v0.2.0

func SafeInt32(v int64) (int32, error)

SafeInt32 converts a 64-bit integer to int32, returning an error when v overflows the INT32 range.

func SafeUint32 added in v0.2.0

func SafeUint32(v int64) (uint32, error)

SafeUint32 converts a 64-bit integer to uint32, returning an error when v falls outside the [0, 4294967295] range.

Types

type Aggregator

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

Aggregator is the SQLite-side wrapper that drives a per-spec aggregate (Step / Done shape) over a single GROUP. SQLite hands each row's args to Step; we strip the encoded option markers (DISTINCT / IGNORE_NULLS / LIMIT / ORDER_BY), thread the resulting Option through to the per-spec step function, and finally encode the Done result back into the over-the-wire form.

Living in the leaf aggrt package alongside Option keeps it importable by every aggregate-family sub-package (aggregate, approx_aggregate, stat_aggregate, hll) without going through internal.

func NewAggregator

func NewAggregator(
	step func([]value.Value, *Option) error,
	done func() (value.Value, error),
) *Aggregator

NewAggregator wires up a fresh Aggregator with the given per-spec step / done callbacks. Returns the wrapper SQLite drives.

func (*Aggregator) Done

func (a *Aggregator) Done() (any, error)

func (*Aggregator) Step

func (a *Aggregator) Step(stepArgs ...any) error

type BindFunction

type BindFunction = func(...value.Value) (value.Value, error)

BindFunction is the signature ncruces' adapter expects from us: take an arg list of (possibly NULL) googlesqlite Values and return either a Value or an error.

func Scalar1

func Scalar1(fn func(value.Value) (value.Value, error)) BindFunction

Scalar1 wraps a 1-arg semantic function. Returns NULL when the single argument is NULL.

func Scalar1KeepNull

func Scalar1KeepNull(fn func(value.Value) (value.Value, error)) BindFunction

Scalar1KeepNull wraps a 1-arg semantic function that needs to observe NULL itself (e.g. IS_NULL).

func Scalar2

func Scalar2(fn func(a, b value.Value) (value.Value, error)) BindFunction

Scalar2 wraps a 2-arg semantic function. Returns NULL when either argument is NULL.

func Scalar2KeepNull

func Scalar2KeepNull(fn func(a, b value.Value) (value.Value, error)) BindFunction

Scalar2KeepNull wraps a 2-arg semantic function that needs to observe NULL itself (e.g. IS_DISTINCT_FROM).

func Scalar3

func Scalar3(fn func(a, b, c value.Value) (value.Value, error)) BindFunction

Scalar3 wraps a 3-arg semantic function. Returns NULL when any argument is NULL.

func ScalarN

func ScalarN(fn func(...value.Value) (value.Value, error)) BindFunction

ScalarN wraps an arbitrary-arity semantic function that should short-circuit when any argument is NULL.

func ScalarNKeepNull

func ScalarNKeepNull(fn func(...value.Value) (value.Value, error)) BindFunction

ScalarNKeepNull wraps a variadic semantic function that needs to observe NULLs itself (e.g. COALESCE).

type CombinationFormatTimeInfo

type CombinationFormatTimeInfo struct {
	AvailableTypes []TimeFormatType
	Parse          func([]rune, *time.Time) (int, error)
	Format         func(*time.Time) ([]rune, error)
}

func (*CombinationFormatTimeInfo) Available

func (i *CombinationFormatTimeInfo) Available(typ TimeFormatType) bool

type DayOfWeek

type DayOfWeek string
const (
	Sunday    DayOfWeek = "Sunday"
	Monday    DayOfWeek = "Monday"
	Tuesday   DayOfWeek = "Tuesday"
	Wednesday DayOfWeek = "Wednesday"
	Thursday  DayOfWeek = "Thursday"
	Friday    DayOfWeek = "Friday"
	Saturday  DayOfWeek = "Saturday"
)

type FormatTimeInfo

type FormatTimeInfo struct {
	AvailableTypes []TimeFormatType
	Parse          ParseFunction
	Format         func(*time.Time) ([]rune, error)
}

func (*FormatTimeInfo) Available

func (i *FormatTimeInfo) Available(typ TimeFormatType) bool

type FuncOption

type FuncOption struct {
	Type  FuncOptionType `json:"type"`
	Value any            `json:"value"`
}

FuncOption is one of the encoded option markers (DISTINCT / IGNORE_NULLS / LIMIT / ORDER_BY) that the analyzer wraps around aggregate / window argument lists. parseAggregateOptions strips them from the runtime arg list and merges them into an AggregatorOption.

func (*FuncOption) UnmarshalJSON

func (o *FuncOption) UnmarshalJSON(b []byte) error

type FuncOptionType

type FuncOptionType string
const (
	OptionUnknown     FuncOptionType = "aggregate_unknown"
	OptionDistinct    FuncOptionType = "aggregate_distinct"
	OptionLimit       FuncOptionType = "aggregate_limit"
	OptionOrderBy     FuncOptionType = "aggregate_order_by"
	OptionIgnoreNulls FuncOptionType = "aggregate_ignore_nulls"
)

type Month

type Month string
const (
	January   Month = "January"
	February  Month = "February"
	March     Month = "March"
	April     Month = "April"
	May       Month = "May"
	June      Month = "June"
	July      Month = "July"
	August    Month = "August"
	September Month = "September"
	October   Month = "October"
	November  Month = "November"
	December  Month = "December"
)

type Option

type Option struct {
	Distinct    bool
	IgnoreNulls bool
	Limit       *int64
	OrderBy     []*OrderBy
}

Option holds the aggregate-call-level flags parsed out of the FuncOption markers in a step's argument list.

func ParseOptions

func ParseOptions(args ...value.Value) ([]value.Value, *Option)

ParseOptions strips the encoded option markers from args and returns the cleaned arg list together with the resulting Option.

type OrderBy

type OrderBy struct {
	Value value.Value `json:"value"`
	IsAsc bool        `json:"isAsc"`
}

OrderBy is the per-arg ordering directive packed into an ORDER_BY(<value>, <isAsc>) marker. The Value field is decoded from its over-the-wire form by UnmarshalJSON below using the canonical value codec.

func (*OrderBy) UnmarshalJSON

func (a *OrderBy) UnmarshalJSON(b []byte) error

type OrderedValue

type OrderedValue struct {
	OrderBy []*OrderBy
	Value   value.Value
}

OrderedValue captures a single value plus the ORDER BY decoration that arrived with it. Aggregators that observe ORDER BY semantics (ARRAY_AGG, STRING_AGG, ARRAY_CONCAT_AGG, ...) buffer rows as OrderedValues and call SortAggregatedValues during Done.

func SortAggregatedValues

func SortAggregatedValues(values []*OrderedValue, opt *Option) []*OrderedValue

SortAggregatedValues stable-sorts the captured values per the ORDER BY directives carried inside each OrderedValue. The caller passes the surrounding Option so SortAggregatedValues can short- circuit when no ORDER BY was attached.

type ParseFunction

type ParseFunction func(text []rune, t *time.Time) (int, error)

type TimeFormatType

type TimeFormatType int
const (
	FormatTypeDate      TimeFormatType = 0
	FormatTypeDatetime  TimeFormatType = 1
	FormatTypeTime      TimeFormatType = 2
	FormatTypeTimestamp TimeFormatType = 3
)

func (TimeFormatType) String

func (t TimeFormatType) String() string

type TimeParserPostProcessor

type TimeParserPostProcessor struct {
	ShouldPostProcessResult func(map[rune][2]int) bool
	PostProcessResult       func([]rune, *time.Time)
}

Jump to

Keyboard shortcuts

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