本文整理汇总了Golang中github.com/pingcap/tidb/util/types.Datum类的典型用法代码示例。如果您正苦于以下问题:Golang Datum类的具体用法?Golang Datum怎么用?Golang Datum使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Datum类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestEvalCoalesce
func (s *testEvalSuite) TestEvalCoalesce(c *C) {
colID := int64(1)
row := make(map[int64]types.Datum)
row[colID] = types.NewIntDatum(100)
xevaluator := &Evaluator{Row: row}
nullDatum := types.Datum{}
nullDatum.SetNull()
notNullDatum := types.NewStringDatum("not-null")
cases := []struct {
expr *tipb.Expr
result types.Datum
}{
{
expr: buildExpr(tipb.ExprType_Coalesce, nullDatum, nullDatum, nullDatum),
result: nullDatum,
},
{
expr: buildExpr(tipb.ExprType_Coalesce, nullDatum, notNullDatum, nullDatum),
result: notNullDatum,
},
{
expr: buildExpr(tipb.ExprType_Coalesce, nullDatum, notNullDatum, types.NewStringDatum("not-null-2"), nullDatum),
result: notNullDatum,
},
}
for _, ca := range cases {
result, err := xevaluator.Eval(ca.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, ca.result.Kind())
cmp, err := result.CompareDatum(xevaluator.sc, ca.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
}
示例2: checkAnyResult
func (e *Evaluator) checkAnyResult(cs *ast.CompareSubqueryExpr, lv types.Datum, result []types.Datum) (d types.Datum, err error) {
hasNull := false
for _, v := range result {
if v.IsNull() {
hasNull = true
continue
}
comRes, err1 := lv.CompareDatum(v)
if err1 != nil {
return d, errors.Trace(err1)
}
res, err1 := getCompResult(cs.Op, comRes)
if err1 != nil {
return d, errors.Trace(err1)
}
if res {
d.SetInt64(boolToInt64(true))
return d, nil
}
}
if hasNull {
// If no matched but we get null, return null.
// Like `insert t (c) values (1),(2),(null)`, then
// `select 0 > any (select c from t)`, returns null.
return d, nil
}
d.SetInt64(boolToInt64(false))
return d, nil
}
示例3: SetSystemVar
// SetSystemVar sets a system variable.
func (s *SessionVars) SetSystemVar(key string, value types.Datum) error {
key = strings.ToLower(key)
if value.IsNull() {
if key != characterSetResults {
return errCantSetToNull
}
delete(s.systems, key)
return nil
}
sVal, err := value.ToString()
if err != nil {
return errors.Trace(err)
}
if key == sqlMode {
sVal = strings.ToUpper(sVal)
if strings.Contains(sVal, "STRICT_TRANS_TABLES") || strings.Contains(sVal, "STRICT_ALL_TABLES") {
s.StrictSQLMode = true
} else {
s.StrictSQLMode = false
}
} else if key == TiDBSnapshot {
err = s.setSnapshotTS(sVal)
if err != nil {
return errors.Trace(err)
}
}
s.systems[key] = sVal
return nil
}
示例4: SetSystemVar
// SetSystemVar sets system variable and updates SessionVars states.
func SetSystemVar(vars *variable.SessionVars, name string, value types.Datum) error {
name = strings.ToLower(name)
if value.IsNull() {
if name != variable.CharacterSetResults {
return variable.ErrCantSetToNull
}
delete(vars.Systems, name)
return nil
}
sVal, err := value.ToString()
if err != nil {
return errors.Trace(err)
}
switch name {
case variable.SQLModeVar:
sVal = strings.ToUpper(sVal)
if strings.Contains(sVal, "STRICT_TRANS_TABLES") || strings.Contains(sVal, "STRICT_ALL_TABLES") {
vars.StrictSQLMode = true
} else {
vars.StrictSQLMode = false
}
case variable.TiDBSnapshot:
err = setSnapshotTS(vars, sVal)
if err != nil {
return errors.Trace(err)
}
case variable.AutocommitVar:
isAutocommit := strings.EqualFold(sVal, "ON") || sVal == "1"
vars.SetStatusFlag(mysql.ServerStatusAutocommit, isAutocommit)
case variable.TiDBSkipConstraintCheck:
vars.SkipConstraintCheck = (sVal == "1")
}
vars.Systems[name] = sVal
return nil
}
示例5: builtinDateFormat
// See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
func builtinDateFormat(args []types.Datum, ctx context.Context) (types.Datum, error) {
var (
isPercent bool
ret []byte
d types.Datum
)
// TODO: Some invalid format like 2000-00-01(the month is 0) will return null.
for _, b := range []byte(args[1].GetString()) {
if isPercent {
if b == '%' {
ret = append(ret, b)
} else {
str, err := convertDateFormat(ctx, args[0], b)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
if str.IsNull() {
return types.Datum{}, nil
}
ret = append(ret, str.GetString()...)
}
isPercent = false
continue
}
if b == '%' {
isPercent = true
} else {
ret = append(ret, b)
}
}
d.SetString(string(ret))
return d, nil
}
示例6: evalUint
func (e *Evaluator) evalUint(val []byte) (types.Datum, error) {
var d types.Datum
_, u, err := codec.DecodeUint(val)
if err != nil {
return d, ErrInvalid.Gen("invalid uint % x", val)
}
d.SetUint64(u)
return d, nil
}
示例7: GetSystemVar
// GetSystemVar gets a system variable.
func (s *SessionVars) GetSystemVar(key string) types.Datum {
var d types.Datum
key = strings.ToLower(key)
sVal, ok := s.systems[key]
if ok {
d.SetString(sVal)
}
return d
}
示例8: updateFirst
func (n *finalAggregater) updateFirst(val types.Datum) error {
ctx := n.getContext()
if ctx.Evaluated {
return nil
}
ctx.Value = val.GetValue()
ctx.Evaluated = true
return nil
}
示例9: evalDecimal
func (e *Evaluator) evalDecimal(val []byte) (types.Datum, error) {
var d types.Datum
_, dec, err := codec.DecodeDecimal(val)
if err != nil {
return d, ErrInvalid.Gen("invalid decimal % x", val)
}
d.SetMysqlDecimal(dec)
return d, nil
}
示例10: evalDuration
func (e *Evaluator) evalDuration(val []byte) (types.Datum, error) {
var d types.Datum
_, i, err := codec.DecodeInt(val)
if err != nil {
return d, ErrInvalid.Gen("invalid duration %d", i)
}
d.SetMysqlDuration(mysql.Duration{Duration: time.Duration(i), Fsp: mysql.MaxFsp})
return d, nil
}
示例11: buildFormBinOp
func (r *rangeBuilder) buildFormBinOp(expr *expression.ScalarFunction) []rangePoint {
// This has been checked that the binary operation is comparison operation, and one of
// the operand is column name expression.
var value types.Datum
var op string
if v, ok := expr.Args[0].(*expression.Constant); ok {
value = v.Value
switch expr.FuncName.L {
case ast.GE:
op = ast.LE
case ast.GT:
op = ast.LT
case ast.LT:
op = ast.GT
case ast.LE:
op = ast.GE
default:
op = expr.FuncName.L
}
} else {
value = expr.Args[1].(*expression.Constant).Value
op = expr.FuncName.L
}
if value.IsNull() {
return nil
}
switch op {
case ast.EQ:
startPoint := rangePoint{value: value, start: true}
endPoint := rangePoint{value: value}
return []rangePoint{startPoint, endPoint}
case ast.NE:
startPoint1 := rangePoint{value: types.MinNotNullDatum(), start: true}
endPoint1 := rangePoint{value: value, excl: true}
startPoint2 := rangePoint{value: value, start: true, excl: true}
endPoint2 := rangePoint{value: types.MaxValueDatum()}
return []rangePoint{startPoint1, endPoint1, startPoint2, endPoint2}
case ast.LT:
startPoint := rangePoint{value: types.MinNotNullDatum(), start: true}
endPoint := rangePoint{value: value, excl: true}
return []rangePoint{startPoint, endPoint}
case ast.LE:
startPoint := rangePoint{value: types.MinNotNullDatum(), start: true}
endPoint := rangePoint{value: value}
return []rangePoint{startPoint, endPoint}
case ast.GT:
startPoint := rangePoint{value: value, start: true, excl: true}
endPoint := rangePoint{value: types.MaxValueDatum()}
return []rangePoint{startPoint, endPoint}
case ast.GE:
startPoint := rangePoint{value: value, start: true}
endPoint := rangePoint{value: types.MaxValueDatum()}
return []rangePoint{startPoint, endPoint}
}
return nil
}
示例12: convertToGoTime
// Convert datum to gotime.
// TODO: This is used for timediff(). After we finish time refactor, we should abandan this function.
func convertToGoTime(sc *variable.StatementContext, d types.Datum) (t time.Time, err error) {
if d.Kind() != types.KindMysqlTime {
d, err = convertToTime(sc, d, mysql.TypeDatetime)
if err != nil {
return t, errors.Trace(err)
}
}
t, err = d.GetMysqlTime().Time.GoTime()
return t, errors.Trace(err)
}
示例13: CastValue
// CastValue casts a value based on column type.
func CastValue(ctx context.Context, val types.Datum, col *Column) (casted types.Datum, err error) {
casted, err = val.ConvertTo(&col.FieldType)
if err != nil {
if variable.GetSessionVars(ctx).StrictSQLMode {
return casted, errors.Trace(err)
}
// TODO: add warnings.
log.Warnf("cast value error %v", err)
}
return casted, nil
}
示例14: checkFsp
func checkFsp(arg types.Datum) (int, error) {
fsp, err := arg.ToInt64()
if err != nil {
return 0, errors.Trace(err)
}
if int(fsp) > mysql.MaxFsp {
return 0, errors.Errorf("Too big precision %d specified. Maximum is 6.", fsp)
} else if fsp < 0 {
return 0, errors.Errorf("Invalid negative %d specified, must in [0, 6].", fsp)
}
return int(fsp), nil
}
示例15: TestFromUnixTime
func (s *testEvaluatorSuite) TestFromUnixTime(c *C) {
defer testleak.AfterTest(c)()
tbl := []struct {
isDecimal bool
integralPart int64
fractionalPart int64
decimal float64
format string
ansLen int
}{
{false, 1451606400, 0, 0, "", 19},
{true, 1451606400, 123456000, 1451606400.123456, "", 26},
{true, 1451606400, 999999000, 1451606400.999999, "", 26},
{true, 1451606400, 999999900, 1451606400.9999999, "", 19},
{false, 1451606400, 0, 0, "%Y %D %M %h:%i:%s %x", 19},
{true, 1451606400, 123456000, 1451606400.123456, "%Y %D %M %h:%i:%s %x", 26},
{true, 1451606400, 999999000, 1451606400.999999, "%Y %D %M %h:%i:%s %x", 26},
{true, 1451606400, 999999900, 1451606400.9999999, "%Y %D %M %h:%i:%s %x", 19},
}
for _, t := range tbl {
var timestamp types.Datum
if !t.isDecimal {
timestamp.SetInt64(t.integralPart)
} else {
timestamp.SetFloat64(t.decimal)
}
// result of from_unixtime() is dependent on specific time zone.
unixTime := time.Unix(t.integralPart, t.fractionalPart).Round(time.Microsecond).String()[:t.ansLen]
if len(t.format) == 0 {
v, err := builtinFromUnixTime([]types.Datum{timestamp}, s.ctx)
c.Assert(err, IsNil)
ans := v.GetMysqlTime()
c.Assert(ans.String(), Equals, unixTime)
} else {
format := types.NewStringDatum(t.format)
v, err := builtinFromUnixTime([]types.Datum{timestamp, format}, s.ctx)
c.Assert(err, IsNil)
result, err := builtinDateFormat([]types.Datum{types.NewStringDatum(unixTime), format}, s.ctx)
c.Assert(err, IsNil)
c.Assert(v.GetString(), Equals, result.GetString())
}
}
v, err := builtinFromUnixTime([]types.Datum{types.NewIntDatum(-12345)}, s.ctx)
c.Assert(err, IsNil)
c.Assert(v.Kind(), Equals, types.KindNull)
_, err = builtinFromUnixTime([]types.Datum{types.NewIntDatum(math.MaxInt32 + 1)}, s.ctx)
c.Assert(err, IsNil)
c.Assert(v.Kind(), Equals, types.KindNull)
}