minato_codec_scalar (minato v0.18.6)

View Source

Codecs for the scalar types that are not temporal and not numeric: bool, bytea, int2, int4, int8, float4, float8, text, varchar, json, jsonb and uuid.

Internal to minato. The supported surface is minato_codec.

Wire formats

  • bool is one byte, 0 or 1, in binary, and t or f in text.
  • bytea is the raw bytes in binary. In text it is the hex output format, a \x prefix followed by hex digits; the older escape format is accepted when decoding but never produced.
  • The integers are big-endian two's complement of 2, 4 and 8 bytes.
  • float4 and float8 are big-endian IEEE 754.
  • text, varchar and json are the raw encoded bytes in both formats, which for a connection using client_encoding = UTF8 means UTF-8. minato does not validate the encoding; the server rejects what it will not accept.
  • jsonb in binary is a single version byte, currently 1, followed by the JSON text. In text it is the JSON text alone.
  • uuid is the 16 raw bytes in binary and the 36 character hyphenated form in text.

Erlang representation

Strings, json and bytea are binaries when decoded and accept any iodata() when encoded.

jsonb is not parsed. It decodes to the JSON document as a binary, with the version byte removed, and encodes from JSON iodata(). Callers that want terms run json:decode/1 and json:encode/1 themselves, which keeps the choice of JSON representation with the caller and keeps the codec free of a JSON parser.

float4 and float8 decode to a float, or to nan, infinity or neg_infinity, because the BEAM has no float value for those three. The same three atoms are accepted when encoding. nan encodes to the quiet NaN, which is what PostgreSQL itself emits and what IEEE 754 recommends; a signalling NaN can trap on hardware that honours the distinction.

A float4 decoded from text is narrowed to float4 precision, so that the same value read in either wire format gives the same term. PostgreSQL prints a float4 with only the digits a float4 needs, and reading those digits as a double gives a number no float4 holds.

In the text format PostgreSQL prints a float in its shortest round-tripping form, which for many values carries no decimal point at all: 0, 1e+30, 1e-30. binary_to_float/1 rejects every one of those, so the text decoder supplies the missing point before parsing.

uuid decoding is governed by the uuid_format option: string, the default, gives the 36 character hyphenated lowercase binary, and binary gives the 16 raw bytes. Encoding always accepts all three input forms: 16 raw bytes, 32 hex characters, or the 36 character hyphenated form, in either case.

Summary

Functions

Decode Format wire bytes to a scalar value.

Encode a scalar value to Format wire bytes.

Functions

decode/4

Decode Format wire bytes to a scalar value.

encode/4

Encode a scalar value to Format wire bytes.