planner

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2021 License: MIT Imports: 15 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(t *Tree, tx *database.Transaction, params []expr.Param) error

Bind updates every node that refers to a database ressource.

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) Left added in v0.10.0

func (n *AggregationNode) Left() Node

func (*AggregationNode) Operation added in v0.10.0

func (n *AggregationNode) Operation() Operation

func (*AggregationNode) Right added in v0.10.0

func (n *AggregationNode) Right() Node

func (*AggregationNode) SetLeft added in v0.10.0

func (n *AggregationNode) SetLeft(ln Node)

func (*AggregationNode) SetRight added in v0.10.0

func (n *AggregationNode) SetRight(rn Node)

func (*AggregationNode) String added in v0.10.0

func (n *AggregationNode) String() string

type ExplainStmt

type ExplainStmt struct {
	Statement query.Statement
}

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) Left added in v0.8.0

func (n *GroupingNode) Left() Node

func (*GroupingNode) Operation added in v0.8.0

func (n *GroupingNode) Operation() Operation

func (*GroupingNode) Right added in v0.8.0

func (n *GroupingNode) Right() Node

func (*GroupingNode) SetLeft added in v0.8.0

func (n *GroupingNode) SetLeft(ln Node)

func (*GroupingNode) SetRight added in v0.8.0

func (n *GroupingNode) SetRight(rn 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 NewDedupNode(n Node, tableName string) Node

func NewDeletionNode

func NewDeletionNode(n Node, tableName string) Node

NewDeletionNode creates a node that delete every document of a stream from their respective table.

func NewGroupingNode added in v0.8.0

func NewGroupingNode(n Node, e expr.Expr) Node

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

func NewLimitNode(n Node, limit int) Node

NewLimitNode creates a node that limits the number of documents processed by the stream.

func NewOffsetNode

func NewOffsetNode(n Node, offset int) Node

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

func NewReplacementNode(n Node, tableName string) Node

NewReplacementNode creates a node that stores every document of a stream in their respective table and primary keys.

func NewSelectionNode

func NewSelectionNode(n Node, cond expr.Expr) Node

NewSelectionNode creates a node that filters documents of a stream, according to the expression condition.

func NewSetNode

func NewSetNode(n Node, path document.Path, e expr.Expr) Node

NewSetNode creates a node that adds or replaces a value at the given path for every document of the stream.

func NewSortNode

func NewSortNode(n Node, sortField expr.Path, direction scanner.Token) Node

NewSortNode creates a node that sorts a stream according to a given document path and a sort direction.

func NewTableInputNode

func NewTableInputNode(tableName string) Node

NewTableInputNode creates an input node that can be used to read documents from a table.

func NewUnsetNode

func NewUnsetNode(n Node, field string) Node

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
)

func (Operation) String

func (i Operation) String() string

type ProjectedExpr added in v0.8.0

type ProjectedExpr struct {
	expr.Expr

	ExprName string
}

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

Aggregator implements the document.AggregatorBuilder interface. It creates a projectedGroupAggregator.

func (*ProjectedGroupAggregatorBuilder) String added in v0.10.0

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) Left

func (n *ProjectionNode) Left() Node

func (*ProjectionNode) Operation

func (n *ProjectionNode) Operation() Operation

func (*ProjectionNode) Right

func (n *ProjectionNode) Right() Node

func (*ProjectionNode) SetLeft

func (n *ProjectionNode) SetLeft(ln Node)

func (*ProjectionNode) SetRight

func (n *ProjectionNode) SetRight(rn 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 NewTree

func NewTree(n Node) *Tree

NewTree creates a new tree with n as root.

func Optimize

func Optimize(t *Tree) (*Tree, error)

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

func PrecalculateExprRule(t *Tree) (*Tree, error)

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

func RemoveUnnecessaryDedupNodeRule(t *Tree) (*Tree, error)

RemoveUnnecessaryDedupNodeRule removes any Dedup nodes where projection is already unique.

func RemoveUnnecessarySelectionNodesRule

func RemoveUnnecessarySelectionNodesRule(t *Tree) (*Tree, error)

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

func SplitANDConditionRule(t *Tree) (*Tree, error)

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

func UseIndexBasedOnSelectionNodeRule(t *Tree) (*Tree, error)

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

func (t *Tree) IsReadOnly() bool

IsReadOnly implements the query.Statement interface.

func (*Tree) Run

func (t *Tree) Run(tx *database.Transaction, params []expr.Param) (query.Result, error)

Run implements the query.Statement interface. It binds the tree to the database resources and executes it.

func (*Tree) String

func (t *Tree) String() string

type Wildcard

type Wildcard struct{}

A Wildcard is a ResultField that iterates over all the fields of a document.

func (Wildcard) Iterate

func (w Wildcard) Iterate(env *expr.Environment, fn func(field string, value document.Value) error) error

Iterate call the document iterate method.

func (Wildcard) Name

func (w Wildcard) Name() string

Name returns the "*" character.

func (Wildcard) String

func (w Wildcard) String() string

Jump to

Keyboard shortcuts

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