本文整理汇总了Golang中github.com/youtube/vitess/go/sqltypes.MakeNumeric函数的典型用法代码示例。如果您正苦于以下问题:Golang MakeNumeric函数的具体用法?Golang MakeNumeric怎么用?Golang MakeNumeric使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MakeNumeric函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSqlQuerySplitQueryInvalidQuery
func TestSqlQuerySplitQueryInvalidQuery(t *testing.T) {
db := setUpSQLQueryTest()
db.AddQuery("SELECT MIN(pk), MAX(pk) FROM test_table", &mproto.QueryResult{
Fields: []mproto.Field{
mproto.Field{Name: "pk", Type: mproto.VT_LONG},
},
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{
sqltypes.MakeNumeric([]byte("1")),
sqltypes.MakeNumeric([]byte("100")),
},
},
})
db.AddQuery("SELECT pk FROM test_table LIMIT 0", &mproto.QueryResult{
Fields: []mproto.Field{
mproto.Field{Name: "pk", Type: mproto.VT_LONG},
},
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{
sqltypes.MakeNumeric([]byte("1")),
},
},
})
testUtils := newTestUtils()
config := testUtils.newQueryServiceConfig()
sqlQuery := NewSqlQuery(config)
dbconfigs := testUtils.newDBConfigs()
err := sqlQuery.allowQueries(nil, &dbconfigs, []SchemaOverride{}, testUtils.newMysqld(&dbconfigs))
if err != nil {
t.Fatalf("allowQueries failed: %v", err)
}
defer sqlQuery.disallowQueries()
ctx := context.Background()
query := proto.SplitQueryRequest{
Query: proto.BoundQuery{
// add limit clause to make SplitQuery fail
Sql: "select * from test_table where count > :count limit 1000",
BindVariables: nil,
},
SplitCount: 10,
SessionID: sqlQuery.sessionID,
}
reply := proto.SplitQueryResult{
Queries: []proto.QuerySplit{
proto.QuerySplit{
Query: proto.BoundQuery{
Sql: "",
BindVariables: nil,
},
RowCount: 10,
},
},
}
if err := sqlQuery.SplitQuery(ctx, nil, &query, &reply); err == nil {
t.Fatalf("SqlQuery.SplitQuery should fail")
}
}
示例2: TestTabletServerSplitQuery
func TestTabletServerSplitQuery(t *testing.T) {
db := setUpTabletServerTest()
db.AddQuery("SELECT MIN(pk), MAX(pk) FROM test_table", &mproto.QueryResult{
Fields: []mproto.Field{
mproto.Field{Name: "pk", Type: mproto.VT_LONG},
},
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{
sqltypes.MakeNumeric([]byte("1")),
sqltypes.MakeNumeric([]byte("100")),
},
},
})
db.AddQuery("SELECT pk FROM test_table LIMIT 0", &mproto.QueryResult{
Fields: []mproto.Field{
mproto.Field{Name: "pk", Type: mproto.VT_LONG},
},
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{
sqltypes.MakeNumeric([]byte("1")),
},
},
})
testUtils := newTestUtils()
config := testUtils.newQueryServiceConfig()
tsv := NewTabletServer(config)
dbconfigs := testUtils.newDBConfigs(db)
err := tsv.StartService(nil, &dbconfigs, []SchemaOverride{}, testUtils.newMysqld(&dbconfigs))
if err != nil {
t.Fatalf("StartService failed: %v", err)
}
defer tsv.StopService()
ctx := context.Background()
query := proto.SplitQueryRequest{
Query: proto.BoundQuery{
Sql: "select * from test_table where count > :count",
BindVariables: nil,
},
SplitCount: 10,
SessionID: tsv.sessionID,
}
reply := proto.SplitQueryResult{
Queries: []proto.QuerySplit{
proto.QuerySplit{
Query: proto.BoundQuery{
Sql: "",
BindVariables: nil,
},
RowCount: 10,
},
},
}
if err := tsv.SplitQuery(ctx, nil, &query, &reply); err != nil {
t.Fatalf("TabletServer.SplitQuery should success: %v, but get error: %v",
query, err)
}
}
示例3: splitBoundariesUintColumn
func (qs *QuerySplitter) splitBoundariesUintColumn(pkMinMax *sqltypes.Result) ([]sqltypes.Value, error) {
boundaries := []sqltypes.Value{}
if pkMinMax == nil || len(pkMinMax.Rows) != 1 || pkMinMax.Rows[0][0].IsNull() || pkMinMax.Rows[0][1].IsNull() {
return boundaries, nil
}
minNumeric := sqltypes.MakeNumeric(pkMinMax.Rows[0][0].Raw())
maxNumeric := sqltypes.MakeNumeric(pkMinMax.Rows[0][1].Raw())
min, err := minNumeric.ParseUint64()
if err != nil {
return nil, err
}
max, err := maxNumeric.ParseUint64()
if err != nil {
return nil, err
}
interval := (max - min) / uint64(qs.splitCount)
if interval == 0 {
return nil, err
}
qs.rowCount = int64(interval)
for i := uint64(1); i < uint64(qs.splitCount); i++ {
v, err := sqltypes.BuildValue(min + interval*i)
if err != nil {
return nil, err
}
boundaries = append(boundaries, v)
}
return boundaries, nil
}
示例4: createTestTableBaseShowTable
func createTestTableBaseShowTable(tableName string) []sqltypes.Value {
return []sqltypes.Value{
sqltypes.MakeString([]byte(tableName)),
sqltypes.MakeString([]byte("USER TABLE")),
sqltypes.MakeNumeric([]byte("1427325875")),
sqltypes.MakeString([]byte("")),
sqltypes.MakeNumeric([]byte("1")),
sqltypes.MakeNumeric([]byte("2")),
sqltypes.MakeNumeric([]byte("3")),
sqltypes.MakeNumeric([]byte("4")),
}
}
示例5: createTestTableUpdatedStats
func createTestTableUpdatedStats(tableName string) []sqltypes.Value {
return []sqltypes.Value{
sqltypes.MakeString([]byte(tableName)),
sqltypes.MakeString([]byte("USER TABLE")),
sqltypes.MakeNumeric([]byte("0")),
sqltypes.MakeString([]byte("")),
sqltypes.MakeNumeric([]byte("4")),
sqltypes.MakeNumeric([]byte("5")),
sqltypes.MakeNumeric([]byte("6")),
sqltypes.MakeNumeric([]byte("7")),
}
}
示例6: parseInt
func (qs *QuerySplitter) parseInt(pkMinMax *mproto.QueryResult) ([]sqltypes.Value, error) {
boundaries := []sqltypes.Value{}
minNumeric := sqltypes.MakeNumeric(pkMinMax.Rows[0][0].Raw())
maxNumeric := sqltypes.MakeNumeric(pkMinMax.Rows[0][1].Raw())
if pkMinMax.Rows[0][0].Raw()[0] == '-' {
// signed values, use int64
min, err := minNumeric.ParseInt64()
if err != nil {
return nil, err
}
max, err := maxNumeric.ParseInt64()
if err != nil {
return nil, err
}
interval := (max - min) / int64(qs.splitCount)
if interval == 0 {
return nil, err
}
qs.rowCount = interval
for i := int64(1); i < int64(qs.splitCount); i++ {
v, err := sqltypes.BuildValue(min + interval*i)
if err != nil {
return nil, err
}
boundaries = append(boundaries, v)
}
return boundaries, nil
}
// unsigned values, use uint64
min, err := minNumeric.ParseUint64()
if err != nil {
return nil, err
}
max, err := maxNumeric.ParseUint64()
if err != nil {
return nil, err
}
interval := (max - min) / uint64(qs.splitCount)
if interval == 0 {
return nil, err
}
qs.rowCount = int64(interval)
for i := uint64(1); i < uint64(qs.splitCount); i++ {
v, err := sqltypes.BuildValue(min + interval*i)
if err != nil {
return nil, err
}
boundaries = append(boundaries, v)
}
return boundaries, nil
}
示例7: TestCodexBuildKey
func TestCodexBuildKey(t *testing.T) {
testUtils := newTestUtils()
newKey := buildKey([]sqltypes.Value{
sqltypes.MakeNumeric([]byte("1")),
sqltypes.MakeNumeric([]byte("2")),
})
testUtils.checkEqual(t, "1.2", newKey)
newKey = buildKey([]sqltypes.Value{
sqltypes.MakeString([]byte("a")),
sqltypes.NULL,
})
testUtils.checkEqual(t, "", newKey)
}
示例8: TestQueryExecutorPlanInsertSubQuery
func TestQueryExecutorPlanInsertSubQuery(t *testing.T) {
db := setUpQueryExecutorTest()
query := "insert into test_table(pk) select pk from test_table where pk = 1 limit 1000"
want := &mproto.QueryResult{
Rows: [][]sqltypes.Value{},
}
db.AddQuery(query, want)
selectQuery := "select pk from test_table where pk = 1 limit 1000"
db.AddQuery(selectQuery, &mproto.QueryResult{
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{sqltypes.MakeNumeric([]byte("2"))},
},
})
insertQuery := "insert into test_table(pk) values (2) /* _stream test_table (pk ) (2 ); */"
db.AddQuery(insertQuery, &mproto.QueryResult{})
ctx := context.Background()
sqlQuery := newTestSQLQuery(ctx, enableRowCache|enableStrict)
qre := newTestQueryExecutor(ctx, sqlQuery, query, newTransaction(sqlQuery))
defer sqlQuery.disallowQueries()
defer testCommitHelper(t, sqlQuery, qre)
checkPlanID(t, planbuilder.PLAN_INSERT_SUBQUERY, qre.plan.PlanId)
got, err := qre.Execute()
if err != nil {
t.Fatalf("qre.Execute() = %v, want nil", err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got: %v, want: %v", got, want)
}
}
示例9: TestQueryExecutorPlanInsertSubQueryAutoCommmit
func TestQueryExecutorPlanInsertSubQueryAutoCommmit(t *testing.T) {
db := setUpQueryExecutorTest()
query := "insert into test_table(pk) select pk from test_table where pk = 1 limit 1000"
want := &mproto.QueryResult{
Rows: [][]sqltypes.Value{},
}
db.AddQuery(query, want)
selectQuery := "select pk from test_table where pk = 1 limit 1000"
db.AddQuery(selectQuery, &mproto.QueryResult{
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{sqltypes.MakeNumeric([]byte("2"))},
},
})
insertQuery := "insert into test_table(pk) values (2) /* _stream test_table (pk ) (2 ); */"
db.AddQuery(insertQuery, &mproto.QueryResult{})
ctx := context.Background()
tsv := newTestTabletServer(ctx, enableRowCache|enableStrict, db)
qre := newTestQueryExecutor(ctx, tsv, query, 0)
defer tsv.StopService()
checkPlanID(t, planbuilder.PlanInsertSubquery, qre.plan.PlanID)
got, err := qre.Execute()
if err != nil {
t.Fatalf("qre.Execute() = %v, want nil", err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got: %v, want: %v", got, want)
}
}
示例10: makeValueString
// makeValueString returns a string that contains all the passed-in rows
// as an insert SQL command's parameters.
func makeValueString(fields []*query.Field, rows [][]sqltypes.Value) string {
buf := bytes.Buffer{}
for i, row := range rows {
if i > 0 {
buf.Write([]byte(",("))
} else {
buf.WriteByte('(')
}
for j, value := range row {
if j > 0 {
buf.WriteByte(',')
}
// convert value back to its original type
if !value.IsNull() {
switch {
case sqltypes.IsIntegral(fields[j].Type):
value = sqltypes.MakeNumeric(value.Raw())
case sqltypes.IsFloat(fields[j].Type):
value = sqltypes.MakeFractional(value.Raw())
}
}
value.EncodeSQL(&buf)
}
buf.WriteByte(')')
}
return buf.String()
}
示例11: TestQueryExecutorPlanInsertSubQuery
func TestQueryExecutorPlanInsertSubQuery(t *testing.T) {
db := setUpQueryExecutorTest()
testUtils := &testUtils{}
fields := []mproto.Field{
mproto.Field{Name: "pk", Type: mproto.VT_LONG},
}
query := "insert into test_table(pk) select pk from test_table where pk = 1 limit 1000"
expected := &mproto.QueryResult{
Fields: fields,
Rows: [][]sqltypes.Value{},
}
db.AddQuery(query, expected)
selectQuery := "select pk from test_table where pk = 1 limit 1000"
db.AddQuery(selectQuery, &mproto.QueryResult{
Fields: fields,
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{sqltypes.MakeNumeric([]byte("2"))},
},
})
insertQuery := "insert into test_table(pk) values (2) /* _stream test_table (pk ) (2 ); */"
db.AddQuery(insertQuery, &mproto.QueryResult{
Fields: fields,
})
qre, sqlQuery := newTestQueryExecutor(
query, context.Background(), enableRowCache|enableTx|enableStrict)
defer sqlQuery.disallowQueries()
defer testCommitHelper(t, sqlQuery, qre)
checkPlanID(t, planbuilder.PLAN_INSERT_SUBQUERY, qre.plan.PlanId)
testUtils.checkEqual(t, expected, qre.Execute())
}
示例12: Execute
func (vc *vcursor) Execute(query *tproto.BoundQuery) (*sqltypes.Result, error) {
vc.query = query
if vc.mustFail {
return nil, errors.New("execute failed")
}
switch {
case strings.HasPrefix(query.Sql, "select"):
if vc.result != nil {
return vc.result, nil
}
result := &sqltypes.Result{
Fields: []*querypb.Field{{
Type: sqltypes.Int32,
}},
RowsAffected: uint64(vc.numRows),
}
for i := 0; i < vc.numRows; i++ {
result.Rows = append(result.Rows, []sqltypes.Value{
sqltypes.MakeNumeric([]byte(fmt.Sprintf("%d", i+1))),
})
}
return result, nil
case strings.HasPrefix(query.Sql, "insert"):
return &sqltypes.Result{InsertID: 1}, nil
case strings.HasPrefix(query.Sql, "delete"):
return &sqltypes.Result{}, nil
}
panic("unexpected")
}
示例13: TestCodexResolveListArg
func TestCodexResolveListArg(t *testing.T) {
testUtils := newTestUtils()
tableInfo := createTableInfo("Table",
[]string{"pk1", "pk2", "col1"},
[]string{"int", "varbinary(128)", "int"},
[]string{"pk1", "pk2"})
key := "var"
bindVariables := make(map[string]interface{})
bindVariables[key] = []interface{}{fmt.Errorf("error is not supported")}
_, err := resolveListArg(tableInfo.GetPKColumn(0), "::"+key, bindVariables)
testUtils.checkTabletError(t, err, ErrFail, "")
bindVariables[key] = []interface{}{"1"}
_, err = resolveListArg(tableInfo.GetPKColumn(0), "::"+key, bindVariables)
testUtils.checkTabletError(t, err, ErrFail, "type mismatch")
bindVariables[key] = []interface{}{10}
result, err := resolveListArg(tableInfo.GetPKColumn(0), "::"+key, bindVariables)
if err != nil {
t.Fatalf("should not get an error, but got error: %v", err)
}
testUtils.checkEqual(t, []sqltypes.Value{sqltypes.MakeNumeric([]byte("10"))}, result)
}
示例14: makeValueString
// makeValueString returns a string that contains all the passed-in rows
// as an insert SQL command's parameters.
func makeValueString(fields []mproto.Field, rows [][]sqltypes.Value) string {
buf := bytes.Buffer{}
for i, row := range rows {
if i > 0 {
buf.Write([]byte(",("))
} else {
buf.WriteByte('(')
}
for j, value := range row {
if j > 0 {
buf.WriteByte(',')
}
// convert value back to its original type
if !value.IsNull() {
switch fields[j].Type {
case mproto.VT_TINY, mproto.VT_SHORT, mproto.VT_LONG, mproto.VT_LONGLONG, mproto.VT_INT24:
value = sqltypes.MakeNumeric(value.Raw())
case mproto.VT_FLOAT, mproto.VT_DOUBLE:
value = sqltypes.MakeFractional(value.Raw())
}
}
value.EncodeSql(&buf)
}
buf.WriteByte(')')
}
return buf.String()
}
示例15: Split
// Split will split the rows into subset for each distribution
func (rs *RowSplitter) Split(result [][][]sqltypes.Value, rows [][]sqltypes.Value) error {
if rs.Type == key.KIT_UINT64 {
for _, row := range rows {
v := sqltypes.MakeNumeric(row[rs.ValueIndex].Raw())
i, err := v.ParseUint64()
if err != nil {
return fmt.Errorf("Non numerical value: %v", err)
}
k := key.Uint64Key(i).KeyspaceId()
for i, kr := range rs.KeyRanges {
if kr.Contains(k) {
result[i] = append(result[i], row)
break
}
}
}
} else {
for _, row := range rows {
k := key.KeyspaceId(row[rs.ValueIndex].Raw())
for i, kr := range rs.KeyRanges {
if kr.Contains(k) {
result[i] = append(result[i], row)
break
}
}
}
}
return nil
}