Path: redb/redb.Postgres/sql/v2-pvt/15_pvt_order.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/15_pvt_order.sql ---- -- ===================================================================== -- 15_pvt_order.sql -- --------------------------------------------------------------------- -- Build an ORDER BY clause for the outer SELECT, referencing CTE -- columns. Returns the empty string when no ordering is requested. -- -- Function: -- pvt_build_order_conditions(p_order jsonb, p_fields jsonb, -- p_base_prefix text DEFAULT '') RETURNS text -- -- Input shape (accepted variants): -- [ -- { "field": "FullName", "dir": "asc" }, -- { "field": "Age", "dir": "desc", "nulls": "last" } -- ] -- [ -- { "field": "FullName", "direction": "ASC" }, -- legacy C# emits this -- { "field_path": "Age", "direction": "DESC" } -- legacy SQL emits this -- ] -- [ -- { "$expr": {"$mul":[{"$field":"Age"},{"$const":2}]}, -- arithmetic -- "dir": "desc" }, -- { "$expr": {"$upper":{"$field":"FirstName"}}, -- function -- "dir": "asc", "nulls": "last" } -- ] -- -- ORDER BY always runs on the OUTER SELECT (over the materialized CTE -- row + JOIN _objects o in narrow Pro shape). Expression compilation -- is delegated to pvt_build_scalar_expr from 17_pvt_expr.sql so the -- same $field/$const/$add/.../$upper/.../$cast grammar that WHERE -- accepts works in ORDER BY too. Base fields are emitted as -- p_base_prefix || quote_ident(), pivot fields as -- quote_ident() (= the CTE column). -- ===================================================================== -- Signature evolved in v0.6.0 (expression-form $expr support) and -- v0.6.1 (p_distinct_on prepend for SELECT DISTINCT ON parity). -- CREATE OR REPLACE cannot change argument lists, so drop legacy forms. DROP FUNCTION IF EXISTS pvt_build_order_conditions(jsonb, jsonb); DROP FUNCTION IF EXISTS pvt_build_order_conditions(jsonb, jsonb, text); DROP FUNCTION IF EXISTS pvt_build_order_conditions(jsonb, jsonb, text, jsonb); DROP FUNCTION IF EXISTS _pvt_compile_order_col(jsonb, jsonb, text); -- ---------- internal helper: compile one entry column expression ------ -- Shared by ORDER BY, DISTINCT ON, GROUP BY and window PARTITION BY -- builders. Returns the column-side SQL fragment (no direction, no -- NULLS, no DISTINCT ON wrapping). -- -- p_listitem_as_id (v0.4.x): controls how ListItem.Value / .Alias are -- emitted. Pivot columns store raw `_ListItem` ids (parity with Pro); -- ORDER BY / DISTINCT ON need the dereferenced string so that sorting -- has a meaningful order, while GROUP BY / SELECT projection keep the -- id directly. Pass `true` from GROUP BY-style builders. CREATE OR REPLACE FUNCTION _pvt_compile_order_col( p_elem jsonb, p_fields jsonb, p_base_prefix text, p_listitem_as_id boolean DEFAULT false ) RETURNS text LANGUAGE plpgsql STABLE AS $BODY$ DECLARE v_field text; v_meta jsonb; v_li_prop text; v_is_arr boolean; v_col text; BEGIN IF p_elem IS NULL OR jsonb_typeof(p_elem) <> 'object' THEN RAISE EXCEPTION '_pvt_compile_order_col: each entry must be a JSON object (got %)', jsonb_typeof(p_elem); END IF; IF p_elem ? '$expr' THEN RETURN pvt_build_scalar_expr(p_elem->'$expr', p_fields, p_base_prefix); END IF; v_field := COALESCE(p_elem->>'field', p_elem->>'field_path'); IF v_field IS NULL OR v_field = '' THEN RAISE EXCEPTION '_pvt_compile_order_col: entry must include "field", "field_path" or "$expr"'; END IF; v_meta := p_fields -> v_field; IF v_meta IS NULL THEN RAISE EXCEPTION '_pvt_compile_order_col: field "%" has no metadata. Did pvt_collect_fields miss it?', v_field; END IF; IF v_meta->>'kind' = 'base' THEN RETURN p_base_prefix || quote_ident(v_meta->>'column'); END IF; v_col := quote_ident(v_field); v_li_prop := v_meta->>'list_item_prop'; v_is_arr := COALESCE((v_meta->>'is_array')::boolean, false); -- ListItem.Value / .Alias scalar: pivot column already holds the -- dereferenced _list_items._value / _alias string (see -- pvt_build_column_expr), so ORDER BY / GROUP BY just reference -- the column directly. No extra _list_items lookup needed. RETURN v_col; END; $BODY$; -- ---------- internal helper: place of NULL of one sort key ------------- -- LINQ puts null first ascending and last descending, and so do Pro, SQL -- Server and SQLite; PostgreSQL does the opposite unless told (v0.7.28: -- the module left it to PostgreSQL, so Free PostgreSQL alone put objects -- without a value last ascending). An explicit "nulls" of the entry wins. -- A base column of the object (kind = base) is never NULL and keeps the -- bare direction, as in Pro: NULLS FIRST on an ascending key would stop -- PostgreSQL from reading the order off its index (a page by _id). CREATE OR REPLACE FUNCTION _pvt_order_nulls( p_elem jsonb, p_fields jsonb, p_dir text ) RETURNS text LANGUAGE plpgsql STABLE AS $BODY$ DECLARE v_nulls text := lower(COALESCE(p_elem->>'nulls', '')); v_field text; BEGIN IF v_nulls = 'first' THEN RETURN ' NULLS FIRST'; END IF; IF v_nulls = 'last' THEN RETURN ' NULLS LAST'; END IF; IF NOT (p_elem ? '$expr') THEN v_field := COALESCE(p_elem->>'field', p_elem->>'field_path'); IF p_fields -> v_field ->> 'kind' = 'base' THEN RETURN ''; END IF; END IF; RETURN CASE WHEN p_dir = 'desc' THEN ' NULLS LAST' ELSE ' NULLS FIRST' END; END; $BODY$; -- ---------- pvt_build_distinct_on_select ------------------------------ -- Render a "DISTINCT ON (expr1, expr2) " SELECT-list prefix from a JSON -- array of {field|$expr} entries. Returns the empty string when -- p_distinct_on is NULL/empty. Used by the orchestrator inside the -- three outer-SELECT shapes (Shape A, narrow, wide). CREATE OR REPLACE FUNCTION pvt_build_distinct_on_select( p_distinct_on jsonb, p_fields jsonb, p_base_prefix text DEFAULT '' ) RETURNS text LANGUAGE plpgsql STABLE AS $BODY$ DECLARE elem jsonb; parts text[] := ARRAY[]::text[]; BEGIN IF p_distinct_on IS NULL OR jsonb_typeof(p_distinct_on) <> 'array' OR jsonb_array_length(p_distinct_on) = 0 THEN RETURN ''; END IF; FOR elem IN SELECT value FROM jsonb_array_elements(p_distinct_on) LOOP parts := parts || _pvt_compile_order_col(elem, p_fields, p_base_prefix); END LOOP; RETURN 'DISTINCT ON (' || array_to_string(parts, ', ') || ') '; END; $BODY$; COMMENT ON FUNCTION pvt_build_distinct_on_select(jsonb, jsonb, text) IS 'Renders a "DISTINCT ON (expr1, expr2) " SELECT-list prefix from a JSON array of {field|$expr} entries. PostgreSQL requires these expressions to also appear at the head of ORDER BY -- the caller pairs this with pvt_build_order_conditions(..., p_distinct_on) which auto-prepends them.'; CREATE OR REPLACE FUNCTION pvt_build_order_conditions( p_order jsonb, p_fields jsonb, p_base_prefix text DEFAULT '', p_distinct_on jsonb DEFAULT NULL ) RETURNS text LANGUAGE plpgsql STABLE AS $BODY$ DECLARE elem jsonb; oel jsonb; dir text; parts text[] := ARRAY[]::text[]; v_col text; v_keys text[] := ARRAY[]::text[]; v_key_part text; BEGIN -- ---------- DISTINCT ON prefix -------------------------------- -- PostgreSQL requires every DISTINCT ON expression to appear at -- the head of ORDER BY. A key takes the direction of the p_order -- entry for the same expression (OrderByDescending(k).DistinctBy(k) -- comes back descending), ASC when p_order has none; that p_order -- entry is then skipped below so the key is not ordered twice. -- v0.7.17: the key used to be forced ASC. IF p_distinct_on IS NOT NULL AND jsonb_typeof(p_distinct_on) = 'array' AND jsonb_array_length(p_distinct_on) > 0 THEN FOR elem IN SELECT value FROM jsonb_array_elements(p_distinct_on) LOOP v_col := _pvt_compile_order_col(elem, p_fields, p_base_prefix); v_key_part := v_col || ' ASC' || _pvt_order_nulls(elem, p_fields, 'asc'); IF p_order IS NOT NULL AND jsonb_typeof(p_order) = 'array' THEN FOR oel IN SELECT value FROM jsonb_array_elements(p_order) LOOP IF _pvt_compile_order_col(oel, p_fields, p_base_prefix) = v_col THEN dir := lower(COALESCE(oel->>'dir', oel->>'direction', 'asc')); v_key_part := v_col || ' ' || upper(dir) || _pvt_order_nulls(oel, p_fields, dir); EXIT; END IF; END LOOP; END IF; parts := parts || v_key_part; v_keys := v_keys || v_col; END LOOP; END IF; IF p_order IS NULL OR jsonb_typeof(p_order) <> 'array' OR jsonb_array_length(p_order) = 0 THEN IF array_length(parts, 1) IS NULL THEN RETURN ''; END IF; RETURN E'\nORDER BY ' || array_to_string(parts, ', '); END IF; FOR elem IN SELECT value FROM jsonb_array_elements(p_order) LOOP dir := lower(COALESCE(elem->>'dir', elem->>'direction', 'asc')); IF dir NOT IN ('asc', 'desc') THEN RAISE EXCEPTION 'pvt_build_order_conditions: invalid direction "%" (asc|desc only)', dir; END IF; v_col := _pvt_compile_order_col(elem, p_fields, p_base_prefix); IF v_col = ANY(v_keys) THEN CONTINUE; -- already at the head as a DISTINCT ON key END IF; parts := parts || (v_col || ' ' || upper(dir) || _pvt_order_nulls(elem, p_fields, dir)); END LOOP; RETURN E'\nORDER BY ' || array_to_string(parts, ', '); END; $BODY$; COMMENT ON FUNCTION pvt_build_order_conditions(jsonb, jsonb, text, jsonb) IS 'Builds an ORDER BY clause for the outer SELECT. Accepts plain {"field"|"field_path","dir","nulls"} entries and Pro-parity {"$expr":,"dir","nulls"} entries; expressions are compiled via pvt_build_scalar_expr. Base fields use p_base_prefix (default '''', or ''o.'' for the narrow Pro-shape outer that JOINs _objects). When p_distinct_on is provided its expressions are prepended (PostgreSQL DISTINCT ON requirement) with the direction of the matching p_order entry, ASC when there is none. Returns empty string when both p_order and p_distinct_on are empty.';