logical

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIllegalAggregate           = errors.New("illegal aggregate")
	ErrIllegalWindowFunction      = errors.New("illegal window function")
	ErrColumnNotFound             = errors.New("column not found or cannot be referenced in this part of the query")
	ErrUpdateOrDeleteWithoutWhere = errors.New("UPDATE and DELETE statements with a FROM table require a WHERE clause")
	ErrUnknownTable               = errors.New("unknown table")
	ErrSetIncompatibleSchemas     = errors.New("incompatible schemas in COMPOUND operation")
	ErrNotNullableColumn          = errors.New("column is not nullable")
	ErrIllegalConflictArbiter     = errors.New("illegal conflict arbiter")
	ErrAmbiguousColumn            = errors.New("ambiguous column")
	ErrWindowAlreadyDefined       = errors.New("window already defined")
	ErrInvalidWindowFunction      = errors.New("invalid window function")
	ErrWindowNotDefined           = errors.New("window not defined")
	ErrFunctionDoesNotExist       = errors.New("function does not exist")
	ErrActionInSQLStmt            = errors.New("actions cannot be used in SQL statements")
	ErrUntypedEmptyArray          = errors.New("cannot detect type for empty array")
)

Functions

func Format

func Format(plan LogicalNode) string

func Traverse

func Traverse(node Traversable, callback func(node Traversable) bool)

Traverse traverses a logical plan in preorder. It will call the callback function for each node in the plan. If the callback function returns false, the traversal will not continue to the children of the node.

Types

type Aggregate

type Aggregate struct {

	// GroupingExpressions are the expressions used
	// in the GROUP BY clause.
	GroupingExpressions []Expression
	// AggregateExpressions are the expressions used
	// in the SELECT clause (e.g. SUM(x), COUNT(y)).
	AggregateExpressions []*IdentifiedExpr
	// Child is the input to the aggregation
	// (e.g. a Project node).
	Child Plan
	// contains filtered or unexported fields
}

func (*Aggregate) Accept

func (s *Aggregate) Accept(v Visitor) any

func (*Aggregate) Children

func (a *Aggregate) Children() []Traversable

func (*Aggregate) Equal

func (a *Aggregate) Equal(other Traversable) bool

func (*Aggregate) Plans

func (a *Aggregate) Plans() []Plan

func (*Aggregate) Relation

func (a *Aggregate) Relation() *Relation

func (*Aggregate) String

func (a *Aggregate) String() string

type AggregateFunctionCall

type AggregateFunctionCall struct {
	FunctionName string
	Args         []Expression
	Star         bool
	Distinct     bool
	// contains filtered or unexported fields
}

func (*AggregateFunctionCall) Accept

func (s *AggregateFunctionCall) Accept(v Visitor) any

func (*AggregateFunctionCall) Children

func (a *AggregateFunctionCall) Children() []Traversable

func (*AggregateFunctionCall) Equal

func (a *AggregateFunctionCall) Equal(other Traversable) bool

func (*AggregateFunctionCall) Field

func (a *AggregateFunctionCall) Field() *Field

func (*AggregateFunctionCall) Plans

func (a *AggregateFunctionCall) Plans() []Plan

func (*AggregateFunctionCall) String

func (a *AggregateFunctionCall) String() string

type AliasExpr

type AliasExpr struct {
	Expr  Expression
	Alias string
}

func (*AliasExpr) Accept

func (s *AliasExpr) Accept(v Visitor) any

func (*AliasExpr) Children

func (a *AliasExpr) Children() []Traversable

func (*AliasExpr) Equal

func (a *AliasExpr) Equal(other Traversable) bool

func (*AliasExpr) Field

func (a *AliasExpr) Field() *Field

func (*AliasExpr) Plans

func (a *AliasExpr) Plans() []Plan

func (*AliasExpr) String

func (a *AliasExpr) String() string

type AnalyzedPlan

type AnalyzedPlan struct {
	// Plan is the plan of the query.
	Plan Plan
	// CTEs are plans for the common table expressions in the query.
	// They are in the order that they were defined.
	CTEs []*Subplan
}

AnalyzedPlan is the full result of a logical plan that has been analyzed.

func CreateLogicalPlan

func CreateLogicalPlan(statement *parse.SQLStatement, tables GetTableFunc,
	vars GetVarTypeFunc, objects GetObjectFunc, isAction IsActionFunc, applyDefaultOrdering bool, defaultNamespace string,
) (analyzed *AnalyzedPlan, err error)

CreateLogicalPlan creates a logical plan from a SQL statement. If applyDefaultOrdering is true, it will rewrite the query to apply default ordering. Default ordering will modify the passed query. If defaultNamespace is not empty, it will be used as the default namespace for all tables.

func (*AnalyzedPlan) Format

func (a *AnalyzedPlan) Format() string

Format formats the plan into a human-readable string.

type ArithmeticOp

type ArithmeticOp struct {
	Left  Expression
	Right Expression
	Op    ArithmeticOperator
}

func (*ArithmeticOp) Accept

func (s *ArithmeticOp) Accept(v Visitor) any

func (*ArithmeticOp) Children

func (a *ArithmeticOp) Children() []Traversable

func (*ArithmeticOp) Equal

func (a *ArithmeticOp) Equal(other Traversable) bool

func (*ArithmeticOp) Field

func (a *ArithmeticOp) Field() *Field

func (*ArithmeticOp) Plans

func (a *ArithmeticOp) Plans() []Plan

func (*ArithmeticOp) String

func (a *ArithmeticOp) String() string

type ArithmeticOperator

type ArithmeticOperator uint8
const (
	Add ArithmeticOperator = iota
	Subtract
	Multiply
	Divide
	Modulo
	Concat
)

type ArrayAccess

type ArrayAccess struct {
	Array Expression
	Index Expression
}

func (*ArrayAccess) Accept

func (s *ArrayAccess) Accept(v Visitor) any

func (*ArrayAccess) Children

func (a *ArrayAccess) Children() []Traversable

func (*ArrayAccess) Equal

func (a *ArrayAccess) Equal(other Traversable) bool

func (*ArrayAccess) Field

func (a *ArrayAccess) Field() *Field

func (*ArrayAccess) Plans

func (a *ArrayAccess) Plans() []Plan

func (*ArrayAccess) String

func (a *ArrayAccess) String() string

type ArrayConstructor

type ArrayConstructor struct {
	Elements []Expression
}

func (*ArrayConstructor) Accept

func (s *ArrayConstructor) Accept(v Visitor) any

func (*ArrayConstructor) Children

func (a *ArrayConstructor) Children() []Traversable

func (*ArrayConstructor) Equal

func (a *ArrayConstructor) Equal(other Traversable) bool

func (*ArrayConstructor) Field

func (a *ArrayConstructor) Field() *Field

func (*ArrayConstructor) Plans

func (a *ArrayConstructor) Plans() []Plan

func (*ArrayConstructor) String

func (a *ArrayConstructor) String() string

type Assignment

type Assignment struct {
	// Column is the column to update.
	Column string
	// Value is the value to update the column to.
	Value Expression
}

Assignment is a struct that represents an assignment in an update statement.

type CartesianProduct

type CartesianProduct struct {
	Left  Plan
	Right Plan
	// contains filtered or unexported fields
}

CartesianProduct is a logical plan node that represents a cartesian product between two relations. This is used in joins and set operations. Kwil doesn't actually allow cartesian products to be executed, but it is a necessary intermediate step for planning complex updates and deletes.

func (*CartesianProduct) Accept

func (s *CartesianProduct) Accept(v Visitor) any

func (*CartesianProduct) Children

func (c *CartesianProduct) Children() []Traversable

func (*CartesianProduct) Equal

func (c *CartesianProduct) Equal(other Traversable) bool

func (*CartesianProduct) Plans

func (c *CartesianProduct) Plans() []Plan

func (*CartesianProduct) Relation

func (c *CartesianProduct) Relation() *Relation

func (*CartesianProduct) String

func (c *CartesianProduct) String() string

type Case

type Case struct {
	// Value is the value that is being compared.
	// Can be nil if there is no value to compare.
	Value Expression
	// WhenClauses are the list of when/then pairs.
	// The first element of each pair is the condition,
	// which must match the data type of Value. If Value
	// is nil, the condition must be a boolean.
	WhenClauses [][2]Expression
	// Else is the else clause. Can be nil.
	Else Expression
}

func (*Case) Accept

func (s *Case) Accept(v Visitor) any

func (*Case) Children

func (c *Case) Children() []Traversable

func (*Case) Equal

func (c *Case) Equal(other Traversable) bool

func (*Case) Field

func (c *Case) Field() *Field

func (*Case) Plans

func (c *Case) Plans() []Plan

func (*Case) String

func (c *Case) String() string

type Collate

type Collate struct {
	Expr      Expression
	Collation CollationType
}

func (*Collate) Accept

func (s *Collate) Accept(v Visitor) any

func (*Collate) Children

func (c *Collate) Children() []Traversable

func (*Collate) Equal

func (c *Collate) Equal(other Traversable) bool

func (*Collate) Field

func (c *Collate) Field() *Field

func (*Collate) Plans

func (c *Collate) Plans() []Plan

func (*Collate) String

func (c *Collate) String() string

type CollationType

type CollationType uint8
const (
	// NoCaseCollation is a collation that is case-insensitive.
	NoCaseCollation CollationType = iota
)

func (CollationType) String

func (c CollationType) String() string

type ColumnRef

type ColumnRef struct {
	// Parent relation name, can be empty.
	// If not specified by user, it will be qualified
	// during the planning phase.
	Parent     string
	ColumnName string
	// contains filtered or unexported fields
}

Column reference

func (*ColumnRef) Accept

func (s *ColumnRef) Accept(v Visitor) any

func (*ColumnRef) Children

func (c *ColumnRef) Children() []Traversable

func (*ColumnRef) Equal

func (c *ColumnRef) Equal(other Traversable) bool

func (*ColumnRef) Field

func (c *ColumnRef) Field() *Field

func (*ColumnRef) Plans

func (c *ColumnRef) Plans() []Plan

func (*ColumnRef) String

func (c *ColumnRef) String() string

type ComparisonOp

type ComparisonOp struct {
	Left  Expression
	Right Expression
	Op    ComparisonOperator
}

func (*ComparisonOp) Accept

func (s *ComparisonOp) Accept(v Visitor) any

func (*ComparisonOp) Children

func (c *ComparisonOp) Children() []Traversable

func (*ComparisonOp) Equal

func (c *ComparisonOp) Equal(other Traversable) bool

func (*ComparisonOp) Field

func (c *ComparisonOp) Field() *Field

func (*ComparisonOp) Plans

func (c *ComparisonOp) Plans() []Plan

func (*ComparisonOp) String

func (c *ComparisonOp) String() string

type ComparisonOperator

type ComparisonOperator uint8
const (
	// operators can have 3 types of classifications:
	// - sargable
	// - not sargable
	// - rarely sarable
	// https://en.wikipedia.org/wiki/Sargable
	// for the purposes of our planner, we will treat rarely
	// sargable as not sargable
	Equal       ComparisonOperator = iota // sargable
	LessThan                              // sargable
	GreaterThan                           // sargable
	// IS and IS NOT are rarely sargable because they can only be used
	// with NULL values or BOOLEAN values
	Is             // rarely sargable
	IsDistinctFrom // not sargable
	Like           // not sargable
	ILike          // not sargable
)

func (ComparisonOperator) String

func (c ComparisonOperator) String() string

type ConflictDoNothing

type ConflictDoNothing struct {

	// ArbiterIndex is the index to use to determine if there is a conflict.
	// If/when Kwil supports partial indexes, we will turn this into a list
	// of indexes. Can be nil when DO NOTHING is used.
	ArbiterIndex Index
	// contains filtered or unexported fields
}

ConflictDoNothing is a struct that represents the resolution of a conflict using DO NOTHING.

func (*ConflictDoNothing) Accept

func (s *ConflictDoNothing) Accept(v Visitor) any

func (*ConflictDoNothing) Children

func (c *ConflictDoNothing) Children() []Traversable

func (*ConflictDoNothing) Equal

func (c *ConflictDoNothing) Equal(other Traversable) bool

func (*ConflictDoNothing) Plans

func (c *ConflictDoNothing) Plans() []Plan

func (*ConflictDoNothing) Relation

func (c *ConflictDoNothing) Relation() *Relation

func (*ConflictDoNothing) String

func (c *ConflictDoNothing) String() string

type ConflictResolution

type ConflictResolution interface {
	Plan
	// contains filtered or unexported methods
}

type ConflictUpdate

type ConflictUpdate struct {

	// ArbiterIndex is the index to use to determine if there is a conflict.
	// If/when Kwil supports partial indexes, we will turn this into a list
	// of indexes. See: https://github.com/cockroachdb/cockroach/issues/53170
	// Cannot be nil when DO UPDATE is used.
	ArbiterIndex Index
	// Assignments are the expressions to update if there is a conflict.
	// Cannot be nil.
	Assignments []*Assignment
	// ConflictFilter is a predicate that allows us to selectively
	// update or raise an error if there is a conflict.
	// Can be nil.
	ConflictFilter Expression
	// contains filtered or unexported fields
}

ConflictUpdate is a struct that represents the resolution of a conflict using DO UPDATE SET.

func (*ConflictUpdate) Accept

func (s *ConflictUpdate) Accept(v Visitor) any

func (*ConflictUpdate) Children

func (c *ConflictUpdate) Children() []Traversable

func (*ConflictUpdate) Equal

func (c *ConflictUpdate) Equal(other Traversable) bool

func (*ConflictUpdate) Plans

func (c *ConflictUpdate) Plans() []Plan

func (*ConflictUpdate) Relation

func (c *ConflictUpdate) Relation() *Relation

func (*ConflictUpdate) String

func (c *ConflictUpdate) String() string

type Delete

type Delete struct {

	// Child is the input to the delete.
	Child Plan
	// Table is the target table name.
	// It will always be the table name and not an alias.
	Table string
	// contains filtered or unexported fields
}

Delete is a node that plans a delete operation.

func (*Delete) Accept

func (s *Delete) Accept(v Visitor) any

func (*Delete) Children

func (d *Delete) Children() []Traversable

func (*Delete) Equal

func (d *Delete) Equal(t Traversable) bool

func (*Delete) Plans

func (d *Delete) Plans() []Plan

func (*Delete) Relation

func (d *Delete) Relation() *Relation

func (*Delete) String

func (d *Delete) String() string

type Distinct

type Distinct struct {
	Child Plan
	// contains filtered or unexported fields
}

func (*Distinct) Accept

func (s *Distinct) Accept(v Visitor) any

func (*Distinct) Children

func (d *Distinct) Children() []Traversable

func (*Distinct) Equal

func (d *Distinct) Equal(other Traversable) bool

func (*Distinct) Plans

func (d *Distinct) Plans() []Plan

func (*Distinct) Relation

func (d *Distinct) Relation() *Relation

func (*Distinct) String

func (d *Distinct) String() string

type EmptyScan

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

func (*EmptyScan) Accept

func (s *EmptyScan) Accept(v Visitor) any

func (*EmptyScan) Children

func (n *EmptyScan) Children() []Traversable

func (*EmptyScan) Equal

func (n *EmptyScan) Equal(other Traversable) bool

func (*EmptyScan) Plans

func (n *EmptyScan) Plans() []Plan

func (*EmptyScan) Relation

func (n *EmptyScan) Relation() *Relation

func (*EmptyScan) String

func (n *EmptyScan) String() string

type ExprRef

type ExprRef struct {
	// Identified is the expression that is being referenced.
	// It is a pointer to the actual expression.
	Identified *IdentifiedExpr
}

ExprRef is a reference to an expression in the query. It is used when a part of the query is referenced in another. For example, SELECT SUM(a) FROM table1 GROUP BY a HAVING SUM(a) > 10, both SUM(a) expressions are the same, and will reference the same ExprRef (which would actually occur in the Aggregate node).

func (*ExprRef) Accept

func (s *ExprRef) Accept(v Visitor) any

func (*ExprRef) Children

func (e *ExprRef) Children() []Traversable

func (*ExprRef) Equal

func (e *ExprRef) Equal(other Traversable) bool

func (*ExprRef) Field

func (e *ExprRef) Field() *Field

func (*ExprRef) Plans

func (e *ExprRef) Plans() []Plan

func (*ExprRef) String

func (e *ExprRef) String() string

type Expression

type Expression interface {
	LogicalNode
	Field() *Field
}

type Field

type Field struct {
	Parent string // the parent relation name
	Name   string // the field name

	// ReferenceID is the ID with which this field can be referenced.
	// It can be empty if the field is not referenced.
	ReferenceID string
	// contains filtered or unexported fields
}

Field is a field in a relation. Parent and Name can be empty, if the expression is a constant. If this is the last expression in a relation, the "Name" field will be the name of the column in the result.

func (*Field) Copy

func (f *Field) Copy() *Field

func (*Field) Equals

func (f *Field) Equals(other *Field) bool

func (*Field) Object

func (f *Field) Object() (map[string]*types.DataType, error)

func (*Field) ResultString

func (f *Field) ResultString() string

ResultString returns a string representation of the field that contains information as to how the field will be represented as a user-facing column in the result.

func (*Field) Scalar

func (f *Field) Scalar() (*types.DataType, error)

func (*Field) String

func (f *Field) String() string

type FieldAccess

type FieldAccess struct {
	Object Expression
	Key    string
}

func (*FieldAccess) Accept

func (s *FieldAccess) Accept(v Visitor) any

func (*FieldAccess) Children

func (f *FieldAccess) Children() []Traversable

func (*FieldAccess) Equal

func (f *FieldAccess) Equal(other Traversable) bool

func (*FieldAccess) Field

func (f *FieldAccess) Field() *Field

func (*FieldAccess) Plans

func (f *FieldAccess) Plans() []Plan

func (*FieldAccess) String

func (f *FieldAccess) String() string

type Filter

type Filter struct {
	Condition Expression
	Child     Plan
	// contains filtered or unexported fields
}

func (*Filter) Accept

func (s *Filter) Accept(v Visitor) any

func (*Filter) Children

func (f *Filter) Children() []Traversable

func (*Filter) Equal

func (f *Filter) Equal(other Traversable) bool

func (*Filter) Plans

func (f *Filter) Plans() []Plan

func (*Filter) Relation

func (f *Filter) Relation() *Relation

func (*Filter) String

func (f *Filter) String() string

type GetObjectFunc

type GetObjectFunc = func(objName string) (obj map[string]*types.DataType, err error)

GetObjectFieldTypeFunc is a function that gets the type of a field in an object.

type GetTableFunc

type GetTableFunc = func(namespace string, tableName string) (table *engine.Table, err error)

GetTableFunc is a function that gets a table by name. It can also be given a namespace to search in. If no namespace is given (passed as ""), it will search in the default namespace.

type GetVarTypeFunc

type GetVarTypeFunc = func(varName string) (dataType *types.DataType, err error)

GetVarTypeFunc is a function that gets the type of a variable.

type IdentifiedExpr

type IdentifiedExpr struct {
	// ID is the unique identifier for the expression.
	ID string
	// Expr is the expression that is being identified.
	Expr Expression
}

IdentifiedExpr is an expression that can be referenced.

func (*IdentifiedExpr) Accept

func (s *IdentifiedExpr) Accept(v Visitor) any

func (*IdentifiedExpr) Children

func (i *IdentifiedExpr) Children() []Traversable

func (*IdentifiedExpr) Equal

func (i *IdentifiedExpr) Equal(other Traversable) bool

func (*IdentifiedExpr) Field

func (i *IdentifiedExpr) Field() *Field

func (*IdentifiedExpr) Plans

func (i *IdentifiedExpr) Plans() []Plan

func (*IdentifiedExpr) String

func (i *IdentifiedExpr) String() string

type Index

type Index interface {
	fmt.Stringer

	Equal(Index) bool
	// contains filtered or unexported methods
}

Index is an interface that represents an index. Since Kwil's internal catalog does not individually name all indexes (e.g. UNIQUE and PRIMARY column constraints), we use this interface to represent an index.

type IndexColumnConstraint

type IndexColumnConstraint struct {
	// Table is the physical table that the index is on.
	Table string
	// Column is the column that the constraint is on.
	Column string
	// ConstraintType is the type of constraint that the index is.
	ConstraintType IndexConstraintType
}

func (*IndexColumnConstraint) Equal

func (i *IndexColumnConstraint) Equal(other Index) bool

func (*IndexColumnConstraint) String

func (i *IndexColumnConstraint) String() string

type IndexConstraintType

type IndexConstraintType uint8
const (
	UniqueConstraintIndex IndexConstraintType = iota
	PrimaryKeyConstraintIndex
)

func (IndexConstraintType) String

func (i IndexConstraintType) String() string

type IndexNamed

type IndexNamed struct {
	// Name is the name of the index.
	Name string
}

IndexNamed is any index that is specified explicitly and has a referenceable name.

func (*IndexNamed) Equal

func (i *IndexNamed) Equal(other Index) bool

func (*IndexNamed) String

func (i *IndexNamed) String() string

type Insert

type Insert struct {

	// Table is the physical table to insert into.
	Table string
	// ReferencedAs is how the table is referenced in the query.
	// It is either a user-defined reference or the table name.
	ReferencedAs string
	// Columns are the columns to insert into.
	Columns []*Field
	// InsertionValues are the values to insert.
	// These can be either values specified using VALUES or a subquery.
	InsertionValues Plan
	// ConflictResolution is the conflict resolution to use if there is a conflict.
	ConflictResolution ConflictResolution
	// contains filtered or unexported fields
}

Insert is a node that plans an insert operation.

func (*Insert) Accept

func (s *Insert) Accept(v Visitor) any

func (*Insert) Children

func (i *Insert) Children() []Traversable

func (*Insert) Equal

func (i *Insert) Equal(t Traversable) bool

func (*Insert) Plans

func (i *Insert) Plans() []Plan

func (*Insert) Relation

func (i *Insert) Relation() *Relation

func (*Insert) String

func (i *Insert) String() string

type IsActionFunc

type IsActionFunc = func(funcName string) bool

IsActionFunc returns true if the passed function is an action

type IsIn

type IsIn struct {
	// Left is the expression that is being compared.
	Left Expression

	Expressions []Expression
	Subquery    *SubqueryExpr
}

func (*IsIn) Accept

func (s *IsIn) Accept(v Visitor) any

func (*IsIn) Children

func (i *IsIn) Children() []Traversable

func (*IsIn) Equal

func (i *IsIn) Equal(other Traversable) bool

func (*IsIn) Field

func (i *IsIn) Field() *Field

func (*IsIn) Plans

func (i *IsIn) Plans() []Plan

func (*IsIn) String

func (i *IsIn) String() string

type Join

type Join struct {
	Left      Plan
	Right     Plan
	JoinType  JoinType
	Condition Expression
	// contains filtered or unexported fields
}

func (*Join) Accept

func (s *Join) Accept(v Visitor) any

func (*Join) Children

func (j *Join) Children() []Traversable

func (*Join) Equal

func (j *Join) Equal(other Traversable) bool

func (*Join) Plans

func (j *Join) Plans() []Plan

func (*Join) Relation

func (j *Join) Relation() *Relation

func (*Join) String

func (j *Join) String() string

type JoinType

type JoinType int
const (
	InnerJoin JoinType = iota
	LeftOuterJoin
	RightOuterJoin
	FullOuterJoin
)

func (JoinType) String

func (j JoinType) String() string

type Limit

type Limit struct {
	Child  Plan
	Limit  Expression
	Offset Expression
	// contains filtered or unexported fields
}

func (*Limit) Accept

func (s *Limit) Accept(v Visitor) any

func (*Limit) Children

func (l *Limit) Children() []Traversable

func (*Limit) Equal

func (l *Limit) Equal(other Traversable) bool

func (*Limit) Plans

func (l *Limit) Plans() []Plan

func (*Limit) Relation

func (l *Limit) Relation() *Relation

func (*Limit) String

func (l *Limit) String() string

type Literal

type Literal struct {
	Value interface{}
	Type  *types.DataType
}

Literal value

func (*Literal) Accept

func (s *Literal) Accept(v Visitor) any

func (*Literal) Children

func (l *Literal) Children() []Traversable

func (*Literal) Equal

func (l *Literal) Equal(other Traversable) bool

func (*Literal) Field

func (l *Literal) Field() *Field

func (*Literal) Plans

func (l *Literal) Plans() []Plan

func (*Literal) String

func (l *Literal) String() string

type LogicalNode

type LogicalNode interface {
	fmt.Stringer
	Traversable
}

func Rewrite

func Rewrite(node LogicalNode, cfg *RewriteConfig) (lp LogicalNode, err error)

Rewrite rewrites a logical plan using the given configuration. It returns the rewritten plan, but it also modifies the original plan in place. The returned plan should always be used.

type LogicalOp

type LogicalOp struct {
	Left  Expression
	Right Expression
	Op    LogicalOperator
}

func (*LogicalOp) Accept

func (s *LogicalOp) Accept(v Visitor) any

func (*LogicalOp) Children

func (l *LogicalOp) Children() []Traversable

func (*LogicalOp) Equal

func (l *LogicalOp) Equal(other Traversable) bool

func (*LogicalOp) Field

func (l *LogicalOp) Field() *Field

func (*LogicalOp) Plans

func (l *LogicalOp) Plans() []Plan

func (*LogicalOp) String

func (l *LogicalOp) String() string

type LogicalOperator

type LogicalOperator uint8
const (
	And LogicalOperator = iota
	Or
)

type Plan

type Plan interface {
	LogicalNode
	Relation() *Relation
	// contains filtered or unexported methods
}

type ProcedureScanSource

type ProcedureScanSource struct {
	// ProcedureName is the name of the procedure being targeted.
	ProcedureName string
	// Args are the base arguments to the procedure.
	Args []Expression
	// ContextualArgs are the arguments that are passed in if
	// the procedure is a foreign procedure.
	ContextualArgs []Expression
	// IsForeign is true if the function is a foreign procedure.
	IsForeign bool
	// contains filtered or unexported fields
}

ProcedureScanSource represents a scan of a function. It can call either a local procedure or foreign procedure that returns a table.

func (*ProcedureScanSource) Accept

func (s *ProcedureScanSource) Accept(v Visitor) any

func (*ProcedureScanSource) Children

func (f *ProcedureScanSource) Children() []Traversable

func (*ProcedureScanSource) Equal

func (f *ProcedureScanSource) Equal(other Traversable) bool

func (*ProcedureScanSource) FormatScan

func (f *ProcedureScanSource) FormatScan() string

func (*ProcedureScanSource) Plans

func (f *ProcedureScanSource) Plans() []Plan

func (*ProcedureScanSource) Relation

func (f *ProcedureScanSource) Relation() *Relation

type Project

type Project struct {

	// Expressions are the expressions that are projected.
	Expressions []Expression
	Child       Plan
	// contains filtered or unexported fields
}

func (*Project) Accept

func (s *Project) Accept(v Visitor) any

func (*Project) Children

func (p *Project) Children() []Traversable

func (*Project) Equal

func (p *Project) Equal(other Traversable) bool

func (*Project) Plans

func (p *Project) Plans() []Plan

func (*Project) Relation

func (p *Project) Relation() *Relation

func (*Project) String

func (p *Project) String() string

type QuerySection

type QuerySection string

type Relation

type Relation struct {
	Fields []*Field
}

Relation is the current relation in the query plan.

func (*Relation) ColumnsByParent

func (s *Relation) ColumnsByParent(name string) []*Field

func (*Relation) Copy

func (r *Relation) Copy() *Relation

func (*Relation) FindReference

func (r *Relation) FindReference(id string) (*Field, error)

FindReference finds a field by its reference ID. If there are many fields with the same reference ID, or no fields with the reference ID, an error is returned.

func (*Relation) Search

func (s *Relation) Search(parent, name string) (*Field, error)

Search searches for a column by parent and name. If the column is not found, an error is returned. If no parent is specified and many columns have the same name, an error is returned.

type Return

type Return struct {

	// Fields are the fields to return.
	Fields []*Field
	// Child is the input to the return.
	Child Plan
	// contains filtered or unexported fields
}

Return is a node that plans a return operation. It specifies columns to return from a query. This is similar to a projection, however it cannot be optimized using pushdowns, since it is at the top level (and therefore user requested).

func (*Return) Accept

func (s *Return) Accept(v Visitor) any

func (*Return) Children

func (r *Return) Children() []Traversable

func (*Return) Equal

func (r *Return) Equal(t Traversable) bool

func (*Return) Plans

func (r *Return) Plans() []Plan

func (*Return) Relation

func (r *Return) Relation() *Relation

Relation returns the relation of the child.

func (*Return) String

func (r *Return) String() string

type RewriteConfig

type RewriteConfig struct {
	// ExprCallback is the function that will be called on each expression.
	// It returns the new node, which will replace the old node,
	// a boolean, which indicates whether the nodes children should be visited,
	// and an error, which will be returned if an error occurs.
	ExprCallback func(Expression) (Expression, bool, error)
	// PlanCallback is the function that will be called on each plan
	// It returns the new node, which will replace the old node,
	// a boolean, which indicates whether the nodes children should be visited,
	// and an error, which will be returned if an error occurs.
	PlanCallback func(Plan) (Plan, bool, error)
	// ScanSourceCallback is the function that will be called on each scan source
	// It returns the new node, which will replace the old node,
	// a boolean, which indicates whether the nodes children should be visited,
	// and an error, which will be returned if an error occurs.
	ScanSourceCallback func(ScanSource) (ScanSource, bool, error)
}

RewriteConfig is a configuration for the rewriter.

type ScalarFunctionCall

type ScalarFunctionCall struct {
	FunctionName string
	Args         []Expression
	// contains filtered or unexported fields
}

Function call

func (*ScalarFunctionCall) Accept

func (s *ScalarFunctionCall) Accept(v Visitor) any

func (*ScalarFunctionCall) Children

func (f *ScalarFunctionCall) Children() []Traversable

func (*ScalarFunctionCall) Equal

func (f *ScalarFunctionCall) Equal(other Traversable) bool

func (*ScalarFunctionCall) Field

func (f *ScalarFunctionCall) Field() *Field

func (*ScalarFunctionCall) Plans

func (f *ScalarFunctionCall) Plans() []Plan

func (*ScalarFunctionCall) String

func (f *ScalarFunctionCall) String() string

type Scan

type Scan struct {
	Source ScanSource
	// RelationName will always be set.
	// If the scan is a table scan and no alias was specified,
	// the RelationName will be the table name.
	// All other scan types (functions and subqueries) require an alias.
	RelationName string
	// Filter holds the optional filter condition that can be
	// pushed down to the scan operation. This allows filtering data
	// during the scan, which can improve query performance by reducing
	// the amount of data processed in subsequent operations.
	// This field won't ever be set in the initial logical plan, but
	// can be set during the optimization phase.
	Filter Expression
	// contains filtered or unexported fields
}

func (*Scan) Accept

func (s *Scan) Accept(v Visitor) any

func (*Scan) Children

func (s *Scan) Children() []Traversable

func (*Scan) Equal

func (s *Scan) Equal(other Traversable) bool

func (*Scan) Plans

func (s *Scan) Plans() []Plan

func (*Scan) Relation

func (s *Scan) Relation() *Relation

func (*Scan) String

func (s *Scan) String() string

type ScanSource

type ScanSource interface {
	Traversable
	FormatScan() string
	Relation() *Relation
}

ScanSource is a source of data that a Scan can be performed on. This is either a physical table, an action call that returns a table, or a subquery.

type SetOperation

type SetOperation struct {
	Left   Plan
	Right  Plan
	OpType SetOperationType
	// contains filtered or unexported fields
}

func (*SetOperation) Accept

func (s *SetOperation) Accept(v Visitor) any

SetOperation

func (*SetOperation) Children

func (s *SetOperation) Children() []Traversable

func (*SetOperation) Equal

func (s *SetOperation) Equal(other Traversable) bool

func (*SetOperation) Plans

func (s *SetOperation) Plans() []Plan

func (*SetOperation) Relation

func (s *SetOperation) Relation() *Relation

func (*SetOperation) String

func (s *SetOperation) String() string

type SetOperationType

type SetOperationType int
const (
	Union SetOperationType = iota
	UnionAll
	Intersect
	Except
)

func (SetOperationType) String

func (s SetOperationType) String() string

type Sort

type Sort struct {
	SortExpressions []*SortExpression
	Child           Plan
	// contains filtered or unexported fields
}

func (*Sort) Accept

func (s *Sort) Accept(v Visitor) any

func (*Sort) Children

func (s *Sort) Children() []Traversable

func (*Sort) Equal

func (s *Sort) Equal(other Traversable) bool

func (*Sort) Plans

func (s *Sort) Plans() []Plan

func (*Sort) Relation

func (s *Sort) Relation() *Relation

func (*Sort) String

func (s *Sort) String() string

type SortExpression

type SortExpression struct {
	Expr      Expression
	Ascending bool
	NullsLast bool
}

func (*SortExpression) String

func (s *SortExpression) String() string

type Subplan

type Subplan struct {
	Plan Plan
	ID   string
	Type SubplanType
	// contains filtered or unexported fields
}

func (*Subplan) Accept

func (s *Subplan) Accept(v Visitor) any

func (*Subplan) Children

func (s *Subplan) Children() []Traversable

func (*Subplan) Equal

func (s *Subplan) Equal(other Traversable) bool

func (*Subplan) Plans

func (s *Subplan) Plans() []Plan

func (*Subplan) Relation

func (s *Subplan) Relation() *Relation

func (*Subplan) String

func (s *Subplan) String() string

type SubplanType

type SubplanType int
const (
	SubplanTypeSubquery SubplanType = iota
	SubplanTypeCTE
	SubplanTypeRecursiveCTE
)

func (SubplanType) String

func (s SubplanType) String() string

type Subquery

type Subquery struct {
	// Plan is the logical plan for the subquery.
	Plan *Subplan

	// Correlated is the list of columns that are correlated
	// to the outer query. If empty, the subquery is uncorrelated.
	Correlated []*Field
}

Subquery holds information for a subquery. It is a TableSource that can be used in a Scan, but also can be used within expressions. If ReturnsRelation is true, it is a TableSource. If false, it is a scalar subquery (used in expressions).

func (*Subquery) Accept

func (s *Subquery) Accept(v Visitor) any

func (*Subquery) Children

func (s *Subquery) Children() []Traversable

func (*Subquery) Equal

func (s *Subquery) Equal(other Traversable) bool

func (*Subquery) FormatScan

func (s *Subquery) FormatScan() string

func (*Subquery) Plans

func (s *Subquery) Plans() []Plan

func (*Subquery) Relation

func (s *Subquery) Relation() *Relation

type SubqueryExpr

type SubqueryExpr struct {
	Query *Subquery
	// If Exists is true, we are checking if the subquery returns any rows.
	// Otherwise, the subquery will return a single value.
	// If the query is a NOT EXISTS, a unary negation will wrap this expression.
	Exists bool
}

func (*SubqueryExpr) Accept

func (s *SubqueryExpr) Accept(v Visitor) any

func (*SubqueryExpr) Children

func (s *SubqueryExpr) Children() []Traversable

func (*SubqueryExpr) Equal

func (s *SubqueryExpr) Equal(other Traversable) bool

func (*SubqueryExpr) Field

func (s *SubqueryExpr) Field() *Field

func (*SubqueryExpr) Plans

func (s *SubqueryExpr) Plans() []Plan

func (*SubqueryExpr) String

func (s *SubqueryExpr) String() string

type TableScanSource

type TableScanSource struct {
	// Namespace is the namespace of the table being scanned.
	Namespace string
	// TableName is the name of the table being scanned.
	TableName string
	Type      TableSourceType
	// contains filtered or unexported fields
}

TableScanSource represents a scan of a physical table or a CTE.

func (*TableScanSource) Accept

func (s *TableScanSource) Accept(v Visitor) any

func (*TableScanSource) Children

func (t *TableScanSource) Children() []Traversable

func (*TableScanSource) Equal

func (t *TableScanSource) Equal(other Traversable) bool

func (*TableScanSource) FormatScan

func (t *TableScanSource) FormatScan() string

func (*TableScanSource) Plans

func (t *TableScanSource) Plans() []Plan

func (*TableScanSource) Relation

func (t *TableScanSource) Relation() *Relation

type TableSourceType

type TableSourceType int
const (
	TableSourcePhysical TableSourceType = iota // physical table (default)
	TableSourceCTE                             // common table expression

)

func (TableSourceType) String

func (t TableSourceType) String() string

type TopLevelPlan

type TopLevelPlan interface {
	Plan
	// contains filtered or unexported methods
}

TopLevelPlan is a logical plan that is at the top level of a query.

type Traversable

type Traversable interface {
	// Children returns the children of the node.
	// These are all LogicalPlans and LogicalExprs that are
	// referenced by the node.
	Children() []Traversable
	// Plans returns all the logical plans that are referenced
	// by the node (or the nearest node that contains them).
	Plans() []Plan
	// Accept is used to traverse the node.
	Accept(Visitor) any
}

Traversable is an interface for nodes that can be traversed.

type Tuples

type Tuples struct {
	Values [][]Expression
	// contains filtered or unexported fields
}

Tuples is a list tuple being inserted into a table.

func (*Tuples) Accept

func (s *Tuples) Accept(v Visitor) any

func (*Tuples) Children

func (t *Tuples) Children() []Traversable

func (*Tuples) Equal

func (t *Tuples) Equal(other Traversable) bool

func (*Tuples) Plans

func (t *Tuples) Plans() []Plan

func (*Tuples) Relation

func (t *Tuples) Relation() *Relation

func (*Tuples) String

func (t *Tuples) String() string

type TypeCast

type TypeCast struct {
	Expr Expression
	Type *types.DataType
}

func (*TypeCast) Accept

func (s *TypeCast) Accept(v Visitor) any

func (*TypeCast) Children

func (t *TypeCast) Children() []Traversable

func (*TypeCast) Equal

func (t *TypeCast) Equal(other Traversable) bool

func (*TypeCast) Field

func (t *TypeCast) Field() *Field

func (*TypeCast) Plans

func (t *TypeCast) Plans() []Plan

func (*TypeCast) String

func (t *TypeCast) String() string

type UnaryOp

type UnaryOp struct {
	Expr Expression
	Op   UnaryOperator
}

func (*UnaryOp) Accept

func (s *UnaryOp) Accept(v Visitor) any

func (*UnaryOp) Children

func (u *UnaryOp) Children() []Traversable

func (*UnaryOp) Equal

func (u *UnaryOp) Equal(other Traversable) bool

func (*UnaryOp) Field

func (u *UnaryOp) Field() *Field

func (*UnaryOp) Plans

func (u *UnaryOp) Plans() []Plan

func (UnaryOp) String

func (u UnaryOp) String() string

type UnaryOperator

type UnaryOperator uint8
const (
	Negate UnaryOperator = iota
	Not
	Positive
)

func (UnaryOperator) String

func (u UnaryOperator) String() string

type Update

type Update struct {

	// Child is the input to the update.
	Child Plan
	// Table is the target table name.
	// It will always be the table name and not an alias.
	Table string
	// Assignments are the assignments to update.
	Assignments []*Assignment
	// contains filtered or unexported fields
}

Update is a node that plans an update operation.

func (*Update) Accept

func (s *Update) Accept(v Visitor) any

func (*Update) Children

func (u *Update) Children() []Traversable

func (*Update) Equal

func (u *Update) Equal(t Traversable) bool

func (*Update) Plans

func (u *Update) Plans() []Plan

func (*Update) Relation

func (u *Update) Relation() *Relation

func (*Update) String

func (u *Update) String() string

type Variable

type Variable struct {
	// name is something like $id, @caller, etc.
	VarName string
	// contains filtered or unexported fields
}

Variable reference

func (*Variable) Accept

func (s *Variable) Accept(v Visitor) any

func (*Variable) Children

func (v *Variable) Children() []Traversable

func (*Variable) Equal

func (v *Variable) Equal(other Traversable) bool

func (*Variable) Field

func (v *Variable) Field() *Field

func (*Variable) Plans

func (v *Variable) Plans() []Plan

func (*Variable) String

func (v *Variable) String() string

type Visitor

type Visitor interface {
	VisitTableScanSource(*TableScanSource) any
	VisitProcedureScanSource(*ProcedureScanSource) any
	VisitSubquery(*Subquery) any
	VisitEmptyScan(*EmptyScan) any
	VisitScan(*Scan) any
	VisitProject(*Project) any
	VisitFilter(*Filter) any
	VisitJoin(*Join) any
	VisitSort(*Sort) any
	VisitLimit(*Limit) any
	VisitDistinct(*Distinct) any
	VisitSetOperation(*SetOperation) any
	VisitAggregate(*Aggregate) any
	VisitWindow(*Window) any
	VisitSubplan(*Subplan) any
	VisitLiteral(*Literal) any
	VisitVariable(*Variable) any
	VisitColumnRef(*ColumnRef) any
	VisitAggregateFunctionCall(*AggregateFunctionCall) any
	VisitScalarFunctionCall(*ScalarFunctionCall) any
	VisitArithmeticOp(*ArithmeticOp) any
	VisitComparisonOp(*ComparisonOp) any
	VisitLogicalOp(*LogicalOp) any
	VisitUnaryOp(*UnaryOp) any
	VisitTypeCast(*TypeCast) any
	VisitAliasExpr(*AliasExpr) any
	VisitArrayAccess(*ArrayAccess) any
	VisitArrayConstructor(*ArrayConstructor) any
	VisitFieldAccess(*FieldAccess) any
	VisitSubqueryExpr(*SubqueryExpr) any
	VisitCollate(*Collate) any
	VisitIsIn(*IsIn) any
	VisitCase(*Case) any
	VisitExprRef(*ExprRef) any
	VisitIdentifiedExpr(*IdentifiedExpr) any
	VisitReturn(*Return) any
	VisitCartesianProduct(*CartesianProduct) any
	VisitUpdate(*Update) any
	VisitDelete(*Delete) any
	VisitInsert(*Insert) any
	VisitConflictDoNothing(*ConflictDoNothing) any
	VisitConflictUpdate(*ConflictUpdate) any
	VisitTuples(*Tuples) any
	VisitWindowFunction(*WindowFunction) any
}

type Window

type Window struct {

	// PartitionBy are the expressions used in the PARTITION BY clause.
	PartitionBy []Expression
	// OrderBy are the expressions used in the ORDER BY clause.
	OrderBy []*SortExpression
	// Functions are the window functions that are applied.
	Functions []*IdentifiedExpr
	// Frames are not yet supported, but when they are, they will
	// be added here.
	Child Plan
	// contains filtered or unexported fields
}

Window is a logical plan node that represents a window function. It applies the window function to the input relation, and returns both the input relation and the window function result as a single relation.

func (*Window) Accept

func (s *Window) Accept(v Visitor) any

func (*Window) Children

func (w *Window) Children() []Traversable

func (*Window) Plans

func (w *Window) Plans() []Plan

func (*Window) Relation

func (w *Window) Relation() *Relation

func (*Window) String

func (w *Window) String() string

type WindowFunction

type WindowFunction struct {
	// Name is the function being applied.
	Name string
	// Args are the arguments to the function.
	Args []Expression
	// Filter is the optional filter condition that can be applied to the function.
	Filter Expression
	// contains filtered or unexported fields
}

WindowFunction references a function being applied in a window.

func (*WindowFunction) Accept

func (w *WindowFunction) Accept(v Visitor) any

func (*WindowFunction) Children

func (w *WindowFunction) Children() []Traversable

func (*WindowFunction) Field

func (w *WindowFunction) Field() *Field

func (*WindowFunction) Plans

func (w *WindowFunction) Plans() []Plan

func (*WindowFunction) String

func (w *WindowFunction) String() string

Jump to

Keyboard shortcuts

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