semantics

package
v0.16.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSingleTable refers to an error happening when something should be used only for single tables
	ErrNotSingleTable = vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] should only be used for single tables")
)

Functions

func GetSubqueryAndOtherSide added in v0.13.0

func GetSubqueryAndOtherSide(node *sqlparser.ComparisonExpr) (*sqlparser.Subquery, sqlparser.Expr)

GetSubqueryAndOtherSide returns the subquery and other side of a comparison, iff one of the sides is a SubQuery

func RewriteDerivedTableExpression added in v0.15.0

func RewriteDerivedTableExpression(expr sqlparser.Expr, vt TableInfo) sqlparser.Expr

RewriteDerivedTableExpression rewrites all the ColName instances in the supplied expression with the expressions behind the column definition of the derived table SELECT foo FROM (SELECT id+42 as foo FROM user) as t We need `foo` to be translated to `id+42` on the inside of the derived table

func ValidAsMapKey added in v0.13.0

func ValidAsMapKey(s sqlparser.SQLNode) bool

Types

type ColumnInfo added in v0.12.0

type ColumnInfo struct {
	Name string
	Type Type
}

ColumnInfo contains information about columns

type DerivedTable added in v0.13.0

type DerivedTable struct {
	ASTNode *sqlparser.AliasedTableExpr
	// contains filtered or unexported fields
}

DerivedTable contains the information about the projection, tables involved in derived table.

func (*DerivedTable) GetVindexTable added in v0.13.0

func (dt *DerivedTable) GetVindexTable() *vindexes.Table

GetVindexTable implements the TableInfo interface

func (*DerivedTable) IsInfSchema added in v0.13.0

func (dt *DerivedTable) IsInfSchema() bool

IsInfSchema implements the TableInfo interface

func (*DerivedTable) Name added in v0.13.0

func (dt *DerivedTable) Name() (sqlparser.TableName, error)

Name implements the TableInfo interface

type ErrType added in v0.16.0

type ErrType int
const (
	Other ErrType = iota
	Unsupported
	Bug
)

type Error added in v0.16.0

type Error struct {
	Code ErrorCode
	// contains filtered or unexported fields
}

func NewError added in v0.16.0

func NewError(code ErrorCode, args ...any) *Error

func (*Error) Error added in v0.16.0

func (n *Error) Error() string

func (*Error) ErrorCode added in v0.16.0

func (n *Error) ErrorCode() vtrpcpb.Code

func (*Error) ErrorState added in v0.16.0

func (n *Error) ErrorState() vterrors.State

type ErrorCode added in v0.16.0

type ErrorCode int
const (
	UnionColumnsDoNotMatch ErrorCode = iota
	UnsupportedMultiTablesInUpdate
	UnsupportedNaturalJoin
	TableNotUpdatable
	UnionWithSQLCalcFoundRows
	SQLCalcFoundRowsUsage
	CantUseOptionHere
	MissingInVSchema
	NotSequenceTable
	NextWithMultipleTables
	LockOnlyWithDual
	QualifiedOrderInUnion
	JSONTables
	Buggy
	ColumnNotFound
	AmbiguousColumn
)

type ExprDependencies added in v0.12.0

type ExprDependencies map[sqlparser.Expr]TableSet

ExprDependencies stores the tables that an expression depends on as a map

type FakeSI added in v0.11.0

type FakeSI struct {
	Tables       map[string]*vindexes.Table
	VindexTables map[string]vindexes.Vindex
}

FakeSI is a fake SchemaInformation for testing

func (FakeSI) ConnCollation added in v0.13.0

func (FakeSI) ConnCollation() collations.ID

func (*FakeSI) FindTableOrVindex added in v0.11.0

FindTableOrVindex implements the SchemaInformation interface

type ProjError added in v0.12.0

type ProjError struct {
	Inner error
}

ProjError is used to mark an error as something that should only be returned if the planner fails to merge everything down to a single route

func (ProjError) Error added in v0.12.0

func (p ProjError) Error() string

type RealTable added in v0.12.0

type RealTable struct {
	ASTNode *sqlparser.AliasedTableExpr
	Table   *vindexes.Table
	// contains filtered or unexported fields
}

RealTable contains the alias table expr and vindex table

func (*RealTable) GetVindexTable added in v0.12.0

func (r *RealTable) GetVindexTable() *vindexes.Table

GetVindexTable implements the TableInfo interface

func (*RealTable) IsInfSchema added in v0.12.0

func (r *RealTable) IsInfSchema() bool

IsInfSchema implements the TableInfo interface

func (*RealTable) Name added in v0.12.0

func (r *RealTable) Name() (sqlparser.TableName, error)

Name implements the TableInfo interface

type SchemaInformation added in v0.11.0

type SchemaInformation interface {
	FindTableOrVindex(tablename sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error)
	ConnCollation() collations.ID
}

SchemaInformation is used tp provide table information from Vschema.

type SemTable

type SemTable struct {
	Tables []TableInfo

	// NotSingleRouteErr stores any errors that have to be generated if the query cannot be planned as a single route.
	NotSingleRouteErr error
	// NotUnshardedErr stores any errors that have to be generated if the query is not unsharded.
	NotUnshardedErr error

	// Recursive contains the dependencies from the expression to the actual tables
	// in the query (i.e. not including derived tables). If an expression is a column on a derived table,
	// this map will contain the accumulated dependencies for the column expression inside the derived table
	Recursive ExprDependencies

	// Direct keeps information about the closest dependency for an expression.
	// It does not recurse inside derived tables and the like to find the original dependencies
	Direct ExprDependencies

	ExprTypes map[sqlparser.Expr]Type

	Comments    *sqlparser.ParsedComments
	SubqueryMap map[sqlparser.Statement][]*sqlparser.ExtractedSubquery
	SubqueryRef map[*sqlparser.Subquery]*sqlparser.ExtractedSubquery

	// ColumnEqualities is used to enable transitive closures
	// if a == b and b == c then a == c
	ColumnEqualities map[columnName][]sqlparser.Expr

	// DefaultCollation is the default collation for this query, which is usually
	// inherited from the connection's default collation.
	Collation collations.ID

	Warning string

	// ExpandedColumns is a map of all the added columns for a given table.
	ExpandedColumns map[sqlparser.TableName][]*sqlparser.ColName
	// contains filtered or unexported fields
}

SemTable contains semantic analysis information about the query.

func Analyze added in v0.11.0

func Analyze(statement sqlparser.Statement, currentDb string, si SchemaInformation) (*SemTable, error)

Analyze analyzes the parsed query.

func EmptySemTable added in v0.13.0

func EmptySemTable() *SemTable

EmptySemTable creates a new empty SemTable

func (*SemTable) ASTEquals added in v0.16.0

func (st *SemTable) ASTEquals() *sqlparser.Comparator

ASTEquals returns a sqlparser.Comparator that uses the semantic information in this SemTable to explicitly compare column names for equality.

func (*SemTable) AddColumnEquality added in v0.12.0

func (st *SemTable) AddColumnEquality(colName *sqlparser.ColName, expr sqlparser.Expr)

AddColumnEquality adds a relation of the given colName to the ColumnEqualities map

func (*SemTable) AddExprs added in v0.11.0

func (st *SemTable) AddExprs(tbl *sqlparser.AliasedTableExpr, cols sqlparser.SelectExprs)

AddExprs adds new select exprs to the SemTable.

func (*SemTable) AndExpressions added in v0.16.0

func (st *SemTable) AndExpressions(exprs ...sqlparser.Expr) sqlparser.Expr

AndExpressions ands together two or more expressions, minimising the expr when possible

func (*SemTable) CollationForExpr added in v0.13.0

func (st *SemTable) CollationForExpr(e sqlparser.Expr) collations.ID

CollationForExpr returns the collation name of expressions in the query

func (*SemTable) ColumnLookup added in v0.13.0

func (st *SemTable) ColumnLookup(*sqlparser.ColName) (int, error)

ColumnLookup implements the TranslationLookup interface

func (*SemTable) ContainsExpr added in v0.16.0

func (st *SemTable) ContainsExpr(e sqlparser.Expr, expres []sqlparser.Expr) bool

func (*SemTable) CopyDependencies added in v0.12.0

func (st *SemTable) CopyDependencies(from, to sqlparser.Expr)

CopyDependencies copies the dependencies from one expression into the other

func (*SemTable) CopyExprInfo added in v0.13.0

func (st *SemTable) CopyExprInfo(src, dest sqlparser.Expr)

CopyExprInfo lookups src in the ExprTypes map and, if a key is found, assign the corresponding Type value of src to dest.

func (*SemTable) DefaultCollation added in v0.13.0

func (st *SemTable) DefaultCollation() collations.ID

func (*SemTable) DirectDeps added in v0.12.0

func (st *SemTable) DirectDeps(expr sqlparser.Expr) TableSet

DirectDeps return the table dependencies of the expression.

func (*SemTable) EqualsExpr added in v0.16.0

func (st *SemTable) EqualsExpr(a, b sqlparser.Expr) bool

EqualsExpr compares two expressions using the semantic analysis information. This means that we use the binding info to recognize that two ColName's can point to the same table column even though they are written differently. Example would be the `foobar` column in the following query: `SELECT foobar FROM tbl ORDER BY tbl.foobar` The expression in the select list is not equal to the one in the ORDER BY, but they point to the same column and would be considered equal by this method

func (*SemTable) FindSubqueryReference added in v0.13.0

func (st *SemTable) FindSubqueryReference(subquery *sqlparser.Subquery) *sqlparser.ExtractedSubquery

FindSubqueryReference goes over the sub queries and searches for it by value equality instead of reference equality

func (*SemTable) GetExprAndEqualities added in v0.12.0

func (st *SemTable) GetExprAndEqualities(expr sqlparser.Expr) []sqlparser.Expr

GetExprAndEqualities returns a slice containing the given expression, and it's known equalities if any

func (*SemTable) GetSelectTables added in v0.11.0

func (st *SemTable) GetSelectTables(node *sqlparser.Select) []TableInfo

GetSelectTables returns the table in the select.

func (*SemTable) GetSubqueryNeedingRewrite added in v0.13.0

func (st *SemTable) GetSubqueryNeedingRewrite() []*sqlparser.ExtractedSubquery

GetSubqueryNeedingRewrite returns a list of sub-queries that need to be rewritten

func (*SemTable) NeedsWeightString added in v0.14.0

func (st *SemTable) NeedsWeightString(e sqlparser.Expr) bool

NeedsWeightString returns true if the given expression needs weight_string to do safe comparisons

func (*SemTable) RecursiveDeps added in v0.12.0

func (st *SemTable) RecursiveDeps(expr sqlparser.Expr) TableSet

RecursiveDeps return the table dependencies of the expression.

func (*SemTable) ReplaceTableSetFor added in v0.13.1

func (st *SemTable) ReplaceTableSetFor(id TableSet, t *sqlparser.AliasedTableExpr)

ReplaceTableSetFor replaces the given single TabletSet with the new *sqlparser.AliasedTableExpr

func (*SemTable) SingleUnshardedKeyspace added in v0.13.0

func (st *SemTable) SingleUnshardedKeyspace() (*vindexes.Keyspace, []*vindexes.Table)

SingleUnshardedKeyspace returns the single keyspace if all tables in the query are in the same, unsharded keyspace

func (*SemTable) TableInfoFor added in v0.11.0

func (st *SemTable) TableInfoFor(id TableSet) (TableInfo, error)

TableInfoFor returns the table info for the table set. It should contains only single table.

func (*SemTable) TableInfoForExpr added in v0.12.0

func (st *SemTable) TableInfoForExpr(expr sqlparser.Expr) (TableInfo, error)

TableInfoForExpr returns the table info of the table that this expression depends on. Careful: this only works for expressions that have a single table dependency

func (*SemTable) TableSetFor

func (st *SemTable) TableSetFor(t *sqlparser.AliasedTableExpr) TableSet

TableSetFor returns the bitmask for this particular table

func (*SemTable) TypeFor added in v0.12.0

func (st *SemTable) TypeFor(e sqlparser.Expr) *querypb.Type

TypeFor returns the type of expressions in the query

type ShardedError added in v0.14.5

type ShardedError struct {
	Inner error
}

ShardedError is used to mark an error as something that should only be returned if the query is not unsharded

func (ShardedError) Error added in v0.14.5

func (p ShardedError) Error() string

type TableInfo added in v0.11.0

type TableInfo interface {
	// Name returns the table name
	Name() (sqlparser.TableName, error)

	// GetVindexTable returns the vschema version of this TableInfo
	GetVindexTable() *vindexes.Table

	// IsInfSchema returns true if this table is information_schema
	IsInfSchema() bool
	// contains filtered or unexported methods
}

TableInfo contains information about tables

type TableSet

type TableSet bitset.Bitset

TableSet is how a set of tables is expressed. Tables get unique bits assigned in the order that they are encountered during semantic analysis.

func EmptyTableSet added in v0.13.0

func EmptyTableSet() TableSet

EmptyTableSet creates an empty TableSet

func MergeTableSets added in v0.12.0

func MergeTableSets(tss ...TableSet) TableSet

MergeTableSets merges all the given TableSet into a single one

func SingleTableSet added in v0.12.0

func SingleTableSet(tableidx int) TableSet

SingleTableSet creates a TableSet that contains only the given table

func TableSetFromIds added in v0.12.0

func TableSetFromIds(tids ...int) (ts TableSet)

TableSetFromIds returns TableSet for all the id passed in argument.

func (TableSet) Constituents

func (ts TableSet) Constituents() (result []TableSet)

Constituents returns a slice with the indices for all tables in this TableSet

func (TableSet) ForEachTable added in v0.12.0

func (ts TableSet) ForEachTable(callback func(int))

ForEachTable calls the given callback with the indices for all tables in this TableSet

func (TableSet) Format added in v0.12.0

func (ts TableSet) Format(f fmt.State, _ rune)

Format formats the TableSet.

func (TableSet) IsEmpty added in v0.15.1

func (ts TableSet) IsEmpty() bool

IsEmpty returns true if there are no tables in the tableset

func (TableSet) IsOverlapping

func (ts TableSet) IsOverlapping(other TableSet) bool

IsOverlapping returns true if at least one table exists in both sets

func (TableSet) IsSolvedBy

func (ts TableSet) IsSolvedBy(other TableSet) bool

IsSolvedBy returns true if all of `ts` is contained in `other`

func (TableSet) KeepOnly added in v0.13.0

func (ts TableSet) KeepOnly(other TableSet) TableSet

KeepOnly removes all the tables not in `other` from this TableSet

func (TableSet) Merge

func (ts TableSet) Merge(other TableSet) TableSet

Merge creates a TableSet that contains both inputs

func (TableSet) NonEmpty added in v0.15.1

func (ts TableSet) NonEmpty() bool

NonEmpty returns true if there are tables in the tableset

func (TableSet) NumberOfTables

func (ts TableSet) NumberOfTables() int

NumberOfTables returns the number of bits set

func (TableSet) Remove added in v0.15.1

func (ts TableSet) Remove(other TableSet) TableSet

Remove returns a new TableSet with all the tables in `other` removed

func (TableSet) TableOffset added in v0.11.0

func (ts TableSet) TableOffset() int

TableOffset returns the offset in the Tables array from TableSet

func (TableSet) WithTable added in v0.15.1

func (ts TableSet) WithTable(tableidx int) TableSet

WithTable returns a new TableSet that contains this table too

type Type added in v0.13.0

type Type struct {
	Type      querypb.Type
	Collation collations.ID
}

Type is the normal querypb.Type with collation

type VindexTable added in v0.12.0

type VindexTable struct {
	Table  TableInfo
	Vindex vindexes.Vindex
}

VindexTable contains a vindexes.Vindex and a TableInfo. The former represents the vindex we are keeping information about, and the latter represents the additional table information (usually a RealTable or an AliasedTable) of our vindex.

func (*VindexTable) GetVindexTable added in v0.12.0

func (v *VindexTable) GetVindexTable() *vindexes.Table

GetVindexTable implements the TableInfo interface

func (*VindexTable) IsInfSchema added in v0.12.0

func (v *VindexTable) IsInfSchema() bool

IsInfSchema implements the TableInfo interface

func (*VindexTable) Name added in v0.12.0

func (v *VindexTable) Name() (sqlparser.TableName, error)

Name implements the TableInfo interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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