Documentation
¶
Overview ¶
Package expr provides a typed expression engine for evaluating SQL expressions against record batches. It replaces the string-based expression parsing with a compiled expression tree built from the SQL parser AST.
Index ¶
- Constants
- Variables
- func FilterPredicate(e Expr) func(b *batch.RecordBatch, row int) bool
- func IntArithOn() bool
- func IsCompileRefusal(err error) bool
- func IsInvalidLiteral(err error) bool
- func IsUnknownFunc(err error) bool
- func RegisterFunc(name string, fn ScalarFunc, ret Ret)
- func ToFloat64(v any) float64
- func ToInt64(v any) int64
- type And
- type ArrayLitExpr
- type Between
- type BinOp
- type BinOpFloat64
- type BinOpInt64
- type BinOpNumeric
- type BoolExpr
- type BoolNullExpr
- type Case
- type CaseWhen
- type Cast
- type Cmp
- type CmpFloat64
- type CmpInt64
- type CmpNetworkLit
- type CmpOp
- type CmpTemporalLit
- type Coalesce
- type ColEmptyStr
- type ColIsNull
- type ColRef
- func (e *ColRef) Eval(b *batch.RecordBatch, row int) any
- func (e *ColRef) EvalFloat64(b *batch.RecordBatch, row int) (float64, bool)
- func (e *ColRef) EvalFloat64Vec(b *batch.RecordBatch, dst []float64, n int) bool
- func (e *ColRef) EvalInt64(b *batch.RecordBatch, row int) (int64, bool)
- func (e *ColRef) EvalString(b *batch.RecordBatch, row int) (string, bool)
- type ColShapeLen
- func (e *ColShapeLen) Eval(b *batch.RecordBatch, row int) any
- func (e *ColShapeLen) EvalFloat64(b *batch.RecordBatch, row int) (float64, bool)
- func (e *ColShapeLen) EvalFloat64Vec(b *batch.RecordBatch, dst []float64, n int) bool
- func (e *ColShapeLen) EvalInt64(b *batch.RecordBatch, row int) (int64, bool)
- func (e *ColShapeLen) EvalVec(b *batch.RecordBatch, out *batch.Vector, n int)
- type Confidence
- type CorrelatedExistsSubquery
- type CorrelatedInSubquery
- type CorrelatedScalarSubquery
- type ExistsSubquery
- type Expr
- func Compile(node plansql.Node) (Expr, error)
- func CompileSelectExpr(expr plansql.Node, alias string) (Expr, string, error)
- func CompileWithFullScope(node plansql.Node, runner SubqueryRunner, outerTables map[string]bool, ...) (Expr, error)
- func CompileWithRunner(node plansql.Node, runner SubqueryRunner) (Expr, error)
- func CompileWithScope(node plansql.Node, runner SubqueryRunner, outerTables map[string]bool) (Expr, error)
- func CompileWithScopeResolver(node plansql.Node, runner SubqueryRunner, outerTables map[string]bool, ...) (Expr, error)
- type Float64Expr
- type FuncCall
- type FuncRegistry
- func (r *FuncRegistry) Has(name string) bool
- func (r *FuncRegistry) Lookup(name string) ScalarFunc
- func (r *FuncRegistry) LookupVec(name string) VecScalarFunc
- func (r *FuncRegistry) Names() []string
- func (r *FuncRegistry) Register(name string, fn ScalarFunc, ret Ret)
- func (r *FuncRegistry) RegisterVec(name string, fn VecScalarFunc)
- func (r *FuncRegistry) RegisterVecReturn(name string, dimFn func() int)
- func (r *FuncRegistry) ReturnType(name string) Ret
- func (r *FuncRegistry) Unregister(name string) bool
- func (r *FuncRegistry) VecReturnDim(name string) (dim int, ok bool)
- type In
- type InSubquery
- type Int64Expr
- type IntervalValue
- type InvalidLiteralError
- type IsBool
- type IsDistinctFrom
- type IsNull
- type Like
- type Lit
- type MissingOuterColumnError
- type Not
- type Or
- type ParamRef
- type Ret
- type ScalarFunc
- type ScalarSubquery
- type SubqueryRunner
- type UDFCall
- type UDFDef
- type UDFPersister
- type UDFStore
- func (s *UDFStore) CompileUDFCall(name string, argExprs []Expr) (Expr, error)
- func (s *UDFStore) Get(name string) (UDFDef, bool)
- func (s *UDFStore) List() []UDFDef
- func (s *UDFStore) LoadDefs(defs []UDFDef) int
- func (s *UDFStore) Register(def UDFDef, isAdmin bool) error
- func (s *UDFStore) SetPersister(p UDFPersister)
- func (s *UDFStore) Unregister(name, caller string, isAdmin bool) error
- type UnaryOp
- type UnknownFuncError
- type VecExpr
- type VecFloat64Expr
- type VecScalarFunc
Constants ¶
const ( // SessionUser is the user name reported by current_user / session_user / // user / current_role. SessionUser = "wadjet" // SessionCatalog is the database name reported by current_catalog / // current_database(). SessionCatalog = "wadjet" // SessionSchema is the schema reported by current_schema. SessionSchema = "public" // ServerVersion is the answer to version(). PostgreSQL drivers parse the // leading "PostgreSQL <major>" to decide which protocol features and // catalog queries they may use, so the string keeps that prefix. ServerVersion = "PostgreSQL 15.0 (Wadjet analytical query engine)" )
--- Session / catalog information functions ---
PostgreSQL clients (pgJDBC, DataGrip, psql, Superset) open a connection by asking who and where they are: current_user, current_schema, current_database. These are answered here rather than only in the pgwire introspection layer so that a query mixing them with real columns — or selecting three of them at once — executes as an ordinary query with an ordinary result shape.
The values are server constants. ScalarFunc is func([]any) any and DefaultRegistry is process-global, so a scalar function cannot see the calling connection's identity; a per-session answer would need a context-carrying evaluation path that does not exist. The constants match what pgwire reports for an unauthenticated session.
Variables ¶
var ( RetBool = Ret{/* contains filtered or unexported fields */} RetInt64 = Ret{/* contains filtered or unexported fields */} RetFloat64 = Ret{/* contains filtered or unexported fields */} RetString = Ret{/* contains filtered or unexported fields */} RetBytes = Ret{/* contains filtered or unexported fields */} RetArray = Ret{/* contains filtered or unexported fields */} RetMap = Ret{/* contains filtered or unexported fields */} RetTimestamp = Ret{/* contains filtered or unexported fields */} // RetVector is embed()'s declaration. The output *dimension* is a // separate, deliberately dynamic answer the registry already carried // before this type existed — see RegisterVecReturn / VecReturnDim. RetVector = Ret{/* contains filtered or unexported fields */} // RetDynamic declares that only the value knows: element_at returns the // element type of its argument, json_extract whatever the document held. // The planner keeps its own fallback for these. It is an explicit // declaration, not an omission — a function whose vec kernel writes a // typed slice must never carry it. RetDynamic = Ret{/* contains filtered or unexported fields */} )
The fixed declarations. These name the type the function's Go results are stored as, not the type SQL calls them: the date/time functions below return formatted strings, so they declare RetString.
var DefaultRegistry = NewFuncRegistry()
DefaultRegistry is the global function registry used by the expression engine.
var DefaultUDFs = NewUDFStore()
DefaultUDFs is the global UDF store.
Functions ¶
func FilterPredicate ¶
func FilterPredicate(e Expr) func(b *batch.RecordBatch, row int) bool
FilterPredicate compiles a boolean expression into the per-row predicate a filter loop calls, resolving the evaluation protocol ONCE here rather than on every row.
A WHERE admits only TRUE, so its answer is always the two-valued collapse `val && !null`. For the comparisons and set predicates that collapse IS their EvalBool — a wrapper whose whole body is a call to EvalBoolNull and a drop of the second result, which #370 left behind when it made the three-valued form the definition. Reached through an interface the wrapper is a call frame the compiler cannot remove, so the row loop pays it on every row; taking EvalBoolNull directly here answers identically with the frame gone.
The connectives stay on EvalBool deliberately. And/Or collapse each operand BEFORE the operator, so they stop at an UNKNOWN left operand where the three-valued form must evaluate the right one to tell FALSE from UNKNOWN. Same answer either way (Kleene min/max agrees with the collapse), but different work — and a right operand that raises, `1/0`, would start raising. IS NULL / IS TRUE are excluded for the mirror reason: there EvalBool is the definition and EvalBoolNull is the wrapper.
func IntArithOn ¶
func IntArithOn() bool
IntArithOn exposes the toggle to the planner: projection output types may only declare Int64 for arithmetic when the runtime will actually take the integer path (see inferProjectionTypeCols).
func IsCompileRefusal ¶ added in v0.18.2
IsCompileRefusal reports whether err is a compile failure that the caller must PROPAGATE rather than fall back around.
The physical planner has six sites that compile an AST and quietly keep going when it will not compile, because a failed compile usually means "this expression is really a reference to an aggregate's output column". Two classes of failure are never that, and both are the answer to the query: a name nothing implements (#341) and a literal that names no value of its type (#505). Naming them together here keeps the six sites from drifting apart as a third class arrives.
func IsInvalidLiteral ¶ added in v0.18.2
IsInvalidLiteral reports whether err is, or wraps, an InvalidLiteralError.
func IsUnknownFunc ¶
IsUnknownFunc reports whether err is, or wraps, an UnknownFuncError.
func RegisterFunc ¶
func RegisterFunc(name string, fn ScalarFunc, ret Ret)
RegisterFunc registers a custom scalar function in the default registry. ret declares what the function returns; see Ret.
Types ¶
type And ¶
type And struct {
Left, Right Expr
}
And is a logical AND.
func (*And) EvalBoolNull ¶
EvalBoolNull: FALSE AND anything is FALSE; otherwise a NULL operand makes it UNKNOWN. Short-circuits on a FALSE left operand.
type ArrayLitExpr ¶
type ArrayLitExpr struct {
Elements []Expr
}
ArrayLitExpr evaluates to a []any containing the evaluated elements.
func (*ArrayLitExpr) Eval ¶
func (e *ArrayLitExpr) Eval(b *batch.RecordBatch, row int) any
type Between ¶
Between checks if a value is between two bounds.
func NewBetween ¶ added in v0.18.1
NewBetween builds a range test, binding the DECIMAL-column-against- numeric-literals shape.
func (*Between) EvalBoolNull ¶
EvalBoolNull: BETWEEN is defined as (x >= lo AND x <= hi), so a NULL bound does not force UNKNOWN — the other half can still answer FALSE (`5 BETWEEN NULL AND 2` is false, and NOT BETWEEN flips it to true).
type BinOpFloat64 ¶
type BinOpFloat64 struct {
Left, Right Float64Expr
Op string
// contains filtered or unexported fields
}
BinOpFloat64 is a typed binary op that operates on float64 without boxing. Uses a pre-resolved arithOp opcode for the hot EvalFloat64 path to avoid per-row string comparison on the Op field. The opcode is resolved lazily so external callers can construct BinOpFloat64 directly with only Op populated.
opReady is a double-checked atomic flag rather than sync.Once: Once.Do builds a closure and loads the done flag on EVERY row, and that closure keeps resolveOpCode too big to inline. This form is small enough that the compiler inlines resolveOpCode straight into EvalFloat64 (verified with -gcflags='-m'), the same guard BinOpNumeric.resolveMode and ColRef.resolve already use. opReady publishes opCode: set last under opMu, read first (and alone) by EvalFloat64 — concurrent pipeline workers share one *BinOpFloat64 through a captured closure, same as those two.
func (*BinOpFloat64) CloneVec ¶
func (e *BinOpFloat64) CloneVec() *BinOpFloat64
CloneVec creates a deep copy of the BinOpFloat64 tree with fresh scratch buffers. Required for parallel pipeline execution where multiple workers must not share mutable vecBuf state. Stateless leaf nodes (ColRef, Literal) are shared; only BinOpFloat64 nodes (which own vecBuf) are cloned.
func (*BinOpFloat64) Eval ¶
func (e *BinOpFloat64) Eval(b *batch.RecordBatch, row int) any
func (*BinOpFloat64) EvalFloat64 ¶
func (e *BinOpFloat64) EvalFloat64(b *batch.RecordBatch, row int) (float64, bool)
func (*BinOpFloat64) EvalFloat64Vec ¶
func (e *BinOpFloat64) EvalFloat64Vec(b *batch.RecordBatch, dst []float64, n int) bool
EvalFloat64Vec evaluates left and right operands in bulk, then applies the arithmetic op in a tight loop. Eliminates ~5 function calls per row.
type BinOpInt64 ¶
type BinOpInt64 struct {
Left, Right Int64Expr
Op string
// contains filtered or unexported fields
}
BinOpInt64 is a typed binary op that operates on int64 without boxing. opCode is resolved lazily through the same double-checked atomic.Bool guard as BinOpFloat64 — see that type for why sync.Once doesn't fit the inliner and this does.
func (*BinOpInt64) Eval ¶
func (e *BinOpInt64) Eval(b *batch.RecordBatch, row int) any
func (*BinOpInt64) EvalFloat64 ¶
func (e *BinOpInt64) EvalFloat64(b *batch.RecordBatch, row int) (float64, bool)
EvalFloat64 allows BinOpInt64 to be used as Float64Expr (int→float promotion).
func (*BinOpInt64) EvalInt64 ¶
func (e *BinOpInt64) EvalInt64(b *batch.RecordBatch, row int) (int64, bool)
type BinOpNumeric ¶
type BinOpNumeric struct {
Left, Right numericOperand
Op string
// contains filtered or unexported fields
}
BinOpNumeric is the mode-resolved arithmetic node.
func (*BinOpNumeric) Eval ¶
func (e *BinOpNumeric) Eval(b *batch.RecordBatch, row int) any
func (*BinOpNumeric) EvalFloat64 ¶
func (e *BinOpNumeric) EvalFloat64(b *batch.RecordBatch, row int) (float64, bool)
EvalFloat64 implements Float64Expr for consumers on the float protocol.
func (*BinOpNumeric) EvalInt64 ¶
func (e *BinOpNumeric) EvalInt64(b *batch.RecordBatch, row int) (int64, bool)
EvalInt64 implements Int64Expr. Only meaningful in int mode; float mode reports not-ok so callers fall back to EvalFloat64/Eval.
type BoolExpr ¶
type BoolExpr interface {
EvalBool(b *batch.RecordBatch, row int) bool
}
BoolExpr evaluates a boolean expression (used for WHERE/HAVING/JOIN conditions). SQL's logic is THREE-valued and EvalBool is the two-valued COLLAPSE a filtering context applies: it answers true only for TRUE — FALSE and UNKNOWN rows are both kept out of a WHERE. The third value is carried by BoolNullExpr, and the two protocols must agree: EvalBool ≡ (val && !null) of EvalBoolNull.
type BoolNullExpr ¶
type BoolNullExpr interface {
EvalBoolNull(b *batch.RecordBatch, row int) (val, null bool)
}
BoolNullExpr is the three-valued boolean protocol (#370): val is the answer and null reports UNKNOWN, in which case val is meaningless. Every boolean operator implements it — it is what lets NOT distinguish UNKNOWN (stays UNKNOWN, row excluded) from FALSE (becomes TRUE, row kept), and what a projection boxes into SQL NULL.
type Case ¶
type Case struct {
Operand Expr // optional: CASE <operand> WHEN ...
Whens []CaseWhen // WHEN condition THEN result
Else Expr // optional ELSE clause
// contains filtered or unexported fields
}
Case is a CASE WHEN ... THEN ... ELSE ... END expression.
type CaseWhen ¶
type CaseWhen struct {
Cond Expr // the condition (or value to compare against operand)
Result Expr
}
CaseWhen is a single WHEN clause in a CASE expression.
type Cmp ¶
Cmp is a comparison expression.
func NewCmp ¶ added in v0.18.1
NewCmp builds a comparison, binding the two operand shapes that cannot be answered from the boxed values: a DECIMAL column against a numeric literal, and two DECIMAL columns against each other.
func (*Cmp) EvalBoolNull ¶
type CmpFloat64 ¶
type CmpFloat64 struct {
Left, Right Float64Expr
Op CmpOp
}
CmpFloat64 is a typed comparison that operates on float64 without boxing.
func (*CmpFloat64) Eval ¶
func (e *CmpFloat64) Eval(b *batch.RecordBatch, row int) any
func (*CmpFloat64) EvalBool ¶
func (e *CmpFloat64) EvalBool(b *batch.RecordBatch, row int) bool
func (*CmpFloat64) EvalBoolNull ¶
func (e *CmpFloat64) EvalBoolNull(b *batch.RecordBatch, row int) (bool, bool)
EvalBoolNull: a not-ok typed operand is a NULL (the operands here are provably float-typed at compile time), so the comparison is UNKNOWN.
type CmpInt64 ¶
CmpInt64 is a typed comparison that operates on int64 without boxing.
func (*CmpInt64) EvalBoolNull ¶
EvalBoolNull: a not-ok typed operand is a NULL (the operands here are provably int-typed at compile time), so the comparison is UNKNOWN.
type CmpNetworkLit ¶ added in v0.18.1
type CmpNetworkLit struct {
Col *ColRef
Lit string // original literal text (generic-fallback operand)
Op CmpOp
Flip bool // literal was the LEFT operand: evaluate as (lit OP col)
// contains filtered or unexported fields
}
CmpNetworkLit compares a bare column against a string literal that parses as an IPv4 address, a MAC address, an IPv6 address, or a CIDR network, without per-row parsing or boxing — CmpTemporalLit's counterpart for network types, and for the same reason: ColRef.Eval boxes a TypeIPv4/ TypeMAC column as its raw encoded int64 (the representation arithmetic and column-to-column ordering comparisons depend on — see networkTextFuncs) and a TypeIPv6/TypeCIDR column as rendered TEXT (Vector.GetValue's default case), so `ip_col = '10.0.0.1'` boxed the column as a decimal digit string and the literal as itself, and `ipv6_col < '2001:db8::10'` boxed the column's address as text and compared it LEXICALLY against the literal — neither is the address's own order (issues found via README verification and #492). Column type is unknown at compile time, so the literal is pre-parsed into every encoding here and the right one picked per batch from the column's resolved type; a non-network column (or a network column whose type doesn't match the literal's parse) delegates to the generic compare() with the original operand order, keeping semantics bit-identical with Cmp in every sub-case — matching CmpTemporalLit's own contract. Comparing the pre-parsed encodings (not as formatted strings) is also what keeps ordering (<, >) correct: IPv4's big-endian uint32, MAC's packed 48 bits, and IPv6's raw 16 bytes all sort the same as the address itself, and CIDR's structural key (kernel.CidrSortKey) sorts the same as PostgreSQL's inet order — where a dotted-quad, colon-hex, or CIDR-notation STRING would sort lexically and disagree with it (e.g. "9.0.0.1" > "10.0.0.1" as text).
func (*CmpNetworkLit) Eval ¶ added in v0.18.1
func (e *CmpNetworkLit) Eval(b *batch.RecordBatch, row int) any
func (*CmpNetworkLit) EvalBool ¶ added in v0.18.1
func (e *CmpNetworkLit) EvalBool(b *batch.RecordBatch, row int) bool
func (*CmpNetworkLit) EvalBoolNull ¶ added in v0.18.1
func (e *CmpNetworkLit) EvalBoolNull(b *batch.RecordBatch, row int) (bool, bool)
type CmpTemporalLit ¶
type CmpTemporalLit struct {
Col *ColRef
Lit string // original literal text (generic-fallback operand)
Op CmpOp
Flip bool // literal was the LEFT operand: evaluate as (lit OP col)
// contains filtered or unexported fields
}
CmpTemporalLit compares a bare column against a string literal that parses as a date/timestamp, without per-row parsing, cache lookups, or boxing — the generic path spent 3.2% of SF100 worker CPU inside the date-parse memo's sync.Map.Load (interface-key hashing dominated; 2026-07-25 re-rank). The literal is parsed once into BOTH temporal units at compile time; the unit is chosen from the column's resolved type per batch. Every non-fast sub-case (non-temporal column, the epoch-zero literal guard) delegates to the generic compare() with the original operand order, keeping semantics bit-identical with Cmp.
func (*CmpTemporalLit) Eval ¶
func (e *CmpTemporalLit) Eval(b *batch.RecordBatch, row int) any
func (*CmpTemporalLit) EvalBool ¶
func (e *CmpTemporalLit) EvalBool(b *batch.RecordBatch, row int) bool
func (*CmpTemporalLit) EvalBoolNull ¶
func (e *CmpTemporalLit) EvalBoolNull(b *batch.RecordBatch, row int) (bool, bool)
type ColEmptyStr ¶
ColEmptyStr evaluates a column compared for equality or inequality against the empty string literal as a zero-length offsets test. Restricted to TypeString: that is the only type for which the generic Cmp path compares ColRef.Eval's boxed string against "" (a TypeBytes column boxes []byte, which compare() handles through a different branch, and the network/UUID types render their bytes).
NULL handling matches Cmp exactly: a NULL operand makes the comparison UNKNOWN for BOTH = and <> — nil on the boxed path, excluded by EvalBool.
func (*ColEmptyStr) Eval ¶
func (e *ColEmptyStr) Eval(b *batch.RecordBatch, row int) any
func (*ColEmptyStr) EvalBool ¶
func (e *ColEmptyStr) EvalBool(b *batch.RecordBatch, row int) bool
func (*ColEmptyStr) EvalBoolNull ¶
func (e *ColEmptyStr) EvalBoolNull(b *batch.RecordBatch, row int) (bool, bool)
type ColIsNull ¶
ColIsNull evaluates `col IS [NOT] NULL` off the null bitmap for a byte-array column, where the generic IsNull node boxes (and for TypeString copies) the value only to test it against nil.
Scoped to TypeString/TypeBytes deliberately: ColRef.Eval returns nil for exactly the null rows of those two types (TypeString via GetString's ok flag, TypeBytes via GetValue's leading null check), so the rewrite is value-identical. Other types keep the generic node.
func (*ColIsNull) EvalBoolNull ¶
EvalBoolNull: IS [NOT] NULL never answers UNKNOWN.
type ColRef ¶
type ColRef struct {
Name string
// contains filtered or unexported fields
}
ColRef reads a column value from the batch. Caches the column index and type after first resolution for zero-allocation reads on numeric types. Parallel pipeline workers share one *ColRef through the captured expression closures, so the resolution writes are published under a lock and read behind resolved.
func (*ColRef) EvalFloat64 ¶
EvalFloat64 reads the column value as float64 without any boxing. Returns (0, false) if null or column not found. Uses cached column type to dispatch directly to the typed data slice, avoiding the extra function call and redundant type switch in GetNumericFloat64.
func (*ColRef) EvalFloat64Vec ¶
EvalFloat64Vec evaluates the column for all rows [0, n) into dst.
func (*ColRef) EvalString ¶
EvalString reads the column value as string without boxing.
type ColShapeLen ¶
type ColShapeLen struct {
Col *ColRef
Mul int // 1 for length/octet_length, 8 for bit_length
Fallback *FuncCall
}
ColShapeLen evaluates length()/octet_length()/bit_length() over a bare column reference by subtracting offsets, never materializing the value. Any column whose stored bytes are not the value ColRef.Eval would box (numeric, temporal, network-rendered, ROW field access) delegates to the generic FuncCall it replaced, so results are unchanged.
func (*ColShapeLen) Eval ¶
func (e *ColShapeLen) Eval(b *batch.RecordBatch, row int) any
func (*ColShapeLen) EvalFloat64 ¶
func (e *ColShapeLen) EvalFloat64(b *batch.RecordBatch, row int) (float64, bool)
func (*ColShapeLen) EvalFloat64Vec ¶
func (e *ColShapeLen) EvalFloat64Vec(b *batch.RecordBatch, dst []float64, n int) bool
EvalFloat64Vec fills dst for rows [0, n), reporting whether any row was null (the VecFloat64Expr contract: the caller re-runs EvalFloat64 per row to set the null bits when this returns true).
func (*ColShapeLen) EvalInt64 ¶
func (e *ColShapeLen) EvalInt64(b *batch.RecordBatch, row int) (int64, bool)
func (*ColShapeLen) EvalVec ¶
func (e *ColShapeLen) EvalVec(b *batch.RecordBatch, out *batch.Vector, n int)
EvalVec fills out for the whole batch. Mirrors FuncCall.EvalVec's contract: writes Float64Data, marking nulls in out.Nulls.
type Confidence ¶
type Confidence uint8
Confidence says how a resolved type was arrived at: whether the declaration DECIDED it or only GUESSED it. A same-as-argument declaration has to answer even when none of its candidate arguments decided anything, and that answer — its fallback — is a guess. Reporting a guess as fact is what typed
SELECT COALESCE(NULLIF(n_name, 'ALGERIA'), 'fallback') FROM nation
Float64, so every row came back as the integer 0: nullif's argument 0 is a bare column, which decides nothing by design (its type comes from the input schema at runtime), so nullif fell back to its numeric default — and coalesce took that for a decision, stopped, and never consulted the string literal in argument 1 that would have decided it correctly (#331).
The fallback itself is right where there is nothing better: NULLIF(int_col, 1) as a projection is numeric and stays numeric. What Confidence adds is that a caller holding another candidate can tell the two apart.
const ( // Undecided: nothing here names a type, and the caller keeps its own // fallback. RetDynamic answers this way, as does an unregistered name. Undecided Confidence = iota // Guessed: a polymorphic declaration reached its fallback because no // candidate argument decided. Still an answer — it is THE answer at top // level — but a caller with a candidate of its own left to ask must // prefer that candidate's decision over this. Guessed // Decided: the declaration names this type outright, or a candidate // argument decided it. Decided )
func (Confidence) String ¶
func (c Confidence) String() string
type CorrelatedExistsSubquery ¶
type CorrelatedExistsSubquery struct {
}
CorrelatedExistsSubquery evaluates a correlated EXISTS subquery per-row.
func (*CorrelatedExistsSubquery) Eval ¶
func (e *CorrelatedExistsSubquery) Eval(b *batch.RecordBatch, row int) any
func (*CorrelatedExistsSubquery) EvalBool ¶
func (e *CorrelatedExistsSubquery) EvalBool(b *batch.RecordBatch, row int) bool
type CorrelatedInSubquery ¶
type CorrelatedInSubquery struct {
}
CorrelatedInSubquery checks if a value is in the result set of a correlated subquery.
func (*CorrelatedInSubquery) Eval ¶
func (e *CorrelatedInSubquery) Eval(b *batch.RecordBatch, row int) any
func (*CorrelatedInSubquery) EvalBool ¶
func (e *CorrelatedInSubquery) EvalBool(b *batch.RecordBatch, row int) bool
func (*CorrelatedInSubquery) EvalBoolNull ¶
func (e *CorrelatedInSubquery) EvalBoolNull(b *batch.RecordBatch, row int) (bool, bool)
EvalBoolNull carries SQL's three-valued IN (#370): a NULL probe is UNKNOWN, and a miss against a result set containing a NULL is UNKNOWN — the NOT IN trap, same rule as the uncorrelated InSubquery.
type CorrelatedScalarSubquery ¶
type CorrelatedScalarSubquery struct {
}
CorrelatedScalarSubquery evaluates a correlated scalar subquery per-row. Unlike ScalarSubquery, it cannot cache the result because the inner query depends on values from the outer row.
func (*CorrelatedScalarSubquery) Eval ¶
func (e *CorrelatedScalarSubquery) Eval(b *batch.RecordBatch, row int) any
type ExistsSubquery ¶
type ExistsSubquery struct {
SQL string
Runner SubqueryRunner
Not bool
// contains filtered or unexported fields
}
ExistsSubquery evaluates to true if a subquery returns any rows. Example: WHERE EXISTS (SELECT 1 FROM orders WHERE orders.user_id = users.id) Uncorrelated: executed once and result cached.
func (*ExistsSubquery) Eval ¶
func (e *ExistsSubquery) Eval(b *batch.RecordBatch, row int) any
func (*ExistsSubquery) EvalBool ¶
func (e *ExistsSubquery) EvalBool(_ *batch.RecordBatch, _ int) bool
type Expr ¶
type Expr interface {
Eval(b *batch.RecordBatch, row int) any
}
Expr evaluates an expression against a record batch row, returning a typed value.
func CompileSelectExpr ¶
CompileSelectExpr compiles a SELECT column expression from our AST. Returns the compiled expression and the output column name.
func CompileWithFullScope ¶
func CompileWithFullScope(node plansql.Node, runner SubqueryRunner, outerTables map[string]bool, outerCols map[string]string) (Expr, error)
CompileWithFullScope is like CompileWithScope but also accepts a column-to-table mapping for resolving unqualified column references in correlated subqueries.
func CompileWithRunner ¶
func CompileWithRunner(node plansql.Node, runner SubqueryRunner) (Expr, error)
CompileWithRunner converts our AST Node into an Expr tree, with support for subquery expressions (scalar subqueries, IN subquery, EXISTS).
func CompileWithScope ¶
func CompileWithScope(node plansql.Node, runner SubqueryRunner, outerTables map[string]bool) (Expr, error)
CompileWithScope converts our AST Node into an Expr tree with full scope information, enabling correlated subquery detection and per-row execution. outerTables contains the table names and aliases from the outer query.
func CompileWithScopeResolver ¶
func CompileWithScopeResolver(node plansql.Node, runner SubqueryRunner, outerTables map[string]bool, outerCols map[string]string, innerCols plansql.TableColumns) (Expr, error)
CompileWithScopeResolver is CompileWithFullScope plus a resolver for the column namespace of a subquery's own FROM clause. It is what makes an unqualified name inside a subquery bind to the subquery first, so a name that merely also exists in the outer query does not turn an uncorrelated subquery into a per-row correlated one (issue #334). A nil resolver keeps the weaker table-identifier heuristic.
type Float64Expr ¶
type Float64Expr interface {
EvalFloat64(b *batch.RecordBatch, row int) (float64, bool)
}
Float64Expr evaluates to float64 without boxing.
type FuncCall ¶
FuncCall represents a scalar function call.
Note: this struct holds NO per-call mutable state. A previous version cached an args buffer on the receiver to avoid per-call allocation, but that was unsafe under parallel pipeline execution: aggPreProject closures (and other wrapped-expression paths) capture the same *FuncCall by pointer rather than cloning it per worker, so concurrent goroutines stomped on the shared args buffer and produced non-deterministic Q02 row counts at SF0.01 (and worse at SF100). The vecFn / prepared lookup caches are still guarded by sync.Once (resolved once per BATCH, off EvalVec — not once per row, so the guard never sat in a row loop). fn is different: it is resolved off Eval, the per-row entry point every one of the 273 scalar functions reaches, so it uses the same double-checked atomic.Bool guard as BinOpFloat64/BinOpInt64's opCode and BinOpNumeric's mode — small enough that resolveFn inlines into Eval (verified with -gcflags='-m'), where sync.Once.Do's closure-plus-load did not.
type FuncRegistry ¶
type FuncRegistry struct {
// contains filtered or unexported fields
}
FuncRegistry is a concurrent-safe registry of scalar functions.
func NewFuncRegistry ¶
func NewFuncRegistry() *FuncRegistry
NewFuncRegistry creates a new empty function registry.
func (*FuncRegistry) Has ¶
func (r *FuncRegistry) Has(name string) bool
Has returns true if a function with the given name exists.
func (*FuncRegistry) Lookup ¶
func (r *FuncRegistry) Lookup(name string) ScalarFunc
Lookup returns the function with the given name, or nil if not found.
func (*FuncRegistry) LookupVec ¶
func (r *FuncRegistry) LookupVec(name string) VecScalarFunc
LookupVec returns the vectorized function with the given name, or nil if not found.
func (*FuncRegistry) Names ¶
func (r *FuncRegistry) Names() []string
Names returns all registered function names.
func (*FuncRegistry) Register ¶
func (r *FuncRegistry) Register(name string, fn ScalarFunc, ret Ret)
Register adds or replaces a scalar function. ret declares the type the function's results are stored as; the planner types projections from it (see Ret). Registering without a declaration does not compile, and registering the zero value panics here rather than letting a mistyped output vector reach a kernel.
func (*FuncRegistry) RegisterVec ¶
func (r *FuncRegistry) RegisterVec(name string, fn VecScalarFunc)
RegisterVec adds a vectorized implementation for a scalar function. A vec kernel writes a typed slice of the output vector, so the function it accelerates must already be registered with the return type that names that slice — registering a kernel for an undeclared function is the exact setup that panicked the server four times, and panics here instead.
func (*FuncRegistry) RegisterVecReturn ¶
func (r *FuncRegistry) RegisterVecReturn(name string, dimFn func() int)
RegisterVecReturn marks a function as returning a VECTOR. dimFn is evaluated lazily (at plan time) to obtain the output dimensionality — embed(), for example, derives it from the configured embedding provider.
func (*FuncRegistry) ReturnType ¶
func (r *FuncRegistry) ReturnType(name string) Ret
ReturnType returns the declared return type of a function. An unregistered name yields the zero Ret, which reports Declared() == false and resolves to "caller keeps its fallback".
func (*FuncRegistry) Unregister ¶
func (r *FuncRegistry) Unregister(name string) bool
Unregister removes a scalar function. Returns true if it existed.
func (*FuncRegistry) VecReturnDim ¶
func (r *FuncRegistry) VecReturnDim(name string) (dim int, ok bool)
VecReturnDim reports whether the named function returns a VECTOR and, if so, its current output dimension. ok is false for non-vector-returning functions.
type In ¶
In checks if a value is in a set.
func NewIn ¶ added in v0.18.1
NewIn builds a set-membership test, binding the DECIMAL-column-against- numeric-literals shape.
func (*In) EvalBoolNull ¶
EvalBoolNull: `x IN (a, b, NULL)` is the chained OR of comparisons, so a match answers TRUE, and a miss with a NULL anywhere in the list is UNKNOWN — never FALSE. NOT IN is its Kleene negation, which is why `1 NOT IN (2, NULL)` must not answer true: PostgreSQL's reading is "I don't know, so no" (#370).
type InSubquery ¶
type InSubquery struct {
Expr Expr
SQL string
Runner SubqueryRunner
Not bool
// contains filtered or unexported fields
}
InSubquery checks if a value is in the result set of a subquery. Example: WHERE user_id IN (SELECT user_id FROM active_users) Uncorrelated: executed once and result set cached in a hash set for O(1) lookup.
func (*InSubquery) Eval ¶
func (e *InSubquery) Eval(b *batch.RecordBatch, row int) any
func (*InSubquery) EvalBool ¶
func (e *InSubquery) EvalBool(b *batch.RecordBatch, row int) bool
func (*InSubquery) EvalBoolNull ¶
func (e *InSubquery) EvalBoolNull(b *batch.RecordBatch, row int) (bool, bool)
type Int64Expr ¶
type Int64Expr interface {
EvalInt64(b *batch.RecordBatch, row int) (int64, bool)
}
Int64Expr evaluates to int64 without boxing.
type IntervalValue ¶
IntervalValue represents a SQL INTERVAL (e.g., INTERVAL '30' DAY).
type InvalidLiteralError ¶ added in v0.18.2
type InvalidLiteralError struct {
Input string // the literal's source text
DestType string // the type it was being read as, e.g. "numeric"
}
InvalidLiteralError names a constant the compiler refused outright: a literal that cannot be read as a value of the type its context demands.
It is a DISTINCT TYPE for exactly the reason UnknownFuncError is one. The physical planner's compile sites are forgiving by design — a projection whose AST will not compile falls back to copying an input column of the same name — and that fallback is right for every compile failure EXCEPT the ones that are the answer. A refused literal has no column to fall back to, so swallowing it turned `SELECT -'abc'` into `column "-'abc'" does not exist`, which sends the reader hunting a name-resolution bug for a perfectly well-diagnosed 22P02 (#505 review finding). Callers test for it with errors.As — IsCompileRefusal below — and propagate.
func (*InvalidLiteralError) Error ¶ added in v0.18.2
func (e *InvalidLiteralError) Error() string
func (*InvalidLiteralError) SQLState ¶ added in v0.18.2
func (e *InvalidLiteralError) SQLState() string
SQLState returns PostgreSQL's invalid_text_representation code, the same one raiseInvalidTextRepresentation raises for the per-row version of this refusal. sqlerr.StateOf picks it up through the Coder interface.
type IsBool ¶
IsBool is `x IS [NOT] TRUE/FALSE`. Distinct from Cmp because it is a NULL-test like IS NULL, not a comparison: NULL IS TRUE answers FALSE and NULL IS NOT TRUE answers TRUE, where a comparison against NULL would be UNKNOWN (#370 — the Cmp spelling was right only while Cmp itself had no UNKNOWN).
func (*IsBool) EvalBoolNull ¶
type IsDistinctFrom ¶
type IsDistinctFrom struct {
Left, Right Expr
Not bool // true for IS NOT DISTINCT FROM
// contains filtered or unexported fields
}
IsDistinctFrom implements PostgreSQL's NULL-safe (in)equality, IS [NOT] DISTINCT FROM (#374). Unlike Cmp, it never answers UNKNOWN: NULL participates as a value here rather than propagating, so two NULLs are NOT DISTINCT (equal) and a NULL against a non-NULL value IS DISTINCT. "NULL IS DISTINCT FROM NULL" is FALSE, never NULL — the one case a COALESCE-based workaround gets wrong for a real sentinel value.
func (*IsDistinctFrom) Eval ¶
func (e *IsDistinctFrom) Eval(b *batch.RecordBatch, row int) any
func (*IsDistinctFrom) EvalBool ¶
func (e *IsDistinctFrom) EvalBool(b *batch.RecordBatch, row int) bool
func (*IsDistinctFrom) EvalBoolNull ¶
func (e *IsDistinctFrom) EvalBoolNull(b *batch.RecordBatch, row int) (bool, bool)
EvalBoolNull always reports null=false: DISTINCT FROM is total over NULL inputs, which is the entire point of the operator.
type IsNull ¶
IsNull checks if an expression is null.
func (*IsNull) EvalBoolNull ¶
EvalBoolNull: IS [NOT] NULL never answers UNKNOWN — it is the operator SQL provides to ASK about NULL.
type Like ¶
Like performs SQL LIKE pattern matching.
func (*Like) EvalBoolNull ¶
EvalBoolNull: LIKE with NULL on either side is UNKNOWN, and NOT LIKE stays UNKNOWN with it (#370).
type Lit ¶
type Lit struct {
Val any
// Text is the numeric literal's source text, kept verbatim. Val is the
// literal boxed for arithmetic — an int64 where one is exact, a float64
// otherwise — and a float64 carries ~15-16 significant decimal digits
// where a DECIMAL(38,10) column carries 38, so the box alone cannot say
// which number was written (#452). Comparisons against a DECIMAL column
// read this instead and compare in the column's own domain; everything
// else keeps reading Val and is unchanged. Empty for a non-numeric
// literal.
Text string
}
Lit returns a constant value.
func (*Lit) EvalFloat64 ¶
func (*Lit) EvalFloat64Vec ¶
EvalFloat64Vec fills dst[0:n] with the literal value.
type MissingOuterColumnError ¶
MissingOuterColumnError reports a correlated subquery whose outer column is absent from the batch the outer query hands it — a planning defect (column pruning, projection, or a rename), not a data condition.
func (*MissingOuterColumnError) Error ¶
func (e *MissingOuterColumnError) Error() string
func (*MissingOuterColumnError) FatalEvalError ¶
func (e *MissingOuterColumnError) FatalEvalError() error
FatalEvalError satisfies the marker the pipeline drivers recover on. Expr's Eval/EvalBool have no error return, so a failure that must not be mistaken for a NULL travels as a panic carrying this value and is turned back into a query error at the pipeline boundary (see exec.FatalEvalPanic).
type Not ¶
type Not struct {
Operand Expr
}
Not is a logical NOT.
func (*Not) EvalBool ¶
func (e *Not) EvalBool(b *batch.RecordBatch, row int) bool
EvalBool: NOT must see the third value — collapsing first turned NOT (UNKNOWN) into true and admitted rows SQL excludes, which was the dangerous half of #370 (`1 NOT IN (2, NULL)` answering true).
func (*Not) EvalBoolNull ¶
EvalBoolNull: NOT UNKNOWN stays UNKNOWN.
type Or ¶
type Or struct {
Left, Right Expr
}
Or is a logical OR.
func (*Or) EvalBoolNull ¶
EvalBoolNull: TRUE OR anything is TRUE; otherwise a NULL operand makes it UNKNOWN. Short-circuits on a TRUE left operand.
type ParamRef ¶
type ParamRef struct {
Index int
// contains filtered or unexported fields
}
ParamRef is an expression node that references a UDF parameter by index.
type Ret ¶
type Ret struct {
// contains filtered or unexported fields
}
Ret is a scalar function's declared return type: the vector type its results can be stored in. It is declared where the function is registered, and the planner types a projection from the same declaration the kernel writes through.
Before this existed the two halves lived apart — a function was registered in this package while its return type was asserted by a hand-maintained name list in the physical planner (isNumericFunc). A function missing from that list was typed String, so the projection allocated a Bytes output vector and the function's vec kernel wrote Float64Data/BoolData off the end of a zero-length slice, killing the server process for every connection. That happened four times (temporal extractors, vector distances, the length family, and starts_with/contains/ends_with) before the list was replaced by this declaration (#310).
The zero value is *undeclared* and the registry refuses it: Register's signature makes a missing declaration a compile error, and the zero value makes a field-named literal that skips it a panic at init.
func RetSameAsArg ¶
RetSameAsArg declares a polymorphic return: the type of the first listed argument the caller can decide, or fallback when it can decide none of them — and Resolve marks that fallback as a guess, so a CALLER with candidates of its own keeps looking rather than inheriting it (see Confidence). With no indices every argument is a candidate, which is what coalesce, greatest and least want; nullif mirrors argument 0 only.
func RetTypeOf ¶
RetTypeOf builds a fixed declaration for a type without a named constant above. Kept for callers registering functions over the network-native types.
func (Ret) Declared ¶
Declared reports whether this is a real declaration rather than the zero value. Registration rejects an undeclared Ret.
func (Ret) Numeric ¶
Numeric reports whether the function always returns a number. It is the registry-backed replacement for the compiler's own hand-maintained numeric name list: a numeric call can be wrapped so it satisfies Float64Expr/ Int64Expr and binary operators over it take the typed path.
func (Ret) Resolve ¶
func (r Ret) Resolve(nargs int, argType func(i int) (batch.TypeID, Confidence)) (batch.TypeID, Confidence)
Resolve returns the output type for a call with nargs arguments, and how confidently. argType reports the type of argument i and how confidently the caller decided it; it is consulted only by polymorphic declarations and may be nil.
Undecided means the caller should keep its own fallback: the function is RetDynamic, or the name is not registered at all.
A polymorphic declaration takes the first candidate argument that DECIDED a type. A candidate that only guessed does not end the search — it is remembered, in preference order, and answered only if no later candidate decides. A guess stays a guess all the way up, so an argument that guessed at any depth never displaces an argument that knows.
type ScalarFunc ¶
ScalarFunc is a scalar function implementation.
type ScalarSubquery ¶
type ScalarSubquery struct {
SQL string
Runner SubqueryRunner
// contains filtered or unexported fields
}
ScalarSubquery evaluates a subquery that returns a single scalar value. Example: WHERE price > (SELECT AVG(price) FROM products) Uncorrelated: executed once and result cached.
The cache is shared by every parallel pipeline worker — one compiled expression tree is captured by all of them (Pipeline.runParallel) — so it is published the same way ColRef publishes its resolution: written under resolveMu, released by an atomic store, and never read before that store is observed. A plain `if !cached { cached = true; ... }` raced: a worker that saw the flag before the value was written compared against a nil threshold, dropped every row of its batches, and the query answered a different row count on every run (#398).
func (*ScalarSubquery) Eval ¶
func (e *ScalarSubquery) Eval(_ *batch.RecordBatch, _ int) any
type SubqueryRunner ¶
SubqueryRunner executes a SQL subquery and returns its result rows. Each row is a map of column name to value.
type UDFCall ¶
type UDFCall struct {
Name string
ArgExprs []Expr // caller-supplied argument expressions
Body Expr // compiled UDF body with ParamRef nodes
// contains filtered or unexported fields
}
UDFCall evaluates a user-defined function by binding arguments, then evaluating the compiled body expression.
type UDFDef ¶
type UDFDef struct {
Name string // function name (lowercase)
Params []string // parameter names (lowercase)
Body string // SQL expression body (e.g. "param1 * 2 + param2")
Owner string // who created this function (empty = system/unowned)
Locked bool // if true, only the owner (or admin) can modify/drop
}
UDFDef defines a user-defined function.
type UDFPersister ¶
UDFPersister is called after UDF register/unregister to persist the current state.
type UDFStore ¶
type UDFStore struct {
// contains filtered or unexported fields
}
UDFStore holds compiled UDF definitions for use by the expression engine. Thread-safe for concurrent reads and writes.
func (*UDFStore) CompileUDFCall ¶
CompileUDFCall creates a UDFCall expression node.
func (*UDFStore) LoadDefs ¶
LoadDefs registers pre-existing UDF definitions (e.g., from KV on startup). Skips compilation errors silently so one bad UDF doesn't block startup.
func (*UDFStore) SetPersister ¶
func (s *UDFStore) SetPersister(p UDFPersister)
SetPersister sets the function called after UDF mutations to persist state.
type UnknownFuncError ¶
type UnknownFuncError struct {
Name string
// Aggregate marks a name this engine recognizes as an aggregate from
// other SQL dialects but does not implement. The distinction matters to
// the reader: an unimplemented aggregate silently dropped the GROUP BY
// as well as the value, so the result had the wrong row COUNT.
Aggregate bool
}
UnknownFuncError names a function the registry cannot resolve.
It is a distinct type because the physical planner's compile sites are forgiving by design: a projection whose AST will not compile falls back to copying an input column of the same name, which is how an aggregate's output column reaches the projection. That fallback is right for every compile failure EXCEPT this one — a name nothing implements has no column to fall back to, so swallowing it converted "unknown function foo" into the far less actionable "column \"foo(x)\" does not exist in the input schema", or, before the check existed, into no message at all. Callers test for this type with errors.As and propagate rather than falling back.
func (*UnknownFuncError) Error ¶
func (e *UnknownFuncError) Error() string
func (*UnknownFuncError) SQLState ¶
func (e *UnknownFuncError) SQLState() string
SQLState returns PostgreSQL's undefined_function code. sqlerr.StateOf picks it up through the Coder interface so the wire reports 42883 rather than the blanket 42000 (#366).
type VecExpr ¶
type VecExpr interface {
EvalVec(b *batch.RecordBatch, out *batch.Vector, n int)
}
VecExpr evaluates an expression for an entire batch at once, writing results directly to the output vector. This avoids per-row interface dispatch and boxing.
type VecFloat64Expr ¶
type VecFloat64Expr interface {
EvalFloat64Vec(b *batch.RecordBatch, dst []float64, n int) bool
}
VecFloat64Expr evaluates an expression for all rows [0, n) at once, writing results to dst. Returns true if any output is null. Eliminates per-row function call overhead (~5 calls/row/expression).