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 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 Expr
- type Insert
- type IsNull
- type Limit
- type Not
- type OnConflict
- 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
- type Values
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 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 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 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 Insert ¶
type Insert struct {
Target RelationRef
Columns []string
Tuples []Tuple
Conflict *OnConflict
}
Insert is the relational assignment for create. Tuples supply the rows.
type OnConflict ¶
OnConflict configures insert-on-conflict behavior.
type Op ¶
type Op string
Op is a whitelisted comparison operator. User input never reaches SQL as a raw operator string, preventing injection.
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.
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.