planbuilder

package
v2.0.0-beta.1+incompat... Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2016 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NoPlan = PlanID(iota)
	SelectUnsharded
	SelectEqual
	SelectIN
	SelectKeyrange
	SelectScatter
	UpdateUnsharded
	UpdateEqual
	DeleteUnsharded
	DeleteEqual
	InsertUnsharded
	InsertSharded
	NumPlans
)

The following constants define all the PlanID values.

View Source
const ListVarName = "_vals"

ListVarName is the bind var name used for plans that require VTGate to compute custom list values, like for IN clauses.

Variables

This section is empty.

Functions

func IsUnique

func IsUnique(v Vindex) bool

IsUnique returns true if the Vindex is Unique.

func Register

func Register(vindexType string, newVindexFunc NewVindexFunc)

Register registers a vindex under the specified vindexType. A duplicate vindexType will generate a panic. New vindexes will be created using these functions at the time of schema loading.

Types

type ByCost

type ByCost []*ColVindex

ByCost provides the interface needed for ColVindexes to be sorted by cost order.

func (ByCost) Len

func (bc ByCost) Len() int

func (ByCost) Less

func (bc ByCost) Less(i, j int) bool

func (ByCost) Swap

func (bc ByCost) Swap(i, j int)

type ClassFormal

type ClassFormal struct {
	ColVindexes []ColVindexFormal
}

ClassFormal is the info for each table class as loaded from the source.

type ColVindex

type ColVindex struct {
	Col    string
	Type   string
	Name   string
	Owned  bool
	Vindex Vindex
}

ColVindex contains the index info for each index of a table.

type ColVindexFormal

type ColVindexFormal struct {
	Col  string
	Name string
}

ColVindexFormal is the info for each indexed column of a table as loaded from the source.

type Functional

type Functional interface {
	Create(VCursor, interface{}) error
	Delete(VCursor, []interface{}, []byte) error
	Unique
}

A Functional vindex is an index that can compute the keyspace id from the id without a lookup. This means that the creation of a functional vindex entry does not take the keyspace id as input. In general, the main reason to support creation functions for functional indexes is for auto-generating ids. A Functional vindex is also required to be Unique. If it's not unique, we cannot determine the target shard for an insert operation.

type FunctionalGenerator

type FunctionalGenerator interface {
	Functional
	Generate(cursor VCursor) (id int64, err error)
}

A FunctionalGenerator vindex is a Functional vindex that can generate new ids.

type JoinBuilder

type JoinBuilder struct {
	// IsLeft is true if the operation is a left join.
	IsLeft bool

	// Left and Right are the nodes for the join.
	Left, Right PlanBuilder
	// contains filtered or unexported fields
}

JoinBuilder is used to build a Join primitive. It's used to buid a normal join or a left join operation. TODO(sougou): struct is incomplete.

func (*JoinBuilder) MarshalJSON

func (jb *JoinBuilder) MarshalJSON() ([]byte, error)

MarshalJSON marshals JoinBuilder into a readable form. It's used for testing and diagnostics. The representation cannot be used to reconstruct a JoinBuilder.

func (*JoinBuilder) Order

func (jb *JoinBuilder) Order() int

Order returns the order of the node.

type Keyspace

type Keyspace struct {
	Name    string
	Sharded bool
}

Keyspace contains the keyspcae info for each Table.

type KeyspaceFormal

type KeyspaceFormal struct {
	Sharded  bool
	Vindexes map[string]VindexFormal
	Classes  map[string]ClassFormal
	Tables   map[string]string
}

KeyspaceFormal is the keyspace info for each keyspace as loaded from the source.

type Lookup

type Lookup interface {
	Create(VCursor, interface{}, []byte) error
	Delete(VCursor, []interface{}, []byte) error
}

A Lookup vindex is one that needs to lookup a previously stored map to compute the keyspace id from an id. This means that the creation of a lookup vindex entry requires a keyspace id as input. A Lookup vindex need not be unique because the keyspace_id, which must be supplied, can be used to determine the target shard for an insert operation.

type LookupGenerator

type LookupGenerator interface {
	Lookup
	Generate(VCursor, []byte) (id int64, err error)
}

A LookupGenerator vindex is a Lookup that can generate new ids.

type NewVindexFunc

type NewVindexFunc func(map[string]interface{}) (Vindex, error)

A NewVindexFunc is a function that creates a Vindex based on the properties specified in the input map. Every vindex must register a NewVindexFunc under a unique vindexType.

type NonUnique

type NonUnique interface {
	Map(cursor VCursor, ids []interface{}) ([][][]byte, error)
}

NonUnique defines the interface for a non-unique vindex. This means that an id can map to multiple keyspace ids.

type Plan

type Plan struct {
	ID PlanID
	// Reason usually contains a string describing the reason
	// for why a certain plan was (or not) chosen.
	Reason string
	Table  *Table
	// Original is the original query.
	Original string
	// Rewritten is the rewritten query. This is empty for
	// all Unsharded plans since the Original query is sufficient.
	Rewritten string
	// Subquery is used for DeleteUnsharded to fetch the column values
	// for owned vindexes so they can be deleted.
	Subquery  string
	ColVindex *ColVindex
	// Values is a single or a list of values that are used
	// for making routing decisions.
	Values interface{}
}

Plan represents the routing strategy for a given query.

func BuildPlan

func BuildPlan(query string, schema *Schema) *Plan

BuildPlan builds a plan for a query based on the specified schema.

func (*Plan) IsMulti

func (pln *Plan) IsMulti() bool

IsMulti returns true if the SELECT query can potentially be sent to more than one shard.

func (*Plan) MarshalJSON

func (pln *Plan) MarshalJSON() ([]byte, error)

MarshalJSON serializes the Plan into a JSON representation.

func (*Plan) Size

func (pln *Plan) Size() int

Size is defined so that Plan can be given to an LRUCache.

type PlanBuilder

type PlanBuilder interface {
	// Order is a number that signifies execution order.
	// A lower Order number Route is executed before a
	// higher one. For a node that contains other nodes,
	// the Order represents the highest order of the leaf
	// nodes.
	Order() int
}

PlanBuilder represents any object that's used to build a plan. The top-level PlanBuilder will be a tree that points to other PlanBuilder objects. Currently, JoinBuilder and RouteBuilder are the only two supported PlanBuilder objects. More will be added as we extend the functionality. Each Builder object builds a Plan object, and they will mirror the same tree. Once all the plans are built, the builder objects will be discarded, and only the Plan objects will remain.

type PlanID

type PlanID int

PlanID is number representing the plan id.

func PlanByName

func PlanByName(s string) (id PlanID, ok bool)

PlanByName returns the PlanID from the plan name. If it cannot be found, then it returns NumPlans.

func (PlanID) MarshalJSON

func (id PlanID) MarshalJSON() ([]byte, error)

MarshalJSON serializes the plan id as a JSON string.

func (PlanID) String

func (id PlanID) String() string

type Reversible

type Reversible interface {
	ReverseMap(cursor VCursor, ks []byte) (interface{}, error)
}

A Reversible vindex is one that can perform a reverse lookup from a keyspace id to an id. This is optional. If present, VTGate can use it to fill column values based on the target keyspace id.

type Route

type Route struct {
	// PlanID will be one of the Select IDs from PlanID.
	PlanID PlanID
	// Keypsace represents the keyspace to which
	// the query will be sent.
	Keyspace *Keyspace
	// Vindex represents the vindex that will be used
	// to resolve the route.
	Vindex Vindex `json:",omitempty"`
	// Values can be a single value or a list of
	// values that will be used as input to the Vindex
	// to compute the target shard(s) where the query must
	// be sent.
	// TODO(sougou): explain contents of Values.
	Values interface{} `json:",omitempty"`
}

Route is a Plan object that represents a route. It can be any one of the Select primitives from PlanID. Some plan ids correspond to a multi-shard query, and some are for a single-shard query. The rules of what can be merged, or what can be pushed down depend on the PlanID. They're explained in code where such decisions are made. TODO(sougou): struct is incomplete. TODO(sougou): integrate with the older v3 Plan.

type RouteBuilder

type RouteBuilder struct {
	// From points to the portion of the AST this route represents.
	From sqlparser.TableExpr

	// Route is the plan object being built. It will contain all the
	// information necessary to execute the route operation.
	Route *Route
	// contains filtered or unexported fields
}

RouteBuilder is used to build a Route primitive. It's used to build one of the Select routes like SelectScatter, etc. Portions of the original Select AST are moved into this node, which will be used to build the final SQL for this route. TODO(sougou): struct is incomplete.

func (*RouteBuilder) MarshalJSON

func (rtb *RouteBuilder) MarshalJSON() ([]byte, error)

MarshalJSON marshals RouteBuilder into a readable form. It's used for testing and diagnostics. The representation cannot be used to reconstruct a RouteBuilder.

func (*RouteBuilder) Order

func (rtb *RouteBuilder) Order() int

Order returns the order of the node.

type Schema

type Schema struct {
	Tables map[string]*Table
}

Schema represents the denormalized version of SchemaFormal, used for building routing plans.

func BuildSchema

func BuildSchema(source *SchemaFormal) (schema *Schema, err error)

BuildSchema builds a Schema from a SchemaFormal.

func LoadFile

func LoadFile(filename string) (schema *Schema, err error)

LoadFile creates a new Schema from a JSON file.

func NewSchema

func NewSchema(data []byte) (schema *Schema, err error)

NewSchema creates a new Schema from a JSON byte array.

func (*Schema) FindTable

func (schema *Schema) FindTable(tablename string) (table *Table, reason string)

FindTable returns a pointer to the Table if found. Otherwise, it returns a reason, which is equivalent to an error.

type SchemaFormal

type SchemaFormal struct {
	Keyspaces map[string]KeyspaceFormal
}

SchemaFormal is the formal representation of the schema as loaded from the source.

type SymbolTable

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

SymbolTable contains the symbols for a SELECT statement. If it's for a subquery, it points to an outer scope. For now, it only contains tables.

func NewSymbolTable

func NewSymbolTable(alias sqlparser.SQLName, table *Table, route *RouteBuilder) *SymbolTable

NewSymbolTable creates a new SymbolTable initialized to contain the provided table alias.

func (*SymbolTable) Add

func (smt *SymbolTable) Add(symbols *SymbolTable) error

Add merges the new symbol table into the current one without merging their routes. This means that the new symbols will belong to different routes

func (*SymbolTable) FindColumn

func (smt *SymbolTable) FindColumn(col *sqlparser.ColName, autoResolve bool) (*TableAlias, *ColVindex)

FindColumn identifies the table referenced in the column expression. It also returns the ColVindex if one exists for the column. If autoResolve is true, and there is only one table in the symbol table, then an unqualified reference is assumed to be implicitly against that table. The table info doesn't contain the full list of columns. So, any column reference is presumed valid until execution time.

func (*SymbolTable) Merge

func (smt *SymbolTable) Merge(symbols *SymbolTable, route *RouteBuilder) error

Merge merges the new symbol table into the current one and makes all the tables part of the specified RouteBuilder.

type Table

type Table struct {
	Name        string
	Keyspace    *Keyspace
	ColVindexes []*ColVindex
	Ordered     []*ColVindex
	Owned       []*ColVindex
}

Table represnts a table in Schema.

type TableAlias

type TableAlias struct {
	// Name represents the name of the alias.
	Name sqlparser.SQLName
	// Keyspace points to the keyspace to which this
	// alias belongs.
	Keyspace *Keyspace
	// CoVindexes is the list of column Vindexes for this alias.
	ColVindexes []*ColVindex
	// Route points to the RouteBuilder object under which this alias
	// was created.
	Route *RouteBuilder
}

TableAlias is part of SymbolTable. It represnts a table alias in a FROM clause. TODO(sougou): Update comments after the struct is finalized.

type Unique

type Unique interface {
	Map(cursor VCursor, ids []interface{}) ([][]byte, error)
}

Unique defines the interface for a unique vindex. For a vindex to be unique, an id has to map to at most one keyspace id.

type VCursor

type VCursor interface {
	Execute(query *querytypes.BoundQuery) (*sqltypes.Result, error)
}

A VCursor is an interface that allows you to execute queries in the current context and session of a VTGate request. Vindexes can use this interface to execute lookup queries.

type Vindex

type Vindex interface {
	// Cost is used by planbuilder to prioritize vindexes.
	// The cost can be 0 if the id is basically a keyspace id.
	// The cost can be 1 if the id can be hashed to a keyspace id.
	// The cost can be 2 or above if the id needs to be looked up
	// from an external data source. These guidelines are subject
	// to change in the future.
	Cost() int

	// Verify must be implented by all vindexes. It should return
	// true if the id can be mapped to the keyspace id.
	Verify(cursor VCursor, id interface{}, ks []byte) (bool, error)
}

Vindex defines the interface required to register a vindex. Additional to these functions, a vindex also needs to satisfy the Unique or NonUnique interface.

func CreateVindex

func CreateVindex(vindexType string, params map[string]interface{}) (Vindex, error)

CreateVindex creates a vindex of the specified type using the supplied params. The type must have been previously registered.

type VindexFormal

type VindexFormal struct {
	Type   string
	Params map[string]interface{}
	Owner  string
}

VindexFormal is the info for each index as loaded from the source.

Jump to

Keyboard shortcuts

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