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 AggregationNode
- func (n *AggregationNode) Bind(tx *database.Transaction, params []expr.Param) (err error)
- func (n *AggregationNode) Left() Node
- func (n *AggregationNode) Operation() Operation
- func (n *AggregationNode) Right() Node
- func (n *AggregationNode) SetLeft(ln Node)
- func (n *AggregationNode) SetRight(rn Node)
- func (n *AggregationNode) String() string
- type ExplainStmt
- type GroupingNode
- func (n *GroupingNode) Bind(tx *database.Transaction, params []expr.Param) (err error)
- func (n *GroupingNode) Left() Node
- func (n *GroupingNode) Operation() Operation
- func (n *GroupingNode) Right() Node
- func (n *GroupingNode) SetLeft(ln Node)
- func (n *GroupingNode) SetRight(rn Node)
- func (n *GroupingNode) String() string
- type IndexIteratorOperator
- type Node
- func NewAggregationNode(n Node, aggregators []document.AggregatorBuilder) Node
- func NewDedupNode(n Node, tableName string) Node
- func NewDeletionNode(n Node, tableName string) Node
- func NewGroupingNode(n Node, e expr.Expr) Node
- func NewIndexInputNode(tableName, indexName string, iop IndexIteratorOperator, path expr.Path, ...) Node
- func NewLimitNode(n Node, limit int) Node
- func NewOffsetNode(n Node, offset int) Node
- func NewProjectionNode(n Node, expressions []ProjectedField, tableName string) Node
- func NewReplacementNode(n Node, tableName string) Node
- func NewSelectionNode(n Node, cond expr.Expr) Node
- func NewSetNode(n Node, path document.Path, e expr.Expr) Node
- func NewSortNode(n Node, sortField expr.Path, direction scanner.Token) Node
- func NewTableInputNode(tableName string) Node
- func NewUnsetNode(n Node, field string) Node
- type Operation
- type ProjectedExpr
- type ProjectedField
- type ProjectedGroupAggregatorBuilder
- 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 Tree
- func NewTree(n Node) *Tree
- func Optimize(t *Tree) (*Tree, error)
- func PrecalculateExprRule(t *Tree) (*Tree, error)
- func RemoveUnnecessaryDedupNodeRule(t *Tree) (*Tree, error)
- func RemoveUnnecessarySelectionNodesRule(t *Tree) (*Tree, error)
- func SplitANDConditionRule(t *Tree) (*Tree, error)
- func UseIndexBasedOnSelectionNodeRule(t *Tree) (*Tree, error)
- type Wildcard
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AggregationNode ¶ added in v0.10.0
type AggregationNode struct { Aggregators []document.AggregatorBuilder // contains filtered or unexported fields }
An AggregationNode is a node that uses aggregate functions to aggregate documents from the stream into one document. If the stream contains a Group node, it will generate one document per group.
func (*AggregationNode) Bind ¶ added in v0.10.0
func (n *AggregationNode) Bind(tx *database.Transaction, params []expr.Param) (err error)
Bind database resources to this node.
func (*AggregationNode) Operation ¶ added in v0.10.0
func (n *AggregationNode) Operation() Operation
func (*AggregationNode) String ¶ added in v0.10.0
func (n *AggregationNode) String() string
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 GroupingNode ¶ added in v0.8.0
type GroupingNode struct { Tx *database.Transaction Params []expr.Param Expr expr.Expr // contains filtered or unexported fields }
A GroupingNode is a node that groups documents by value.
func (*GroupingNode) Bind ¶ added in v0.8.0
func (n *GroupingNode) Bind(tx *database.Transaction, params []expr.Param) (err error)
Bind database resources to this node.
func (*GroupingNode) String ¶ added in v0.8.0
func (n *GroupingNode) String() string
type IndexIteratorOperator ¶
type IndexIteratorOperator interface {
IterateIndex(idx *database.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 NewAggregationNode ¶ added in v0.10.0
func NewAggregationNode(n Node, aggregators []document.AggregatorBuilder) Node
NewAggregationNode creates an AggregationNode.
func NewDedupNode ¶ added in v0.9.0
func NewDeletionNode ¶
NewDeletionNode creates a node that delete every document of a stream from their respective table.
func NewGroupingNode ¶ added in v0.8.0
NewGroupingNode creates a GroupingNode.
func NewIndexInputNode ¶
func NewIndexInputNode(tableName, indexName string, iop IndexIteratorOperator, path expr.Path, 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 []ProjectedField, 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 value at the given path for every document of the stream.
func NewSortNode ¶
NewSortNode creates a node that sorts a stream according to a given document path 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 removes a value at a given path 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 path 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 path and a direction. Sort // Set is an operation that adds a value or replaces at a given path for every document of the stream. Set // Unset is an operation that removes a value at a given path from every document of a stream Unset // Group is an operation that groups documents based on a given path. Group // Aggregation is an operation that creates one document per group of documents. Aggregation // Dedup is an operation that removes duplicate documents from a stream Dedup )
type ProjectedExpr ¶ added in v0.8.0
ProjectedExpr turns any expression into a ResultField.
func (ProjectedExpr) Iterate ¶ added in v0.8.0
func (r ProjectedExpr) Iterate(env *expr.Environment, fn func(field string, value document.Value) error) error
Iterate evaluates Expr and calls fn once with the result.
func (ProjectedExpr) Name ¶ added in v0.8.0
func (r ProjectedExpr) Name() string
Name returns the raw expression.
func (ProjectedExpr) String ¶ added in v0.8.0
func (r ProjectedExpr) String() string
type ProjectedField ¶ added in v0.8.0
type ProjectedField interface { Iterate(env *expr.Environment, fn func(field string, value document.Value) error) error Name() string }
A ProjectedField is a field that will be part of the projected document that will be returned at the end of a Select statement.
type ProjectedGroupAggregatorBuilder ¶ added in v0.10.0
type ProjectedGroupAggregatorBuilder struct { Expr expr.Expr // contains filtered or unexported fields }
ProjectedGroupAggregatorBuilder references the expression used in the GROUP BY clause so that it can be used in the SELECT clause.
func (*ProjectedGroupAggregatorBuilder) Aggregator ¶ added in v0.10.0
func (p *ProjectedGroupAggregatorBuilder) Aggregator(group document.Value) document.Aggregator
Aggregator implements the document.AggregatorBuilder interface. It creates a projectedGroupAggregator.
func (*ProjectedGroupAggregatorBuilder) String ¶ added in v0.10.0
func (p *ProjectedGroupAggregatorBuilder) String() string
type ProjectionNode ¶
type ProjectionNode struct { Expressions []ProjectedField // 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 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 RemoveUnnecessaryDedupNodeRule ¶ added in v0.9.0
RemoveUnnecessaryDedupNodeRule removes any Dedup nodes where projection is already unique.
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 path expression 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.