Referencia · §8.4

Binder

binder

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

Resolves names through nested scopes — a subquery sees enclosing rows as Outer { depth, index }, which makes it correlated — reports ambiguous (42702) and unknown (42703, 42P01) names, infers types (an untyped string literal or NULL takes the other side’s type in comparisons and arithmetic, never in ||; a $n takes its context’s type — including INSERT/UPDATE target columns, for describe — one for the statement, its first place’s: date ± $1 a number of days, timestamp + $1 an interval, interval * $1 a float, a later coercion to the other side’s type not changing it), lifts aggregates into an Aggregate node (grouped expressions matched structurally; other column references are 42803), and places ORDER BY keys (output name or position, else a hidden extra column, refused under DISTINCT). A lateral FROM item (LATERAL (subquery), or a function) is bound with the items before it — a join’s left side, or the comma-separated items before it — visible as the row it runs for (Outer at depth 1, the enclosing queries’ rows a level further); reading them, its join runs it for each left row (binder::lateral_join: an index nested loop’s JoinAlgo::Lookup, below), else it is an item like any other. A lateral item cannot be the side a RIGHT or FULL join keeps whole (42P10, PostgreSQL’s rule); a subquery not marked LATERAL that names an item before it is 42P01 with a hint to mark it.

Grouping sets (grouping_sets): the parser turns GROUPING SETS, ROLLUP, CUBE and () into the list of sets (the items’ sets crossed; GROUP BY DISTINCT drops repeated sets), each its keys’ positions among all the keys named. The binder binds the keys once, and one more key after them, the set’s mask (a bit per key the set leaves out); the select list, HAVING and ORDER BY are bound over that layout as over one grouping, GROUPING(k, …) a call over the mask. The plan is a UNION ALL of an Aggregate per set over the same input, each projected to the layout — its keys, NULL for the others, its mask a constant, the aggregates — so every executor runs it as ordinary groupings.

Row constructors (rows): (a, b, …) and ROW(…) compared — =, <>, <, <=, >, >=, IS [NOT] DISTINCT FROM, [NOT] IN (rows), IS [NOT] NULL — are rewritten before binding to comparisons of their fields with PostgreSQL’s meaning: = every pair equal, <> one unequal, the ordering ones decided by the first pair not equal (a1 < b1 OR (a1 = b1 AND …), a NULL pair before it making the result NULL), with the first pair’s bound (a1 <= b1, implied) before it, so an index on the first column bounds the scan. Unequal lengths are 42601. A row [NOT] IN (subquery) (as many columns as fields, else 42601) is CASE WHEN EXISTS (… WHERE row = (cols)) THEN true WHEN EXISTS (… WHERE (row = (cols)) IS NULL) THEN NULL ELSE false END — the row comparison’s meaning, so NOT IN leaves out a row a NULL might equal; = ANY / ALL (subquery) with a row is 0A000. Anywhere else a row is a value of its own, a record (DataType::Record, Value::Record): compared field by field, a NULL field after the others and equal to a NULL (PostgreSQL’s record values; so DISTINCT (a, b), GROUP BY (a, b) and ORDER BY (a, b) work, also vectorized, in parallel and spilled), written as record_out does ((1,"a b",)); it has key, row and spill encodings but is never a column’s type (CREATE TABLE … AS of one: 42P16).

A statement’s own WITH with a data-modifying CTE, or before INSERT, UPDATE or DELETE, is a Statement::WithStatement: the session runs the CTEs’ statements in order and gives each part bound after them — the next CTE’s statement, then the statement itself — the CTEs before it (binder::GivenCte): a query CTE run where it stands, as its rows (under WITH RECURSIVE bound there), a data-modifying one as its RETURNING rows (a Values). DESCRIBE gives them as their columns, with no rows, and runs nothing. An aggregate’s FILTER (WHERE c) (agg_filter) is bound as an aggregate of its own wrapping the inner one, fed c first and passing on the rows it holds for.