expression

package
v0.0.0-...-08a93fc Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2019 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CurrentTimestamp is the keyword getting default value for datetime and timestamp type.
	CurrentTimestamp = "CURRENT_TIMESTAMP"

	// ZeroTimestamp shows the zero datetime and timestamp.
	ZeroTimestamp = "0000-00-00 00:00:00"
)
View Source
var DynamicFuncs = map[string]int{
	"rand":           0,
	"connection_id":  0,
	"current_user":   0,
	"database":       0,
	"found_rows":     0,
	"last_insert_id": 0,
	"user":           0,
	"version":        0,
	"sleep":          0,
	ast.GetVar:       0,
	ast.SetVar:       0,
	ast.Values:       0,
}

DynamicFuncs are those functions that use input parameter ctx or return an uncertain result would not be constant folded the value 0 means nothing

View Source
var EvalAstExpr func(expr ast.ExprNode, ctx context.Context) (types.Datum, error)

EvalAstExpr evaluates ast expression directly.

View Source
var MaxPropagateColsCnt = 100

MaxPropagateColsCnt means the max number of columns that can participate propagation.

View Source
var Null = &Constant{
	Value:   types.NewDatum(nil),
	RetType: types.NewFieldType(mysql.TypeTiny),
}

Null stands for null constant.

View Source
var One = &Constant{
	Value:   types.NewDatum(1),
	RetType: types.NewFieldType(mysql.TypeTiny),
}

One stands for a number 1.

View Source
var Zero = &Constant{
	Value:   types.NewDatum(0),
	RetType: types.NewFieldType(mysql.TypeTiny),
}

Zero stands for a number 0.

Functions

func EvalBool

func EvalBool(expr Expression, row []types.Datum, ctx context.Context) (bool, error)

EvalBool evaluates expression to a boolean value.

func GetTimeValue

func GetTimeValue(ctx context.Context, v interface{}, tp byte, fsp int) (types.Datum, error)

GetTimeValue gets the time value with type tp.

func IsCurrentTimeExpr

func IsCurrentTimeExpr(e ast.ExprNode) bool

IsCurrentTimeExpr returns whether e is CurrentTimeExpr.

Types

type AggFunctionMode

type AggFunctionMode int

AggFunctionMode stands for the aggregation function's mode.

const (
	// CompleteMode function accepts origin data.
	CompleteMode AggFunctionMode = iota
	// FinalMode function accepts partial data.
	FinalMode
)

type AggregationFunction

type AggregationFunction interface {
	fmt.Stringer
	json.Marshaler
	// Update during executing.
	Update(row []types.Datum, groupKey []byte, ctx context.Context) error

	// StreamUpdate updates data using streaming algo.
	StreamUpdate(row []types.Datum, ctx context.Context) error

	// SetMode sets aggFunctionMode for aggregate function.
	SetMode(mode AggFunctionMode)

	// GetMode gets aggFunctionMode from aggregate function.
	GetMode() AggFunctionMode

	// GetGroupResult will be called when all data have been processed.
	GetGroupResult(groupKey []byte) types.Datum

	// GetStreamResult gets a result using streaming agg.
	GetStreamResult() types.Datum

	// GetArgs stands for getting all arguments.
	GetArgs() []Expression

	// GetName gets the aggregation function name.
	GetName() string

	// SetArgs sets argument by index.
	SetArgs(args []Expression)

	// Clear collects the mapper's memory.
	Clear()

	// IsDistinct indicates if the aggregate function contains distinct attribute.
	IsDistinct() bool

	// SetContext sets the aggregate evaluation context.
	SetContext(ctx map[string](*aggEvaluateContext))

	// Equal checks whether two aggregation functions are equal.
	Equal(agg AggregationFunction, ctx context.Context) bool

	// Clone copies an aggregate function totally.
	Clone() AggregationFunction

	// GetType gets field type of aggregate function.
	GetType() *types.FieldType

	// CalculateDefaultValue gets the default value when the aggregate function's input is null.
	// The input stands for the schema of Aggregation's child. If the function can't produce a default value, the second
	// return value will be false.
	CalculateDefaultValue(schema Schema, ctx context.Context) (types.Datum, bool)
}

AggregationFunction stands for aggregate functions.

func NewAggFunction

func NewAggFunction(funcType string, funcArgs []Expression, distinct bool) AggregationFunction

NewAggFunction creates a new AggregationFunction.

type Assignment

type Assignment struct {
	Col  *Column
	Expr Expression
}

Assignment represents a set assignment in Update, such as Update t set c1 = hex(12), c2 = c3 where c2 = 1

type BuiltinFunc

type BuiltinFunc func([]types.Datum, context.Context) (types.Datum, error)

BuiltinFunc is the function signature for builtin functions

func BuiltinValuesFactory

func BuiltinValuesFactory(offset int) BuiltinFunc

BuiltinValuesFactory generates values builtin function.

func CastFuncFactory

func CastFuncFactory(tp *types.FieldType) (BuiltinFunc, error)

CastFuncFactory produces builtin function according to field types. See https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html

type Column

type Column struct {
	FromID  string
	ColName model.CIStr
	DBName  model.CIStr
	TblName model.CIStr
	RetType *types.FieldType
	// Position means the position of this column that appears in the select fields.
	// e.g. SELECT name as id , 1 - id as id , 1 + name as id, name as id from src having id = 1;
	// There are four ids in the same schema, so you can't identify the column through the FromID and ColName.
	Position int
	// IsAggOrSubq means if this column is referenced to a Aggregation column or a Subquery column.
	// If so, this column's name will be the plain sql text.
	IsAggOrSubq bool

	// Only used for execution.
	Index int
	// contains filtered or unexported fields
}

Column represents a column.

func ExtractColumns

func ExtractColumns(expr Expression) (cols []*Column)

ExtractColumns extracts all columns from an expression.

func (*Column) Clone

func (col *Column) Clone() Expression

Clone implements Expression interface.

func (*Column) Decorrelate

func (col *Column) Decorrelate(_ Schema) Expression

Decorrelate implements Expression interface.

func (*Column) Equal

func (col *Column) Equal(expr Expression, _ context.Context) bool

Equal implements Expression interface.

func (*Column) Eval

func (col *Column) Eval(row []types.Datum, _ context.Context) (types.Datum, error)

Eval implements Expression interface.

func (*Column) GetType

func (col *Column) GetType() *types.FieldType

GetType implements Expression interface.

func (*Column) HashCode

func (col *Column) HashCode() []byte

HashCode implements Expression interface.

func (*Column) IsCorrelated

func (col *Column) IsCorrelated() bool

IsCorrelated implements Expression interface.

func (*Column) MarshalJSON

func (col *Column) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Column) ResolveIndices

func (col *Column) ResolveIndices(schema Schema)

ResolveIndices implements Expression interface.

func (*Column) String

func (col *Column) String() string

String implements Stringer interface.

type Constant

type Constant struct {
	Value   types.Datum
	RetType *types.FieldType
}

Constant stands for a constant value.

func (*Constant) Clone

func (c *Constant) Clone() Expression

Clone implements Expression interface.

func (*Constant) Decorrelate

func (c *Constant) Decorrelate(_ Schema) Expression

Decorrelate implements Expression interface.

func (*Constant) Equal

func (c *Constant) Equal(b Expression, ctx context.Context) bool

Equal implements Expression interface.

func (*Constant) Eval

func (c *Constant) Eval(_ []types.Datum, _ context.Context) (types.Datum, error)

Eval implements Expression interface.

func (*Constant) GetType

func (c *Constant) GetType() *types.FieldType

GetType implements Expression interface.

func (*Constant) HashCode

func (c *Constant) HashCode() []byte

HashCode implements Expression interface.

func (*Constant) IsCorrelated

func (c *Constant) IsCorrelated() bool

IsCorrelated implements Expression interface.

func (*Constant) MarshalJSON

func (c *Constant) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Constant) ResolveIndices

func (c *Constant) ResolveIndices(_ Schema)

ResolveIndices implements Expression interface.

func (*Constant) String

func (c *Constant) String() string

String implements fmt.Stringer interface.

type CorrelatedColumn

type CorrelatedColumn struct {
	Column

	Data *types.Datum
}

CorrelatedColumn stands for a column in a correlated sub query.

func (*CorrelatedColumn) Clone

func (col *CorrelatedColumn) Clone() Expression

Clone implements Expression interface.

func (*CorrelatedColumn) Decorrelate

func (col *CorrelatedColumn) Decorrelate(schema Schema) Expression

Decorrelate implements Expression interface.

func (*CorrelatedColumn) Equal

func (col *CorrelatedColumn) Equal(expr Expression, ctx context.Context) bool

Equal implements Expression interface.

func (*CorrelatedColumn) Eval

func (col *CorrelatedColumn) Eval(row []types.Datum, _ context.Context) (types.Datum, error)

Eval implements Expression interface.

func (*CorrelatedColumn) IsCorrelated

func (col *CorrelatedColumn) IsCorrelated() bool

IsCorrelated implements Expression interface.

func (*CorrelatedColumn) ResolveIndices

func (col *CorrelatedColumn) ResolveIndices(_ Schema)

ResolveIndices implements Expression interface.

type Expression

type Expression interface {
	fmt.Stringer
	json.Marshaler
	// Eval evaluates an expression through a row.
	Eval(row []types.Datum, ctx context.Context) (types.Datum, error)

	// Get the expression return type.
	GetType() *types.FieldType

	// Clone copies an expression totally.
	Clone() Expression

	// HashCode create the hashcode for expression
	HashCode() []byte

	// Equal checks whether two expressions are equal.
	Equal(e Expression, ctx context.Context) bool

	// IsCorrelated checks if this expression has correlated key.
	IsCorrelated() bool

	// Decorrelate try to decorrelate the expression by schema.
	Decorrelate(schema Schema) Expression

	// ResolveIndices resolves indices by the given schema.
	ResolveIndices(schema Schema)
}

Expression represents all scalar expression in SQL.

func Column2Exprs

func Column2Exprs(cols []*Column) []Expression

Column2Exprs will transfer column slice to expression slice.

func ColumnSubstitute

func ColumnSubstitute(expr Expression, schema Schema, newExprs []Expression) Expression

ColumnSubstitute substitutes the columns in filter to expressions in select fields. e.g. select * from (select b as a from t) k where a < 10 => select * from (select b as a from t where b < 10) k.

func ComposeCNFCondition

func ComposeCNFCondition(ctx context.Context, conditions ...Expression) Expression

ComposeCNFCondition composes CNF items into a balance deep CNF tree, which benefits a lot for pb decoder/encoder.

func ComposeDNFCondition

func ComposeDNFCondition(ctx context.Context, conditions ...Expression) Expression

ComposeDNFCondition composes DNF items into a balance deep DNF tree.

func EvaluateExprWithNull

func EvaluateExprWithNull(ctx context.Context, schema Schema, expr Expression) (Expression, error)

EvaluateExprWithNull sets columns in schema as null and calculate the final result of the scalar function. If the Expression is a non-constant value, it means the result is unknown.

func FoldConstant

func FoldConstant(ctx context.Context, expr Expression) Expression

FoldConstant does constant folding optimization on an expression.

func NewFunction

func NewFunction(ctx context.Context, funcName string, retType *types.FieldType, args ...Expression) (Expression, error)

NewFunction creates a new scalar function or constant.

func PropagateConstant

func PropagateConstant(ctx context.Context, conditions []Expression) []Expression

PropagateConstant propagate constant values of equality predicates and inequality predicates in a condition.

func ScalarFuncs2Exprs

func ScalarFuncs2Exprs(funcs []*ScalarFunction) []Expression

ScalarFuncs2Exprs converts []*ScalarFunction to []Expression.

func SplitCNFItems

func SplitCNFItems(onExpr Expression) []Expression

SplitCNFItems splits CNF items. CNF means conjunctive normal form, e.g. "a and b and c".

func SplitDNFItems

func SplitDNFItems(onExpr Expression) []Expression

SplitDNFItems splits DNF items. DNF means disjunctive normal form, e.g. "a or b or c".

type KeyInfo

type KeyInfo []*Column

KeyInfo stores the columns of one unique key or primary key.

func (KeyInfo) Clone

func (ki KeyInfo) Clone() KeyInfo

Clone copies the entire UniqueKey.

type ScalarFunction

type ScalarFunction struct {
	FuncName model.CIStr
	// TODO: Implement type inference here, now we use ast's return type temporarily.
	RetType  *types.FieldType
	Function builtinFunc
}

ScalarFunction is the function that returns a value.

func NewCastFunc

func NewCastFunc(tp *types.FieldType, arg Expression, ctx context.Context) *ScalarFunction

NewCastFunc creates a new cast function.

func NewValuesFunc

func NewValuesFunc(offset int, retTp *types.FieldType, ctx context.Context) *ScalarFunction

NewValuesFunc creates a new values function.

func (*ScalarFunction) Clone

func (sf *ScalarFunction) Clone() Expression

Clone implements Expression interface.

func (*ScalarFunction) Decorrelate

func (sf *ScalarFunction) Decorrelate(schema Schema) Expression

Decorrelate implements Expression interface.

func (*ScalarFunction) Equal

func (sf *ScalarFunction) Equal(e Expression, ctx context.Context) bool

Equal implements Expression interface.

func (*ScalarFunction) Eval

func (sf *ScalarFunction) Eval(row []types.Datum, _ context.Context) (types.Datum, error)

Eval implements Expression interface.

func (*ScalarFunction) GetArgs

func (sf *ScalarFunction) GetArgs() []Expression

GetArgs gets arguments of function.

func (*ScalarFunction) GetCtx

func (sf *ScalarFunction) GetCtx() context.Context

GetCtx gets the context of function.

func (*ScalarFunction) GetType

func (sf *ScalarFunction) GetType() *types.FieldType

GetType implements Expression interface.

func (*ScalarFunction) HashCode

func (sf *ScalarFunction) HashCode() []byte

HashCode implements Expression interface.

func (*ScalarFunction) IsCorrelated

func (sf *ScalarFunction) IsCorrelated() bool

IsCorrelated implements Expression interface.

func (*ScalarFunction) MarshalJSON

func (sf *ScalarFunction) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*ScalarFunction) ResolveIndices

func (sf *ScalarFunction) ResolveIndices(schema Schema)

ResolveIndices implements Expression interface.

func (*ScalarFunction) String

func (sf *ScalarFunction) String() string

String implements fmt.Stringer interface.

type Schema

type Schema struct {
	Columns []*Column
	Keys    []KeyInfo
}

Schema stands for the row schema and unique key information get from input.

func MergeSchema

func MergeSchema(lSchema, rSchema Schema) Schema

MergeSchema will merge two schema into one schema.

func NewSchema

func NewSchema(cols []*Column) Schema

NewSchema returns a schema made by its parameter.

func TableInfo2Schema

func TableInfo2Schema(tbl *model.TableInfo) Schema

TableInfo2Schema converts table info to schema.

func (*Schema) Append

func (s *Schema) Append(col *Column)

Append append new column to the columns stored in schema.

func (Schema) Clone

func (s Schema) Clone() Schema

Clone copies the total schema.

func (Schema) FindColumn

func (s Schema) FindColumn(astCol *ast.ColumnName) (*Column, error)

FindColumn finds an Column from schema for a ast.ColumnName. It compares the db/table/column names. If there are more than one result, it will raise ambiguous error.

func (Schema) GetColumnIndex

func (s Schema) GetColumnIndex(col *Column) int

GetColumnIndex finds the index for a column.

func (Schema) GetColumnsIndices

func (s Schema) GetColumnsIndices(cols []*Column) (ret []int)

GetColumnsIndices will return a slice which contains the position of each column in schema. If there is one column that doesn't match, nil will be returned.

func (Schema) Len

func (s Schema) Len() int

Len returns the number of columns in schema.

func (Schema) RetrieveColumn

func (s Schema) RetrieveColumn(col *Column) *Column

RetrieveColumn retrieves column in expression from the columns in schema.

func (*Schema) SetUniqueKeys

func (s *Schema) SetUniqueKeys(keys []KeyInfo)

SetUniqueKeys will set the value of Schema.Keys.

func (Schema) String

func (s Schema) String() string

String implements fmt.Stringer interface.

type VarAssignment

type VarAssignment struct {
	Name        string
	Expr        Expression
	IsDefault   bool
	IsGlobal    bool
	IsSystem    bool
	ExtendValue *Constant
}

VarAssignment represents a variable assignment in Set, such as set global a = 1.

Jump to

Keyboard shortcuts

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