本文整理匯總了Golang中github.com/cockroachdb/cockroach/sql/parser.DDecimal類的典型用法代碼示例。如果您正苦於以下問題:Golang DDecimal類的具體用法?Golang DDecimal怎麽用?Golang DDecimal使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了DDecimal類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: datumFromProto
func datumFromProto(d driver.Datum) parser.Datum {
arg := d.Payload
if arg == nil {
return parser.DNull
}
switch t := arg.(type) {
case *driver.Datum_BoolVal:
return parser.DBool(t.BoolVal)
case *driver.Datum_IntVal:
return parser.DInt(t.IntVal)
case *driver.Datum_FloatVal:
return parser.DFloat(t.FloatVal)
case *driver.Datum_DecimalVal:
dd := parser.DDecimal{}
if _, ok := dd.SetString(t.DecimalVal); !ok {
panic(fmt.Sprintf("could not parse string %q as decimal", t.DecimalVal))
}
return dd
case *driver.Datum_BytesVal:
return parser.DBytes(t.BytesVal)
case *driver.Datum_StringVal:
return parser.DString(t.StringVal)
case *driver.Datum_DateVal:
return parser.DDate(t.DateVal)
case *driver.Datum_TimeVal:
return parser.DTimestamp{Time: t.TimeVal.GoTime()}
case *driver.Datum_IntervalVal:
return parser.DInterval{Duration: time.Duration(t.IntervalVal)}
default:
panic(fmt.Sprintf("unexpected type %T", t))
}
}
示例2: BenchmarkDecodeBinaryDecimal
func BenchmarkDecodeBinaryDecimal(b *testing.B) {
wbuf := writeBuffer{bytecount: metric.NewCounter()}
expected := new(parser.DDecimal)
expected.SetString("-1728718718271827121233.1212121212")
wbuf.writeBinaryDatum(expected)
rbuf := readBuffer{msg: wbuf.wrapped.Bytes()}
plen, err := rbuf.getUint32()
if err != nil {
b.Fatal(err)
}
bytes, err := rbuf.getBytes(int(plen))
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
got, err := decodeOidDatum(oid.T_numeric, formatBinary, bytes)
b.StopTimer()
if err != nil {
b.Fatal(err)
} else if got.Compare(expected) != 0 {
b.Fatalf("expected %s, got %s", expected, got)
}
}
}
示例3: result
func (a *decimalVarianceAggregate) result() (parser.Datum, error) {
if a.count.Cmp(decimalTwo) < 0 {
return parser.DNull, nil
}
a.tmp.Sub(&a.count, decimalOne)
dd := parser.DDecimal{}
dd.QuoRound(&a.sqrDiff, &a.tmp, decimal.Precision, inf.RoundHalfUp)
return dd, nil
}
示例4: Arg
// Arg implements the parser.Args interface.
// TODO: This does not support arguments of the SQL 'Date' type, as there is not
// an equivalent type in Go's standard library. It's not currently needed by any
// of our internal tables.
func (gp golangParameters) Arg(name string) (parser.Datum, bool) {
i, err := processPositionalArgument(name)
if err != nil {
return nil, false
}
if i < 1 || int(i) > len(gp) {
return nil, false
}
arg := gp[i-1]
if arg == nil {
return parser.DNull, true
}
// A type switch to handle a few explicit types with special semantics.
switch t := arg.(type) {
// Datums are passed along as is.
case parser.Datum:
return t, true
// Time datatypes get special representation in the database.
case time.Time:
return parser.DTimestamp{Time: t}, true
case time.Duration:
return parser.DInterval{Duration: t}, true
case *inf.Dec:
dd := parser.DDecimal{}
dd.Set(t)
return dd, true
}
// Handle all types which have an underlying type that can be stored in the
// database.
// Note: if this reflection becomes a performance concern in the future,
// commonly used types could be added explicitly into the type switch above
// for a performance gain.
val := reflect.ValueOf(arg)
switch val.Kind() {
case reflect.Bool:
return parser.DBool(val.Bool()), true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return parser.DInt(val.Int()), true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return parser.DInt(val.Uint()), true
case reflect.Float32, reflect.Float64:
return parser.DFloat(val.Float()), true
case reflect.String:
return parser.DString(val.String()), true
case reflect.Slice:
// Handle byte slices.
if val.Type().Elem().Kind() == reflect.Uint8 {
return parser.DBytes(val.Bytes()), true
}
}
panic(fmt.Sprintf("unexpected type %T", arg))
}
示例5: BenchmarkWriteBinaryDecimal
func BenchmarkWriteBinaryDecimal(b *testing.B) {
buf := writeBuffer{bytecount: metric.NewCounter()}
dec := new(parser.DDecimal)
dec.SetString("-1728718718271827121233.1212121212")
// Warm up the buffer.
buf.writeBinaryDatum(dec)
buf.wrapped.Reset()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
buf.writeBinaryDatum(dec)
b.StopTimer()
buf.wrapped.Reset()
}
}
示例6: add
func (a *sumAggregate) add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
if a.sum == nil {
switch t := datum.(type) {
case parser.DDecimal:
// Make copy of decimal to allow for modification later.
dd := parser.DDecimal{}
dd.Set(&t.Dec)
datum = dd
}
a.sum = datum
return nil
}
switch t := datum.(type) {
case parser.DInt:
if v, ok := a.sum.(parser.DInt); ok {
a.sum = v + t
return nil
}
case parser.DFloat:
if v, ok := a.sum.(parser.DFloat); ok {
a.sum = v + t
return nil
}
case parser.DDecimal:
if v, ok := a.sum.(parser.DDecimal); ok {
v.Add(&v.Dec, &t.Dec)
a.sum = v
return nil
}
}
return util.Errorf("unexpected SUM argument type: %s", datum.Type())
}
示例7: decodeOidDatum
//.........這裏部分代碼省略.........
d = parser.DInt(i)
default:
return d, fmt.Errorf("unsupported int2 format code: %d", code)
}
case oid.T_int4:
switch code {
case formatText:
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return d, err
}
d = parser.DInt(i)
case formatBinary:
var i int32
err := binary.Read(bytes.NewReader(b), binary.BigEndian, &i)
if err != nil {
return d, err
}
d = parser.DInt(i)
default:
return d, fmt.Errorf("unsupported int4 format code: %d", code)
}
case oid.T_int8:
switch code {
case formatText:
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return d, err
}
d = parser.DInt(i)
case formatBinary:
var i int64
err := binary.Read(bytes.NewReader(b), binary.BigEndian, &i)
if err != nil {
return d, err
}
d = parser.DInt(i)
default:
return d, fmt.Errorf("unsupported int8 format code: %d", code)
}
case oid.T_float4:
switch code {
case formatText:
f, err := strconv.ParseFloat(string(b), 64)
if err != nil {
return d, err
}
d = parser.DFloat(f)
case formatBinary:
var f float32
err := binary.Read(bytes.NewReader(b), binary.BigEndian, &f)
if err != nil {
return d, err
}
d = parser.DFloat(f)
default:
return d, fmt.Errorf("unsupported float4 format code: %d", code)
}
case oid.T_float8:
switch code {
case formatText:
f, err := strconv.ParseFloat(string(b), 64)
if err != nil {
return d, err
}
d = parser.DFloat(f)
case formatBinary:
var f float64
err := binary.Read(bytes.NewReader(b), binary.BigEndian, &f)
if err != nil {
return d, err
}
d = parser.DFloat(f)
default:
return d, fmt.Errorf("unsupported float8 format code: %d", code)
}
case oid.T_numeric:
switch code {
case formatText:
dd := parser.DDecimal{}
if _, ok := dd.SetString(string(b)); !ok {
return nil, fmt.Errorf("could not parse string %q as decimal", b)
}
d = dd
default:
return d, fmt.Errorf("unsupported numeric format code: %d", code)
}
case oid.T_text:
switch code {
case formatText:
d = parser.DString(b)
default:
return d, fmt.Errorf("unsupported text format code: %d", code)
}
// TODO(mjibson): implement date/time types
default:
return d, fmt.Errorf("unsupported OID: %v", id)
}
return d, nil
}
示例8: unmarshalColumnValue
// unmarshalColumnValue decodes the value from a key-value pair using the type
// expected by the column. An error is returned if the value's type does not
// match the column's type.
func unmarshalColumnValue(kind ColumnType_Kind, value *roachpb.Value) (parser.Datum, *roachpb.Error) {
if value == nil {
return parser.DNull, nil
}
switch kind {
case ColumnType_BOOL:
v, err := value.GetInt()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DBool(v != 0), nil
case ColumnType_INT:
v, err := value.GetInt()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DInt(v), nil
case ColumnType_FLOAT:
v, err := value.GetFloat()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DFloat(v), nil
case ColumnType_DECIMAL:
v, err := value.GetDecimal()
if err != nil {
return nil, roachpb.NewError(err)
}
dd := parser.DDecimal{}
dd.Set(v)
return dd, nil
case ColumnType_STRING:
v, err := value.GetBytes()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DString(v), nil
case ColumnType_BYTES:
v, err := value.GetBytes()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DBytes(v), nil
case ColumnType_DATE:
v, err := value.GetInt()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DDate(v), nil
case ColumnType_TIMESTAMP:
v, err := value.GetTime()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DTimestamp{Time: v}, nil
case ColumnType_INTERVAL:
v, err := value.GetInt()
if err != nil {
return nil, roachpb.NewError(err)
}
return parser.DInterval{Duration: time.Duration(v)}, nil
default:
return nil, roachpb.NewErrorf("unsupported column type: %s", kind)
}
}
示例9: decodeTableKey
func decodeTableKey(valType parser.Datum, key []byte, dir encoding.Direction) (
parser.Datum, []byte, error) {
if (dir != encoding.Ascending) && (dir != encoding.Descending) {
return nil, nil, util.Errorf("invalid direction: %d", dir)
}
var isNull bool
if key, isNull = encoding.DecodeIfNull(key); isNull {
return parser.DNull, key, nil
}
var rkey []byte
var err error
switch valType.(type) {
case parser.DBool:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
return parser.DBool(i != 0), rkey, err
case parser.DInt:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
return parser.DInt(i), rkey, err
case parser.DFloat:
var f float64
if dir == encoding.Ascending {
rkey, f, err = encoding.DecodeFloatAscending(key, nil)
} else {
rkey, f, err = encoding.DecodeFloatDescending(key, nil)
}
return parser.DFloat(f), rkey, err
case parser.DDecimal:
var d *inf.Dec
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeDecimalAscending(key, nil)
} else {
rkey, d, err = encoding.DecodeDecimalDescending(key, nil)
}
dd := parser.DDecimal{}
dd.Set(d)
return dd, rkey, err
case parser.DString:
var r string
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeStringAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeStringDescending(key, nil)
}
return parser.DString(r), rkey, err
case parser.DBytes:
var r []byte
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
return parser.DBytes(r), rkey, err
case parser.DDate:
var t int64
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeVarintAscending(key)
} else {
rkey, t, err = encoding.DecodeVarintDescending(key)
}
return parser.DDate(t), rkey, err
case parser.DTimestamp:
var t time.Time
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeDescending(key)
}
return parser.DTimestamp{Time: t}, rkey, err
case parser.DInterval:
var d int64
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeVarintAscending(key)
} else {
rkey, d, err = encoding.DecodeVarintDescending(key)
}
return parser.DInterval{Duration: time.Duration(d)}, rkey, err
default:
return nil, nil, util.Errorf("TODO(pmattis): decoded index key: %s", valType.Type())
}
}