Documentation
¶
Overview ¶
Package relalg defines a dialect-neutral relational algebra intermediate representation (IR). DML tools construct Expr trees (logical plans); the codegen package renders them into dialect-specific SQL.
The IR is relationally complete for safe read queries (selection, projection, aggregation, sort, limit, distinct) and DML (insert/update/delete over a predicate). Join and union are intentionally absent until needed — the surface stays deterministic, inject-proof, and gateable, excluding NL2SQL.
Expr and Predicate are sealed interfaces: their marker methods (rel, pred) are unexported, so external packages cannot add nodes. This keeps the algebra closed and the codegen exhaustively matchable.
Index ¶
- Constants
- Variables
- func HasExists(p Predicate) bool
- func ValidatePredicate(p Predicate) error
- func ValidatePredicateWithMaxINCardinality(p Predicate, maxIN int) error
- type AggCall
- type AggFunc
- type Aggregate
- type And
- type Call
- type Condition
- type Delete
- type Distinct
- type Exists
- type Expr
- type Having
- type Insert
- type Join
- type JoinPair
- type Limit
- type Not
- type Op
- type Or
- type OrderTerm
- type Predicate
- type Project
- type ProjectItem
- type RelationRef
- type Scan
- type Select
- type SetItem
- type Sort
- type Tuple
- type Update
Constants ¶
const DefaultMaxINCardinality = 1000
DefaultMaxINCardinality is the safe default used by ValidatePredicate.
Variables ¶
var ErrINCardinality = errors.New("relalg: invalid IN cardinality")
ErrINCardinality is returned when an IN-list is empty, malformed, or exceeds the configured bound.
var ErrInvalidAggFunc = errors.New("relalg: invalid aggregate function")
ErrInvalidAggFunc is returned when an aggregate uses a function outside the whitelist.
var ErrInvalidOp = errors.New("relalg: invalid operator")
ErrInvalidOp is returned when a predicate uses an operator outside the whitelist.
var ErrInvalidPredicate = errors.New("relalg: invalid predicate")
ErrInvalidPredicate is returned when a known predicate has an empty or otherwise malformed shape.
var ErrUnknownPredicate = errors.New("relalg: unknown predicate")
ErrUnknownPredicate is returned when ValidatePredicate encounters a predicate type it does not recognize.
Functions ¶
func ValidatePredicate ¶
ValidatePredicate recursively checks that every Condition uses a whitelisted operator. It returns nil for a nil predicate. Codegen calls this before rendering so malformed input fails early rather than producing bad SQL.
func ValidatePredicateWithMaxINCardinality ¶
ValidatePredicateWithMaxINCardinality validates a predicate and bounds every IN/NOT IN list. maxIN must be positive; zero never means unbounded.
Types ¶
type AggCall ¶
type AggCall struct {
Func AggFunc
Field string
Distinct bool
// Name is the output column the call is known by above the γ node: a
// τ or a Having names it (or a group-by field). The SQL never aliases
// it: the call itself is rendered where it is named.
Name string
}
AggCall is one aggregate in a γ node.
type AggFunc ¶
type AggFunc string
AggFunc is a whitelisted aggregate function name. Like Op, a user-supplied value never reaches SQL as a raw function string, preventing injection.
type Call ¶
type Call struct {
Procedure RelationRef
Args []any
}
Call invokes a stored procedure with positional arguments.
type Delete ¶
type Delete struct {
Target RelationRef
Predicate Predicate
}
Delete is the relational assignment for delete, constrained by a predicate.
type Distinct ¶
type Distinct struct {
Input Expr
}
Distinct applies δ (deduplication) to its input.
type Exists ¶ added in v0.1.18
type Exists struct {
Relation RelationRef
On []JoinPair
Predicate Predicate
}
Marker methods seal the Predicate interface. Exists holds when a row of Relation matches the row it is evaluated on (On: the row's Left column equals Relation's Right one) and Predicate, which names Relation's columns unqualified.
type Expr ¶
type Expr interface {
// contains filtered or unexported methods
}
Expr is a relational algebra expression (logical plan node). Sealed: external packages cannot implement it.
type Having ¶ added in v0.1.18
Having keeps the groups of an aggregate whose predicate is TRUE. Its conditions name the aggregate's output: a group-by field or an AggCall's Name.
type Insert ¶
type Insert struct {
Target RelationRef
Columns []string
Tuples []Tuple
}
Insert is the relational assignment for create. Tuples supply the rows.
type Join ¶ added in v0.1.18
type Join struct {
Input Expr
Right RelationRef
Alias string
On []JoinPair
RightPredicate Predicate
}
Join extends each row of its input with the row of Right that matches On and RightPredicate, or with NULLs when none does: a left outer join of a relation matching at most one row (On names a key of Right). Right's columns are named Alias.column above it; RightPredicate (Right's row policy) names them unqualified.
type JoinPair ¶ added in v0.1.18
type JoinPair struct {
Left, Right string
}
JoinPair is one equality of a join: a column of the left side and one of the right.
type Op ¶
type Op string
Op is a whitelisted comparison operator. User input never reaches SQL as a raw operator string, preventing injection.
type OrderTerm ¶
type OrderTerm struct {
Field string
Dir string
// Nullable marks a field that may hold NULL. NULLs sort last whatever
// the direction; a term not nullable is rendered without placing them
// (the database's default), which keeps an index usable for the order.
Nullable bool
}
OrderTerm is one column in a τ node. Dir is "asc" or "desc".
type Predicate ¶
type Predicate interface {
// contains filtered or unexported methods
}
Predicate is a parameterized boolean expression tree. Values bind to args at codegen time, never string-interpolated, preventing injection. Sealed.
type Project ¶
type Project struct {
Input Expr
Items []ProjectItem
}
Project applies π (column picking / renaming) to its input.
type ProjectItem ¶
ProjectItem is one column in a projection: the source field and an optional alias.
func ProjectFields ¶ added in v0.1.13
func ProjectFields(fields []string) []ProjectItem
ProjectFields is the projection of fields, each as it is named.
type RelationRef ¶
RelationRef names a base relation (table/view/procedure) by schema and name.
type Scan ¶
type Scan struct {
Relation RelationRef
Alias string
}
Scan is a base relation reference.
type Update ¶
type Update struct {
Target RelationRef
Predicate Predicate
Set []SetItem
}
Update is the relational assignment for update, constrained by a predicate.