MCPcopy Create free account
hub / github.com/dolthub/doltgresql / EncodeNonsortingDecimal

Function EncodeNonsortingDecimal

postgres/parser/encoding/decimal.go:543–608  ·  view source on GitHub ↗

EncodeNonsortingDecimal returns the resulting byte slice with the encoded decimal appended to b. The encoding is limited compared to standard encodings in this package in that - It will not sort lexicographically - It does not encode its length or terminate itself, so decoding functions must be prov

(b []byte, d *apd.Decimal)

Source from the content-addressed store, hash-verified

541// decimalInfinity -> decimalInfinity
542// decimalNaNDesc -> decimalNaNDesc
543func EncodeNonsortingDecimal(b []byte, d *apd.Decimal) []byte {
544 neg := d.Negative
545 switch d.Form {
546 case apd.Finite:
547 // ignore
548 case apd.Infinite:
549 if neg {
550 return append(b, decimalNegativeInfinity)
551 }
552 return append(b, decimalInfinity)
553 case apd.NaN:
554 return append(b, decimalNaN)
555 default:
556 panic(errors.Errorf("unknown form: %s", d.Form))
557 }
558
559 // We only encode "0" as decimalZero. All others ("0.0", "-0", etc) are
560 // encoded like normal values.
561 if d.IsZero() && !neg && d.Exponent == 0 {
562 return append(b, decimalZero)
563 }
564
565 // Determine the exponent of the decimal, with the
566 // exponent defined as .xyz * 10^exp.
567 nDigits := int(d.NumDigits())
568 e := nDigits + int(d.Exponent)
569
570 bNat := d.Coeff.Bits()
571
572 var buf []byte
573 if n := UpperBoundNonsortingDecimalSize(d); n <= cap(b)-len(b) {
574 // We append the marker directly to the input buffer b below, so
575 // we are off by 1 for each of these, which explains the adjustments.
576 buf = b[len(b)+1 : len(b)+1]
577 } else {
578 buf = make([]byte, 0, n-1)
579 }
580
581 switch {
582 case neg && e > 0:
583 b = append(b, decimalNegLarge)
584 buf = encodeNonsortingDecimalValue(uint64(e), bNat, buf)
585 return append(b, buf...)
586 case neg && e == 0:
587 b = append(b, decimalNegMedium)
588 buf = encodeNonsortingDecimalValueWithoutExp(bNat, buf)
589 return append(b, buf...)
590 case neg && e < 0:
591 b = append(b, decimalNegSmall)
592 buf = encodeNonsortingDecimalValue(uint64(-e), bNat, buf)
593 return append(b, buf...)
594 case !neg && e < 0:
595 b = append(b, decimalPosSmall)
596 buf = encodeNonsortingDecimalValue(uint64(-e), bNat, buf)
597 return append(b, buf...)
598 case !neg && e == 0:
599 b = append(b, decimalPosMedium)
600 buf = encodeNonsortingDecimalValueWithoutExp(bNat, buf)

Callers 1

Calls 4

ErrorfMethod · 0.65

Tested by

no test coverage detected