rows

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package rows compiles a per-shape encoder plan from a RowDescription and provides the hot row loop that writes JSON.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendArray

func AppendArray(dst, body []byte, plan *Plan) ([]byte, error)

AppendArray writes the row as a JSON array (used by columnar mode).

func AppendObject

func AppendObject(dst, body []byte, plan *Plan) ([]byte, error)

DataRow body layout:

int16  column count
for each column:
  int32  length (-1 for NULL)
  bytes  value (length bytes; absent if length == -1)

AppendObject decodes one DataRow body and appends a JSON object using the supplied plan. Returns the new dst.

The column read loop is hand-inlined: a helper would add one function frame per cell and force the slice header through memory on every call. The hot loop runs per DataRow and this is the single biggest cell-level win we can bank without SIMD. The only shared helper is the bounds guard; keep the decoding arithmetic here.

A plan-bytecode variant (inline switch on a per-column opcode) was prototyped and measured flat-to-slightly-negative on both the mixed micro-bench (49.3 ns/op vs 46.2 ns/op) and the live 100k-row shapes (-1 to -2% on covered shapes, within run-to-run noise). With a warm branch predictor the func-pointer Encoder is already close to free; the switch overhead did not pay back. Reverted. See git history if you want to re-explore with SIMD or per-shape codegen.

func AppendTOONRow

func AppendTOONRow(dst, body []byte, plan *Plan) ([]byte, error)

AppendTOONRow decodes one DataRow and emits a TOON row:

val1,val2,...,valN\n

Values reuse the existing per-column Encoder because its JSON form already matches TOON's rules for the scalars we care about:

  • numbers (int/float/numeric): bare decimal digits
  • bool: literal true / false
  • null: literal null
  • strings, date/time, uuid: "..." with JSON escapes
  • json / jsonb: emitted as raw JSON (object, array, string, number, bool, null). The TOON parser is expected to bracket-balance `{}` / `[]` and string-aware scan so an inner comma does not split a cell. This preserves the jsonb passthrough win.

Row terminator is '\n'. There is no trailing comma.

func PickResultFormats

func PickResultFormats(cols []Column) []int16

PickResultFormats returns one int16 per column describing the format we want the server to send (1=binary, 0=text), based on which OIDs have specialised binary encoders. Equivalent to PickResultFormatsEx(cols, false).

func PickResultFormatsEx

func PickResultFormatsEx(cols []Column, binaryNumeric bool) []int16

PickResultFormatsEx is PickResultFormats with the BinaryNumeric toggle. When binaryNumeric is true, NUMERIC columns are requested in binary format.

Types

type Column

type Column struct {
	Name           string
	TableOID       uint32
	AttrNum        int16
	TypeOID        protocol.OID
	TypeMod        int32
	TypeLen        int16
	Format         int16
	Encoder        types.Encoder
	KeyPrefix      []byte // pre-built `"name":`
	KeyPrefixComma []byte // pre-built `,"name":` for columns > 0
}

Column captures the parsed RowDescription field plus its compiled encoder.

type Plan

type Plan struct {
	Columns []Column
	// ColumnsArrayHeader is the JSON header for the columnar mode:
	// `{"columns":["a","b"],"rows":[`
	ColumnsArrayHeader []byte
	// TOONHeader is the header for the TOON streaming mode:
	// `[?]{col1,col2,...}\n`. Streamed row count is unknown hence `?`.
	TOONHeader []byte
}

Plan is the cached per-shape execution plan for a result set.

func ParseRowDescription

func ParseRowDescription(body []byte) (*Plan, error)

ParseRowDescription reads a 'T' message body and returns a compiled Plan.

Body layout (server -> client):

int16  field count
for each field:
  cstring name
  int32   table OID  (0 if not from a table)
  int16   attribute number
  int32   type OID
  int16   type size
  int32   type modifier
  int16   format code

func ParseRowDescriptionForFormats

func ParseRowDescriptionForFormats(body []byte, formats []int16) (*Plan, error)

ParseRowDescriptionForFormats parses a 'T' body and applies binary encoders for any column whose format code (in `formats`) is 1.

func (*Plan) ApplyFormats

func (p *Plan) ApplyFormats(formats []int16)

ApplyFormats rewrites the encoder for each column to match the supplied per-column format codes. Used after Bind asks the server for binary on some columns. Equivalent to ApplyFormatsEx(formats, false).

func (*Plan) ApplyFormatsEx

func (p *Plan) ApplyFormatsEx(formats []int16, binaryNumeric bool)

ApplyFormatsEx is ApplyFormats with the BinaryNumeric toggle. When binaryNumeric is true, NUMERIC columns whose format is 1 pick up the binary decoder from numeric_bin.go.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL