MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / EncodeNonsortingDecimal

Function EncodeNonsortingDecimal

pkg/util/encoding/decimal.go:529–594  ·  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

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

Callers 1

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…