planner

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 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 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 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

func NewDeletionNode(n Node, tableName string) Node

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

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 []ResultField, 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, field string, e expr.Expr) Node

NewSetNode creates a node that adds or replaces a field for every document of the stream.

func NewSortNode

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

NewSortNode creates a node that sorts a stream according to a given document field 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 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
)

func (Operation) String

func (i Operation) String() string

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

type ResultFieldExpr struct {
	expr.Expr

	ExprName string
}

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

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(stack expr.EvalStack, 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