當前位置: 首頁>>代碼示例>>Golang>>正文


Golang types.NewFieldType函數代碼示例

本文整理匯總了Golang中github.com/pingcap/tidb/util/types.NewFieldType函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewFieldType函數的具體用法?Golang NewFieldType怎麽用?Golang NewFieldType使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewFieldType函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: SetUpSuite

func (ts *testMemoryTableSuite) SetUpSuite(c *C) {
	driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
	store, err := driver.Open("memory")
	c.Check(err, IsNil)
	ts.store = store
	ts.se, err = tidb.CreateSession(ts.store)
	c.Assert(err, IsNil)

	// create table
	tp1 := types.NewFieldType(mysql.TypeLong)
	col1 := &model.ColumnInfo{
		ID:        1,
		Name:      model.NewCIStr("a"),
		Offset:    0,
		FieldType: *tp1,
	}
	tp2 := types.NewFieldType(mysql.TypeVarchar)
	tp2.Flen = 255
	col2 := &model.ColumnInfo{
		ID:        2,
		Name:      model.NewCIStr("b"),
		Offset:    1,
		FieldType: *tp2,
	}

	tblInfo := &model.TableInfo{
		ID:      100,
		Name:    model.NewCIStr("t"),
		Columns: []*model.ColumnInfo{col1, col2},
	}
	alloc := autoid.NewMemoryAllocator(int64(10))
	ts.tbl, _ = tables.MemoryTableFromMeta(alloc, tblInfo)
}
開發者ID:XuHuaiyu,項目名稱:tidb,代碼行數:33,代碼來源:memory_tables_test.go

示例2: TestCast

func (s *testCastSuite) TestCast(c *C) {
	f := types.NewFieldType(mysql.TypeLonglong)

	expr := &FunctionCast{
		Expr: Value{1},
		Tp:   f,
	}

	f.Flag |= mysql.UnsignedFlag
	c.Assert(len(expr.String()), Greater, 0)
	f.Flag = 0
	c.Assert(len(expr.String()), Greater, 0)
	f.Tp = mysql.TypeDatetime
	c.Assert(len(expr.String()), Greater, 0)

	f.Tp = mysql.TypeLonglong
	_, err := expr.Clone()
	c.Assert(err, IsNil)

	c.Assert(expr.IsStatic(), IsTrue)

	v, err := expr.Eval(nil, nil)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, int64(1))

	f.Flag |= mysql.UnsignedFlag
	v, err = expr.Eval(nil, nil)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, uint64(1))

	f.Tp = mysql.TypeString
	f.Charset = charset.CharsetBin
	v, err = expr.Eval(nil, nil)
	c.Assert(err, IsNil)
	c.Assert(v, DeepEquals, []byte("1"))

	expr.Expr = Value{nil}
	v, err = expr.Eval(nil, nil)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, nil)

	expr.Expr = mockExpr{err: errors.New("must error")}
	_, err = expr.Clone()
	c.Assert(err, NotNil)

	_, err = expr.Eval(nil, nil)
	c.Assert(err, NotNil)

	// For String()
	f = types.NewFieldType(mysql.TypeLonglong)
	expr = &FunctionCast{
		Expr: Value{1},
		Tp:   f,
	}
	str := expr.String()
	c.Assert(str, Equals, "CAST(1 AS SIGNED)")
	expr.IsConvert = true
	str = expr.String()
	c.Assert(str, Equals, "CONVERT(1, SIGNED)")
}
開發者ID:ninefive,項目名稱:tidb,代碼行數:60,代碼來源:cast_test.go

示例3: buildExplain

func (b *planBuilder) buildExplain(explain *ast.ExplainStmt) Plan {
	if show, ok := explain.Stmt.(*ast.ShowStmt); ok {
		return b.buildShow(show)
	}
	targetPlan, err := Optimize(b.ctx, explain.Stmt, b.is)
	if err != nil {
		b.err = errors.Trace(err)
		return nil
	}
	p := &Explain{StmtPlan: targetPlan}
	addChild(p, targetPlan)
	schema := make(expression.Schema, 0, 3)
	schema = append(schema, &expression.Column{
		ColName: model.NewCIStr("ID"),
		RetType: types.NewFieldType(mysql.TypeString),
	})
	schema = append(schema, &expression.Column{
		ColName: model.NewCIStr("Json"),
		RetType: types.NewFieldType(mysql.TypeString),
	})
	schema = append(schema, &expression.Column{
		ColName: model.NewCIStr("ParentID"),
		RetType: types.NewFieldType(mysql.TypeString),
	})
	p.SetSchema(schema)
	return p
}
開發者ID:pingcap,項目名稱:tidb,代碼行數:27,代碼來源:planbuilder.go

示例4: aggregateFunc

func (v *typeInferrer) aggregateFunc(x *ast.AggregateFuncExpr) {
	name := strings.ToLower(x.F)
	switch name {
	case ast.AggFuncCount:
		ft := types.NewFieldType(mysql.TypeLonglong)
		ft.Flen = 21
		ft.Charset = charset.CharsetBin
		ft.Collate = charset.CollationBin
		x.SetType(ft)
	case ast.AggFuncMax, ast.AggFuncMin:
		x.SetType(x.Args[0].GetType())
	case ast.AggFuncSum, ast.AggFuncAvg:
		ft := types.NewFieldType(mysql.TypeNewDecimal)
		ft.Charset = charset.CharsetBin
		ft.Collate = charset.CollationBin
		x.SetType(ft)
	case ast.AggFuncGroupConcat:
		ft := types.NewFieldType(mysql.TypeVarString)
		ft.Charset = v.defaultCharset
		cln, err := charset.GetDefaultCollation(v.defaultCharset)
		if err != nil {
			v.err = err
		}
		ft.Collate = cln
		x.SetType(ft)
	}
}
開發者ID:mumubusu,項目名稱:tidb,代碼行數:27,代碼來源:typeinferer.go

示例5: TestField

func (*testFieldSuite) TestField(c *C) {
	f := &field.Field{
		Expr:   expression.Value{Val: "c1+1"},
		AsName: "a",
	}
	s := f.String()
	c.Assert(len(s), Greater, 0)

	ft := types.NewFieldType(mysql.TypeLong)
	ft.Flen = 20
	ft.Flag |= mysql.UnsignedFlag | mysql.ZerofillFlag
	c.Assert(ft.String(), Equals, "int(20) UNSIGNED ZEROFILL")

	ft = types.NewFieldType(mysql.TypeFloat)
	ft.Flen = 20
	ft.Decimal = 10
	c.Assert(ft.String(), Equals, "float(20,10)")

	ft = types.NewFieldType(mysql.TypeTimestamp)
	ft.Decimal = 8
	c.Assert(ft.String(), Equals, "timestamp(8)")

	ft = types.NewFieldType(mysql.TypeVarchar)
	ft.Flag |= mysql.BinaryFlag
	ft.Charset = "utf8"
	ft.Collate = "utf8_unicode_gi"
	c.Assert(ft.String(), Equals, "varchar BINARY CHARACTER SET utf8 COLLATE utf8_unicode_gi")
}
開發者ID:lovedboy,項目名稱:tidb,代碼行數:28,代碼來源:field_test.go

示例6: addAggregation

func (p *physicalTableSource) addAggregation(agg *PhysicalAggregation) expression.Schema {
	if p.client == nil {
		return nil
	}
	for _, f := range agg.AggFuncs {
		pb := aggFuncToPBExpr(p.client, f)
		if pb == nil {
			p.clear()
			return nil
		}
		p.AggFuncs = append(p.AggFuncs, pb)
	}
	for _, item := range agg.GroupByItems {
		pb := groupByItemToPB(p.client, item)
		if pb == nil {
			p.clear()
			return nil
		}
		p.GbyItems = append(p.GbyItems, pb)
	}
	p.Aggregated = true
	gk := types.NewFieldType(mysql.TypeBlob)
	gk.Charset = charset.CharsetBin
	gk.Collate = charset.CollationBin
	p.AggFields = append(p.AggFields, gk)
	var schema expression.Schema
	cursor := 0
	schema = append(schema, &expression.Column{Index: cursor, ColName: model.NewCIStr(fmt.Sprint(agg.GroupByItems))})
	agg.GroupByItems = []expression.Expression{schema[cursor]}
	newAggFuncs := make([]expression.AggregationFunction, len(agg.AggFuncs))
	for i, aggFun := range agg.AggFuncs {
		fun := expression.NewAggFunction(aggFun.GetName(), nil, false)
		var args []expression.Expression
		colName := model.NewCIStr(fmt.Sprint(aggFun.GetArgs()))
		if needCount(fun) {
			cursor++
			schema = append(schema, &expression.Column{Index: cursor, ColName: colName})
			args = append(args, schema[cursor])
			ft := types.NewFieldType(mysql.TypeLonglong)
			ft.Flen = 21
			ft.Charset = charset.CharsetBin
			ft.Collate = charset.CollationBin
			p.AggFields = append(p.AggFields, ft)
		}
		if needValue(fun) {
			cursor++
			schema = append(schema, &expression.Column{Index: cursor, ColName: colName})
			args = append(args, schema[cursor])
			p.AggFields = append(p.AggFields, agg.schema[i].GetType())
		}
		fun.SetArgs(args)
		fun.SetMode(expression.FinalMode)
		newAggFuncs[i] = fun
	}
	agg.AggFuncs = newAggFuncs
	return schema
}
開發者ID:jmptrader,項目名稱:tidb,代碼行數:57,代碼來源:physical_plans.go

示例7: TestAlterSpecification

func (s *testDDLSuite) TestAlterSpecification(c *C) {
	tbl := []*AlterSpecification{
		{
			Action: AlterTableOpt,
		},
		{
			Action: AlterDropColumn,
			Name:   "c1",
		},
		{Action: AlterDropPrimaryKey},
		{Action: AlterDropForeignKey,
			Name: "c"},
		{Action: AlterDropIndex,
			Name: "index_c"},
		{Action: AlterAddConstr,
			Constraint: nil},
		{Action: AlterAddConstr,
			Constraint: &coldef.TableConstraint{
				Tp: coldef.ConstrPrimaryKey,
				Keys: []*coldef.IndexColName{
					{
						ColumnName: "a",
						Length:     10,
					},
				},
			}},
		{Action: AlterAddColumn,
			Column: &coldef.ColumnDef{
				Name: "c",
				Tp:   types.NewFieldType(mysql.TypeLong),
			},
			Position: &ColumnPosition{}},
		{Action: AlterAddColumn,
			Column: &coldef.ColumnDef{
				Name: "c",
				Tp:   types.NewFieldType(mysql.TypeLong),
			},
			Position: &ColumnPosition{Type: ColumnPositionFirst}},
		{Action: AlterAddColumn,
			Column: &coldef.ColumnDef{
				Name: "c",
				Tp:   types.NewFieldType(mysql.TypeLong),
			},
			Position: &ColumnPosition{Type: ColumnPositionAfter,
				RelativeColumn: "c"}},

		// Invalid action returns empty string
		{Action: -1},
	}

	for _, spec := range tbl {
		c.Assert(len(spec.String()), GreaterEqual, 0)
	}
}
開發者ID:lovedboy,項目名稱:tidb,代碼行數:54,代碼來源:alter_test.go

示例8: handleFuncCallExpr

func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
	var (
		tp  *types.FieldType
		chs = charset.CharsetBin
	)
	switch x.FnName.L {
	case "abs", "ifnull", "nullif":
		tp = x.Args[0].GetType()
	case "pow", "power", "rand":
		tp = types.NewFieldType(mysql.TypeDouble)
	case "curdate", "current_date", "date":
		tp = types.NewFieldType(mysql.TypeDate)
	case "curtime", "current_time":
		tp = types.NewFieldType(mysql.TypeDuration)
		tp.Decimal = v.getFsp(x)
	case "current_timestamp":
		tp = types.NewFieldType(mysql.TypeDatetime)
	case "microsecond", "second", "minute", "hour", "day", "week", "month", "year",
		"dayofweek", "dayofmonth", "dayofyear", "weekday", "weekofyear", "yearweek",
		"found_rows", "length":
		tp = types.NewFieldType(mysql.TypeLonglong)
	case "now", "sysdate":
		tp = types.NewFieldType(mysql.TypeDatetime)
		tp.Decimal = v.getFsp(x)
	case "dayname", "version", "database", "user", "current_user",
		"concat", "concat_ws", "left", "lower", "repeat", "replace", "upper":
		tp = types.NewFieldType(mysql.TypeVarString)
		chs = v.defaultCharset
	case "connection_id":
		tp = types.NewFieldType(mysql.TypeLonglong)
		tp.Flag |= mysql.UnsignedFlag
	case "if":
		// TODO: fix this
		// See: https://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html#function_if
		// The default return type of IF() (which may matter when it is stored into a temporary table) is calculated as follows.
		// Expression	Return Value
		// expr2 or expr3 returns a string	string
		// expr2 or expr3 returns a floating-point value	floating-point
		// expr2 or expr3 returns an integer	integer
		tp = x.Args[1].GetType()
	default:
		tp = types.NewFieldType(mysql.TypeUnspecified)
	}
	// If charset is unspecified.
	if len(tp.Charset) == 0 {
		tp.Charset = chs
		cln := charset.CollationBin
		if chs != charset.CharsetBin {
			var err error
			cln, err = charset.GetDefaultCollation(chs)
			if err != nil {
				v.err = err
			}
		}
		tp.Collate = cln
	}
	x.SetType(tp)
}
開發者ID:yuanfeng0905,項目名稱:tidb,代碼行數:58,代碼來源:typeinferer.go

示例9: NewValueExpr

// NewValueExpr creates a ValueExpr with value, and sets default field type.
func NewValueExpr(value interface{}) *ValueExpr {
	ve := &ValueExpr{}
	ve.Data = types.RawData(value)
	// TODO: make it more precise.
	switch value.(type) {
	case nil:
		ve.Type = types.NewFieldType(mysql.TypeNull)
	case bool, int64:
		ve.Type = types.NewFieldType(mysql.TypeLonglong)
	case uint64:
		ve.Type = types.NewFieldType(mysql.TypeLonglong)
		ve.Type.Flag |= mysql.UnsignedFlag
	case string, UnquoteString:
		ve.Type = types.NewFieldType(mysql.TypeVarchar)
		ve.Type.Charset = mysql.DefaultCharset
		ve.Type.Collate = mysql.DefaultCollationName
	case float64:
		ve.Type = types.NewFieldType(mysql.TypeDouble)
	case []byte:
		ve.Type = types.NewFieldType(mysql.TypeBlob)
		ve.Type.Charset = "binary"
		ve.Type.Collate = "binary"
	case mysql.Bit:
		ve.Type = types.NewFieldType(mysql.TypeBit)
	case mysql.Hex:
		ve.Type = types.NewFieldType(mysql.TypeVarchar)
		ve.Type.Charset = "binary"
		ve.Type.Collate = "binary"
	case *types.DataItem:
		ve.Type = value.(*types.DataItem).Type
	default:
		panic(fmt.Sprintf("illegal literal value type:%T", value))
	}
	return ve
}
開發者ID:yzl11,項目名稱:vessel,代碼行數:36,代碼來源:expressions.go

示例10: buildApply

func (b *planBuilder) buildApply(p, inner LogicalPlan, schema expression.Schema, checker *ApplyConditionChecker) LogicalPlan {
	ap := &Apply{
		InnerPlan:       inner,
		OuterSchema:     schema,
		Checker:         checker,
		baseLogicalPlan: newBaseLogicalPlan(App, b.allocator),
	}
	ap.initID()
	addChild(ap, p)
	innerSchema := inner.GetSchema().DeepCopy()
	if checker == nil {
		for _, col := range innerSchema {
			col.IsAggOrSubq = true
		}
		ap.SetSchema(append(p.GetSchema().DeepCopy(), innerSchema...))
	} else {
		ap.SetSchema(append(p.GetSchema().DeepCopy(), &expression.Column{
			FromID:      ap.id,
			ColName:     model.NewCIStr("exists_row"),
			RetType:     types.NewFieldType(mysql.TypeTiny),
			IsAggOrSubq: true,
		}))
	}
	ap.correlated = p.IsCorrelated()
	return ap
}
開發者ID:yuyongwei,項目名稱:tidb,代碼行數:26,代碼來源:newplanbuilder.go

示例11: TestCast

func (s *testEvaluatorSuite) TestCast(c *C) {
	defer testleak.AfterTest(c)()
	f := types.NewFieldType(mysql.TypeLonglong)

	expr := &ast.FuncCastExpr{
		Expr: ast.NewValueExpr(1),
		Tp:   f,
	}
	ast.SetFlag(expr)
	v, err := Eval(s.ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, testutil.DatumEquals, types.NewDatum(int64(1)))

	f.Flag |= mysql.UnsignedFlag
	v, err = Eval(s.ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, testutil.DatumEquals, types.NewDatum(uint64(1)))

	f.Tp = mysql.TypeString
	f.Charset = charset.CharsetBin
	v, err = Eval(s.ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, testutil.DatumEquals, types.NewDatum([]byte("1")))

	f.Tp = mysql.TypeString
	f.Charset = "utf8"
	v, err = Eval(s.ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, testutil.DatumEquals, types.NewDatum("1"))

	expr.Expr = ast.NewValueExpr(nil)
	v, err = Eval(s.ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v.Kind(), Equals, types.KindNull)
}
開發者ID:pingcap,項目名稱:tidb,代碼行數:35,代碼來源:evaluator_test.go

示例12: tryToPushDownAgg

// tryToPushDownAgg tries to push down an aggregate function into a join path. If all aggFuncs are first row, we won't
// process it temporarily. If not, We will add additional group by columns and first row functions. We make a new aggregation
// operator.
func (a *aggPushDownSolver) tryToPushDownAgg(aggFuncs []expression.AggregationFunction, gbyCols []*expression.Column, join *Join, childIdx int) LogicalPlan {
	child := join.GetChildByIndex(childIdx).(LogicalPlan)
	if a.allFirstRow(aggFuncs) {
		return child
	}
	agg := a.makeNewAgg(aggFuncs, gbyCols)
	child.SetParents(agg)
	agg.SetChildren(child)
	agg.correlated = agg.correlated || child.IsCorrelated()
	// If agg has no group-by item, it will return a default value, which may cause some bugs.
	// So here we add a group-by item forcely.
	if len(agg.GroupByItems) == 0 {
		agg.GroupByItems = []expression.Expression{&expression.Constant{
			Value:   types.NewDatum(0),
			RetType: types.NewFieldType(mysql.TypeLong)}}
	}
	if (childIdx == 0 && join.JoinType == RightOuterJoin) || (childIdx == 1 && join.JoinType == LeftOuterJoin) {
		var existsDefaultValues bool
		join.DefaultValues, existsDefaultValues = a.getDefaultValues(agg)
		if !existsDefaultValues {
			return child
		}
	}
	return agg
}
開發者ID:pingcap,項目名稱:tidb,代碼行數:28,代碼來源:aggregation_push_down.go

示例13: GetType

// GetType implements AggregationFunction interface.
func (af *avgFunction) GetType() *types.FieldType {
	ft := types.NewFieldType(mysql.TypeNewDecimal)
	ft.Charset = charset.CharsetBin
	ft.Collate = charset.CollationBin
	ft.Decimal = af.Args[0].GetType().Decimal
	return ft
}
開發者ID:pingcap,項目名稱:tidb,代碼行數:8,代碼來源:aggregation.go

示例14: buildExists

func (b *planBuilder) buildExists(p LogicalPlan) LogicalPlan {
out:
	for {
		switch p.(type) {
		// This can be removed when in exists clause,
		// e.g. exists(select count(*) from t order by a) is equal to exists t.
		case *Trim, *Projection, *Sort, *Aggregation:
			p = p.GetChildByIndex(0).(LogicalPlan)
			p.SetParents()
		default:
			break out
		}
	}
	exists := &Exists{baseLogicalPlan: newBaseLogicalPlan(Ext, b.allocator)}
	exists.self = exists
	exists.initID()
	addChild(exists, p)
	newCol := &expression.Column{
		FromID:  exists.id,
		RetType: types.NewFieldType(mysql.TypeTiny),
		ColName: model.NewCIStr("exists_col")}
	exists.SetSchema([]*expression.Column{newCol})
	exists.correlated = p.IsCorrelated()
	return exists
}
開發者ID:jmptrader,項目名稱:tidb,代碼行數:25,代碼來源:logical_plan_builder.go

示例15: buildApply

// buildApply builds apply plan with outerPlan and innerPlan. Everytime we fetch a record from outerPlan and apply it to
// innerPlan. This way is the so-called correlated execution.
func (b *planBuilder) buildApply(outerPlan, innerPlan LogicalPlan, checker *ApplyConditionChecker) LogicalPlan {
	ap := &Apply{
		Checker:         checker,
		baseLogicalPlan: newBaseLogicalPlan(App, b.allocator),
	}
	ap.self = ap
	ap.initIDAndContext(b.ctx)
	addChild(ap, outerPlan)
	addChild(ap, innerPlan)
	corColumns := innerPlan.extractCorrelatedCols()
	ap.correlated = outerPlan.IsCorrelated()
	for _, corCol := range corColumns {
		// If the outer column can't be resolved from this outer schema, it should be resolved by outer schema.
		if idx := outerPlan.GetSchema().GetIndex(&corCol.Column); idx == -1 {
			ap.correlated = true
			break
		}
	}
	innerSchema := innerPlan.GetSchema().Clone()
	if checker == nil {
		for _, col := range innerSchema {
			col.IsAggOrSubq = true
		}
		ap.SetSchema(append(outerPlan.GetSchema().Clone(), innerSchema...))
	} else {
		ap.SetSchema(append(outerPlan.GetSchema().Clone(), &expression.Column{
			FromID:      ap.id,
			ColName:     model.NewCIStr("exists_row"),
			RetType:     types.NewFieldType(mysql.TypeTiny),
			IsAggOrSubq: true,
		}))
	}
	return ap
}
開發者ID:pingcap,項目名稱:tidb,代碼行數:36,代碼來源:logical_plan_builder.go


注:本文中的github.com/pingcap/tidb/util/types.NewFieldType函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。