logicalir

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package logicalir contains the experimental logical SQL IR and rewrite evaluation API. It is intentionally independent from the current optimizer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateLogicalQuery

func ValidateLogicalQuery(q *LogicalQuery) error

Types

type Aggregate

type Aggregate struct {
	Relation RelationID
	Input    RelationNode
	GroupBy  []Expr
	Items    []ProjectionExpr
}

func (*Aggregate) ID

func (a *Aggregate) ID() RelationID

func (*Aggregate) OutputSchema

func (a *Aggregate) OutputSchema() OutputSchema

type AnalysisCompleteness

type AnalysisCompleteness uint8
const (
	CompletenessComplete AnalysisCompleteness = iota
	CompletenessPartial
	CompletenessOverflow
)

type AnalysisSnapshot

type AnalysisSnapshot struct {
	Query      *LogicalQuery
	Rewrites   *RewriteEvaluation
	Catalog    *CatalogSnapshot
	Statistics *StatisticsSnapshot
	Explain    *ExplainSnapshot
	Cost       *CostSnapshot
}

func (*AnalysisSnapshot) Clone

func (s *AnalysisSnapshot) Clone() *AnalysisSnapshot

func (*AnalysisSnapshot) Validate

func (s *AnalysisSnapshot) Validate() error

type AttemptID

type AttemptID string

type AttemptStatus

type AttemptStatus uint8
const (
	AttemptComplete AttemptStatus = iota
	AttemptDegraded
	AttemptFailed
)

type BindingID

type BindingID string

type CatalogSnapshot

type CatalogSnapshot struct {
	Identity  string
	Relations map[RelationID]OutputSchema
}

type ColumnBinding

type ColumnBinding struct {
	ID            BindingID
	QueryBlock    QueryBlockID
	Relation      RelationID
	Column, Alias string
	Type          TypeInfo
	Nullable      Knowledge
}

type ConstantFoldingRule

type ConstantFoldingRule struct{}

ConstantFoldingRule is a concrete logical rewrite rule for predicate expressions. It only changes predicate expressions with a provable result.

func (ConstantFoldingRule) Apply

func (ConstantFoldingRule) Match

func (ConstantFoldingRule) Name

func (ConstantFoldingRule) Name() string

type CostComparison

type CostComparison struct {
	Baseline, Candidate float64
	Unit, Model         string
}

type CostSnapshot

type CostSnapshot struct {
	Identity    string
	Comparisons []CostComparison
}

type Determinism

type Determinism uint8
const (
	DeterminismUnknown Determinism = iota
	Deterministic
	NonDeterministic
)

type ExplainSnapshot

type ExplainSnapshot struct {
	Identity    string
	Annotations map[RelationID]string
}

type Expr

type Expr struct {
	Kind       ExprKind
	Name       string
	Binding    *BindingID
	Children   []Expr
	SourceSpan SourceSpan
}

func FoldConstants

func FoldConstants(e Expr) (Expr, bool)

FoldConstants folds boolean and simple numeric literal expressions when the result is fully determined by the IR. Unknown expressions are preserved.

type ExprKind

type ExprKind uint8
const (
	ExprUnknown ExprKind = iota
	ExprColumn
	ExprLiteral
	ExprCall
	ExprUnary
	ExprBinary
	ExprAnd
	ExprOr
)

type ExpressionChange

type ExpressionChange struct {
	PredicateID   PredicateID
	Before, After Expr
}

type Filter

type Filter struct {
	Relation   RelationID
	Input      RelationNode
	Predicates []PredicateID
}

func (*Filter) ID

func (f *Filter) ID() RelationID

func (*Filter) OutputSchema

func (f *Filter) OutputSchema() OutputSchema

type FilterDiff

type FilterDiff struct {
	Relation   RelationID
	Predicates []PredicateID
}

type HeuristicAssessment

type HeuristicAssessment struct {
	IsHeuristic  bool
	Completeness string
	Reasons      []string
}

type Join

type Join struct {
	Relation    RelationID
	Left, Right RelationNode
	JoinType    JoinType
	Conditions  []PredicateID
}

func (*Join) ID

func (j *Join) ID() RelationID

func (*Join) OutputSchema

func (j *Join) OutputSchema() OutputSchema

type JoinType

type JoinType uint8
const (
	InnerJoin JoinType = iota
	LeftJoin
	RightJoin
	FullJoin
)

type Knowledge

type Knowledge uint8
const (
	KnowledgeUnknown Knowledge = iota
	KnowledgeFalse
	KnowledgeTrue
)

type Limit

type Limit struct {
	Relation RelationID
	Input    RelationNode
	Spec     LimitSpec
}

func (*Limit) ID

func (l *Limit) ID() RelationID

func (*Limit) OutputSchema

func (l *Limit) OutputSchema() OutputSchema

type LimitSpec

type LimitSpec struct{ Count, Offset Expr }

type LogicalQuery

type LogicalQuery struct {
	Root       RelationNode
	Blocks     map[QueryBlockID]*QueryBlock
	Predicates map[PredicateID]*Predicate
	Bindings   map[BindingID]*ColumnBinding
	Schema     OutputSchema
}

func (*LogicalQuery) CloneForRewrite

func (q *LogicalQuery) CloneForRewrite() *LogicalQuery

type NullBehavior

type NullBehavior uint8
const (
	NullBehaviorUnknown NullBehavior = iota
	NullPropagating
	NullRejecting
)

type OrderItem

type OrderItem struct {
	Expr Expr
	Desc bool
}

type OutputColumn

type OutputColumn struct {
	Name    string
	Binding BindingID
}

type OutputSchema

type OutputSchema struct {
	Knowledge SchemaKnowledge
	Columns   []OutputColumn
}

type Predicate

type Predicate struct {
	ID          PredicateID
	Expr        Expr
	Origin      PredicateOrigin
	Scope       QueryBlockID
	References  []BindingID
	Nullability NullBehavior
	Determinism Determinism
	SourceSpan  SourceSpan
}

type PredicateAnalysis

type PredicateAnalysis struct {
	Original     PredicateID
	References   []BindingID
	Pushable     bool
	Overflow     bool
	Completeness AnalysisCompleteness
}

func AnalyzePredicate

func AnalyzePredicate(p *Predicate, maxRefs int) PredicateAnalysis

type PredicateCopy

type PredicateCopy struct {
	PredicateID PredicateID
	From, To    RelationID
	Recheck     RecheckPolicy
}

type PredicateID

type PredicateID string

type PredicateMove

type PredicateMove struct {
	PredicateID PredicateID
	From, To    RelationID
	Recheck     RecheckPolicy
}

type PredicateOrigin

type PredicateOrigin uint8
const (
	PredicateFromWhere PredicateOrigin = iota
	PredicateFromJoinOn
	PredicateFromHaving
	PredicateGenerated
)

type PredicatePushdownRule

type PredicatePushdownRule struct{ Options PushdownOptions }

PredicatePushdownRule adapts the initial pushdown implementation to the general rewrite interface.

func (PredicatePushdownRule) Apply

func (PredicatePushdownRule) Match

func (PredicatePushdownRule) Name

func (r PredicatePushdownRule) Name() string

type Project

type Project struct {
	Relation RelationID
	Input    RelationNode
	Items    []ProjectionExpr
}

func (*Project) ID

func (p *Project) ID() RelationID

func (*Project) OutputSchema

func (p *Project) OutputSchema() OutputSchema

type ProjectionExpr

type ProjectionExpr struct {
	Expr  Expr
	Alias string
}

type ProjectionPruningOptions

type ProjectionPruningOptions struct{ Required []BindingID }

type ProjectionPruningRule

type ProjectionPruningRule struct{ Options ProjectionPruningOptions }

func (ProjectionPruningRule) Apply

func (ProjectionPruningRule) Match

func (ProjectionPruningRule) Name

type PushdownAction

type PushdownAction uint8
const (
	PushdownKept PushdownAction = iota
	PushdownCopied
	PushdownMoved
	PushdownPartiallyCopied
	PushdownRejected
	PushdownUnknown
)

type PushdownAttempt

type PushdownAttempt struct {
	ID              AttemptID
	Strategy        string
	Base, Candidate *LogicalQuery
	Decisions       []PushdownDecision
	Diff            QueryDiff
	Safety          SafetyAssessment
	Heuristic       HeuristicAssessment
	Cost            *CostComparison
	Status          AttemptStatus
}

func TryPredicatePushdown

func TryPredicatePushdown(base *LogicalQuery, opts PushdownOptions) (*PushdownAttempt, error)

type PushdownDecision

type PushdownDecision struct {
	PredicateID        PredicateID
	FromNode, ToNode   RelationID
	Action             PushdownAction
	Reason             PushdownReason
	Proof              string
	Confidence         float64
	Recheck            RecheckPolicy
	DerivedPredicateID *PredicateID
}

type PushdownOptions

type PushdownOptions struct {
	ID       AttemptID
	Strategy string
}

type PushdownReason

type PushdownReason string
const (
	ReasonSingleInput      PushdownReason = "single_input"
	ReasonOuterJoinBarrier PushdownReason = "outer_join_barrier"
	ReasonCrossRelation    PushdownReason = "cross_relation"
	ReasonUnsupported      PushdownReason = "unsupported"
)

type QueryBlock

type QueryBlock struct {
	ID         QueryBlockID
	From       RelationNode
	Projection []ProjectionExpr
	Predicates []PredicateID
	GroupBy    []Expr
	Having     []Expr
	OrderBy    []OrderItem
	Limit      *LimitSpec
	Distinct   bool
	SourceSpan SourceSpan
}

type QueryBlockID

type QueryBlockID string

type QueryDiff

type QueryDiff struct {
	AddedFilters       []FilterDiff
	RemovedFilters     []FilterDiff
	MovedPredicates    []PredicateMove
	CopiedPredicates   []PredicateCopy
	Residuals          []PredicateID
	ChangedExpressions []ExpressionChange
}

func DiffLogicalQueries

func DiffLogicalQueries(base, candidate *LogicalQuery) QueryDiff

DiffLogicalQueries computes the structural filter differences used by rewrite consumers. It intentionally compares logical predicate identities, leaving expression equivalence to the rule that produced the candidate.

type RecheckPolicy

type RecheckPolicy uint8
const (
	RecheckNotRequired RecheckPolicy = iota
	RecheckRequired
)

type RelationID

type RelationID string

type RelationNode

type RelationNode interface {
	ID() RelationID
	OutputSchema() OutputSchema
	// contains filtered or unexported methods
}

type RewriteEvaluation

type RewriteEvaluation struct {
	Base     *LogicalQuery
	Attempts []*RewriteResult
	Selected *RewriteResult
}

RewriteEvaluation keeps multiple independent candidates for one immutable logical baseline. Selected is only an internal model choice.

func EvaluateRewriteRules

func EvaluateRewriteRules(base *LogicalQuery, rules []RewriteRule) (*RewriteEvaluation, error)

EvaluateRewriteRules evaluates each matching rule against the same base.

type RewriteResult

type RewriteResult struct {
	Strategy  string
	Base      *LogicalQuery
	Candidate *LogicalQuery
	Decisions []PushdownDecision
	Diff      QueryDiff
	Safety    SafetyAssessment
	Heuristic HeuristicAssessment
	Status    AttemptStatus
}

RewriteResult is the common result returned by any logical rewrite rule.

type RewriteRule

type RewriteRule interface {
	Name() string
	Match(*LogicalQuery) bool
	Apply(*LogicalQuery) (*RewriteResult, error)
}

RewriteRule describes a semantic transformation over a logical query. Rules produce candidates; selection and execution are deliberately outside this API.

type SafetyAssessment

type SafetyAssessment struct {
	Status       SafetyStatus
	Proofs       []SafetyProof
	Missing      []string
	SemanticRisk []string
}

func AssessRewriteSafety

func AssessRewriteSafety(decisions []PushdownDecision) SafetyAssessment

AssessRewriteSafety applies the common conservative gate for executable rewrites. Unknown placements cannot be emitted without an explicit proof.

type SafetyProof

type SafetyProof struct {
	PredicateID  PredicateID
	Rule, Detail string
}

type SafetyStatus

type SafetyStatus uint8
const (
	ProvenSafe SafetyStatus = iota
	ConditionallySafe
	HeuristicOnly
	Unsafe
	Unknown
)

type SchemaKnowledge

type SchemaKnowledge uint8
const (
	SchemaUnknown SchemaKnowledge = iota
	SchemaPartial
	SchemaComplete
)

type SetOp

type SetOp struct {
	Relation RelationID
	Op       SetOpType
	Inputs   []RelationNode
}

func (*SetOp) ID

func (s *SetOp) ID() RelationID

func (*SetOp) OutputSchema

func (s *SetOp) OutputSchema() OutputSchema

type SetOpType

type SetOpType uint8
const (
	SetUnion SetOpType = iota
	SetIntersect
	SetExcept
)

type Sort

type Sort struct {
	Relation RelationID
	Input    RelationNode
	Items    []OrderItem
}

func (*Sort) ID

func (s *Sort) ID() RelationID

func (*Sort) OutputSchema

func (s *Sort) OutputSchema() OutputSchema

type SourceSpan

type SourceSpan struct{ Start, End int }

type StatisticsSnapshot

type StatisticsSnapshot struct {
	Identity  string
	RowCounts map[RelationID]float64
}

type TableScan

type TableScan struct {
	Relation             RelationID
	Schema, Table, Alias string
	Output               OutputSchema
}

func (*TableScan) ID

func (t *TableScan) ID() RelationID

func (*TableScan) OutputSchema

func (t *TableScan) OutputSchema() OutputSchema

type TypeInfo

type TypeInfo struct{ Name string }

Jump to

Keyboard shortcuts

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