Path: redb/redb.Postgres/sql/v2-pvt/11_pvt_column_expr.sql Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Postgres/sql/v2-pvt/11_pvt_column_expr.sql ---- -- ===================================================================== -- 11_pvt_column_expr.sql -- --------------------------------------------------------------------- -- Build a single column expression for the PVT CTE: each scheme field -- becomes a `MAX(v._) FILTER (WHERE v._id_structure = ) -- AS ""` aggregate; base fields are projected from `o.*` -- directly without aggregation. -- -- Functions: -- pvt_db_type_to_value_column(p_db_type text) RETURNS text -- pvt_build_column_expr(p_field_name text, p_field_meta jsonb) RETURNS text -- ===================================================================== -- ---------- pvt_db_type_to_value_column ------------------------------- -- Maps a logical db_type (as stored in _scheme_metadata_cache.db_type) -- to the actual physical column name on the _values table. Returns -- NULL for unsupported types so the caller can raise a clear error. CREATE OR REPLACE FUNCTION pvt_db_type_to_value_column(p_db_type text) RETURNS text LANGUAGE plpgsql IMMUTABLE AS $BODY$ BEGIN RETURN CASE p_db_type WHEN 'String' THEN '_String' WHEN 'Long' THEN '_Long' WHEN 'Double' THEN '_Double' WHEN 'Numeric' THEN '_Numeric' WHEN 'Boolean' THEN '_Boolean' WHEN 'Guid' THEN '_Guid' WHEN 'DateTimeOffset' THEN '_DateTimeOffset' WHEN 'ByteArray' THEN '_ByteArray' WHEN 'ListItem' THEN '_ListItem' WHEN 'Object' THEN '_Object' -- Legacy / type_semantic aliases. WHEN 'TimeSpan' THEN '_Long' -- Ticks WHEN 'TimeOnly' THEN '_Long' -- Ticks WHEN 'DateTime' THEN '_DateTimeOffset' ELSE NULL END; END; $BODY$; COMMENT ON FUNCTION pvt_db_type_to_value_column(text) IS 'Maps a logical db_type to the physical typed column name on _values. Returns NULL if the type is unknown.'; -- ---------- pvt_build_column_expr ------------------------------------- -- Builds a single SELECT-list expression for the PVT pivot CTE. -- Dispatches on the rich FieldInfo emitted by pvt_resolve_field_path -- (kind/sid/db_type/db_column/is_array/list_item_prop/dict_key/parent_sid). -- -- Adopts the Pro pivot idiom `(array_agg(...) FILTER (...))[1]` for -- scalars (works for booleans/uuids/non-orderable types) and the -- "_array_index IS NULL" / "IS NOT NULL" / "= ''" filter -- conventions to distinguish scalar / array / dict element values. -- -- Nested-dict fields (parent_sid AND dict_key both set) are NOT -- emitted as pivot columns; they live in a side CTE generated by -- pvt_build_cte_sql and reach the outer query via a LEFT JOIN alias. CREATE OR REPLACE FUNCTION pvt_build_column_expr( p_field_name text, p_field_meta jsonb, p_array_index_in_outer boolean DEFAULT false -- when true, omit `AND v._array_index IS NULL` from scalar FILTER; caller hoists it to an outer WHERE for inline-pivot shape (Pro-parity GROUP BY). ) RETURNS text LANGUAGE plpgsql STABLE AS $BODY$ DECLARE v_kind text; v_sid bigint; v_db_type text; v_col text; v_alias text; v_li_prop text; v_dict_key text; v_parent_sid bigint; v_is_array boolean; BEGIN IF p_field_meta IS NULL THEN RAISE EXCEPTION 'pvt_build_column_expr: p_field_meta is NULL for field "%"', p_field_name; END IF; v_alias := quote_ident(p_field_name); v_kind := p_field_meta->>'kind'; v_li_prop := p_field_meta->>'list_item_prop'; v_dict_key := p_field_meta->>'dict_key'; v_parent_sid := NULLIF(p_field_meta->>'parent_sid', '')::bigint; v_is_array := COALESCE((p_field_meta->>'is_array')::boolean, false); -- ---- Base field: straight projection from _objects. ------------- IF v_kind = 'base' THEN RETURN format('o.%I AS %s', p_field_meta->>'column', v_alias); END IF; v_sid := (p_field_meta->>'sid')::bigint; v_db_type := p_field_meta->>'db_type'; -- ---- Nested-dict: handled by a side CTE; must not reach here. --- IF v_parent_sid IS NOT NULL AND v_dict_key IS NOT NULL THEN RAISE EXCEPTION 'pvt_build_column_expr: nested-dict field "%" must be emitted via side CTE (caller bug)', p_field_name; END IF; -- ---- ListItem.Value / .Alias -------------------------------- -- Scalar projection emits the raw _ListItem id (parity with Pro); -- consumers wrap it in a _list_items lookup as needed (WHERE- -- builder, ORDER BY-builder). GROUP BY / SELECT pass the id -- through untouched. Array variants still dereference so that -- $arrayContains 'admin' / && {...} keep string semantics. IF v_li_prop = 'Value' THEN -- Pivot source provides `LEFT JOIN _list_items li ON -- li._id = v._ListItem` (emitted by pvt_build_cte_sql when any -- field is Value/Alias). Scalar takes [1], array keeps array_agg. IF v_is_array THEN RETURN format( 'array_agg(li._value) FILTER (WHERE v._id_structure = %s AND v._array_index IS NOT NULL) AS %s', v_sid::text, v_alias); END IF; IF p_array_index_in_outer THEN RETURN format( '(array_agg(li._value) FILTER (WHERE v._id_structure = %s))[1] AS %s', v_sid::text, v_alias); END IF; RETURN format( '(array_agg(li._value) FILTER (WHERE v._id_structure = %s AND v._array_index IS NULL))[1] AS %s', v_sid::text, v_alias); END IF; IF v_li_prop = 'Alias' THEN IF v_is_array THEN RETURN format( 'array_agg(li._alias) FILTER (WHERE v._id_structure = %s AND v._array_index IS NOT NULL) AS %s', v_sid::text, v_alias); END IF; IF p_array_index_in_outer THEN RETURN format( '(array_agg(li._alias) FILTER (WHERE v._id_structure = %s))[1] AS %s', v_sid::text, v_alias); END IF; RETURN format( '(array_agg(li._alias) FILTER (WHERE v._id_structure = %s AND v._array_index IS NULL))[1] AS %s', v_sid::text, v_alias); END IF; -- ---- Resolve typed column --------------------------------------- v_col := pvt_db_type_to_value_column(v_db_type); IF v_col IS NULL THEN RAISE EXCEPTION 'pvt_build_column_expr: unsupported db_type "%" for field "%"', v_db_type, p_field_name; END IF; -- ListItem.Id: project the foreign key column itself (bigint). IF v_li_prop = 'Id' THEN v_col := '_ListItem'; END IF; -- ---- Simple dictionary: PhoneBook[home] --> _array_index='' -- NOTE: value column references use %s (bare) instead of %I (quoted). -- Free PG DDL declares _String/_Long/... unquoted, so the actual -- column names are case-folded to lowercase; quoting them via %I -- (which preserves mixed case) would break the lookup. IF v_dict_key IS NOT NULL AND v_parent_sid IS NULL THEN RETURN format( '(array_agg(v.%s) FILTER (WHERE v._id_structure = %s AND v._array_index = %L))[1] AS %s', v_col, v_sid::text, v_dict_key, v_alias); END IF; -- ---- Array pivot ------------------------------------------------ IF v_is_array THEN RETURN format( 'array_agg(v.%s) FILTER (WHERE v._id_structure = %s AND v._array_index IS NOT NULL) AS %s', v_col, v_sid::text, v_alias); END IF; -- ---- Scalar pivot (Pro idiom) ----------------------------------- IF p_array_index_in_outer THEN RETURN format( '(array_agg(v.%s) FILTER (WHERE v._id_structure = %s))[1] AS %s', v_col, v_sid::text, v_alias); END IF; RETURN format( '(array_agg(v.%s) FILTER (WHERE v._id_structure = %s AND v._array_index IS NULL))[1] AS %s', v_col, v_sid::text, v_alias); END; $BODY$; COMMENT ON FUNCTION pvt_build_column_expr(text, jsonb, boolean) IS 'Builds a single SELECT-list expression for the PVT pivot CTE. Dispatches on the rich FieldInfo jsonb (kind/sid/db_type/is_array/list_item_prop/dict_key/parent_sid). Supports base / scalar / array / simple-dict / ListItem.Id|Value|Alias (scalar+array). Nested-dict fields are deferred to a side CTE emitted by pvt_build_cte_sql. p_array_index_in_outer=true drops `AND v._array_index IS NULL` from scalar/ListItem.Id|Value|Alias FILTER clauses; caller must hoist it to an outer WHERE (Pro-parity inline GROUP BY shape).';;