sqltypes

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: BSD-3-Clause Imports: 11 Imported by: 42

Documentation

Overview

Package sqltypes implements interfaces and types that represent SQL values.

Index

Constants

View Source
const (
	// DecimalLongLongDigits decimal longlong digits.
	DecimalLongLongDigits = 22
	// FloatDigits float decimal precision.
	FloatDigits = 6
	// DoubleDigits double decimal precision.
	DoubleDigits = 15
)

Vitess data types. These are idiomatically named synonyms for the querypb.Type values.

Variables

View Source
var (
	// NULL represents the NULL value.
	NULL = Value{}
	// DontEscape tells you if a character should not be escaped.
	DontEscape = byte(255)
)
View Source
var NullBindVariable = &querypb.BindVariable{Type: querypb.Type_NULL_TYPE}

NullBindVariable is a bindvar with NULL value.

View Source
var SQLDecodeMap [256]byte

SQLDecodeMap is the reverse of SQLEncodeMap

View Source
var SQLEncodeMap [256]byte

SQLEncodeMap specifies how to escape binary data with '\'. Complies to http://dev.mysql.com/doc/refman/5.1/en/string-syntax.html

Functions

func BuildBindVariable

func BuildBindVariable(v interface{}) (*querypb.BindVariable, error)

BuildBindVariable builds a *querypb.BindVariable from a valid input type.

func BuildBindVariables

func BuildBindVariables(in map[string]interface{}) (map[string]*querypb.BindVariable, error)

BuildBindVariables builds a map[string]*querypb.BindVariable from a map[string]interface{}.

func BytesBindVariable

func BytesBindVariable(v []byte) *querypb.BindVariable

BytesBindVariable converts a []byte to a bind var.

func CastToBool

func CastToBool(v Value) bool

CastToBool used to cast the Value to a boolean.

func CompareFloat64

func CompareFloat64(x, y float64) int

CompareFloat64 returns an integer comparing the float64 x to y.

func CompareInt64

func CompareInt64(x, y int64) int

CompareInt64 returns an integer comparing the int64 x to y.

func CompareUint64

func CompareUint64(x, y uint64) int

CompareUint64 returns an integer comparing the uint64 x to y.

func CopyBindVariables

func CopyBindVariables(bindVariables map[string]*querypb.BindVariable) map[string]*querypb.BindVariable

CopyBindVariables returns a shallow-copy of the given bindVariables map.

func Float32BindVariable

func Float32BindVariable(v float32) *querypb.BindVariable

Float32BindVariable converts a float32 to a bind var.

func Float64BindVariable

func Float64BindVariable(v float64) *querypb.BindVariable

Float64BindVariable converts a float64 to a bind var.

func Int32BindVariable

func Int32BindVariable(v int32) *querypb.BindVariable

Int32BindVariable converts an int32 to a bind var.

func Int64BindVariable

func Int64BindVariable(v int64) *querypb.BindVariable

Int64BindVariable converts an int64 to a bind var.

func IsBinary

func IsBinary(t querypb.Type) bool

IsBinary returns true if querypb.Type is a binary.

func IsFloat

func IsFloat(t querypb.Type) bool

IsFloat returns true is querypb.Type is a floating point.

func IsIntegral

func IsIntegral(t querypb.Type) bool

IsIntegral returns true if querypb.Type is an integral (signed/unsigned) that can be represented using up to 64 binary bits.

func IsQuoted

func IsQuoted(t querypb.Type) bool

IsQuoted returns true if querypb.Type is a quoted text or binary.

func IsSigned

func IsSigned(t querypb.Type) bool

IsSigned returns true if querypb.Type is a signed integral.

func IsTemporal

func IsTemporal(t querypb.Type) bool

IsTemporal returns true if Value is time type.

func IsText

func IsText(t querypb.Type) bool

IsText returns true if querypb.Type is a text.

func IsUnsigned

func IsUnsigned(t querypb.Type) bool

IsUnsigned returns true if querypb.Type is an unsigned integral. Caution: this is not the same as !IsSigned.

func MySQLToType

func MySQLToType(mysqlType, flags int64) (typ querypb.Type, err error)

MySQLToType computes the vitess type from mysql type and flags.

func NullsafeCompare

func NullsafeCompare(v1, v2 Value) int

NullsafeCompare returns 0 if v1==v2, -1 if v1<v2, and 1 if v1>v2. NULL is the lowest value. If any value is numeric, then a numeric comparison is performed after necessary conversions. If none are numeric, then it's a simple binary comparison.

func ParseMySQLValues

func ParseMySQLValues(buf *common.Buffer, typ querypb.Type) (interface{}, error)

func StringBindVariable

func StringBindVariable(v string) *querypb.BindVariable

StringBindVariable converts a string to a bind var.

func TypeToMySQL

func TypeToMySQL(typ querypb.Type) (mysqlType, flags int64)

TypeToMySQL returns the equivalent mysql type and flag for a vitess type.

func Uint64BindVariable

func Uint64BindVariable(v uint64) *querypb.BindVariable

Uint64BindVariable converts a uint64 to a bind var.

func ValidateBindVariable

func ValidateBindVariable(bv *querypb.BindVariable) error

ValidateBindVariable returns an error if the bind variable has inconsistent fields.

func ValidateBindVariables

func ValidateBindVariables(bv map[string]*querypb.BindVariable) error

ValidateBindVariables validates a map[string]*querypb.BindVariable.

func ValueBindVariable

func ValueBindVariable(v Value) *querypb.BindVariable

ValueBindVariable converts a Value to a bind var.

func ValueToProto

func ValueToProto(v Value) *querypb.Value

ValueToProto converts Value to a *querypb.Value.

Types

type AggEvaluateContext

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

AggEvaluateContext is used to store intermediate result when calculating aggregate functions.

func NewAggEvalCtxs

func NewAggEvalCtxs(aggrs []*Aggregation, x []Value) []*AggEvaluateContext

NewAggEvalCtxs new evalCtxs.

type AggrType

type AggrType string

AggrType type.

const (
	// AggrTypeNull enum.
	AggrTypeNull AggrType = ""

	// AggrTypeCount enum.
	AggrTypeCount AggrType = "COUNT"

	// AggrTypeSum enum.
	AggrTypeSum AggrType = "SUM"

	// AggrTypeMin enum.
	AggrTypeMin AggrType = "MIN"

	// AggrTypeMax enum.
	AggrTypeMax AggrType = "MAX"

	// AggrTypeAvg enum.
	AggrTypeAvg AggrType = "AVG"

	// AggrTypeGroupBy enum.
	AggrTypeGroupBy AggrType = "GROUP BY"
)

type Aggregation

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

Aggregation operator.

func NewAggregation

func NewAggregation(index int, aggrTyp AggrType, distinct, isPushDown bool) *Aggregation

NewAggregation new an Aggregetion.

func (*Aggregation) FixField

func (aggr *Aggregation) FixField(field *querypb.Field)

FixField used to fix querypb.Field lenght and decimal.

func (*Aggregation) GetResult

func (aggr *Aggregation) GetResult(evalCtx *AggEvaluateContext) Value

GetResult used to get Value finally.

func (*Aggregation) InitEvalCtx

func (aggr *Aggregation) InitEvalCtx(x []Value) *AggEvaluateContext

InitEvalCtx used to init the AggEvaluateContext.

func (*Aggregation) Update

func (aggr *Aggregation) Update(x []Value, evalCtx *AggEvaluateContext)

Update during executing.

type BinWriter

type BinWriter interface {
	Write([]byte) (int, error)
	WriteByte(byte) error
}

BinWriter interface is used for encoding values. Types like bytes.Buffer conform to this interface. We expect the writer objects to be in-memory buffers. So, we don't expect the write operations to fail.

type PlanValue

type PlanValue struct {
	Key     string
	Value   Value
	ListKey string
	Values  []PlanValue
}

PlanValue represents a value or a list of values for a column that will later be resolved using bind vars and used to perform plan actions like generating the final query or deciding on a route.

Plan values are typically used as a slice ([]planValue) where each entry is for one column. For situations where the required output is a list of rows (like in the case of multi-value inserts), the representation is pivoted. For example, a statement like this:

INSERT INTO t VALUES (1, 2), (3, 4)

will be represented as follows:

[]PlanValue{
	Values: {1, 3},
	Values: {2, 4},
}

For WHERE clause items that contain a combination of equality expressions and IN clauses like this:

WHERE pk1 = 1 AND pk2 IN (2, 3, 4)

The plan values will be represented as follows:

[]PlanValue{
	Value: 1,
	Values: {2, 3, 4},
}

When converted into rows, columns with single values are replicated as the same for all rows:

[][]Value{
	{1, 2},
	{1, 3},
	{1, 4},
}

type Result

type Result struct {
	Fields       []*querypb.Field      `json:"fields"`
	RowsAffected uint64                `json:"rows_affected"`
	InsertID     uint64                `json:"insert_id"`
	Warnings     uint16                `json:"warnings"`
	Rows         [][]Value             `json:"rows"`
	Extras       *querypb.ResultExtras `json:"extras"`
	State        ResultState
}

Result represents a query result.

func (*Result) AppendResult

func (result *Result) AppendResult(src *Result)

AppendResult will combine the Results Objects of one result to another result.Note currently it doesn't handle cases like if two results have different fields.We will enhance this function.

func (*Result) Copy

func (result *Result) Copy() *Result

Copy creates a deep copy of Result.

func (*Result) Limit

func (result *Result) Limit(offset, limit int)

Limit used to cutoff the rows based on the MySQL LIMIT and OFFSET clauses.

func (*Result) RemoveColumns

func (result *Result) RemoveColumns(idxs ...int)

RemoveColumns used to remove columns who in the idxs.

func (*Result) Repair

func (result *Result) Repair(fields []*querypb.Field)

Repair fixes the type info in the rows to conform to the supplied field types.

func (*Result) StripFieldNames

func (result *Result) StripFieldNames() *Result

StripFieldNames will return a new Result that has the same Rows, but the Field objects will have their Name emptied. Note we don't proto.Copy each Field for performance reasons, but we only copy the individual fields.

type ResultState

type ResultState int

ResultState enum.

const (
	// RStateNone enum.
	RStateNone ResultState = iota
	// RStateFields enum.
	RStateFields
	// RStateRows enum.
	RStateRows
	// RStateFinished enum.
	RStateFinished
)

type ResultStream

type ResultStream interface {
	// Recv returns the next result on the stream.
	// It will return io.EOF if the stream ended.
	Recv() (*Result, error)
}

ResultStream is an interface for receiving Result. It is used for RPC interfaces.

type Row

type Row []Value

Row operations.

func (Row) Copy

func (r Row) Copy() []Value

Copy used to clone the new value.

type Value

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

Value can store any SQL value. If the value represents an integral type, the bytes are always stored as a canonical representation that matches how MySQL returns such values.

func BindVariableToValue

func BindVariableToValue(bv *querypb.BindVariable) (Value, error)

BindVariableToValue converts a bind var into a Value.

func BuildConverted

func BuildConverted(typ querypb.Type, goval interface{}) (v Value, err error)

BuildConverted is like BuildValue except that it tries to convert a string or []byte to an integral if the target type is an integral. We don't perform other implicit conversions because they're unsafe.

func BuildIntegral

func BuildIntegral(val string) (n Value, err error)

BuildIntegral builds an integral type from a string representation. The type will be Int64 or Uint64. Int64 will be preferred where possible.

func BuildValue

func BuildValue(goval interface{}) (v Value, err error)

BuildValue builds a value from any go type. sqltype.Value is also allowed.

func Cast

func Cast(v Value, typ querypb.Type) (Value, error)

Cast converts a Value to the target type.

func GetResults

func GetResults(aggrs []*Aggregation, evalCtxs []*AggEvaluateContext, x []Value) ([]Value, []int)

GetResults will be called when all data have been processed.

func MakeString

func MakeString(val []byte) Value

MakeString makes a VarBinary Value.

func MakeTrusted

func MakeTrusted(typ querypb.Type, val []byte) Value

MakeTrusted makes a new Value based on the type. If the value is an integral, then val must be in its canonical form. This function should only be used if you know the value and type conform to the rules. Every place this function is called, a comment is needed that explains why it's justified. Functions within this package are exempt.

func Max

func Max(v1, v2 Value) Value

Max returns the maximum of v1 and v2. If one of the values is NULL, it returns the other value. If both are NULL, it returns NULL.

func Min

func Min(v1, v2 Value) Value

Min returns the minimum of v1 and v2. If one of the values is NULL, it returns the other value. If both are NULL, it returns NULL.

func NewFloat32

func NewFloat32(v float32) Value

NewFloat32 builds an Float64 Value.

func NewFloat64

func NewFloat64(v float64) Value

NewFloat64 builds an Float64 Value.

func NewInt32

func NewInt32(v int32) Value

NewInt32 builds an Int64 Value.

func NewInt64

func NewInt64(v int64) Value

NewInt64 builds an Int64 Value.

func NewIntegral

func NewIntegral(val string) (n Value, err error)

NewIntegral builds an integral type from a string representation. The type will be Int64 or Uint64. Int64 will be preferred where possible.

func NewUint64

func NewUint64(v uint64) Value

NewUint64 builds an Uint64 Value.

func NewValue

func NewValue(typ querypb.Type, val []byte) (v Value, err error)

NewValue builds a Value using typ and val. If the value and typ don't match, it returns an error.

func NewVarBinary

func NewVarBinary(v string) Value

NewVarBinary builds a VarBinary Value. The input is a string because it's the most common use case.

func NewVarChar

func NewVarChar(v string) Value

NewVarChar builds a VarChar Value.

func NullsafeAdd

func NullsafeAdd(v1, v2 Value, resultType querypb.Type, prec int) (Value, error)

NullsafeAdd adds two Values in a null-safe manner. A null value is treated as 0. If both values are null, then a null is returned. If both values are not null, a numeric value is built from each input: Signed->int64, Unsigned->uint64, Float->float64, Decimal->Decimal. Otherwise the 'best type fit' is chosen for the number: int64 or float64. Addition is performed by upgrading types as needed, or in case of overflow: int64->uint64, int64->float64, uint64->float64. Unsigned ints can only be added to positive ints. After the addition, if one of the input types was Decimal, then a Decimal is built. Otherwise, the final type of the result is preserved.

func NullsafeDiv

func NullsafeDiv(v1, v2 Value, resultType querypb.Type, prec int) (Value, error)

NullsafeDiv used to divide two Values in a null-safe manner. The result always is float64 or decimal, so cast to float64 first.

func NullsafeSum

func NullsafeSum(sum, v Value, resultType querypb.Type, prec int) (Value, error)

NullsafeSum adds v to sum, used in 'sum' aggregation. The `sum` aggregation's result is `float64` or `decimal`, So need cast the value to `float64`. By using NullsafeSum instead of NullsafeAdd, can avoid overflow between two integers.

func ProtoToValue

func ProtoToValue(v *querypb.Value) Value

ProtoToValue converts a *querypb.Value to a Value.

func ValueFromBytes

func ValueFromBytes(typ querypb.Type, val []byte) (v Value, err error)

ValueFromBytes builds a Value using typ and val. It ensures that val matches the requested type. If type is an integral it's converted to a canonical form. Otherwise, the original representation is preserved.

func (Value) EncodeASCII

func (v Value) EncodeASCII(b BinWriter)

EncodeASCII encodes the value using 7-bit clean ascii bytes.

func (Value) EncodeSQL

func (v Value) EncodeSQL(b BinWriter)

EncodeSQL encodes the value into an SQL statement. Can be binary.

func (Value) IsBinary

func (v Value) IsBinary() bool

IsBinary returns true if Value is binary.

func (Value) IsFloat

func (v Value) IsFloat() bool

IsFloat returns true if Value is a float.

func (Value) IsIntegral

func (v Value) IsIntegral() bool

IsIntegral returns true if Value is an integral.

func (Value) IsNull

func (v Value) IsNull() bool

IsNull returns true if Value is null.

func (Value) IsQuoted

func (v Value) IsQuoted() bool

IsQuoted returns true if Value must be SQL-quoted.

func (Value) IsSigned

func (v Value) IsSigned() bool

IsSigned returns true if Value is a signed integral.

func (Value) IsTemporal

func (v Value) IsTemporal() bool

IsTemporal returns true if Value is time type.

func (Value) IsText

func (v Value) IsText() bool

IsText returns true if Value is a collatable text.

func (Value) IsUnsigned

func (v Value) IsUnsigned() bool

IsUnsigned returns true if Value is an unsigned integral.

func (Value) Len

func (v Value) Len() int

Len returns the length.

func (Value) ParseFloat64

func (v Value) ParseFloat64() (val float64, err error)

ParseFloat64 will parse a Value into an float64. It does not check the type.

func (Value) ParseInt64

func (v Value) ParseInt64() (val int64, err error)

ParseInt64 will parse a Value into an int64. It does not check the type.

func (Value) ParseUint64

func (v Value) ParseUint64() (val uint64, err error)

ParseUint64 will parse a Value into a uint64. It does not check the type.

func (Value) Raw

func (v Value) Raw() []byte

Raw returns the raw bytes. All types are currently implemented as []byte. You should avoid using this function. If you do, you should treat the bytes as read-only.

func (Value) String

func (v Value) String() string

String returns the raw value as a string.

func (Value) ToMySQL

func (v Value) ToMySQL() ([]byte, error)

ToMySQL converts Value to a mysql type value.

func (Value) ToNative

func (v Value) ToNative() interface{}

ToNative converts Value to a native go type. This does not work for sqltypes.Tuple. The function panics if there are inconsistencies.

func (Value) ToString

func (v Value) ToString() string

ToString returns the value as MySQL would return it as string. If the value is not convertible like in the case of Expression, it returns nil.

func (Value) Type

func (v Value) Type() querypb.Type

Type returns the type of Value.

type Values

type Values []Value

Values represents the array of Value.

func (Values) Len

func (vs Values) Len() int

Len implements the interface.

Jump to

Keyboard shortcuts

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