Documentation
¶
Overview ¶
Package planner provides types to describe and manage the lifecycle of a query. A query is represented as a tree, which itself represents a stream of documents. Each node of the tree is an operation that transforms that stream, following rules of relational algebra. Once a tree is created, it can be optimized by a list of rules.
Index ¶
- func Bind(t *Tree, tx *database.Transaction, params []expr.Param) error
- type ExplainStmt
- type IndexIteratorOperator
- type Node
- func NewDeletionNode(n Node, tableName string) Node
- func NewIndexInputNode(tableName, indexName string, iop IndexIteratorOperator, filter expr.Expr, ...) Node
- func NewLimitNode(n Node, limit int) Node
- func NewOffsetNode(n Node, offset int) Node
- func NewProjectionNode(n Node, expressions []ResultField, tableName string) Node
- func NewReplacementNode(n Node, tableName string) Node
- func NewSelectionNode(n Node, cond expr.Expr) Node
- func NewSetNode(n Node, field string, e expr.Expr) Node
- func NewSortNode(n Node, sortField expr.FieldSelector, direction scanner.Token) Node
- func NewTableInputNode(tableName string) Node
- func NewUnsetNode(n Node, field string) Node
- type Operation
- type ProjectionNode
- func (n *ProjectionNode) Bind(tx *database.Transaction, params []expr.Param) (err error)
- func (n *ProjectionNode) Left() Node
- func (n *ProjectionNode) Operation() Operation
- func (n *ProjectionNode) Right() Node
- func (n *ProjectionNode) SetLeft(ln Node)
- func (n *ProjectionNode) SetRight(rn Node)
- func (n *ProjectionNode) String() string
- type ResultField
- type ResultFieldExpr
- type Tree
- type Wildcard
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ExplainStmt ¶
ExplainStmt is a query.Statement that displays information about how a statement is going to be executed, without executing it.
func (*ExplainStmt) IsReadOnly ¶
func (s *ExplainStmt) IsReadOnly() bool
IsReadOnly indicates that this statement doesn't write anything into the database.
func (*ExplainStmt) Run ¶
func (s *ExplainStmt) Run(tx *database.Transaction, params []expr.Param) (query.Result, error)
Run analyses the inner statement and displays its execution plan. If the statement is a tree, Bind and Optimize will be called prior to displaying all the operations. Explain currently only works on SELECT, UPDATE and DELETE statements.
type IndexIteratorOperator ¶
type IndexIteratorOperator interface {
IterateIndex(idx index.Index, tb *database.Table, v document.Value, fn func(d document.Document) error) error
}
IndexIteratorOperator is an operator that can be used as an input node.
type Node ¶
type Node interface { Operation() Operation Left() Node Right() Node SetLeft(Node) SetRight(Node) Bind(tx *database.Transaction, params []expr.Param) error }
A Node represents an operation on the stream.
func NewDeletionNode ¶
NewDeletionNode creates a node that delete every document of a stream from their respective table.
func NewIndexInputNode ¶
func NewIndexInputNode(tableName, indexName string, iop IndexIteratorOperator, filter expr.Expr, orderByDirection scanner.Token) Node
NewIndexInputNode creates a node that can be used to read documents using an index.
func NewLimitNode ¶
NewLimitNode creates a node that limits the number of documents processed by the stream.
func NewOffsetNode ¶
NewOffsetNode creates a node that skips a certain number of documents from the stream.
func NewProjectionNode ¶
func NewProjectionNode(n Node, expressions []ResultField, tableName string) Node
NewProjectionNode creates a ProjectionNode.
func NewReplacementNode ¶
NewReplacementNode creates a node that stores every document of a stream in their respective table and primary keys.
func NewSelectionNode ¶
NewSelectionNode creates a node that filters documents of a stream, according to the expression condition.
func NewSetNode ¶
NewSetNode creates a node that adds or replaces a field for every document of the stream.
func NewSortNode ¶
NewSortNode creates a node that sorts a stream according to a given document field and a sort direction.
func NewTableInputNode ¶
NewTableInputNode creates an input node that can be used to read documents from a table.
func NewUnsetNode ¶
NewUnsetNode creates a node that adds or replaces a field for every document of the stream.
type Operation ¶
type Operation int
An Operation can manipulate and transform a stream of documents.
const ( // Input is a node from where data is read. It represents a stream of documents. Input Operation = iota // Selection (σ) is an operation that filters documents that satisfy a given condition. Selection // Projection (∏) is an operation that selects a list of fields from each document of a stream. Projection // Rename (ρ) is an operation that renames a field from each document of a stream. Rename // Deletion is an operation that removes all of the documents of a stream from their respective table. Deletion // Replacement is an operation that stores every document of a stream in their respective keys. Replacement // Limit is an operation that only allows a certain number of documents to be processed // by the stream. Limit // Skip is an operation that ignores a certain number of documents. Skip // Sort is an operation that sorts a stream of document according to a given field and a direction. Sort // Set is an operation that adds or replaces a field for every document of the stream. Set // Unset is an operation that removes a field from every document of a stream Unset )
type ProjectionNode ¶
type ProjectionNode struct { Expressions []ResultField // contains filtered or unexported fields }
A ProjectionNode is a node that uses the given expressions to create a new document for each document of the stream. Each expression can extract fields from the incoming document, call functions, execute arithmetic operations. etc.
func (*ProjectionNode) Bind ¶
func (n *ProjectionNode) Bind(tx *database.Transaction, params []expr.Param) (err error)
Bind database resources to this node.
func (*ProjectionNode) String ¶
func (n *ProjectionNode) String() string
type ResultField ¶
type ResultField interface { Iterate(stack expr.EvalStack, fn func(field string, value document.Value) error) error Name() string }
A ResultField is a field that will be part of the result document that will be returned at the end of a Select statement.
type ResultFieldExpr ¶
ResultFieldExpr turns any expression into a ResultField.
func (ResultFieldExpr) Iterate ¶
func (r ResultFieldExpr) Iterate(stack expr.EvalStack, fn func(field string, value document.Value) error) error
Iterate evaluates Expr and calls fn once with the result.
func (ResultFieldExpr) Name ¶
func (r ResultFieldExpr) Name() string
Name returns the raw expression.
func (ResultFieldExpr) String ¶
func (r ResultFieldExpr) String() string
type Tree ¶
type Tree struct {
Root Node
}
A Tree describes the flow of a stream of documents. Each node will manipulate the stream using relational algebra operations.
func Optimize ¶
Optimize takes a tree, applies a list of optimization rules and returns an optimized tree. Depending on the rule, the tree may be modified in place or replaced by a new one.
func PrecalculateExprRule ¶
PrecalculateExprRule evaluates any constant sub-expression that can be evaluated before running the query and replaces it by the result of the evaluation. The result of constant sub-expressions, like "3 + 4", is always the same and thus can be precalculated. Examples:
3 + 4 --> 7 3 + 1 > 10 - a --> 4 > 10 - a
func RemoveUnnecessarySelectionNodesRule ¶
RemoveUnnecessarySelectionNodesRule removes any selection node whose condition is a constant expression that evaluates to a truthy value. if it evaluates to a falsy value, it considers that the tree will not stream any document, so it returns an empty tree.
func SplitANDConditionRule ¶
SplitANDConditionRule splits any selection node whose condition is one or more AND operators into one or more selection nodes. The condition won't be split if the expression tree contains an OR operation. Example:
this: σ(a > 2 AND b != 3 AND c < 2) becomes this: σ(a > 2) σ(b != 3) σ(c < 2)
func UseIndexBasedOnSelectionNodeRule ¶
UseIndexBasedOnSelectionNodeRule scans the tree for the first selection node whose condition is an operator that satisfies the following criterias: - implements the indexIteratorOperator interface - one of its operands is a field selector that is indexed - the other operand is a literal value or a parameter If found, it will replace the input node by an indexInputNode using this index.
func (*Tree) IsReadOnly ¶
IsReadOnly implements the query.Statement interface.