当前位置: 首页>>代码示例>>Golang>>正文


Golang types.Convert函数代码示例

本文整理汇总了Golang中github.com/pingcap/tidb/util/types.Convert函数的典型用法代码示例。如果您正苦于以下问题:Golang Convert函数的具体用法?Golang Convert怎么用?Golang Convert使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Convert函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handleLogicOperation

func (e *Evaluator) handleLogicOperation(o *ast.BinaryOperationExpr) bool {
	leftVal, err := types.Convert(o.L.GetValue(), o.GetType())
	if err != nil {
		e.err = err
		return false
	}
	rightVal, err := types.Convert(o.R.GetValue(), o.GetType())
	if err != nil {
		e.err = err
		return false
	}
	if leftVal == nil || rightVal == nil {
		o.SetValue(nil)
		return true
	}
	var boolVal bool
	switch o.Op {
	case opcode.AndAnd:
		boolVal = leftVal != zeroI64 && rightVal != zeroI64
	case opcode.OrOr:
		boolVal = leftVal != zeroI64 || rightVal != zeroI64
	case opcode.LogicXor:
		boolVal = (leftVal == zeroI64 && rightVal != zeroI64) || (leftVal != zeroI64 && rightVal == zeroI64)
	default:
		panic("should never happen")
	}
	if boolVal {
		o.SetValue(oneI64)
	} else {
		o.SetValue(zeroI64)
	}
	return true
}
开发者ID:yzl11,项目名称:vessel,代码行数:33,代码来源:evaluator_binop.go

示例2: Eval

// Eval implements the Expression Eval interface.
func (e *Extract) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
	v, err := e.Date.Eval(ctx, args)
	if v == nil || err != nil {
		return nil, errors.Trace(err)
	}

	f := types.NewFieldType(mysql.TypeDatetime)
	f.Decimal = mysql.MaxFsp

	v, err = types.Convert(v, f)
	if v == nil || err != nil {
		return nil, errors.Trace(err)
	}

	t, ok := v.(mysql.Time)
	if !ok {
		return nil, errors.Errorf("need time type, but got %T", v)
	}

	n, err1 := extractTime(e.Unit, t)
	if err1 != nil {
		return nil, errors.Trace(err1)
	}

	return n, nil
}
开发者ID:Brian110,项目名称:tidb,代码行数:27,代码来源:extract.go

示例3: funcExtract

func (e *Evaluator) funcExtract(v *ast.FuncExtractExpr) bool {
	val := v.Date.GetValue()
	if val == nil {
		v.SetValue(nil)
		return true
	}

	f := types.NewFieldType(mysql.TypeDatetime)
	f.Decimal = mysql.MaxFsp
	var err error
	val, err = types.Convert(val, f)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	if val == nil {
		v.SetValue(nil)
		return true
	}

	t, ok := val.(mysql.Time)
	if !ok {
		e.err = ErrInvalidOperation.Gen("need time type, but got %T", val)
		return false
	}
	n, err1 := mysql.ExtractTimeNum(v.Unit, t)
	if err1 != nil {
		e.err = errors.Trace(err1)
		return false
	}
	v.SetValue(n)
	return true
}
开发者ID:mrtoms,项目名称:tidb,代码行数:33,代码来源:evaluator.go

示例4: Next

// Next implements Executor Next interface.
func (e *IndexRangeExec) Next() (*Row, error) {
	if e.iter == nil {
		seekVals := make([]interface{}, len(e.scan.idx.Columns))
		for i := 0; i < len(e.lowVals); i++ {
			var err error
			if e.lowVals[i] == plan.MinNotNullVal {
				seekVals[i] = []byte{}
			} else {
				seekVals[i], err = types.Convert(e.lowVals[i], e.scan.valueTypes[i])
				if err != nil {
					return nil, errors.Trace(err)
				}
			}
		}
		txn, err := e.scan.ctx.GetTxn(false)
		if err != nil {
			return nil, errors.Trace(err)
		}
		e.iter, _, err = e.scan.idx.X.Seek(txn, seekVals)
		if err != nil {
			return nil, types.EOFAsNil(err)
		}
	}

	for {
		if e.finished {
			return nil, nil
		}
		idxKey, h, err := e.iter.Next()
		if err != nil {
			return nil, types.EOFAsNil(err)
		}
		if !e.skipLowCmp {
			var cmp int
			cmp, err = indexCompare(idxKey, e.lowVals)
			if err != nil {
				return nil, errors.Trace(err)
			}
			if cmp < 0 || (cmp == 0 && e.lowExclude) {
				continue
			}
			e.skipLowCmp = true
		}
		cmp, err := indexCompare(idxKey, e.highVals)
		if err != nil {
			return nil, errors.Trace(err)
		}
		if cmp > 0 || (cmp == 0 && e.highExclude) {
			// This span has finished iteration.
			e.finished = true
			continue
		}
		var row *Row
		row, err = e.lookupRow(h)
		if err != nil {
			return nil, errors.Trace(err)
		}
		return row, nil
	}
}
开发者ID:lovedboy,项目名称:tidb,代码行数:61,代码来源:executor.go

示例5: Next

// Next implements Executor Next interface.
func (e *UnionExec) Next() (*Row, error) {
	for {
		if e.cursor >= len(e.Sels) {
			return nil, nil
		}
		sel := e.Sels[e.cursor]
		row, err := sel.Next()
		if err != nil {
			return nil, errors.Trace(err)
		}
		if row == nil {
			e.cursor++
			continue
		}
		if e.cursor != 0 {
			for i := range row.Data {
				// The column value should be casted as the same type of the first select statement in corresponding position
				rf := e.fields[i]
				row.Data[i], err = types.Convert(row.Data[i], &rf.Column.FieldType)
				if err != nil {
					return nil, errors.Trace(err)
				}
			}
		}
		for i, v := range row.Data {
			e.fields[i].Expr.SetValue(v)
		}
		return row, nil
	}
}
开发者ID:steffengy,项目名称:tidb,代码行数:31,代码来源:executor.go

示例6: Eval

// Eval implements the Expression Eval interface.
func (c *Conversion) Eval(ctx context.Context, args map[interface{}]interface{}) (v interface{}, err error) {
	Val, err := c.Val.Eval(ctx, args)
	if err != nil {
		return
	}
	ft := types.NewFieldType(c.Tp)
	return types.Convert(Val, ft)
}
开发者ID:npk,项目名称:tidb,代码行数:9,代码来源:conversion.go

示例7: backfillColumnData

func (d *ddl) backfillColumnData(t table.Table, columnInfo *model.ColumnInfo, handles []int64, reorgInfo *reorgInfo) error {
	for _, handle := range handles {
		log.Info("[ddl] backfill column...", handle)

		err := kv.RunInNewTxn(d.store, true, func(txn kv.Transaction) error {
			if err := d.isReorgRunnable(txn); err != nil {
				return errors.Trace(err)
			}

			// First check if row exists.
			exist, err := checkRowExist(txn, t, handle)
			if err != nil {
				return errors.Trace(err)
			} else if !exist {
				// If row doesn't exist, skip it.
				return nil
			}

			backfillKey := t.RecordKey(handle, &column.Col{ColumnInfo: *columnInfo})
			backfillValue, err := txn.Get(backfillKey)
			if err != nil && !kv.IsErrNotFound(err) {
				return errors.Trace(err)
			}
			if backfillValue != nil {
				return nil
			}

			value, _, err := tables.GetColDefaultValue(nil, columnInfo)
			if err != nil {
				return errors.Trace(err)
			}

			// must convert to the column field type.
			v, err := types.Convert(value, &columnInfo.FieldType)
			if err != nil {
				return errors.Trace(err)
			}

			err = lockRow(txn, t, handle)
			if err != nil {
				return errors.Trace(err)
			}

			err = t.SetColValue(txn, backfillKey, v)
			if err != nil {
				return errors.Trace(err)
			}

			return errors.Trace(reorgInfo.UpdateHandle(txn, handle))
		})

		if err != nil {
			return errors.Trace(err)
		}
	}

	return nil
}
开发者ID:youprofit,项目名称:tidb,代码行数:58,代码来源:column.go

示例8: binaryOperation

func (e *Evaluator) binaryOperation(o *ast.BinaryOperationExpr) bool {
	// all operands must have same column.
	if e.err = hasSameColumnCount(o.L, o.R); e.err != nil {
		return false
	}

	// row constructor only supports comparison operation.
	switch o.Op {
	case opcode.LT, opcode.LE, opcode.GE, opcode.GT, opcode.EQ, opcode.NE, opcode.NullEQ:
	default:
		if !checkAllOneColumn(o.L) {
			e.err = errors.Errorf("Operand should contain 1 column(s)")
			return false
		}
	}

	leftVal, err := types.Convert(o.L.GetValue(), o.GetType())
	if err != nil {
		e.err = err
		return false
	}
	rightVal, err := types.Convert(o.R.GetValue(), o.GetType())
	if err != nil {
		e.err = err
		return false
	}
	if leftVal == nil || rightVal == nil {
		o.SetValue(nil)
		return true
	}

	switch o.Op {
	case opcode.AndAnd, opcode.OrOr, opcode.LogicXor:
		return e.handleLogicOperation(o)
	case opcode.LT, opcode.LE, opcode.GE, opcode.GT, opcode.EQ, opcode.NE, opcode.NullEQ:
		return e.handleComparisonOp(o)
	case opcode.RightShift, opcode.LeftShift, opcode.And, opcode.Or, opcode.Xor:
		// TODO: MySQL doesn't support and not, we should remove it later.
		return e.handleBitOp(o)
	case opcode.Plus, opcode.Minus, opcode.Mod, opcode.Div, opcode.Mul, opcode.IntDiv:
		return e.handleArithmeticOp(o)
	default:
		panic("should never happen")
	}
}
开发者ID:yzl11,项目名称:vessel,代码行数:45,代码来源:evaluator_binop.go

示例9: castValues

// castValues casts values based on columns type.
func castValues(rec []interface{}, cols []*model.ColumnInfo) (err error) {
	for _, c := range cols {
		rec[c.Offset], err = types.Convert(rec[c.Offset], &c.FieldType)
		if err != nil {
			return errors.Trace(err)
		}
	}
	return nil
}
开发者ID:xudongQiu,项目名称:tidb,代码行数:10,代码来源:common.go

示例10: filterBinOp

func (r *TableDefaultPlan) filterBinOp(ctx context.Context, x *expression.BinaryOperation) (plan.Plan, bool, error) {
	ok, name, rval, err := x.IsIdentCompareVal()
	if err != nil {
		return r, false, errors.Trace(err)
	}
	if !ok {
		return r, false, nil
	}
	if rval == nil {
		// if nil, any <, <=, >, >=, =, != operator will do nothing
		// any value compared null returns null
		// TODO: if we support <=> later, we must handle null
		return &NullPlan{r.GetFields()}, true, nil
	}

	_, tn, cn := field.SplitQualifiedName(name)
	t := r.T
	if tn != "" && tn != t.TableName().L {
		return r, false, nil
	}
	c := column.FindCol(t.Cols(), cn)
	if c == nil {
		return nil, false, errors.Errorf("No such column: %s", cn)
	}
	var seekVal interface{}
	if seekVal, err = types.Convert(rval, &c.FieldType); err != nil {
		return nil, false, errors.Trace(err)
	}
	spans := toSpans(x.Op, rval, seekVal)
	if c.IsPKHandleColumn(r.T.Meta()) {
		if r.rangeScan {
			spans = filterSpans(r.spans, spans)
		}
		return &TableDefaultPlan{
			T:         r.T,
			Fields:    r.Fields,
			rangeScan: true,
			spans:     spans,
		}, true, nil
	} else if r.rangeScan {
		// Already filtered on PK handle column, should not switch to index plan.
		return r, false, nil
	}

	ix := t.FindIndexByColName(cn)
	if ix == nil { // Column cn has no index.
		return r, false, nil
	}
	return &indexPlan{
		src:     t,
		col:     c,
		unique:  ix.Unique,
		idxName: ix.Name.O,
		idx:     ix.X,
		spans:   spans,
	}, true, nil
}
开发者ID:lovedboy,项目名称:tidb,代码行数:57,代码来源:from.go

示例11: CastValues

// CastValues casts values based on columns type.
func CastValues(ctx context.Context, rec []interface{}, cols []*Col) (err error) {
	for _, c := range cols {
		rec[c.Offset], err = types.Convert(rec[c.Offset], &c.FieldType)
		if err != nil {
			return errors.Trace(err)
		}
	}
	return nil
}
开发者ID:lovedboy,项目名称:tidb,代码行数:10,代码来源:column.go

示例12: Filter

// Filter implements plan.Plan Filter interface.
// Filter merges BinaryOperations and determines the lower and upper bound.
func (r *indexPlan) Filter(ctx context.Context, expr expression.Expression) (plan.Plan, bool, error) {
	var spans []*indexSpan
	switch x := expr.(type) {
	case *expression.BinaryOperation:
		ok, name, val, err := x.IsIdentCompareVal()
		if err != nil {
			return nil, false, err
		}
		if !ok {
			break
		}
		_, tname, cname := field.SplitQualifiedName(name)
		if tname != "" && r.src.TableName().L != tname {
			break
		}
		if r.col.ColumnInfo.Name.L != cname {
			break
		}
		seekVal, err := types.Convert(val, &r.col.FieldType)
		if err != nil {
			return nil, false, errors.Trace(err)
		}
		spans = filterSpans(r.spans, toSpans(x.Op, val, seekVal))
	case *expression.Ident:
		if r.col.Name.L != x.L {
			break
		}
		spans = filterSpans(r.spans, toSpans(opcode.GE, minNotNullVal, nil))
	case *expression.UnaryOperation:
		if x.Op != '!' {
			break
		}

		cname, ok := x.V.(*expression.Ident)
		if !ok {
			break
		}
		if r.col.Name.L != cname.L {
			break
		}
		spans = filterSpans(r.spans, toSpans(opcode.EQ, nil, nil))
	}

	if spans == nil {
		return r, false, nil
	}

	return &indexPlan{
		src:     r.src,
		col:     r.col,
		unique:  r.unique,
		idxName: r.idxName,
		idx:     r.idx,
		spans:   spans,
	}, true, nil
}
开发者ID:huozhe3136,项目名称:tidb,代码行数:58,代码来源:index.go

示例13: fetchSrc

func (p *UnionPlan) fetchSrc(ctx context.Context, i int, t memkv.Temp) error {
	src := p.Srcs[i+1]
	distinct := p.Distincts[i]

	// Use the ResultFields of the first select statement as the final ResultFields
	rfs := p.Srcs[0].GetFields()
	if len(src.GetFields()) != len(rfs) {
		return errors.New("The used SELECT statements have a different number of columns")
	}
	for {
		row, err := src.Next(ctx)
		if row == nil || err != nil {
			return errors.Trace(err)
		}
		srcRfs := src.GetFields()
		for i := range row.Data {
			// The column value should be casted as the same type of the first select statement in corresponding position
			srcRf := srcRfs[i]
			rf := rfs[i]
			/*
			 * The lengths of the columns in the UNION result take into account the values retrieved by all of the SELECT statements
			 * SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
			 * +---------------+
			 * | REPEAT('a',1) |
			 * +---------------+
			 * | a             |
			 * | bbbbbbbbbb    |
			 * +---------------+
			 */
			if srcRf.Flen > rf.Col.Flen {
				rf.Col.Flen = srcRf.Col.Flen
			}
			row.Data[i], err = types.Convert(row.Data[i], &rf.Col.FieldType)
			if err != nil {
				return errors.Trace(err)
			}
		}
		if distinct {
			// distinct union, check duplicate
			v, getErr := t.Get(row.Data)
			if getErr != nil {
				return errors.Trace(getErr)
			}
			if len(v) > 0 {
				// Find duplicate, ignore it
				continue
			}
		}
		p.rows = append(p.rows, row)
		if err := t.Set(row.Data, []interface{}{true}); err != nil {
			return errors.Trace(err)
		}
	}
}
开发者ID:superwood,项目名称:tidb,代码行数:54,代码来源:union.go

示例14: Eval

// Eval implements the Expression Eval interface.
func (da *DateAdd) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
	dv, err := da.Date.Eval(ctx, args)
	if dv == nil || err != nil {
		return nil, errors.Trace(err)
	}

	sv, err := types.ToString(dv)
	if err != nil {
		return nil, errors.Trace(err)
	}

	f := types.NewFieldType(mysql.TypeDatetime)
	f.Decimal = mysql.MaxFsp

	dv, err = types.Convert(sv, f)
	if dv == nil || err != nil {
		return nil, errors.Trace(err)
	}

	t, ok := dv.(mysql.Time)
	if !ok {
		return nil, errors.Errorf("need time type, but got %T", dv)
	}

	iv, err := da.Interval.Eval(ctx, args)
	if iv == nil || err != nil {
		return nil, errors.Trace(err)
	}

	format, err := types.ToString(iv)
	if err != nil {
		return nil, errors.Trace(err)
	}

	years, months, days, durations, err := mysql.ExtractTimeValue(da.Unit, strings.TrimSpace(format))
	if err != nil {
		return nil, errors.Trace(err)
	}

	t.Time = t.Time.Add(durations)
	t.Time = t.Time.AddDate(int(years), int(months), int(days))

	// "2011-11-11 10:10:20.000000" outputs "2011-11-11 10:10:20".
	if t.Time.Nanosecond() == 0 {
		t.Fsp = 0
	}

	return t, nil
}
开发者ID:js-for-kids,项目名称:tidb,代码行数:50,代码来源:date_add.go

示例15: evalArgs

func (da *DateArith) evalArgs(ctx context.Context, args map[interface{}]interface{}) (
	*evalArgsResult, error) {
	ret := &evalArgsResult{time: mysql.ZeroTimestamp}

	dVal, err := da.Date.Eval(ctx, args)
	if dVal == nil || err != nil {
		return ret, errors.Trace(err)
	}
	dValStr, err := types.ToString(dVal)
	if err != nil {
		return ret, errors.Trace(err)
	}
	f := types.NewFieldType(mysql.TypeDatetime)
	f.Decimal = mysql.MaxFsp
	dVal, err = types.Convert(dValStr, f)
	if dVal == nil || err != nil {
		return ret, errors.Trace(err)
	}

	var ok bool
	ret.time, ok = dVal.(mysql.Time)
	if !ok {
		return ret, errors.Errorf("need time type, but got %T", dVal)
	}

	iVal, err := da.Interval.Eval(ctx, args)
	if iVal == nil || err != nil {
		ret.time = mysql.ZeroTimestamp
		return ret, errors.Trace(err)
	}
	// handle adddate(expr,days) or subdate(expr,days) form
	if da.Form == DateArithDaysForm {
		if iVal, err = da.evalDaysForm(iVal); err != nil {
			return ret, errors.Trace(err)
		}
	}

	iValStr, err := types.ToString(iVal)
	if err != nil {
		return ret, errors.Trace(err)
	}
	ret.year, ret.month, ret.day, ret.duration, err = mysql.ExtractTimeValue(da.Unit, strings.TrimSpace(iValStr))
	if err != nil {
		return ret, errors.Trace(err)
	}

	return ret, nil
}
开发者ID:yzl11,项目名称:vessel,代码行数:48,代码来源:date_arith.go


注:本文中的github.com/pingcap/tidb/util/types.Convert函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。