Expand description
The COPY FROM STDIN codec framework.
A codec binds one COPY data format to its implementation. Codecs are
stateless strategies: they validate the options they accept at
statement-parse time, describe which column types they can ingest, and
create the stateful RecordParser per stream.
Formats are not limited to PostgreSQL’s own — GreptimeDB-specific
formats (a line protocol, a JSON-lines dialect, …) register exactly
like csv does, and may define their own options through the open
WITH (...) clause.
§Parsing and conversion
A RecordParser consumes raw CopyData bytes and emits complete,
insertable rows: one ParsedRow per record, in the order of the
targeted columns. Parsers validate record shape (field counts) and
convert field values themselves — csv delegates both splitting and
typed conversion to arrow-csv’s incremental decoder (the engine
behind COPY ... FROM 'file.csv') and turns decoded batches into
rows with the shared api::helper::vectors_to_rows. A future format
that carries values as text (e.g. a line protocol) would reintroduce
shared field-conversion helpers here; see the removed text codec in
git history for the shape they had.
§Options
The WITH (...) clause is open: every KEY value entry the client
writes is collected into a [CopyOptionSet] (see options) regardless
of whether any format understands it — the statement is parsed
textually (see parse) precisely so options beyond sqlparser’s COPY
grammar can reach the codecs. A codec builder takes the options it
understands (take_string, take_single_byte, take_flag, or raw
take_value for codec-specific shapes) and rejects the rest with
reject_leftovers, so a future format like
COPY t FROM STDIN WITH (FORMAT influxdb_line, precision 'ns') only
needs its own codec and registry entry.
To add a format:
- implement
CopyInCodecfor a config struct holding the format’s validated options (seecsv::CsvCodecfor the example), - write a builder
fn(CopyOptionSet) -> PgWireResult<Arc<dyn CopyInCodec>>that applies defaults and rejects options the format does not accept, - register the builder under every accepted
FORMATname (including aliases) inREGISTERED_FORMATSin the parent module.
Traits§
- Copy
InCodec 🔒 - A COPY FROM STDIN data format implementation.
- Record
Parser 🔒 - Incremental parser of one COPY FROM STDIN stream.
Type Aliases§
- Parsed
Row 🔒 - A parsed, insertable row: one gRPC value per targeted column, in COPY field order. Every parser emits these directly, whatever mechanism it used to produce them.