func flatten(data types.Datum) (types.Datum, error) { switch data.Kind() { case types.KindMysqlTime: // for mysql datetime, timestamp and date type v, err := data.GetMysqlTime().ToPackedUint() return types.NewUintDatum(v), errors.Trace(err) case types.KindMysqlDuration: // for mysql time type data.SetInt64(int64(data.GetMysqlDuration().Duration)) return data, nil case types.KindMysqlEnum: data.SetUint64(data.GetMysqlEnum().Value) return data, nil case types.KindMysqlSet: data.SetUint64(data.GetMysqlSet().Value) return data, nil case types.KindMysqlBit: data.SetUint64(data.GetMysqlBit().Value) return data, nil case types.KindMysqlHex: data.SetInt64(data.GetMysqlHex().Value) return data, nil default: return data, nil } }
func datumExpr(d types.Datum) *tipb.Expr { expr := new(tipb.Expr) switch d.Kind() { case types.KindInt64: expr.Tp = tipb.ExprType_Int64 expr.Val = codec.EncodeInt(nil, d.GetInt64()) case types.KindUint64: expr.Tp = tipb.ExprType_Uint64 expr.Val = codec.EncodeUint(nil, d.GetUint64()) case types.KindString: expr.Tp = tipb.ExprType_String expr.Val = d.GetBytes() case types.KindBytes: expr.Tp = tipb.ExprType_Bytes expr.Val = d.GetBytes() case types.KindFloat32: expr.Tp = tipb.ExprType_Float32 expr.Val = codec.EncodeFloat(nil, d.GetFloat64()) case types.KindFloat64: expr.Tp = tipb.ExprType_Float64 expr.Val = codec.EncodeFloat(nil, d.GetFloat64()) case types.KindMysqlDuration: expr.Tp = tipb.ExprType_MysqlDuration expr.Val = codec.EncodeInt(nil, int64(d.GetMysqlDuration().Duration)) case types.KindMysqlDecimal: expr.Tp = tipb.ExprType_MysqlDecimal expr.Val = codec.EncodeDecimal(nil, d) default: expr.Tp = tipb.ExprType_Null } return expr }
func flatten(data types.Datum) (types.Datum, error) { switch data.Kind() { case types.KindMysqlTime: // for mysql datetime, timestamp and date type b, err := data.GetMysqlTime().Marshal() if err != nil { return types.NewDatum(nil), errors.Trace(err) } return types.NewDatum(b), nil case types.KindMysqlDuration: // for mysql time type data.SetInt64(int64(data.GetMysqlDuration().Duration)) return data, nil case types.KindMysqlDecimal: data.SetString(data.GetMysqlDecimal().String()) return data, nil case types.KindMysqlEnum: data.SetUint64(data.GetMysqlEnum().Value) return data, nil case types.KindMysqlSet: data.SetUint64(data.GetMysqlSet().Value) return data, nil case types.KindMysqlBit: data.SetUint64(data.GetMysqlBit().Value) return data, nil case types.KindMysqlHex: data.SetInt64(data.GetMysqlHex().Value) return data, nil default: return data, nil } }
func dumpTextValue(mysqlType uint8, value types.Datum) ([]byte, error) { switch value.Kind() { case types.KindInt64: return strconv.AppendInt(nil, value.GetInt64(), 10), nil case types.KindUint64: return strconv.AppendUint(nil, value.GetUint64(), 10), nil case types.KindFloat32: return strconv.AppendFloat(nil, value.GetFloat64(), 'f', -1, 32), nil case types.KindFloat64: return strconv.AppendFloat(nil, value.GetFloat64(), 'f', -1, 64), nil case types.KindString, types.KindBytes: return value.GetBytes(), nil case types.KindMysqlTime: return hack.Slice(value.GetMysqlTime().String()), nil case types.KindMysqlDuration: return hack.Slice(value.GetMysqlDuration().String()), nil case types.KindMysqlDecimal: return hack.Slice(value.GetMysqlDecimal().String()), nil case types.KindMysqlEnum: return hack.Slice(value.GetMysqlEnum().String()), nil case types.KindMysqlSet: return hack.Slice(value.GetMysqlSet().String()), nil case types.KindMysqlBit: return hack.Slice(value.GetMysqlBit().ToString()), nil case types.KindMysqlHex: return hack.Slice(value.GetMysqlHex().ToString()), nil default: return nil, errInvalidType.Gen("invalid type %T", value) } }
func coerceArithmetic(a types.Datum) (d types.Datum, err error) { switch a.Kind() { case types.KindString, types.KindBytes: // MySQL will convert string to float for arithmetic operation f, err := types.StrToFloat(a.GetString()) if err != nil { return d, errors.Trace(err) } d.SetFloat64(f) return d, errors.Trace(err) case types.KindMysqlTime: // if time has no precision, return int64 t := a.GetMysqlTime() de := t.ToNumber() if t.Fsp == 0 { d.SetInt64(de.IntPart()) return d, nil } d.SetMysqlDecimal(de) return d, nil case types.KindMysqlDuration: // if duration has no precision, return int64 du := a.GetMysqlDuration() de := du.ToNumber() if du.Fsp == 0 { d.SetInt64(de.IntPart()) return d, nil } d.SetMysqlDecimal(de) return d, nil case types.KindMysqlHex: d.SetFloat64(a.GetMysqlHex().ToNumber()) return d, nil case types.KindMysqlBit: d.SetFloat64(a.GetMysqlBit().ToNumber()) return d, nil case types.KindMysqlEnum: d.SetFloat64(a.GetMysqlEnum().ToNumber()) return d, nil case types.KindMysqlSet: d.SetFloat64(a.GetMysqlSet().ToNumber()) return d, nil default: return a, nil } }
func (b *executorBuilder) datumToPBExpr(client kv.Client, d types.Datum) *tipb.Expr { var tp tipb.ExprType var val []byte switch d.Kind() { case types.KindNull: tp = tipb.ExprType_Null case types.KindInt64: tp = tipb.ExprType_Int64 val = codec.EncodeInt(nil, d.GetInt64()) case types.KindUint64: tp = tipb.ExprType_Uint64 val = codec.EncodeUint(nil, d.GetUint64()) case types.KindString: tp = tipb.ExprType_String val = d.GetBytes() case types.KindBytes: tp = tipb.ExprType_Bytes val = d.GetBytes() case types.KindFloat32: tp = tipb.ExprType_Float32 val = codec.EncodeFloat(nil, d.GetFloat64()) case types.KindFloat64: tp = tipb.ExprType_Float64 val = codec.EncodeFloat(nil, d.GetFloat64()) case types.KindMysqlDuration: tp = tipb.ExprType_MysqlDuration val = codec.EncodeInt(nil, int64(d.GetMysqlDuration().Duration)) case types.KindMysqlDecimal: tp = tipb.ExprType_MysqlDecimal val = codec.EncodeDecimal(nil, d.GetMysqlDecimal()) default: return nil } if !client.SupportRequestType(kv.ReqTypeSelect, int64(tp)) { return nil } return &tipb.Expr{Tp: tp.Enum(), Val: val} }
func convertDateFormat(ctx context.Context, arg types.Datum, b byte) (types.Datum, error) { var d types.Datum var err error switch b { case 'b': d, err = builtinMonthName([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(d.GetString()[:3]) } case 'M': d, err = builtinMonthName([]types.Datum{arg}, ctx) case 'm': d, err = builtinMonth([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'c': d, err = builtinMonth([]types.Datum{arg}, ctx) case 'D': d, err = abbrDayOfMonth(arg, ctx) case 'd': d, err = builtinDayOfMonth([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'e': d, err = builtinDayOfMonth([]types.Datum{arg}, ctx) case 'j': d, err = builtinDayOfYear([]types.Datum{arg}, ctx) if err == nil { d.SetString(fmt.Sprintf("%03d", d.GetInt64())) } case 'H', 'k': d, err = builtinHour([]types.Datum{arg}, ctx) if err == nil && b == 'H' && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'h', 'I', 'l': d, err = builtinHour([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { if d.GetInt64() > 12 { d.SetInt64(d.GetInt64() - 12) } else if d.GetInt64() == 0 { d.SetInt64(12) } d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'i': d, err = builtinMinute([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'p': d, err = builtinHour([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { if d.GetInt64() < 12 { d.SetString("AM") break } d.SetString("PM") } case 'r': d, err = to12Hour(arg, ctx) case 'T': d, err = builtinTime([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { duration := types.Duration{ Duration: d.GetMysqlDuration().Duration, Fsp: 0} d.SetMysqlDuration(duration) } case 'S', 's': d, err = builtinSecond([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'f': d, err = builtinMicroSecond([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%06d", d.GetInt64())) } case 'U': d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(0)}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'u': d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(1)}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'V': d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(2)}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'v': d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(3)}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%02d", d.GetInt64())) } case 'a': d, err = builtinDayName([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(d.GetString()[:3]) } case 'W': d, err = builtinDayName([]types.Datum{arg}, ctx) case 'w': d, err = builtinDayOfWeek([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetInt64(d.GetInt64() - 1) } case 'X': d, err = builtinYearWeek([]types.Datum{arg, types.NewIntDatum(2)}, ctx) if err == nil && !d.IsNull() { if d.GetInt64() == math.MaxUint32 { break } str := fmt.Sprintf("%04d", d.GetInt64()) d.SetString(fmt.Sprintf("%04s", str[:4])) } case 'x': d, err = builtinYearWeek([]types.Datum{arg, types.NewIntDatum(3)}, ctx) if err == nil && !d.IsNull() { if d.GetInt64() == math.MaxUint32 { break } str := fmt.Sprintf("%04d", d.GetInt64()) d.SetString(fmt.Sprintf("%04s", str[:4])) } case 'Y': d, err = builtinYear([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%04d", d.GetInt64())) } case 'y': d, err = builtinYear([]types.Datum{arg}, ctx) if err == nil && !d.IsNull() { str := fmt.Sprintf("%04d", d.GetInt64()) d.SetString(fmt.Sprintf("%02s", str[2:])) } default: d.SetString(string(b)) } if err == nil && !d.IsNull() { d.SetString(fmt.Sprintf("%v", d.GetValue())) } return d, errors.Trace(err) }