本文整理汇总了Golang中github.com/pingcap/tidb/util/types.ToInt64函数的典型用法代码示例。如果您正苦于以下问题:Golang ToInt64函数的具体用法?Golang ToInt64怎么用?Golang ToInt64使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToInt64函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: buildTableRanges
func (r *rangeBuilder) buildTableRanges(rangePoints []rangePoint) []TableRange {
tableRanges := make([]TableRange, 0, len(rangePoints)/2)
for i := 0; i < len(rangePoints); i += 2 {
startPoint := rangePoints[i]
if startPoint.value.Kind() == types.KindNull || startPoint.value.Kind() == types.KindMinNotNull {
startPoint.value.SetInt64(math.MinInt64)
}
startInt, err := types.ToInt64(startPoint.value.GetValue())
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
startDatum := types.NewDatum(startInt)
cmp, err := startDatum.CompareDatum(startPoint.value)
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
if cmp < 0 || (cmp == 0 && startPoint.excl) {
startInt++
}
endPoint := rangePoints[i+1]
if endPoint.value.Kind() == types.KindNull {
endPoint.value.SetInt64(math.MinInt64)
} else if endPoint.value.Kind() == types.KindMaxValue {
endPoint.value.SetInt64(math.MaxInt64)
}
endInt, err := types.ToInt64(endPoint.value.GetValue())
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
endDatum := types.NewDatum(endInt)
cmp, err = endDatum.CompareDatum(endPoint.value)
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
if cmp > 0 || (cmp == 0 && endPoint.excl) {
endInt--
}
if startInt > endInt {
continue
}
tableRanges = append(tableRanges, TableRange{LowVal: startInt, HighVal: endInt})
}
return tableRanges
}
示例2: builtinAbs
func builtinAbs(args []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
switch x := args[0].(type) {
case nil:
return nil, nil
case uint, uint8, uint16, uint32, uint64:
return x, nil
case int, int8, int16, int32, int64:
// we don't need to handle error here, it must be success
v, _ := types.ToInt64(args[0])
if v >= 0 {
return x, nil
}
// TODO: handle overflow if x is MinInt64
return -v, nil
case *types.DataItem:
args[0] = x.Data
return builtinAbs(args, ctx)
default:
// we will try to convert other types to float
// TODO: if time has no precision, it will be a integer
f, err := types.ToFloat64(args[0])
return math.Abs(f), errors.Trace(err)
}
}
示例3: buildTableRanges
func (r *rangeBuilder) buildTableRanges(rangePoints []rangePoint) []TableRange {
tableRanges := make([]TableRange, 0, len(rangePoints)/2)
for i := 0; i < len(rangePoints); i += 2 {
startPoint := rangePoints[i]
if startPoint.value == nil || startPoint.value == MinNotNullVal {
startPoint.value = math.MinInt64
}
startInt, err := types.ToInt64(startPoint.value)
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
cmp, err := types.Compare(startInt, startPoint.value)
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
if cmp < 0 || (cmp == 0 && startPoint.excl) {
startInt++
}
endPoint := rangePoints[i+1]
if endPoint.value == nil {
endPoint.value = math.MinInt64
} else if endPoint.value == MaxVal {
endPoint.value = math.MaxInt64
}
endInt, err := types.ToInt64(endPoint.value)
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
cmp, err = types.Compare(endInt, endPoint.value)
if err != nil {
r.err = errors.Trace(err)
return tableRanges
}
if cmp > 0 || (cmp == 0 && endPoint.excl) {
endInt--
}
if startInt > endInt {
continue
}
tableRanges = append(tableRanges, TableRange{LowVal: startInt, HighVal: endInt})
}
return tableRanges
}
示例4: getFsp
func (v *typeInferrer) getFsp(x *ast.FuncCallExpr) int {
if len(x.Args) == 1 {
a := x.Args[0].GetValue()
fsp, err := types.ToInt64(a)
if err != nil {
v.err = err
}
return int(fsp)
}
return 0
}
示例5: builtinRand
func builtinRand(args []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
if len(args) == 1 {
seed, err := types.ToInt64(args[0])
if err != nil {
return nil, errors.Trace(err)
}
rand.Seed(seed)
}
return rand.Float64(), nil
}
示例6: checkFsp
func checkFsp(arg interface{}) (int, error) {
fsp, err := types.ToInt64(arg)
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
}
示例7: toSeekKey
func (r *TableDefaultPlan) toSeekKey(seekVal interface{}) (kv.Key, error) {
var handle int64
var err error
if seekVal == nil {
handle = math.MinInt64
} else {
handle, err = types.ToInt64(seekVal)
if err != nil {
return nil, errors.Trace(err)
}
}
return tables.EncodeRecordKey(r.T.TableID(), handle, 0), nil
}
示例8: handleBitOp
func (e *Evaluator) handleBitOp(o *ast.BinaryOperationExpr) bool {
a, b := types.Coerce(o.L.GetValue(), o.R.GetValue())
if types.IsNil(a) || types.IsNil(b) {
o.SetValue(nil)
return true
}
x, err := types.ToInt64(a)
if err != nil {
e.err = errors.Trace(err)
return false
}
y, err := types.ToInt64(b)
if err != nil {
e.err = errors.Trace(err)
return false
}
// use a int64 for bit operator, return uint64
switch o.Op {
case opcode.And:
o.SetValue(uint64(x & y))
case opcode.Or:
o.SetValue(uint64(x | y))
case opcode.Xor:
o.SetValue(uint64(x ^ y))
case opcode.RightShift:
o.SetValue(uint64(x) >> uint64(y))
case opcode.LeftShift:
o.SetValue(uint64(x) << uint64(y))
default:
e.err = ErrInvalidOperation.Gen("invalid op %v in bit operation", o.Op)
return false
}
return true
}
示例9: evalDaysForm
func (da *DateArith) evalDaysForm(val interface{}) (interface{}, error) {
switch val.(type) {
case string:
if strings.ToLower(val.(string)) == "false" {
return 0, nil
}
if strings.ToLower(val.(string)) == "true" {
return 1, nil
}
val = reg.FindString(val.(string))
}
return types.ToInt64(val)
}
示例10: parseDayInterval
func parseDayInterval(value interface{}) (int64, error) {
switch v := value.(type) {
case string:
s := strings.ToLower(v)
if s == "false" {
return 0, nil
} else if s == "true" {
return 1, nil
}
value = reg.FindString(v)
}
return types.ToInt64(value)
}
示例11: evalBitOp
// Operator: &, ~, |, ^, <<, >>
// See https://dev.mysql.com/doc/refman/5.7/en/bit-functions.html
func (o *BinaryOperation) evalBitOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
a, b, err := o.get2(ctx, args)
if err != nil {
return nil, err
}
if a == nil || b == nil {
return nil, nil
}
x, err := types.ToInt64(a)
if err != nil {
return nil, o.traceErr(err)
}
y, err := types.ToInt64(b)
if err != nil {
return nil, o.traceErr(err)
}
// use a int64 for bit operator, return uint64
switch o.Op {
case opcode.And:
return uint64(x & y), nil
case opcode.Or:
return uint64(x | y), nil
case opcode.Xor:
return uint64(x ^ y), nil
case opcode.RightShift:
return uint64(x) >> uint64(y), nil
case opcode.LeftShift:
return uint64(x) << uint64(y), nil
default:
return nil, o.errorf("invalid op %v in bit operation", o.Op)
}
}
示例12: Eval
// Eval implements the Expression Eval interface.
func (f *FunctionSubstringIndex) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
fs, err := f.StrExpr.Eval(ctx, args)
if err != nil {
return nil, errors.Trace(err)
}
str, ok := fs.(string)
if !ok {
return nil, errors.Errorf("Substring_Index invalid args, need string but get %T", fs)
}
t, err := f.Delim.Eval(ctx, args)
if err != nil {
return nil, errors.Trace(err)
}
delim, ok := t.(string)
if !ok {
return nil, errors.Errorf("Substring_Index invalid delim, need string but get %T", t)
}
t, err = f.Count.Eval(ctx, args)
if err != nil {
return nil, errors.Trace(err)
}
c, err := types.ToInt64(t)
if err != nil {
return nil, errors.Trace(err)
}
count := int(c)
strs := strings.Split(str, delim)
var (
start = 0
end = len(strs)
)
if count > 0 {
// If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
if count < end {
end = count
}
} else {
// If count is negative, everything to the right of the final delimiter (counting from the right) is returned.
count = -count
if count < end {
start = end - count
}
}
substrs := strs[start:end]
return strings.Join(substrs, delim), nil
}
示例13: funcLocate
func (e *Evaluator) funcLocate(v *ast.FuncLocateExpr) bool {
// eval str
fs := v.Str.GetValue()
if types.IsNil(fs) {
v.SetValue(nil)
return true
}
str, err := types.ToString(fs)
if err != nil {
e.err = errors.Trace(err)
return false
}
// eval substr
fs = v.SubStr.GetValue()
if types.IsNil(fs) {
v.SetValue(nil)
return true
}
substr, err := types.ToString(fs)
if err != nil {
e.err = errors.Trace(err)
return false
}
// eval pos
pos := 0
if v.Pos != nil {
t := v.Pos.GetValue()
p, err := types.ToInt64(t)
if err != nil {
e.err = errors.Trace(err)
return false
}
pos = int(p)
}
// eval locate
if pos < 0 || pos > len(str) {
e.err = ErrInvalidOperation.Gen("Locate invalid pos args: %d", pos)
return false
}
str = str[pos:]
i := strings.Index(str, substr)
v.SetValue(i + 1 + pos)
return true
}
示例14: funcSubstringIndex
func (e *Evaluator) funcSubstringIndex(v *ast.FuncSubstringIndexExpr) bool {
fs := v.StrExpr.GetValue()
str, err := types.ToString(fs)
if err != nil {
e.err = ErrInvalidOperation.Gen("Substring_Index invalid args, need string but get %T", fs)
return false
}
t := v.Delim.GetValue()
delim, err := types.ToString(t)
if err != nil {
e.err = ErrInvalidOperation.Gen("Substring_Index invalid delim, need string but get %T", t)
return false
}
t = v.Count.GetValue()
c, err := types.ToInt64(t)
if err != nil {
e.err = errors.Trace(err)
return false
}
count := int(c)
strs := strings.Split(str, delim)
var (
start = 0
end = len(strs)
)
if count > 0 {
// If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
if count < end {
end = count
}
} else {
// If count is negative, everything to the right of the final delimiter (counting from the right) is returned.
count = -count
if count < end {
start = end - count
}
}
substrs := strs[start:end]
v.SetValue(strings.Join(substrs, delim))
return true
}
示例15: AddRecord
// AddRecord implements table.Table AddRecord interface.
func (t *MemoryTable) AddRecord(ctx context.Context, r []types.Datum) (recordID int64, err error) {
if t.pkHandleCol != nil {
recordID, err = types.ToInt64(r[t.pkHandleCol.Offset].GetValue())
if err != nil {
return 0, errors.Trace(err)
}
} else {
recordID, err = t.alloc.Alloc(t.ID)
if err != nil {
return 0, errors.Trace(err)
}
}
item := &itemPair{
handle: itemKey(recordID),
data: r,
}
t.mu.Lock()
defer t.mu.Unlock()
if t.tree.Get(itemKey(recordID)) != nil {
return 0, kv.ErrKeyExists
}
t.tree.ReplaceOrInsert(item)
return
}