本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/parser.Datum类的典型用法代码示例。如果您正苦于以下问题:Golang Datum类的具体用法?Golang Datum怎么用?Golang Datum使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Datum类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: convertDatum
// convertDatum returns a Go primitive value equivalent of val, of the
// type expected by col. If val's type is incompatible with col, or if
// col's type is not yet implemented, an error is returned.
func convertDatum(col ColumnDescriptor, val parser.Datum) (interface{}, error) {
if val == parser.DNull {
return nil, nil
}
switch col.Type.Kind {
case ColumnType_BOOL:
if v, ok := val.(parser.DBool); ok {
return bool(v), nil
}
case ColumnType_INT:
if v, ok := val.(parser.DInt); ok {
return int64(v), nil
}
case ColumnType_FLOAT:
if v, ok := val.(parser.DFloat); ok {
return float64(v), nil
}
// case ColumnType_DECIMAL:
// case ColumnType_DATE:
// case ColumnType_TIMESTAMP:
case ColumnType_STRING, ColumnType_BYTES:
if v, ok := val.(parser.DString); ok {
return string(v), nil
}
default:
return nil, fmt.Errorf("unsupported type: %s", val.Type())
}
return nil, fmt.Errorf("value type %s doesn't match type %s of column %q",
val.Type(), col.Type.Kind, col.Name)
}
示例2: encodeInclusiveEndValue
// Encodes datum at the end of key, using direction `dir` for the encoding.
// The key is a span end key, which is exclusive, but `val` needs to
// be inclusive. So if datum is the last end constraint, we transform it accordingly.
func encodeInclusiveEndValue(
key roachpb.Key, datum parser.Datum, dir encoding.Direction,
isLastEndConstraint bool) roachpb.Key {
// Since the end of a span is exclusive, if the last constraint is an
// inclusive one, we might need to make the key exclusive by applying a
// PrefixEnd(). We normally avoid doing this by transforming "a = x" to
// "a = x±1" for the last end constraint, depending on the encoding direction
// (since this keeps the key nice and pretty-printable).
// However, we might not be able to do the ±1.
needExclusiveKey := false
if isLastEndConstraint {
if dir == encoding.Ascending {
if datum.IsMax() {
needExclusiveKey = true
} else {
datum = datum.Next()
}
} else {
if datum.IsMin() || !datum.HasPrev() {
needExclusiveKey = true
} else {
datum = datum.Prev()
}
}
}
key, pErr := encodeTableKey(key, datum, dir)
if pErr != nil {
panic(pErr)
}
if needExclusiveKey {
key = key.PrefixEnd()
}
return key
}
示例3: decodeTableKey
func decodeTableKey(valType parser.Datum, key []byte) (parser.Datum, []byte, error) {
var isNull bool
if key, isNull = encoding.DecodeIfNull(key); isNull {
return parser.DNull, key, nil
}
switch valType.(type) {
case parser.DBool:
rkey, i, err := encoding.DecodeVarint(key)
return parser.DBool(i != 0), rkey, err
case parser.DInt:
rkey, i, err := encoding.DecodeVarint(key)
return parser.DInt(i), rkey, err
case parser.DFloat:
rkey, f, err := encoding.DecodeFloat(key, nil)
return parser.DFloat(f), rkey, err
case parser.DString:
rkey, r, err := encoding.DecodeString(key, nil)
return parser.DString(r), rkey, err
case parser.DBytes:
rkey, r, err := encoding.DecodeString(key, nil)
return parser.DBytes(r), rkey, err
case parser.DDate:
rkey, t, err := encoding.DecodeTime(key)
return parser.DDate{Time: t}, rkey, err
case parser.DTimestamp:
rkey, t, err := encoding.DecodeTime(key)
return parser.DTimestamp{Time: t}, rkey, err
case parser.DInterval:
rkey, d, err := encoding.DecodeVarint(key)
return parser.DInterval{Duration: time.Duration(d)}, rkey, err
default:
return nil, nil, util.Errorf("TODO(pmattis): decoded index key: %s", valType.Type())
}
}
示例4: decodeTableKey
func decodeTableKey(valType parser.Datum, key []byte) (parser.Datum, []byte, error) {
var isNull bool
if key, isNull = encoding.DecodeIfNull(key); isNull {
return parser.DNull, key, nil
}
switch valType.(type) {
case parser.DBool:
var i int64
key, i = encoding.DecodeVarint(key)
return parser.DBool(i != 0), key, nil
case parser.DInt:
var i int64
key, i = encoding.DecodeVarint(key)
return parser.DInt(i), key, nil
case parser.DFloat:
var f float64
key, f = encoding.DecodeFloat(key, nil)
return parser.DFloat(f), key, nil
case parser.DString:
var r string
key, r = encoding.DecodeString(key, nil)
return parser.DString(r), key, nil
default:
return nil, nil, util.Errorf("TODO(pmattis): decoded index key: %s", valType.Type())
}
}
示例5: add
func (a *varianceAggregate) add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
var d parser.DFloat
switch t := datum.(type) {
case parser.DInt:
d = parser.DFloat(t)
case parser.DFloat:
d = t
// case parser.DDecimal:
// TODO(nvanbenschoten) add support for decimal variance and stddev
// aggregation functions. Will require adding decimal.Sqrt() to library.
default:
return util.Errorf("unexpected VARIANCE argument type: %s", datum.Type())
}
// Uses the Knuth/Welford method for accurately computing variance online in a
// single pass. See http://www.johndcook.com/blog/standard_deviation/ and
// https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm.
a.count++
delta := d - a.mean
a.mean += delta / parser.DFloat(a.count)
a.sqrDiff += delta * (d - a.mean)
return nil
}
示例6: Add
func (a *sumAggregate) Add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
if a.sum == nil {
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
}
}
return fmt.Errorf("unexpected SUM argument type: %s", datum.Type())
}
示例7: Less
func (n *valuesNode) Less(i, j int) bool {
// TODO(pmattis): An alternative to this type of field-based comparison would
// be to construct a sort-key per row using encodeTableKey(). Using a
// sort-key approach would likely fit better with a disk-based sort.
ra, rb := n.rows[i], n.rows[j]
for _, c := range n.ordering {
var da, db parser.Datum
if c.direction == encoding.Ascending {
da = ra[c.colIdx]
db = rb[c.colIdx]
} else {
da = rb[c.colIdx]
db = ra[c.colIdx]
}
// TODO(pmattis): This is assuming that the datum types are compatible. I'm
// not sure this always holds as `CASE` expressions can return different
// types for a column for different rows. Investigate how other RDBMs
// handle this.
if c := da.Compare(db); c < 0 {
return true
} else if c > 0 {
return false
}
}
return true
}
示例8: DecodeTableValue
// DecodeTableValue decodes a value encoded by EncodeTableValue.
func DecodeTableValue(a *DatumAlloc, valType parser.Datum, b []byte) (parser.Datum, []byte, error) {
// TODO(dan): Merge this and DecodeTableKey.
if len(b) == 0 {
return nil, nil, util.Errorf("empty slice")
}
if roachpb.ValueType(b[0]) == roachpb.ValueType_NULL {
return parser.DNull, b[1:], nil
}
var err error
switch valType.(type) {
case *parser.DBool:
var i int64
b, i, err = roachpb.DecodeIntValue(b)
// No need to chunk allocate DBool as MakeDBool returns either
// parser.DBoolTrue or parser.DBoolFalse.
return parser.MakeDBool(parser.DBool(i != 0)), b, err
case *parser.DInt:
var i int64
b, i, err = roachpb.DecodeIntValue(b)
return a.NewDInt(parser.DInt(i)), b, err
case *parser.DFloat:
var f float64
b, f, err = roachpb.DecodeFloatValue(b)
return a.NewDFloat(parser.DFloat(f)), b, err
case *parser.DDecimal:
var d *inf.Dec
b, d, err = roachpb.DecodeDecimalValue(b)
dd := a.NewDDecimal(parser.DDecimal{})
dd.Set(d)
return dd, b, err
case *parser.DString:
var data []byte
b, data, err = roachpb.DecodeBytesValue(b)
return a.NewDString(parser.DString(data)), b, err
case *parser.DBytes:
var data []byte
b, data, err = roachpb.DecodeBytesValue(b)
return a.NewDBytes(parser.DBytes(data)), b, err
case *parser.DDate:
var i int64
b, i, err = roachpb.DecodeIntValue(b)
return a.NewDDate(parser.DDate(i)), b, err
case *parser.DTimestamp:
var t time.Time
b, t, err = roachpb.DecodeTimeValue(b)
return a.NewDTimestamp(parser.DTimestamp{Time: t}), b, err
case *parser.DTimestampTZ:
var t time.Time
b, t, err = roachpb.DecodeTimeValue(b)
return a.NewDTimestampTZ(parser.DTimestampTZ{Time: t}), b, err
case *parser.DInterval:
var d duration.Duration
b, d, err = roachpb.DecodeDurationValue(b)
return a.NewDInterval(parser.DInterval{Duration: d}), b, err
default:
return nil, nil, util.Errorf("TODO(pmattis): decoded index value: %s", valType.Type())
}
}
示例9: DecodeTableValue
// DecodeTableValue decodes a value encoded by EncodeTableValue.
func DecodeTableValue(a *DatumAlloc, valType parser.Datum, b []byte) (parser.Datum, []byte, error) {
_, dataOffset, _, typ, err := encoding.DecodeValueTag(b)
if err != nil {
return nil, b, err
}
if typ == encoding.Null {
return parser.DNull, b[dataOffset:], nil
}
switch valType.(type) {
case *parser.DBool:
var x bool
b, x, err = encoding.DecodeBoolValue(b)
// No need to chunk allocate DBool as MakeDBool returns either
// parser.DBoolTrue or parser.DBoolFalse.
return parser.MakeDBool(parser.DBool(x)), b, err
case *parser.DInt:
var i int64
b, i, err = encoding.DecodeIntValue(b)
return a.NewDInt(parser.DInt(i)), b, err
case *parser.DFloat:
var f float64
b, f, err = encoding.DecodeFloatValue(b)
return a.NewDFloat(parser.DFloat(f)), b, err
case *parser.DDecimal:
var d *inf.Dec
b, d, err = encoding.DecodeDecimalValue(b)
dd := a.NewDDecimal(parser.DDecimal{})
dd.Set(d)
return dd, b, err
case *parser.DString:
var data []byte
b, data, err = encoding.DecodeBytesValue(b)
return a.NewDString(parser.DString(data)), b, err
case *parser.DBytes:
var data []byte
b, data, err = encoding.DecodeBytesValue(b)
return a.NewDBytes(parser.DBytes(data)), b, err
case *parser.DDate:
var i int64
b, i, err = encoding.DecodeIntValue(b)
return a.NewDDate(parser.DDate(i)), b, err
case *parser.DTimestamp:
var t time.Time
b, t, err = encoding.DecodeTimeValue(b)
return a.NewDTimestamp(parser.DTimestamp{Time: t}), b, err
case *parser.DTimestampTZ:
var t time.Time
b, t, err = encoding.DecodeTimeValue(b)
return a.NewDTimestampTZ(parser.DTimestampTZ{Time: t}), b, err
case *parser.DInterval:
var d duration.Duration
b, d, err = encoding.DecodeDurationValue(b)
return a.NewDInterval(parser.DInterval{Duration: d}), b, err
default:
return nil, nil, errors.Errorf("TODO(pmattis): decoded index value: %s", valType.Type())
}
}
示例10: CheckColumnType
// CheckColumnType verifies that a given value is compatible
// with the type requested by the column. If the value is a
// placeholder, the type of the placeholder gets populated.
func CheckColumnType(col ColumnDescriptor, val parser.Datum, args parser.MapArgs) error {
if val == parser.DNull {
return nil
}
var ok bool
var err error
var set parser.Datum
switch col.Type.Kind {
case ColumnType_BOOL:
_, ok = val.(*parser.DBool)
set, err = args.SetInferredType(val, parser.TypeBool)
case ColumnType_INT:
_, ok = val.(*parser.DInt)
set, err = args.SetInferredType(val, parser.TypeInt)
case ColumnType_FLOAT:
_, ok = val.(*parser.DFloat)
set, err = args.SetInferredType(val, parser.TypeFloat)
case ColumnType_DECIMAL:
_, ok = val.(*parser.DDecimal)
set, err = args.SetInferredType(val, parser.TypeDecimal)
case ColumnType_STRING:
_, ok = val.(*parser.DString)
set, err = args.SetInferredType(val, parser.TypeString)
case ColumnType_BYTES:
_, ok = val.(*parser.DBytes)
if !ok {
_, ok = val.(*parser.DString)
}
set, err = args.SetInferredType(val, parser.TypeBytes)
case ColumnType_DATE:
_, ok = val.(*parser.DDate)
set, err = args.SetInferredType(val, parser.TypeDate)
case ColumnType_TIMESTAMP:
_, ok = val.(*parser.DTimestamp)
set, err = args.SetInferredType(val, parser.TypeTimestamp)
case ColumnType_TIMESTAMPTZ:
_, ok = val.(*parser.DTimestampTZ)
set, err = args.SetInferredType(val, parser.TypeTimestampTZ)
case ColumnType_INTERVAL:
_, ok = val.(*parser.DInterval)
set, err = args.SetInferredType(val, parser.TypeInterval)
default:
return util.Errorf("unsupported column type: %s", col.Type.Kind)
}
// Check that the value cast has succeeded.
// We ignore the case where it has failed because val was a DArg,
// which is signalled by SetInferredType returning a non-nil assignment.
if !ok && set == nil {
return fmt.Errorf("value type %s doesn't match type %s of column %q",
val.Type(), col.Type.Kind, col.Name)
}
return err
}
示例11: SetDatum
// SetDatum initializes the EncDatum with the given Datum.
func (ed *EncDatum) SetDatum(typ ColumnType_Kind, d parser.Datum) {
if d == nil {
panic("nil datum given")
}
if d != parser.DNull && !typ.ToDatumType().TypeEqual(d) {
panic(fmt.Sprintf("invalid datum type given: %s, expected %s",
d.Type(), typ.ToDatumType().Type()))
}
ed.Type = typ
ed.encoded = nil
ed.Datum = d
}
示例12: SanitizeVarFreeExpr
// SanitizeVarFreeExpr verifies a default expression is valid, has the
// correct type and contains no variable expressions.
func SanitizeVarFreeExpr(expr parser.Expr, expectedType parser.Datum, context string) error {
if parser.ContainsVars(expr) {
return exprContainsVarsError(context, expr)
}
typedExpr, err := parser.TypeCheck(expr, nil, expectedType)
if err != nil {
return err
}
if defaultType := typedExpr.ReturnType(); !expectedType.TypeEqual(defaultType) {
return incompatibleExprTypeError(context, expectedType, defaultType)
}
return nil
}
示例13: SanitizeDefaultExpr
// SanitizeDefaultExpr verifies a default expression is valid and has the
// correct type.
func SanitizeDefaultExpr(expr parser.Expr, colDatumType parser.Datum) error {
typedExpr, err := parser.TypeCheck(expr, nil, colDatumType)
if err != nil {
return err
}
if defaultType := typedExpr.ReturnType(); !colDatumType.TypeEqual(defaultType) {
return incompatibleColumnDefaultTypeError(colDatumType, defaultType)
}
if parser.ContainsVars(typedExpr) {
return defaultContainsPlaceholdersError(typedExpr)
}
return nil
}
示例14: protoFromDatum
func protoFromDatum(datum parser.Datum) driver.Datum {
if datum == parser.DNull {
return driver.Datum{}
}
switch vt := datum.(type) {
case parser.DBool:
return driver.Datum{
Payload: &driver.Datum_BoolVal{BoolVal: bool(vt)},
}
case parser.DInt:
return driver.Datum{
Payload: &driver.Datum_IntVal{IntVal: int64(vt)},
}
case parser.DFloat:
return driver.Datum{
Payload: &driver.Datum_FloatVal{FloatVal: float64(vt)},
}
case *parser.DDecimal:
return driver.Datum{
Payload: &driver.Datum_DecimalVal{DecimalVal: vt.Dec.String()},
}
case parser.DBytes:
return driver.Datum{
Payload: &driver.Datum_BytesVal{BytesVal: []byte(vt)},
}
case parser.DString:
return driver.Datum{
Payload: &driver.Datum_StringVal{StringVal: string(vt)},
}
case parser.DDate:
return driver.Datum{
Payload: &driver.Datum_DateVal{DateVal: int64(vt)},
}
case parser.DTimestamp:
wireTimestamp := driver.Timestamp(vt.Time)
return driver.Datum{
Payload: &driver.Datum_TimeVal{
TimeVal: &wireTimestamp,
},
}
case parser.DInterval:
return driver.Datum{
Payload: &driver.Datum_IntervalVal{IntervalVal: vt.Nanoseconds()},
}
default:
panic(util.Errorf("unsupported result type: %s", datum.Type()))
}
}
示例15: initWhere
func (n *scanNode) initWhere(where *parser.Where) *roachpb.Error {
if where == nil {
return nil
}
n.filter, n.pErr = n.resolveQNames(where.Expr)
if n.pErr == nil {
var whereType parser.Datum
var err error
whereType, err = n.filter.TypeCheck(n.planner.evalCtx.Args)
n.pErr = roachpb.NewError(err)
if n.pErr == nil {
if !(whereType == parser.DummyBool || whereType == parser.DNull) {
n.pErr = roachpb.NewUErrorf("argument of WHERE must be type %s, not type %s", parser.DummyBool.Type(), whereType.Type())
}
}
}
if n.pErr == nil {
// Normalize the expression (this will also evaluate any branches that are
// constant).
var err error
n.filter, err = n.planner.parser.NormalizeExpr(n.planner.evalCtx, n.filter)
n.pErr = roachpb.NewError(err)
}
if n.pErr == nil {
n.filter, n.pErr = n.planner.expandSubqueries(n.filter, 1)
}
return n.pErr
}