Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

109 KB
Raw Blame
//! This module exposes a expanded dynamic query builder which is able to build specific soup queries
//! which filter out content basd on some input ast

#[cfg(test)]
mod test;

mod database_row;
mod document_ids;
mod initiative;
mod property_filter;

use database_row::{build_database_row_filter, database_row_opted_in, database_row_top_clause};
pub(in crate::outbound::pg_soup_repo) use document_ids::DocumentIdArrays;
use initiative::initiative_top_clause;
pub(in crate::outbound::pg_soup_repo) use initiative::{
    build_initiative_filter, initiative_access_clause, initiative_opted_in,
};

#[cfg(test)]
use item_filters::ast::initiative::InitiativeLiteral;
use std::str::FromStr;

use chrono::{DateTime, Utc};
use document_sub_type::DocumentSubType;
use filter_ast::Expr;
use item_filters::ast::{
    EntityFilterAst,
    calendar_event::CalendarEventLiteral,
    chat::ChatLiteral,
    date::DateLiteral,
    document::DocumentLiteral,
    email::Email,
    project::ProjectLiteral,
    properties::{PropertiesLiteral, PropertyEntityType, PropertyMatchValue},
};
use macro_user_id::user_id::MacroUserIdStr;
use model_owner::Owner;
use models_pagination::{Query, SimpleSortMethod};
use models_soup::{
    calendar_event::SoupCalendarEvent,
    chat::SoupChat,
    database_row::SoupDatabaseRow,
    document::{SoupDocument, SoupDocumentSubType},
    initiative::SoupInitiative,
    item::SoupItem,
    project::SoupProject,
};
use recursion::CollapsibleExt;
use sqlx::{
    PgConnection, PgPool, Postgres, QueryBuilder, Row, Transaction, postgres::PgRow,
    prelude::FromRow,
};
use system_properties::{StatusOption, SystemPropertyKey};
use tracing::Instrument;
use uuid::Uuid;

use crate::domain::models::{
    DynamicSort, SoupDocumentServerFacts, SoupProjectionHydration, SoupSortDirection,
    grouping::ItemGroupingInfo,
};
use crate::outbound::pg_soup_repo::grouping::{
    GroupJoinClause, group_join_clause, group_order_expr, group_select_expr,
    property_date_expression, property_value_sql,
};
use crate::outbound::pg_soup_repo::type_err;
use models_grouping::{GroupByField, GroupingConfig, date_bucket_sql_order};

fn property_sort_join(enabled: bool, parameter: usize) -> String {
    if !enabled {
        return String::new();
    }
    let property_definition_id = format!("${parameter}::uuid");
    let data_type =
        format!("(SELECT data_type FROM property_definitions WHERE id = {property_definition_id})");
    let value = property_value_sql(
        "ep_sort",
        &property_definition_id,
        &data_type,
        "NULL::jsonb",
        "po_group",
    );
    format!(
        r#" LEFT JOIN entity_properties ep_sort
        ON ep_sort.property_definition_id = ${parameter}::uuid
            AND ep_sort.entity_id = all_items.id::text
            AND ep_sort.entity_type = all_items.property_entity_type
        LEFT JOIN LATERAL (SELECT ({key})::{sql_type} AS key) property_sort ON true "#,
        key = value.sort_key,
        sql_type = value.sort_type,
    )
}

fn sort_key_expression(enabled: bool) -> &'static str {
    if enabled {
        "property_sort.key"
    } else {
        "0::double precision"
    }
}

/// The caller's source-id array, after the existing query parameters and optional property sort.
fn source_ids_slot(sort: DynamicSort, grouped: bool) -> usize {
    let fixed_parameters = if grouped { 10 } else { 8 };
    let property_parameters = if matches!(sort, DynamicSort::Property { .. }) {
        2
    } else {
        0
    };
    fixed_parameters + property_parameters + 1
}

/// First document-ID array, after the caller's source ids.
fn first_document_id_slot(sort: DynamicSort, grouped: bool) -> usize {
    source_ids_slot(sort, grouped) + 1
}

// The non-null and missing-key branches both follow SQL's NULLS LAST order.
fn property_keyset(
    sort: DynamicSort,
    key: &str,
    cursor_key: usize,
    timestamp: &str,
    id: &str,
) -> String {
    let comparison = match sort {
        DynamicSort::Property {
            direction: SoupSortDirection::Asc,
            ..
        } => ">",
        DynamicSort::Property {
            direction: SoupSortDirection::Desc,
            ..
        } => "<",
        DynamicSort::Timestamp(_) => unreachable!("timestamp uses the deployed tuple predicate"),
    };
    format!(
        "($4::timestamptz IS NULL OR (
            (${cursor_key}::double precision IS NULL AND {key} IS NULL
                AND ({timestamp}, {id}::text) < ($4, $5))
            OR (${cursor_key}::double precision IS NOT NULL AND (
                {key} IS NULL OR {key} {comparison} ${cursor_key}::double precision
                OR ({key} = ${cursor_key}::double precision
                    AND ({timestamp}, {id}::text) < ($4, $5))
            ))))"
    )
}

fn property_order(sort: DynamicSort, key: &str, timestamp: &str, id: &str) -> String {
    match sort {
        DynamicSort::Property { direction, .. } => {
            let direction = match direction {
                SoupSortDirection::Asc => "ASC",
                SoupSortDirection::Desc => "DESC",
            };
            format!("{key} {direction} NULLS LAST, {timestamp} DESC, {id} DESC")
        }
        DynamicSort::Timestamp(_) => format!("{timestamp} DESC, {id} DESC"),
    }
}

fn date_range_group_expression(grouping: &GroupingConfig) -> Option<String> {
    let (_, ranges) = grouping.field.date_ranges()?;
    let date = property_date_expression("ep_group");
    let mut sql = format!("CASE WHEN ep_group.data_type != 'DATE' OR ({date}) IS NULL THEN '-1'");
    for (index, range) in ranges.ranges.iter().enumerate() {
        let start = range
            .start
            .map(|bound| format!("({date}) >= '{}'::timestamptz", bound.to_rfc3339()));
        let end = range
            .end
            .map(|bound| format!("({date}) < '{}'::timestamptz", bound.to_rfc3339()));
        let bounds = start
            .into_iter()
            .chain(end)
            .collect::<Vec<_>>()
            .join(" AND ");
        sql.push_str(&format!(
            " WHEN {} THEN '{index}'",
            if bounds.is_empty() { "TRUE" } else { &bounds }
        ));
    }
    sql.push_str(" ELSE '-1' END");
    Some(sql)
}
static PREFIX: &str = r#"
    WITH user_source_ids AS (
        SELECT cp.channel_id::text as source_id FROM comms_channel_participants cp
            WHERE cp.user_id = $1 AND cp.left_at IS NULL
        UNION ALL
        SELECT t.team_id::text FROM team_user t
            WHERE t.user_id = $1
        UNION ALL
        SELECT $1
    ),
"#;

/// The `user_source_ids` rows, bound into the document, chat, and project
/// access gates by [`bound_access_semi_join`].
async fn fetch_user_source_ids(
    connection: &mut PgConnection,
    user_id: &str,
) -> Result<Vec<String>, sqlx::Error> {
    let source_ids = sqlx::query_scalar!(
        r#"
        SELECT cp.channel_id::text FROM comms_channel_participants cp
            WHERE cp.user_id = $1 AND cp.left_at IS NULL
        UNION ALL
        SELECT t.team_id::text FROM team_user t
            WHERE t.user_id = $1
        UNION ALL
        SELECT $1
        "#,
        user_id
    )
    .fetch_all(connection)
    .await?;
    Ok(source_ids.into_iter().flatten().collect())
}

// -- Lightweight top clauses: only id + sort_ts (plus filter-required joins) --

static DOCUMENT_TASK_PROPERTY_JOINS: &str = r#"
                LEFT JOIN entity_properties ep_assignees
                    ON dt.sub_type = 'task'
                    AND ep_assignees.entity_id = d.id
                    AND ep_assignees.entity_type = 'TASK'
                    AND ep_assignees.property_definition_id = $8
                LEFT JOIN entity_properties ep_status
                    ON dt.sub_type = 'task'
                    AND ep_status.entity_id = d.id
                    AND ep_status.entity_type = 'TASK'
                    AND ep_status.property_definition_id = $7
"#;

fn document_top_clause(
    sort_method: SimpleSortMethod,
    needs_sub_type_join: bool,
    property_sort: bool,
    task_only: bool,
) -> String {
    let sub_type_join = if needs_sub_type_join || (property_sort && !task_only) {
        r#"                LEFT JOIN document_sub_type dt ON dt.document_id = d.id
"#
    } else {
        ""
    };
    let property_column = match (property_sort, task_only) {
        (true, true) => ", 'TASK'::property_entity_type as property_entity_type",
        (true, false) => {
            ", CASE WHEN dt.sub_type = 'task' THEN 'TASK'::property_entity_type ELSE 'DOCUMENT'::property_entity_type END as property_entity_type"
        }
        (false, _) => "",
    };
    format!(
        r#"
                SELECT
                    'document'::text as item_type,
                    d.id,
                    {}::timestamptz as sort_ts{property_column}
                FROM "Document" d
{}"#,
        top_sort_expr("d", sort_method),
        sub_type_join
    )
}

fn chat_top_clause(sort_method: SimpleSortMethod, property_sort: bool) -> String {
    let user_history_join = if top_needs_user_history(sort_method) {
        r#"                LEFT JOIN "UserHistory" uh ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1
"#
    } else {
        ""
    };
    let property_column = if property_sort {
        ", 'CHAT'::property_entity_type as property_entity_type"
    } else {
        ""
    };
    format!(
        r#"
                SELECT
                    'chat'::text as item_type,
                    c.id,
                    {}::timestamptz as sort_ts{property_column}
                FROM "Chat" c
{}"#,
        top_sort_expr("c", sort_method),
        user_history_join
    )
}

fn project_top_clause(sort_method: SimpleSortMethod, property_sort: bool) -> String {
    let user_history_join = if top_needs_user_history(sort_method) {
        r#"                LEFT JOIN "UserHistory" uh
                    ON uh."itemId" = p.id
                    AND uh."itemType" = 'project'
                    AND uh."userId" = $1
"#
    } else {
        ""
    };
    let property_column = if property_sort {
        ", 'PROJECT'::property_entity_type as property_entity_type"
    } else {
        ""
    };
    format!(
        r#"
                SELECT
                    'project'::text as item_type,
                    p.id,
                    {}::timestamptz as sort_ts{property_column}
                FROM "Project" p
{}"#,
        top_sort_expr("p", sort_method),
        user_history_join
    )
}

// -- Grouped top clauses: include project_id for grouping support --

fn grouped_document_top_clause(
    sort_method: SimpleSortMethod,
    needs_sub_type_join: bool,
    task_only: bool,
) -> String {
    let sort_ts = top_sort_expr("d", sort_method);
    let property_entity_type = if task_only {
        "'TASK'::property_entity_type"
    } else {
        "CASE WHEN dt.sub_type = 'task' THEN 'TASK'::property_entity_type ELSE 'DOCUMENT'::property_entity_type END"
    };
    let sub_type_join = if needs_sub_type_join || !task_only {
        "LEFT JOIN document_sub_type dt ON dt.document_id = d.id"
    } else {
        ""
    };
    format!(
        r#"
                SELECT
                    'document'::text as item_type,
                    d.id,
                    {sort_ts}::timestamptz as sort_ts,
                    d."projectId"::text as project_id,
                    {property_entity_type} as property_entity_type
                FROM "Document" d
                {sub_type_join}
"#
    )
}

fn grouped_chat_top_clause(sort_method: SimpleSortMethod) -> String {
    let sort_ts = top_sort_expr("c", sort_method);
    let user_history_join = if top_needs_user_history(sort_method) {
        r#"LEFT JOIN "UserHistory" uh ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1"#
    } else {
        ""
    };
    format!(
        r#"
                SELECT
                    'chat'::text as item_type,
                    c.id,
                    {sort_ts}::timestamptz as sort_ts,
                    c."projectId"::text as project_id,
                    'CHAT'::property_entity_type as property_entity_type
                FROM "Chat" c
                {user_history_join}
"#
    )
}

fn grouped_project_top_clause(sort_method: SimpleSortMethod) -> String {
    let sort_ts = top_sort_expr("p", sort_method);
    let user_history_join = if top_needs_user_history(sort_method) {
        r#"LEFT JOIN "UserHistory" uh ON uh."itemId" = p.id AND uh."itemType" = 'project' AND uh."userId" = $1"#
    } else {
        ""
    };
    format!(
        r#"
                SELECT
                    'project'::text as item_type,
                    p.id,
                    {sort_ts}::timestamptz as sort_ts,
                    p."parentId"::text as project_id,
                    'PROJECT'::property_entity_type as property_entity_type
                FROM "Project" p
                {user_history_join}
"#
    )
}

fn grouped_calendar_event_top_clause(sort_method: SimpleSortMethod) -> String {
    let sort_ts = match sort_method {
        SimpleSortMethod::CreatedAt => "event.created_at",
        SimpleSortMethod::ViewedAt => "'1970-01-01 00:00:00+00'",
        SimpleSortMethod::UpdatedAt | SimpleSortMethod::ViewedUpdated => {
            "GREATEST(event.updated_at, event.last_reminder_fired_at)"
        }
    };
    format!(
        r#"
                SELECT
                    'calendar_event'::text as item_type,
                    event.id::text as id,
                    {sort_ts}::timestamptz as sort_ts,
                    NULL::text as project_id,
                    'CALENDAR_EVENT'::property_entity_type as property_entity_type
                FROM calendar_events event
                WHERE (
                      event.owner_id = $1
                      OR EXISTS (
                          SELECT 1
                          FROM macro_user_links link
                          WHERE link.link_id = event.source_link_id
                            AND link.primary_macro_id = $1
                      )
                  )
"#
    )
}

// -- Detail clauses: full columns, joined back from TopItems --

static DOCUMENT_DETAIL_CLAUSE: &str = r#"
        SELECT
            'document' as "item_type",
            d.id as "id",
            CAST(COALESCE(di.id, db.id) as TEXT) as "document_version_id",
            d.owner as "user_id",
            d.name as "name",
            d."branchedFromId" as "branched_from_id",
            d."branchedFromVersionId" as "branched_from_version_id",
            d."documentFamilyId" as "document_family_id",
            d."fileType" as "file_type",
            d."createdAt"::timestamptz as "created_at",
            d."updatedAt"::timestamptz as "updated_at",
            d."projectId" as "project_id",
            NULL as "is_persistent",
            NULL::text as "model",
            di.sha as "sha",
            dt.sub_type as "sub_type",
            EXISTS (
                SELECT 1
                FROM document_email de
                WHERE de.document_id = d.id
            ) as "is_email_attachment",
            (
                dt.sub_type IS DISTINCT FROM 'task'
                OR EXISTS (
                    SELECT 1
                    FROM entity_properties ep_assignees_projection
                    WHERE ep_assignees_projection.entity_id = d.id
                        AND ep_assignees_projection.entity_type = 'TASK'
                        AND ep_assignees_projection.property_definition_id = $8
                        AND ep_assignees_projection.values->'value' @> jsonb_build_array(
                            jsonb_build_object('entity_id', $1)
                        )
                )
            ) as "is_important",
            ARRAY(
                SELECT status_option_id::uuid
                FROM jsonb_array_elements_text(
                    CASE
                        WHEN jsonb_typeof(ep_status.values->'value') = 'array'
                        THEN ep_status.values->'value'
                        ELSE '[]'::jsonb
                    END
                ) AS status_option_id
            ) as "status_option_ids",
            uh."updatedAt"::timestamptz as "viewed_at",
            t.sort_ts as "sort_ts",
            t.sort_key as "sort_key",
            CASE
                WHEN dt.sub_type = 'task'
                    AND ep_status.values->'value' ? $6
                THEN true
                WHEN dt.sub_type = 'task'
                THEN false
                ELSE NULL
            END as "is_completed",
            d."deletedAt"::timestamptz as "deleted_at",
            NULL::jsonb as "calendar_event",
            NULL::jsonb as "database_row"
        FROM TopItems t
        INNER JOIN "Document" d ON d.id = t.id
        LEFT JOIN document_sub_type dt ON dt.document_id = d.id
        LEFT JOIN entity_properties ep_status
            ON dt.sub_type = 'task'
            AND ep_status.entity_id = d.id
            AND ep_status.entity_type = 'TASK'
            AND ep_status.property_definition_id = $7
        LEFT JOIN "UserHistory" uh
            ON uh."itemId" = d.id AND uh."itemType" = 'document' AND uh."userId" = $1
        LEFT JOIN LATERAL (
            SELECT b.id
            FROM "DocumentBom" b
            WHERE b."documentId" = d.id
            ORDER BY b."createdAt" DESC
            LIMIT 1
        ) db ON true
        LEFT JOIN LATERAL (
            SELECT i.id, i.sha
            FROM "DocumentInstance" i
            WHERE i."documentId" = d.id
            ORDER BY i."updatedAt" DESC
            LIMIT 1
        ) di ON true
        WHERE t.item_type = 'document'
"#;

static CHAT_DETAIL_CLAUSE: &str = r#"
        SELECT
            'chat' as "item_type",
            c.id as "id",
            NULL as "document_version_id",
            c."userId" as "user_id",
            c.name as "name",
            NULL as "branched_from_id",
            NULL as "branched_from_version_id",
            NULL as "document_family_id",
            NULL as "file_type",
            c."createdAt"::timestamptz as "created_at",
            c."updatedAt"::timestamptz as "updated_at",
            c."projectId" as "project_id",
            c."isPersistent" as "is_persistent",
            c.model as "model",
            NULL as "sha",
            NULL as "sub_type",
            false as "is_email_attachment",
            true as "is_important",
            ARRAY[]::uuid[] as "status_option_ids",
            uh."updatedAt"::timestamptz as "viewed_at",
            t.sort_ts as "sort_ts",
            t.sort_key as "sort_key",
            NULL as "is_completed",
            c."deletedAt"::timestamptz as "deleted_at",
            NULL::jsonb as "calendar_event",
            NULL::jsonb as "database_row"
        FROM TopItems t
        INNER JOIN "Chat" c ON c.id = t.id
        LEFT JOIN "UserHistory" uh
            ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1
        WHERE t.item_type = 'chat'
"#;

static PROJECT_DETAIL_CLAUSE: &str = r#"
        SELECT
            'project' as "item_type",
            p.id as "id",
            NULL as "document_version_id",
            p."userId" as "user_id",
            p.name as "name",
            NULL as "branched_from_id",
            NULL as "branched_from_version_id",
            NULL as "document_family_id",
            NULL as "file_type",
            p."createdAt"::timestamptz as "created_at",
            p."updatedAt"::timestamptz as "updated_at",
            p."parentId" as "project_id",
            NULL as "is_persistent",
            NULL::text as "model",
            NULL as "sha",
            NULL as "sub_type",
            false as "is_email_attachment",
            true as "is_important",
            ARRAY[]::uuid[] as "status_option_ids",
            uh."updatedAt"::timestamptz as "viewed_at",
            t.sort_ts as "sort_ts",
            t.sort_key as "sort_key",
            NULL as "is_completed",
            p."deletedAt"::timestamptz as "deleted_at",
            NULL::jsonb as "calendar_event",
            NULL::jsonb as "database_row"
        FROM TopItems t
        INNER JOIN "Project" p ON p.id = t.id
        LEFT JOIN "UserHistory" uh
            ON uh."itemId" = p.id
            AND uh."itemType" = 'project'
            AND uh."userId" = $1
        WHERE t.item_type = 'project'
"#;

static INITIATIVE_DETAIL_CLAUSE: &str = include_str!("dynamic/initiative_detail.sql");

static DATABASE_ROW_DETAIL_CLAUSE: &str = include_str!("dynamic/database_row_detail.sql");

// -- Grouped detail clauses: join from GroupedItems, include group columns --

static GROUPED_DOCUMENT_DETAIL_CLAUSE: &str = r#"
        SELECT
            'document' as "item_type",
            d.id as "id",
            CAST(COALESCE(di.id, db.id) as TEXT) as "document_version_id",
            d.owner as "user_id",
            d.name as "name",
            d."branchedFromId" as "branched_from_id",
            d."branchedFromVersionId" as "branched_from_version_id",
            d."documentFamilyId" as "document_family_id",
            d."fileType" as "file_type",
            d."createdAt"::timestamptz as "created_at",
            d."updatedAt"::timestamptz as "updated_at",
            d."projectId" as "project_id",
            NULL::boolean as "is_persistent",
            NULL::text as "model",
            di.sha as "sha",
            dt.sub_type as "sub_type",
            EXISTS (
                SELECT 1
                FROM document_email de
                WHERE de.document_id = d.id
            ) as "is_email_attachment",
            (
                dt.sub_type IS DISTINCT FROM 'task'
                OR EXISTS (
                    SELECT 1
                    FROM entity_properties ep_assignees_projection
                    WHERE ep_assignees_projection.entity_id = d.id
                        AND ep_assignees_projection.entity_type = 'TASK'
                        AND ep_assignees_projection.property_definition_id = $8
                        AND ep_assignees_projection.values->'value' @> jsonb_build_array(
                            jsonb_build_object('entity_id', $1)
                        )
                )
            ) as "is_important",
            ARRAY(
                SELECT status_option_id::uuid
                FROM jsonb_array_elements_text(
                    CASE
                        WHEN jsonb_typeof(ep_status.values->'value') = 'array'
                        THEN ep_status.values->'value'
                        ELSE '[]'::jsonb
                    END
                ) AS status_option_id
            ) as "status_option_ids",
            uh."updatedAt"::timestamptz as "viewed_at",
            gi.sort_ts as "sort_ts",
            gi.sort_key as "sort_key",
            CASE
                WHEN dt.sub_type = 'task'
                    AND ep_status.values->'value' ? $6
                THEN true
                WHEN dt.sub_type = 'task'
                THEN false
                ELSE NULL
            END as "is_completed",
            d."deletedAt"::timestamptz as "deleted_at",
            NULL::jsonb as "calendar_event",
            NULL::jsonb as "database_row",
            gi.group_key as "group_key",
            gi.group_order as "group_order",
            gi.group_total_count as "group_total_count",
            gi.row_in_group as "row_in_group"
        FROM GroupedItems gi
        INNER JOIN "Document" d ON d.id = gi.id
        LEFT JOIN document_sub_type dt ON dt.document_id = d.id
        LEFT JOIN entity_properties ep_status
            ON dt.sub_type = 'task'
            AND ep_status.entity_id = d.id
            AND ep_status.entity_type = 'TASK'
            AND ep_status.property_definition_id = $7
        LEFT JOIN "UserHistory" uh
            ON uh."itemId" = d.id AND uh."itemType" = 'document' AND uh."userId" = $1
        LEFT JOIN LATERAL (
            SELECT b.id
            FROM "DocumentBom" b
            WHERE b."documentId" = d.id
            ORDER BY b."createdAt" DESC
            LIMIT 1
        ) db ON true
        LEFT JOIN LATERAL (
            SELECT i.id, i.sha
            FROM "DocumentInstance" i
            WHERE i."documentId" = d.id
            ORDER BY i."updatedAt" DESC
            LIMIT 1
        ) di ON true
        WHERE gi.item_type = 'document'
"#;

static GROUPED_CHAT_DETAIL_CLAUSE: &str = r#"
        SELECT
            'chat' as "item_type",
            c.id as "id",
            NULL::text as "document_version_id",
            c."userId" as "user_id",
            c.name as "name",
            NULL::text as "branched_from_id",
            NULL::bigint as "branched_from_version_id",
            NULL::bigint as "document_family_id",
            NULL::text as "file_type",
            c."createdAt"::timestamptz as "created_at",
            c."updatedAt"::timestamptz as "updated_at",
            c."projectId" as "project_id",
            c."isPersistent" as "is_persistent",
            c.model as "model",
            NULL::text as "sha",
            NULL::document_sub_type_value as "sub_type",
            false as "is_email_attachment",
            true as "is_important",
            ARRAY[]::uuid[] as "status_option_ids",
            uh."updatedAt"::timestamptz as "viewed_at",
            gi.sort_ts as "sort_ts",
            gi.sort_key as "sort_key",
            NULL::boolean as "is_completed",
            c."deletedAt"::timestamptz as "deleted_at",
            NULL::jsonb as "calendar_event",
            NULL::jsonb as "database_row",
            gi.group_key as "group_key",
            gi.group_order as "group_order",
            gi.group_total_count as "group_total_count",
            gi.row_in_group as "row_in_group"
        FROM GroupedItems gi
        INNER JOIN "Chat" c ON c.id = gi.id
        LEFT JOIN "UserHistory" uh
            ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1
        WHERE gi.item_type = 'chat'
"#;

static GROUPED_PROJECT_DETAIL_CLAUSE: &str = r#"
        SELECT
            'project' as "item_type",
            p.id as "id",
            NULL::text as "document_version_id",
            p."userId" as "user_id",
            p.name as "name",
            NULL::text as "branched_from_id",
            NULL::bigint as "branched_from_version_id",
            NULL::bigint as "document_family_id",
            NULL::text as "file_type",
            p."createdAt"::timestamptz as "created_at",
            p."updatedAt"::timestamptz as "updated_at",
            p."parentId" as "project_id",
            NULL::boolean as "is_persistent",
            NULL::text as "model",
            NULL::text as "sha",
            NULL::document_sub_type_value as "sub_type",
            false as "is_email_attachment",
            true as "is_important",
            ARRAY[]::uuid[] as "status_option_ids",
            uh."updatedAt"::timestamptz as "viewed_at",
            gi.sort_ts as "sort_ts",
            gi.sort_key as "sort_key",
            NULL::boolean as "is_completed",
            p."deletedAt"::timestamptz as "deleted_at",
            NULL::jsonb as "calendar_event",
            NULL::jsonb as "database_row",
            gi.group_key as "group_key",
            gi.group_order as "group_order",
            gi.group_total_count as "group_total_count",
            gi.row_in_group as "row_in_group"
        FROM GroupedItems gi
        INNER JOIN "Project" p ON p.id = gi.id
        LEFT JOIN "UserHistory" uh
            ON uh."itemId" = p.id
            AND uh."itemType" = 'project'
            AND uh."userId" = $1
        WHERE gi.item_type = 'project'
"#;

static GROUPED_INITIATIVE_DETAIL_CLAUSE: &str =
    include_str!("dynamic/initiative_grouped_detail.sql");

static GROUPED_DATABASE_ROW_DETAIL_CLAUSE: &str =
    include_str!("dynamic/database_row_grouped_detail.sql");

static GROUPED_CALENDAR_EVENT_DETAIL_CLAUSE: &str = r#"
        SELECT
            'calendar_event' as "item_type",
            event.id::text as "id",
            NULL::text as "document_version_id",
            event.owner_id as "user_id",
            event.title as "name",
            NULL::text as "branched_from_id",
            NULL::bigint as "branched_from_version_id",
            NULL::bigint as "document_family_id",
            NULL::text as "file_type",
            event.created_at as "created_at",
            event.updated_at as "updated_at",
            NULL::text as "project_id",
            NULL::boolean as "is_persistent",
            NULL::text as "model",
            NULL::text as "sha",
            NULL::document_sub_type_value as "sub_type",
            false as "is_email_attachment",
            true as "is_important",
            ARRAY[]::uuid[] as "status_option_ids",
            NULL::timestamptz as "viewed_at",
            gi.sort_ts as "sort_ts",
            gi.sort_key as "sort_key",
            NULL::boolean as "is_completed",
            NULL::timestamptz as "deleted_at",
            jsonb_build_object(
                'id', event.id,
                'ownerId', event.owner_id,
                'icalUid', event.ical_uid,
                'title', event.title,
                'description', event.description,
                'location', event.location,
                'status', event.status,
                'visibility', event.visibility,
                'transparency', event.transparency,
                'time', CASE
                    WHEN event.starts_at IS NOT NULL THEN jsonb_build_object(
                        'kind', 'timed',
                        'startsAt', event.starts_at,
                        'endsAt', event.ends_at,
                        'timeZone', event.time_zone
                    )
                    ELSE jsonb_build_object(
                        'kind', 'allDay',
                        'startDate', event.start_date,
                        'endDate', event.end_date
                    )
                END,
                'organizerEmail', event.organizer_email,
                'organizerName', event.organizer_name,
                'conferenceUrl', event.conference_url,
                'conferenceProvider', event.conference_provider,
                'isReadOnly', event.is_read_only,
                'createdAt', event.created_at,
                'updatedAt', event.updated_at,
                'lastReminderFiredAt', event.last_reminder_fired_at,
                'extra', NULL
            ) as "calendar_event",
            NULL::jsonb as "database_row",
            gi.group_key as "group_key",
            gi.group_order as "group_order",
            gi.group_total_count as "group_total_count",
            gi.row_in_group as "row_in_group"
        FROM GroupedItems gi
        INNER JOIN calendar_events event ON event.id::text = gi.id
        WHERE gi.item_type = 'calendar_event'
"#;

static GROUPED_EMPTY_COMBINED_CLAUSE: &str = r#"
        SELECT
            'document' as "item_type",
            NULL::text as "id",
            NULL::text as "document_version_id",
            NULL::text as "user_id",
            NULL::text as "name",
            NULL::text as "branched_from_id",
            NULL::bigint as "branched_from_version_id",
            NULL::bigint as "document_family_id",
            NULL::text as "file_type",
            NULL::timestamptz as "created_at",
            NULL::timestamptz as "updated_at",
            NULL::text as "project_id",
            NULL::boolean as "is_persistent",
            NULL::text as "model",
            NULL::text as "sha",
            NULL::document_sub_type_value as "sub_type",
            false as "is_email_attachment",
            true as "is_important",
            ARRAY[]::uuid[] as "status_option_ids",
            NULL::timestamptz as "viewed_at",
            NULL::timestamptz as "sort_ts",
            0::double precision as "sort_key",
            NULL::boolean as "is_completed",
            NULL::timestamptz as "deleted_at",
            NULL::jsonb as "calendar_event",
            NULL::jsonb as "database_row",
            NULL::text as "group_key",
            NULL::double precision as "group_order",
            NULL::bigint as "group_total_count",
            NULL::bigint as "row_in_group"
        WHERE false
"#;

pub(in crate::outbound::pg_soup_repo) fn build_notification_exists_clause(
    entity_id_sql: &str,
    entity_type: &str,
    predicate_sql: &str,
) -> String {
    format!(
        r#"EXISTS (
            SELECT 1
            FROM notification n
            JOIN user_notification un ON un.notification_id = n.id
            WHERE un.user_id = $1
              AND un.deleted_at IS NULL
              AND n.event_item_type = '{entity_type}'
              AND n.event_item_id = ({entity_id_sql})::text
              AND {predicate_sql}
        )"#
    )
}

pub(in crate::outbound::pg_soup_repo) fn build_notification_state_clause(
    entity_id_sql: &str,
    entity_type: &str,
    state: item_filters::NotificationState,
) -> String {
    build_notification_exists_clause(
        entity_id_sql,
        entity_type,
        NotificationPredicate::state(state).sql(),
    )
}

// A union of exact states for one EXISTS predicate. Do not intersect these on
// AND: separate literals may be witnessed by different notification rows. A
// conjunction folds only when one side implies the other, since the narrower
// side's witness row satisfies both.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::outbound::pg_soup_repo) struct NotificationPredicate(u8);

impl NotificationPredicate {
    pub(in crate::outbound::pg_soup_repo) fn state(state: item_filters::NotificationState) -> Self {
        use item_filters::NotificationState::*;
        Self(match state {
            Unseen => 1,
            Seen => 2,
            Done => 4,
        })
    }

    /// Whether every row matching `self` also matches `other`.
    fn implies(self, other: Self) -> bool {
        self.0 & other.0 == self.0
    }

    pub(in crate::outbound::pg_soup_repo) fn sql(self) -> &'static str {
        match self.0 {
            1 => "un.state = 'unseen'",
            2 => "un.state = 'seen'",
            3 => "un.state IN ('unseen', 'seen')",
            4 => "un.state = 'done'",
            5 => "un.state IN ('unseen', 'done')",
            6 => "un.state IN ('seen', 'done')",
            7 => "TRUE",
            _ => "FALSE",
        }
    }
}

fn expr_contains_notification<T>(
    expr: &Expr<T>,
    notification_predicate: impl Fn(&T) -> Option<NotificationPredicate> + Copy,
) -> bool {
    match expr {
        Expr::And(a, b) | Expr::Or(a, b) => {
            expr_contains_notification(a, notification_predicate)
                || expr_contains_notification(b, notification_predicate)
        }
        Expr::Not(a) => expr_contains_notification(a, notification_predicate),
        Expr::Literal(lit) => notification_predicate(lit).is_some(),
    }
}

fn clone_expr<T: Clone>(expr: &Expr<T>) -> Expr<T> {
    match expr {
        Expr::And(a, b) => Expr::and(clone_expr(a), clone_expr(b)),
        Expr::Or(a, b) => Expr::or(clone_expr(a), clone_expr(b)),
        Expr::Not(a) => Expr::is_not(clone_expr(a)),
        Expr::Literal(lit) => Expr::val(lit.clone()),
    }
}

/// Push NOT down to the literals (De Morgan), eliminating double negation.
/// Valid under SQL three-valued logic (De Morgan holds in Kleene logic), so
/// this never changes which rows match. The payoff is plan quality: Postgres
/// only turns *syntactically* top-level `NOT EXISTS (...)` conjuncts into
/// anti-joins — a `NOT (EXISTS a OR EXISTS b)` stays a per-row SubPlan
/// filter, which both costs more and wrecks the row estimates that
/// join-order choice depends on.
fn push_not_inward<T: Clone>(expr: &Expr<T>, negated: bool) -> Expr<T> {
    match expr {
        Expr::Not(inner) => push_not_inward(inner, !negated),
        Expr::And(a, b) => {
            let (a, b) = (push_not_inward(a, negated), push_not_inward(b, negated));
            if negated {
                Expr::or(a, b)
            } else {
                Expr::and(a, b)
            }
        }
        Expr::Or(a, b) => {
            let (a, b) = (push_not_inward(a, negated), push_not_inward(b, negated));
            if negated {
                Expr::and(a, b)
            } else {
                Expr::or(a, b)
            }
        }
        Expr::Literal(lit) => {
            let lit = Expr::val(lit.clone());
            if negated { Expr::is_not(lit) } else { lit }
        }
    }
}

/// Splits one notification-state predicate off the top-level conjunction of
/// `expr`, returning it with whatever remains of the tree, so the query can
/// drive from the viewer's notifications instead of probing them per row.
/// `None` when the tree has no such conjunct that is equivalent to the whole
/// tree's notification constraint (NOT, a notification literal under OR with
/// other predicates, or two conjuncts neither of which implies the other).
pub(in crate::outbound::pg_soup_repo) fn strip_notification_conjunction<T: Clone>(
    expr: &Expr<T>,
    notification_predicate: impl Fn(&T) -> Option<NotificationPredicate> + Copy,
) -> Option<(NotificationPredicate, Option<Expr<T>>)> {
    match expr {
        Expr::Literal(lit) => notification_predicate(lit).map(|pred| (pred, None)),
        Expr::And(a, b) => {
            let left = strip_notification_conjunction(a, notification_predicate);
            let right = strip_notification_conjunction(b, notification_predicate);
            match (left, right) {
                (Some((left_pred, left_expr)), Some((right_pred, right_expr)))
                    if left_pred.implies(right_pred) || right_pred.implies(left_pred) =>
                {
                    // The client's unread feed asks for `(unseen OR seen) AND
                    // unseen`: the narrower side's witness row is also one for
                    // the wider side, so only the narrower predicate remains.
                    let pred = if left_pred.implies(right_pred) {
                        left_pred
                    } else {
                        right_pred
                    };
                    let stripped = match (left_expr, right_expr) {
                        (Some(left), Some(right)) => Some(Expr::and(left, right)),
                        (Some(expr), None) | (None, Some(expr)) => Some(expr),
                        (None, None) => None,
                    };
                    Some((pred, stripped))
                }
                (Some((pred, left_expr)), None)
                    if !expr_contains_notification(b, notification_predicate) =>
                {
                    let right_expr = clone_expr(b);
                    Some((
                        pred,
                        Some(match left_expr {
                            Some(left_expr) => Expr::and(left_expr, right_expr),
                            None => right_expr,
                        }),
                    ))
                }
                (None, Some((pred, right_expr)))
                    if !expr_contains_notification(a, notification_predicate) =>
                {
                    let left_expr = clone_expr(a);
                    Some((
                        pred,
                        Some(match right_expr {
                            Some(right_expr) => Expr::and(left_expr, right_expr),
                            None => left_expr,
                        }),
                    ))
                }
                _ => None,
            }
        }
        Expr::Or(a, b) => {
            match (
                strip_notification_conjunction(a, notification_predicate),
                strip_notification_conjunction(b, notification_predicate),
            ) {
                (Some((a, None)), Some((b, None))) => {
                    Some((NotificationPredicate(a.0 | b.0), None))
                }
                _ => None,
            }
        }
        Expr::Not(_) => None,
    }
}

fn build_notification_items_cte(
    builder: &mut QueryBuilder<'_, Postgres>,
    predicate: NotificationPredicate,
    item_types: &[&str],
) {
    if item_types.is_empty() {
        return;
    }
    let item_types = item_types
        .iter()
        .map(|item_type| format!("'{item_type}'"))
        .collect::<Vec<_>>()
        .join(", ");
    // NOT MATERIALIZED: the set is small (a user's active notifications) and
    // inlining it lets estimates flow into the arm joins, so the planner can
    // drive an arm from the notification side instead of nested-looping the
    // full accessible corpus against it (the materialized fence defaulted the
    // CTE estimate to ~1 row, which picked a join-filter nested loop that
    // compared every accessible item against every notification).
    builder.push(format!(
        r#"NotificationItems AS NOT MATERIALIZED (
        SELECT DISTINCT n.event_item_type, n.event_item_id
        FROM user_notification un
        JOIN notification n ON n.id = un.notification_id
        WHERE un.user_id = $1
          AND un.deleted_at IS NULL
          AND {}
          AND n.event_item_type IN ({item_types})
    ),
"#,
        predicate.sql()
    ));
}

fn build_notification_join(entity_alias: &str, item_type: &str) -> String {
    format!(
        r#"                INNER JOIN NotificationItems ni_{item_type}
                    ON ni_{item_type}.event_item_type = '{item_type}'
                    AND ni_{item_type}.event_item_id = {entity_alias}.id::text
"#
    )
}

fn build_task_include_cbm_atm_nc_clause() -> String {
    // The completed-status option id is interpolated (not `$6`-bound) so this
    // clause stays usable from queries with a different parameter layout —
    // the touched-by-me page reuses these folds with its own bind positions.
    format!(
        r#"(
        dt.sub_type = 'task'
        AND d.owner = $1
        AND ep_assignees.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', $1))
        AND NOT COALESCE(ep_status.values->'value' ? '{completed}', false)
    )"#,
        completed = StatusOption::COMPLETED_UUID
    )
}

/// NULL-safe equality for nullable columns: FALSE (not UNKNOWN) on NULL, so
/// `NOT` includes NULL rows — e.g. root projects under a negated `pid` filter.
fn nullable_eq(col: &str, val: impl std::fmt::Display) -> String {
    format!("({col} = '{val}' AND {col} IS NOT NULL)")
}

fn date_predicate(col: &str, lit: &DateLiteral) -> String {
    match lit {
        DateLiteral::GreaterThan(dt) => format!("{col} > '{}'::timestamptz", dt.to_rfc3339()),
        DateLiteral::LessThan(dt) => format!("{col} < '{}'::timestamptz", dt.to_rfc3339()),
        DateLiteral::GreaterThanOrEqual(dt) => {
            format!("{col} >= '{}'::timestamptz", dt.to_rfc3339())
        }
        DateLiteral::LessThanOrEqual(dt) => format!("{col} <= '{}'::timestamptz", dt.to_rfc3339()),
    }
}

/// Renders the document filter as ` AND ...` against the `d` alias.
/// `id_arrays` binds its runs of document ids as arrays (see
/// [`document_ids`]); without it every id renders inline, for queries with no
/// free parameter slots.
pub(in crate::outbound::pg_soup_repo) fn build_document_filter(
    ast: Option<&Expr<DocumentLiteral>>,
    id_arrays: Option<&mut DocumentIdArrays>,
) -> String {
    let Some(expr) = ast else {
        return String::new();
    };
    let expr = push_not_inward(expr, false);
    let formatting = match id_arrays {
        Some(id_arrays) => document_ids::render(&expr, id_arrays, document_literal_sql),
        None => expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) => format!("({a} AND {b})"),
            filter_ast::ExprFrame::Or(a, b) => format!("({a} OR {b})"),
            filter_ast::ExprFrame::Not(a) => format!("(NOT {a})"),
            filter_ast::ExprFrame::Literal(literal) => document_literal_sql(literal),
        }),
    };
    if formatting.is_empty() {
        String::new()
    } else {
        format!(" AND {}", formatting)
    }
}

fn document_literal_sql(literal: DocumentLiteral) -> String {
    match literal {
        DocumentLiteral::FileType(f) => {
            format!(r#"d."fileType" = '{f}'"#)
        }
        DocumentLiteral::Id(i) => format!("d.id = '{i}'"),
        DocumentLiteral::ProjectId(p) => nullable_eq(r#"d."projectId""#, p),
        DocumentLiteral::Owner(o) => {
            format!("d.owner = {}", sql_string_literal(&o.principal_id()))
        }
        DocumentLiteral::Importance(true) => {
            // "Important" documents: non-tasks OR tasks where user is an assignee
            r#"(
                dt.sub_type IS NULL
                OR dt.sub_type != 'task'
                OR ep_assignees.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', $1))
            )"#
                .to_string()
        }
        // DocumentLiteral::Importance(false) => String::new()
        DocumentLiteral::Importance(false) => {
            // "Unimportant" documents: tasks where user is NOT an assignee
            r#"(
                dt.sub_type = 'task'
                -- special null handling for jsonb column
                AND (ep_assignees.values = 'null'
                OR NOT ep_assignees.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', $1)))
            )"#
                .to_string()
        }
        DocumentLiteral::NotificationState(state) => {
            build_notification_state_clause("d.id", "document", state)
        }
        DocumentLiteral::IncludeCbmAtmNc(true) => build_task_include_cbm_atm_nc_clause(),
        // false is equivalent to disabled/no-op. Rendered as TRUE (not an
        // empty string): an empty operand inside And/Or/Not produces invalid
        // SQL like `( AND x)` or `(NOT )`, and TRUE also gives NOT the right
        // meaning (a disabled predicate matches everything, its negation
        // nothing).
        DocumentLiteral::IncludeCbmAtmNc(false) => "TRUE".to_string(),
        // EXISTS instead of a predicate on the LEFT-JOINed `dt` alias: this
        // keeps the top clause free of the join when only SubType literals
        // are present, and `NOT (EXISTS ...)` becomes a plannable anti-join
        // (the old `(dt.sub_type IS NOT NULL AND ...)` OR-shape collapsed row
        // estimates to ~1 on broad arms, picking pathological nested loops).
        DocumentLiteral::SubType(st) => {
            format!(
                "EXISTS (SELECT 1 FROM document_sub_type dst_f WHERE dst_f.document_id = d.id AND dst_f.sub_type = '{st}')"
            )
        }
        DocumentLiteral::IsEmailAttachment(true) => {
            r#"EXISTS(SELECT 1 FROM document_email WHERE document_id = d.id)"#.to_string()
        }
        DocumentLiteral::IsEmailAttachment(false) => {
            r#"NOT EXISTS(SELECT 1 FROM document_email WHERE document_id = d.id)"#.to_string()
        }
        DocumentLiteral::CreatedAt(lit) => date_predicate(r#"d."createdAt""#, &lit),
        DocumentLiteral::UpdatedAt(lit) => date_predicate(r#"d."updatedAt""#, &lit),
        DocumentLiteral::Property(lit) => property_literal_predicate(&lit, "d.id"),
        DocumentLiteral::EmailAttachmentParticipant(email) => {
            email_attachment_participant_predicate(&email, "d.id")
        }
        DocumentLiteral::NameContains(name) => format!(
            "STRPOS(LOWER(d.name), {}) > 0",
            sql_string_literal(&name.to_lowercase())
        ),
        // The Soup domain replaces CRM associations with the associated
        // document ids before querying, so an unresolved one matches nothing.
        DocumentLiteral::CrmAssociated(_) => "FALSE".to_string(),
    }
}

/// Matches documents uploaded from an email attachment whose message was sent
/// by, or addressed to, an address matching `email`.
fn email_attachment_participant_predicate(email: &Email, document_id_sql: &str) -> String {
    let address_predicate = match email {
        Email::Complete(address) => format!(
            "LOWER(c.email_address) = {}",
            sql_string_literal(&address.0.as_ref().to_lowercase())
        ),
        Email::Domain(domain) => format!(
            "LOWER(SPLIT_PART(c.email_address, '@', 2)) = {}",
            sql_string_literal(&domain.to_lowercase())
        ),
        Email::Partial(fragment) => format!(
            "STRPOS(LOWER(c.email_address), {}) > 0",
            sql_string_literal(&fragment.to_lowercase())
        ),
    };
    format!(
        r#"EXISTS (
            SELECT 1
            FROM document_email de
            JOIN email_attachments ea ON ea.id = de.email_attachment_id
            JOIN email_messages m ON m.id = ea.message_id
            JOIN LATERAL (
                SELECT m.from_contact_id AS contact_id
                UNION ALL
                SELECT r.contact_id FROM email_message_recipients r WHERE r.message_id = m.id
            ) participant ON TRUE
            JOIN email_contacts c ON c.id = participant.contact_id
            WHERE de.document_id = {document_id_sql}
            AND {address_predicate}
        )"#
    )
}

/// A single-quoted SQL string literal with embedded quotes doubled. Used for
/// every caller-supplied string these bind-free builders inline: the calendar
/// filter's statuses and attendee/organizer emails, and the owner principals,
/// none of which are constrained to the shape of the uuids the sibling
/// builders inline.
fn sql_string_literal(value: &str) -> String {
    format!("'{}'", value.replace('\'', "''"))
}

/// Renders the calendar-event filter bind-free: in the hand-numbered grouped
/// query, `push_bind` placeholders would start at `$1` and collide with the
/// positionally-bound user id. The notification clauses reference `$1` as the
/// requesting user, which both this query and `cursor_soup` guarantee.
fn build_calendar_event_filter(ast: Option<&Expr<CalendarEventLiteral>>) -> String {
    let Some(expr) = ast else {
        return String::new();
    };
    let expr = push_not_inward(expr, false);
    let formatting = expr.collapse_frames(|frame| match frame {
        filter_ast::ExprFrame::And(a, b) => format!("({a} AND {b})"),
        filter_ast::ExprFrame::Or(a, b) => format!("({a} OR {b})"),
        filter_ast::ExprFrame::Not(a) => format!("(NOT {a})"),
        filter_ast::ExprFrame::Literal(CalendarEventLiteral::Id(id)) => {
            format!("event.id = '{id}'")
        }
        filter_ast::ExprFrame::Literal(CalendarEventLiteral::Status(status)) => {
            format!("event.status = {}", sql_string_literal(&status))
        }
        filter_ast::ExprFrame::Literal(CalendarEventLiteral::StartsBefore(value)) => {
            format!(
                "COALESCE(event.starts_at, event.start_date::timestamp AT TIME ZONE 'UTC') < '{}'::timestamptz",
                value.to_rfc3339()
            )
        }
        filter_ast::ExprFrame::Literal(CalendarEventLiteral::EndsAfter(value)) => {
            format!(
                "COALESCE(event.ends_at, event.end_date::timestamp AT TIME ZONE 'UTC') > '{}'::timestamptz",
                value.to_rfc3339()
            )
        }
        filter_ast::ExprFrame::Literal(CalendarEventLiteral::Attendee(email)) => {
            format!(
                "EXISTS (SELECT 1 FROM calendar_event_attendees attendee \
                 WHERE attendee.event_id = event.id AND attendee.email = lower({}))",
                sql_string_literal(&email)
            )
        }
        filter_ast::ExprFrame::Literal(CalendarEventLiteral::Organizer(email)) => {
            format!(
                "lower(event.organizer_email) = lower({})",
                sql_string_literal(&email)
            )
        }
        filter_ast::ExprFrame::Literal(CalendarEventLiteral::NotificationState(state)) => {
            build_notification_state_clause("event.id", "calendar_event", state)
        }
    });
    if formatting.is_empty() {
        String::new()
    } else {
        format!(" AND {}", formatting)
    }
}

pub(in crate::outbound::pg_soup_repo) fn build_chat_filter(
    ast: Option<&Expr<ChatLiteral>>,
) -> String {
    let Some(expr) = ast else {
        return String::new();
    };
    let expr = push_not_inward(expr, false);
    let formatting = expr.collapse_frames(|frame| match frame {
        filter_ast::ExprFrame::And(a, b) => format!("({a} AND {b})"),
        filter_ast::ExprFrame::Or(a, b) => format!("({a} OR {b})"),
        filter_ast::ExprFrame::Not(a) => format!("(NOT {a})"),
        filter_ast::ExprFrame::Literal(ChatLiteral::ProjectId(p)) => {
            nullable_eq(r#"c."projectId""#, p)
        }
        // todo? I'm not sure what a chat role filter looks like.
        // No-op literals render as TRUE, not an empty string — an empty
        // operand inside And/Or/Not produces invalid SQL like `( AND x)`.
        filter_ast::ExprFrame::Literal(ChatLiteral::Role(_r)) => "TRUE".to_string(),
        filter_ast::ExprFrame::Literal(ChatLiteral::ChatId(i)) => format!("c.id = '{i}'"),
        filter_ast::ExprFrame::Literal(ChatLiteral::Owner(o)) => {
            format!(r#"c."userId" = {}"#, sql_string_literal(&o.principal_id()))
        }
        // all chats are important
        filter_ast::ExprFrame::Literal(ChatLiteral::Importance(true)) => "TRUE".to_string(),
        // all chats are important, so if importance is false, exclude them
        filter_ast::ExprFrame::Literal(ChatLiteral::Importance(false)) => "1=0".to_string(),
        filter_ast::ExprFrame::Literal(ChatLiteral::NotificationState(state)) => {
            build_notification_state_clause("c.id", "chat", state)
        }
        filter_ast::ExprFrame::Literal(ChatLiteral::CreatedAt(lit)) => {
            date_predicate(r#"c."createdAt""#, &lit)
        }
        filter_ast::ExprFrame::Literal(ChatLiteral::UpdatedAt(lit)) => {
            date_predicate(r#"c."updatedAt""#, &lit)
        }
    });
    if formatting.is_empty() {
        String::new()
    } else {
        format!(" AND {}", formatting)
    }
}

pub(in crate::outbound::pg_soup_repo) fn build_project_filter(
    ast: Option<&Expr<ProjectLiteral>>,
) -> String {
    let Some(expr) = ast else {
        return String::new();
    };
    let expr = push_not_inward(expr, false);
    let formatting = expr.collapse_frames(|frame| match frame {
        filter_ast::ExprFrame::And(a, b) => format!("({a} AND {b})"),
        filter_ast::ExprFrame::Or(a, b) => format!("({a} OR {b})"),
        filter_ast::ExprFrame::Not(a) => format!("(NOT {a})"),
        filter_ast::ExprFrame::Literal(ProjectLiteral::ProjectId(p)) => {
            nullable_eq(r#"p."parentId""#, p)
        }
        filter_ast::ExprFrame::Literal(ProjectLiteral::ProjectIdSelf(p)) => {
            format!(r#"p.id = '{p}'"#)
        }
        filter_ast::ExprFrame::Literal(ProjectLiteral::Owner(o)) => {
            format!(r#"p."userId" = {}"#, sql_string_literal(&o.principal_id()))
        }
        // all projects are important; TRUE (not empty) keeps the literal
        // valid SQL inside And/Or/Not.
        filter_ast::ExprFrame::Literal(ProjectLiteral::Importance(true)) => "TRUE".to_string(),
        // all projects are important, so if importance is false, exclude them
        filter_ast::ExprFrame::Literal(ProjectLiteral::Importance(false)) => "1=0".to_string(),
        filter_ast::ExprFrame::Literal(ProjectLiteral::NotificationState(state)) => {
            build_notification_state_clause("p.id", "project", state)
        }
        filter_ast::ExprFrame::Literal(ProjectLiteral::CreatedAt(lit)) => {
            date_predicate(r#"p."createdAt""#, &lit)
        }
        filter_ast::ExprFrame::Literal(ProjectLiteral::UpdatedAt(lit)) => {
            date_predicate(r#"p."updatedAt""#, &lit)
        }
    });
    if formatting.is_empty() {
        String::new()
    } else {
        format!(" AND {}", formatting)
    }
}

pub(in crate::outbound::pg_soup_repo) fn build_properties_filter(
    ast: Option<&Expr<PropertiesLiteral>>,
    entity_id_sql: &str,
) -> String {
    let Some(expr) = ast else {
        return String::new();
    };
    let formatting = property_filter::predicate(expr, entity_id_sql);
    if formatting.is_empty() {
        String::new()
    } else {
        format!(" AND {}", formatting)
    }
}

/// An EXISTS check that the entity identified by `entity_id_sql` has a
/// property value matching `literal`.
fn property_literal_predicate(literal: &PropertiesLiteral, entity_id_sql: &str) -> String {
    let PropertiesLiteral {
        property_definition_id,
        entity_type,
        value,
    } = literal;
    let value_predicate = match value {
        PropertyMatchValue::SelectOption(option_id) => {
            format!("ep_prop.values->'value' ? '{option_id}'")
        }
        PropertyMatchValue::EntityRef(entity_id) => {
            format!(
                "ep_prop.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', '{entity_id}'))"
            )
        }
        PropertyMatchValue::DateRange(range) => {
            let date_value = property_date_expression("ep_prop");
            let mut predicates = vec![format!("{date_value} IS NOT NULL")];
            if let Some(start) = range.start {
                predicates.push(format!(
                    "{date_value} >= '{}'::timestamptz",
                    start.to_rfc3339()
                ));
            }
            if let Some(end) = range.end {
                predicates.push(format!(
                    "{date_value} < '{}'::timestamptz",
                    end.to_rfc3339()
                ));
            }
            predicates.join(" AND ")
        }
    };
    let entity_type_clause = match entity_type {
        Some(et) => format!("AND ep_prop.entity_type = '{et}'"),
        None => String::new(),
    };
    format!(
        r#"EXISTS (
            SELECT 1 FROM entity_properties ep_prop
            WHERE ep_prop.entity_id = {entity_id_sql}
            {entity_type_clause}
            AND ep_prop.property_definition_id = '{property_definition_id}'
            AND {value_predicate}
        )"#
    )
}

/// Prove that every matching document belongs to the task property namespace.
fn document_filter_is_task_only(ast: Option<&Expr<DocumentLiteral>>) -> bool {
    ast.is_some_and(|expr| {
        expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) => a || b,
            filter_ast::ExprFrame::Or(a, b) => a && b,
            filter_ast::ExprFrame::Not(_) => false,
            filter_ast::ExprFrame::Literal(DocumentLiteral::SubType(DocumentSubType::Task)) => true,
            filter_ast::ExprFrame::Literal(_) => false,
        })
    })
}

pub(in crate::outbound::pg_soup_repo) fn document_filter_needs_task_property_joins(
    ast: Option<&Expr<DocumentLiteral>>,
) -> bool {
    ast.is_some_and(|expr| {
        expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) | filter_ast::ExprFrame::Or(a, b) => a || b,
            filter_ast::ExprFrame::Not(a) => a,
            filter_ast::ExprFrame::Literal(DocumentLiteral::Importance(_))
            | filter_ast::ExprFrame::Literal(DocumentLiteral::IncludeCbmAtmNc(true)) => true,
            filter_ast::ExprFrame::Literal(_) => false,
        })
    })
}

pub(in crate::outbound::pg_soup_repo) fn properties_filter_can_apply_to(
    ast: Option<&Expr<PropertiesLiteral>>,
    entity_types: &[PropertyEntityType],
) -> bool {
    ast.is_none_or(|expr| {
        expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) => a && b,
            filter_ast::ExprFrame::Or(a, b) => a || b,
            // Be conservative: NOT can turn an inapplicable predicate into a match.
            filter_ast::ExprFrame::Not(_) => true,
            filter_ast::ExprFrame::Literal(PropertiesLiteral { entity_type, .. }) => {
                entity_type.is_none_or(|entity_type| entity_types.contains(&entity_type))
            }
        })
    })
}

pub(in crate::outbound::pg_soup_repo) fn chat_filter_is_impossible(
    ast: Option<&Expr<ChatLiteral>>,
) -> bool {
    ast.is_some_and(|expr| {
        expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) => a || b,
            filter_ast::ExprFrame::Or(a, b) => a && b,
            filter_ast::ExprFrame::Not(_) => false,
            filter_ast::ExprFrame::Literal(ChatLiteral::ChatId(id)) => id.is_nil(),
            filter_ast::ExprFrame::Literal(ChatLiteral::Importance(false)) => true,
            filter_ast::ExprFrame::Literal(_) => false,
        })
    })
}

pub(in crate::outbound::pg_soup_repo) fn document_filter_is_impossible(
    ast: Option<&Expr<DocumentLiteral>>,
) -> bool {
    ast.is_some_and(|expr| {
        expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) => a || b,
            filter_ast::ExprFrame::Or(a, b) => a && b,
            filter_ast::ExprFrame::Not(_) => false,
            filter_ast::ExprFrame::Literal(DocumentLiteral::Id(id)) => id.is_nil(),
            filter_ast::ExprFrame::Literal(_) => false,
        })
    })
}

pub(in crate::outbound::pg_soup_repo) fn project_filter_is_impossible(
    ast: Option<&Expr<ProjectLiteral>>,
) -> bool {
    ast.is_some_and(|expr| {
        expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) => a || b,
            filter_ast::ExprFrame::Or(a, b) => a && b,
            filter_ast::ExprFrame::Not(_) => false,
            filter_ast::ExprFrame::Literal(ProjectLiteral::ProjectIdSelf(id)) => id.is_nil(),
            filter_ast::ExprFrame::Literal(ProjectLiteral::Importance(false)) => true,
            filter_ast::ExprFrame::Literal(_) => false,
        })
    })
}

pub(in crate::outbound::pg_soup_repo) fn calendar_event_filter_is_impossible(
    ast: Option<&Expr<CalendarEventLiteral>>,
) -> bool {
    ast.is_some_and(|expr| {
        expr.collapse_frames(|frame| match frame {
            filter_ast::ExprFrame::And(a, b) => a || b,
            filter_ast::ExprFrame::Or(a, b) => a && b,
            filter_ast::ExprFrame::Not(_) => false,
            filter_ast::ExprFrame::Literal(CalendarEventLiteral::Id(id)) => id.is_nil(),
            filter_ast::ExprFrame::Literal(_) => false,
        })
    })
}

fn push_union_separator(builder: &mut QueryBuilder<'_, Postgres>, needs_separator: &mut bool) {
    if *needs_separator {
        builder.push(" UNION ALL ");
    }
    *needs_separator = true;
}

fn top_sort_expr(alias: &str, sort_method: SimpleSortMethod) -> String {
    match sort_method {
        SimpleSortMethod::ViewedAt => {
            r#"COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')"#.to_string()
        }
        SimpleSortMethod::ViewedUpdated => {
            format!(r#"COALESCE(uh."updatedAt", {alias}."updatedAt")"#)
        }
        SimpleSortMethod::CreatedAt => format!(r#"{alias}."createdAt""#),
        SimpleSortMethod::UpdatedAt => format!(r#"{alias}."updatedAt""#),
    }
}

fn top_needs_user_history(sort_method: SimpleSortMethod) -> bool {
    matches!(
        sort_method,
        SimpleSortMethod::ViewedAt | SimpleSortMethod::ViewedUpdated
    )
}

/// Per-arm access gate. Semantically identical to the old
/// `AccessibleItems AS MATERIALIZED` CTE + `INNER JOIN` (an item is visible
/// iff at least one `entity_access` row matches one of the user's sources;
/// IN dedupes exactly like the CTE's DISTINCT did), but expressed as a
/// flattenable semi-join so the planner can pick a direction per arm:
/// hash the user's accessible set when the arm is broad, or probe
/// `entity_access` per candidate row (via
/// `idx_entity_access_entity_text_type_source`) when the arm's own filters
/// are selective. The materialized form pinned the worst plan — the whole
/// corpus was computed and probed against the item table on every page.
pub(in crate::outbound::pg_soup_repo) fn access_semi_join(
    id_sql: &str,
    entity_type: &str,
) -> String {
    format!(
        r#"{id_sql} IN (
                    SELECT ea.entity_id::text
                    FROM entity_access ea
                    JOIN user_source_ids us ON us.source_id = ea.source_id
                    WHERE ea.entity_type = '{entity_type}'
                )"#
    )
}

/// [`access_semi_join`] over the caller's source ids, bound as a constant
/// array at parameter `source_ids`. Postgres costs the `user_source_ids` join at the average
/// source's grant count, so it planned callers holding hundreds of thousands
/// of grants with one item probe per grant; it estimates the array from
/// `entity_access.source_id` statistics and hashes the grants instead.
fn bound_access_semi_join(id_sql: &str, entity_type: &str, source_ids: usize) -> String {
    format!(
        r#"{id_sql} IN (
                    SELECT ea.entity_id::text
                    FROM entity_access ea
                    WHERE ea.source_id = ANY(${source_ids}::text[])
                        AND ea.entity_type = '{entity_type}'
                )"#
    )
}

fn document_top_where_clause(sort_method: SimpleSortMethod, source_ids: usize) -> String {
    let user_history_join = if top_needs_user_history(sort_method) {
        r#"                LEFT JOIN "UserHistory" uh ON uh."itemId" = d.id AND uh."itemType" = 'document' AND uh."userId" = $1
"#
    } else {
        ""
    };
    format!(
        r#"{user_history_join}                WHERE d."deletedAt" IS NULL
                AND {}
"#,
        bound_access_semi_join("d.id", "document", source_ids)
    )
}

fn chat_top_where_clause(source_ids: usize) -> String {
    format!(
        r#"                WHERE c."deletedAt" IS NULL
                AND {}
"#,
        bound_access_semi_join("c.id", "chat", source_ids)
    )
}

fn project_top_where_clause(source_ids: usize) -> String {
    format!(
        r#"                WHERE p."deletedAt" IS NULL
                AND {}
"#,
        bound_access_semi_join("p.id", "project", source_ids)
    )
}

fn build_query<'a>(
    filter_ast: &'a EntityFilterAst,
    exclude_frecency: bool,
    sort: DynamicSort,
    document_ids: &mut DocumentIdArrays,
) -> QueryBuilder<'a, Postgres> {
    let sort_method = sort.timestamp_method();
    let source_ids = source_ids_slot(sort, false);
    let property_sort = matches!(sort, DynamicSort::Property { .. });
    let mut builder = sqlx::QueryBuilder::new(PREFIX);

    let include_initiatives = initiative_opted_in(filter_ast.initiative_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Initiative],
        );
    let include_database_rows = database_row_opted_in(filter_ast.database_row_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::DatabaseRow],
        );
    let include_documents = !document_filter_is_impossible(filter_ast.document_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Document, PropertyEntityType::Task],
        );
    let include_chats = !chat_filter_is_impossible(filter_ast.chat_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Chat],
        );
    let include_projects = !project_filter_is_impossible(filter_ast.project_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Project],
        );

    let document_notification = filter_ast.document_filter.as_deref().and_then(|expr| {
        strip_notification_conjunction(expr, |lit| match lit {
            DocumentLiteral::NotificationState(state) => Some(NotificationPredicate::state(*state)),
            _ => None,
        })
    });
    let chat_notification = filter_ast.chat_filter.as_deref().and_then(|expr| {
        strip_notification_conjunction(expr, |lit| match lit {
            ChatLiteral::NotificationState(state) => Some(NotificationPredicate::state(*state)),
            _ => None,
        })
    });
    let project_notification = filter_ast.project_filter.as_deref().and_then(|expr| {
        strip_notification_conjunction(expr, |lit| match lit {
            ProjectLiteral::NotificationState(state) => Some(NotificationPredicate::state(*state)),
            _ => None,
        })
    });

    let notification_predicates = [
        document_notification.as_ref().map(|(pred, _)| *pred),
        chat_notification.as_ref().map(|(pred, _)| *pred),
        project_notification.as_ref().map(|(pred, _)| *pred),
    ];
    let optimized_notification_predicate = notification_predicates
        .into_iter()
        .flatten()
        .try_fold(None, |acc, pred| match acc {
            Some(acc) if acc != pred => None,
            Some(acc) => Some(Some(acc)),
            None => Some(Some(pred)),
        })
        .flatten();

    let document_filter =
        if optimized_notification_predicate.is_some() && document_notification.is_some() {
            document_notification
                .as_ref()
                .and_then(|(_, expr)| expr.as_ref())
        } else {
            filter_ast.document_filter.as_deref()
        };
    let chat_filter = if optimized_notification_predicate.is_some() && chat_notification.is_some() {
        chat_notification
            .as_ref()
            .and_then(|(_, expr)| expr.as_ref())
    } else {
        filter_ast.chat_filter.as_deref()
    };
    let project_filter =
        if optimized_notification_predicate.is_some() && project_notification.is_some() {
            project_notification
                .as_ref()
                .and_then(|(_, expr)| expr.as_ref())
        } else {
            filter_ast.project_filter.as_deref()
        };

    if let Some(predicate) = optimized_notification_predicate {
        let mut item_types = Vec::with_capacity(3);
        if include_documents && document_notification.is_some() {
            item_types.push("document");
        }
        if include_chats && chat_notification.is_some() {
            item_types.push("chat");
        }
        if include_projects && project_notification.is_some() {
            item_types.push("project");
        }
        build_notification_items_cte(&mut builder, predicate, &item_types);
    }

    // TopItems CTE: lightweight id + sort_ts with filters, cursor, and limit
    builder.push("TopItems AS (");
    builder.push("SELECT all_items.item_type, all_items.id, all_items.sort_ts, ");
    builder.push(sort_key_expression(property_sort));
    builder.push(" AS sort_key FROM (");

    let mut needs_separator = false;

    if include_documents {
        push_union_separator(&mut builder, &mut needs_separator);
        // Subtype is needed only for task-property filters or property-sort namespaces.
        let needs_task_property_joins = document_filter_needs_task_property_joins(document_filter);
        builder.push(document_top_clause(
            sort_method,
            needs_task_property_joins,
            property_sort,
            document_filter_is_task_only(document_filter),
        ));
        if optimized_notification_predicate.is_some() && document_notification.is_some() {
            builder.push(build_notification_join("d", "document"));
        }
        if needs_task_property_joins {
            builder.push(DOCUMENT_TASK_PROPERTY_JOINS);
        }
        builder.push(document_top_where_clause(sort_method, source_ids));
        builder.push(build_document_filter(document_filter, Some(document_ids)));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "d.id",
        ));
    }

    if include_chats {
        push_union_separator(&mut builder, &mut needs_separator);
        // Chat top clause (lightweight)
        builder.push(chat_top_clause(sort_method, property_sort));
        if optimized_notification_predicate.is_some() && chat_notification.is_some() {
            builder.push(build_notification_join("c", "chat"));
        }
        builder.push(chat_top_where_clause(source_ids));
        builder.push(build_chat_filter(chat_filter));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "c.id",
        ));
    }

    if include_projects {
        push_union_separator(&mut builder, &mut needs_separator);
        // Project top clause (lightweight)
        builder.push(project_top_clause(sort_method, property_sort));
        if optimized_notification_predicate.is_some() && project_notification.is_some() {
            builder.push(build_notification_join("p", "project"));
        }
        builder.push(project_top_where_clause(source_ids));
        builder.push(build_project_filter(project_filter));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "p.id",
        ));
    }

    if include_initiatives {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(initiative_top_clause(sort_method, false, property_sort));
        builder.push(build_initiative_filter(
            filter_ast.initiative_filter.as_deref(),
        ));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "i.id::text",
        ));
    }

    if include_database_rows {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(database_row_top_clause(sort_method, false, property_sort));
        builder.push(build_database_row_filter(
            filter_ast.database_row_filter.as_deref(),
        ));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "r.id::text",
        ));
    }

    if !needs_separator {
        builder.push(
            "SELECT 'document'::text as item_type, NULL::text as id, NULL::timestamptz as sort_ts",
        );
        if property_sort {
            builder.push(", NULL::property_entity_type as property_entity_type");
        }
        builder.push(" WHERE false");
    }

    builder.push(") all_items ");
    builder.push(property_sort_join(property_sort, 9));

    // Frecency exclusion join (only when exclude_frecency is true)
    if exclude_frecency {
        builder.push(
            r#"LEFT JOIN frecency_aggregates fa
                ON fa.entity_id = all_items.id
                AND fa.entity_type = all_items.item_type
                AND fa.user_id = $1
            WHERE fa.id IS NULL AND ("#,
        );
    } else {
        builder.push("WHERE ");
    }

    // Cursor condition
    if property_sort {
        builder.push(property_keyset(
            sort,
            "property_sort.key",
            10,
            "all_items.sort_ts",
            "all_items.id",
        ));
    } else {
        // Preserve the deployed timestamp predicate and its tie-breaking order.
        builder.push(
            "($4::timestamptz IS NULL) OR (all_items.sort_ts, all_items.id::text) < ($4, $5)",
        );
    }

    if exclude_frecency {
        builder.push(")");
    }

    if property_sort {
        builder.push(" ORDER BY ");
        builder.push(property_order(
            sort,
            "sort_key",
            "all_items.sort_ts",
            "all_items.id",
        ));
        builder.push(" LIMIT $3");
    } else {
        builder.push(" ORDER BY all_items.sort_ts DESC, all_items.id DESC LIMIT $3");
    }
    builder.push("), ");

    // Combined CTE: full detail joins back from TopItems. Keep this in sync
    // with the TopItems branches so task-only/property-specific filters do not
    // pay planning/execution cost for impossible entity types.
    builder.push("Combined AS (");

    let mut needs_separator = false;
    if include_documents {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(DOCUMENT_DETAIL_CLAUSE);
    }
    if include_chats {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(CHAT_DETAIL_CLAUSE);
    }
    if include_projects {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(PROJECT_DETAIL_CLAUSE);
    }
    if include_initiatives {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(INITIATIVE_DETAIL_CLAUSE);
    }
    if include_database_rows {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(DATABASE_ROW_DETAIL_CLAUSE);
    }
    if !needs_separator {
        builder.push(
            r#"SELECT
                'document' as "item_type",
                NULL::text as "id",
                NULL::text as "document_version_id",
                NULL::text as "user_id",
                NULL::text as "name",
                NULL::text as "branched_from_id",
                NULL::bigint as "branched_from_version_id",
                NULL::bigint as "document_family_id",
                NULL::text as "file_type",
                NULL::timestamptz as "created_at",
                NULL::timestamptz as "updated_at",
                NULL::text as "project_id",
                NULL::boolean as "is_persistent",
                NULL::text as "model",
                NULL::text as "sha",
                NULL::document_sub_type_value as "sub_type",
                false as "is_email_attachment",
                false as "is_important",
                ARRAY[]::uuid[] as "status_option_ids",
                NULL::timestamptz as "viewed_at",
                NULL::timestamptz as "sort_ts",
                0::double precision as "sort_key",
                NULL::boolean as "is_completed",
                NULL::timestamptz as "deleted_at",
                NULL::jsonb as "calendar_event",
                NULL::jsonb as "database_row"
            WHERE false"#,
        );
    }
    builder.push(") SELECT * FROM Combined ORDER BY ");
    builder.push(property_order(
        sort,
        "\"sort_key\"",
        "\"sort_ts\"",
        "\"id\"",
    ));
    builder.push(" LIMIT $3");

    builder
}

#[derive(Debug, FromRow)]
struct DocumentRow {
    id: String,
    user_id: String,
    document_version_id: String,
    name: String,
    sha: Option<String>,
    file_type: Option<String>,
    document_family_id: Option<i64>,
    branched_from_id: Option<String>,
    branched_from_version_id: Option<i64>,
    project_id: Option<String>,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    viewed_at: Option<DateTime<Utc>>,
    sub_type: Option<DocumentSubType>,
    #[sqlx(default)]
    is_email_attachment: bool,
    #[sqlx(default)]
    is_important: bool,
    #[sqlx(default)]
    status_option_ids: Vec<Uuid>,
    is_completed: Option<bool>,
    deleted_at: Option<DateTime<Utc>>,
}

#[derive(Debug, FromRow)]
struct ChatRow {
    id: String,
    user_id: String,
    name: String,
    model: Option<String>,
    project_id: Option<String>,
    #[sqlx(default)]
    is_persistent: bool,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    viewed_at: Option<DateTime<Utc>>,
    deleted_at: Option<DateTime<Utc>>,
}

#[derive(Debug, FromRow)]
struct ProjectRow {
    id: String,
    user_id: String,
    name: String,
    project_id: Option<String>,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    viewed_at: Option<DateTime<Utc>>,
    deleted_at: Option<DateTime<Utc>>,
}

#[derive(Debug, FromRow)]
struct InitiativeRow {
    id: String,
    user_id: String,
    name: String,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    viewed_at: Option<DateTime<Utc>>,
}

#[derive(Debug, FromRow)]
struct DatabaseRowRow {
    id: String,
    user_id: String,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    database_row: sqlx::types::Json<DatabaseRowPlacement>,
}

/// Where a row sits, as the detail clauses build it.
#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct DatabaseRowPlacement {
    table_id: Uuid,
    database_id: Uuid,
    position: String,
    created_by: Option<String>,
}

#[derive(Debug)]
enum SoupRow {
    Document(DocumentRow),
    Chat(ChatRow),
    Project(ProjectRow),
    Initiative(InitiativeRow),
    DatabaseRow(DatabaseRowRow),
    CalendarEvent(SoupCalendarEvent<()>),
}

impl<'a> FromRow<'a, PgRow> for SoupRow {
    fn from_row(row: &'a PgRow) -> Result<Self, sqlx::Error> {
        let item_type: &'a str = row.try_get("item_type")?;
        match item_type {
            "document" => Ok(SoupRow::Document(DocumentRow::from_row(row)?)),
            "chat" => Ok(SoupRow::Chat(ChatRow::from_row(row)?)),
            "project" => Ok(SoupRow::Project(ProjectRow::from_row(row)?)),
            "initiative" => Ok(SoupRow::Initiative(InitiativeRow::from_row(row)?)),
            "database_row" => Ok(SoupRow::DatabaseRow(DatabaseRowRow::from_row(row)?)),
            "calendar_event" => {
                let value: serde_json::Value = row.try_get("calendar_event")?;
                let event = serde_json::from_value(value)
                    .map_err(|error| sqlx::Error::Decode(Box::new(error)))?;
                Ok(SoupRow::CalendarEvent(event))
            }
            _ => Err(sqlx::Error::TypeNotFound {
                type_name: item_type.to_string(),
            }),
        }
    }
}

impl SoupRow {
    fn document_server_facts(&self) -> Option<SoupDocumentServerFacts> {
        match self {
            Self::Document(row) => Some(SoupDocumentServerFacts {
                is_email_attachment: row.is_email_attachment,
                is_important: row.is_important,
                status_option_ids: row.status_option_ids.clone(),
            }),
            Self::Chat(_)
            | Self::Project(_)
            | Self::Initiative(_)
            | Self::DatabaseRow(_)
            | Self::CalendarEvent(_) => None,
        }
    }

    fn into_projection_hydration(self) -> Result<SoupProjectionHydration, sqlx::Error> {
        let document_server_facts = self.document_server_facts();
        Ok(SoupProjectionHydration {
            item: self.into_soup_item()?,
            document_server_facts,
            sort_key: None,
        })
    }

    fn into_soup_item(self) -> Result<SoupItem<()>, sqlx::Error> {
        Ok(match self {
            SoupRow::Document(DocumentRow {
                id,
                user_id,
                document_version_id,
                name,
                sha,
                file_type,
                document_family_id,
                branched_from_id,
                branched_from_version_id,
                project_id,
                created_at,
                updated_at,
                viewed_at,
                sub_type,
                is_email_attachment: _,
                is_important: _,
                status_option_ids: _,
                is_completed,
                deleted_at,
            }) => SoupItem::Document(SoupDocument {
                id: Uuid::parse_str(&id).map_err(type_err)?,
                document_version_id: document_version_id
                    .parse()
                    .map_err(|e| sqlx::Error::Decode(Box::new(e)))?,
                owner_id: Owner::from_principal_str(&user_id).map_err(type_err)?,
                name,
                file_type,
                sha,
                project_id: project_id
                    .as_deref()
                    .map(Uuid::from_str)
                    .transpose()
                    .map_err(type_err)?,
                branched_from_id: branched_from_id
                    .as_deref()
                    .map(Uuid::from_str)
                    .transpose()
                    .map_err(type_err)?,
                branched_from_version_id,
                document_family_id,
                created_at,
                updated_at,
                viewed_at,
                sub_type: SoupDocumentSubType::from_db(sub_type, is_completed),
                deleted_at,
                extra: (),
            }),
            SoupRow::Chat(ChatRow {
                id,
                user_id,
                name,
                model,
                project_id,
                is_persistent,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
            }) => SoupItem::Chat(SoupChat {
                id: Uuid::parse_str(&id).map_err(type_err)?,
                name,
                model,
                owner_id: Owner::from_principal_str(&user_id).map_err(type_err)?,
                project_id: project_id
                    .as_deref()
                    .map(Uuid::parse_str)
                    .transpose()
                    .map_err(type_err)?,
                is_persistent,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
                extra: (),
            }),
            SoupRow::Project(ProjectRow {
                id,
                user_id,
                name,
                project_id,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
            }) => SoupItem::Project(SoupProject {
                id: Uuid::parse_str(&id).map_err(type_err)?,
                name,
                owner_id: Owner::from_principal_str(&user_id).map_err(type_err)?,
                parent_id: project_id
                    .as_deref()
                    .map(Uuid::from_str)
                    .transpose()
                    .map_err(type_err)?,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
                extra: (),
            }),
            SoupRow::Initiative(row) => SoupItem::Initiative(SoupInitiative {
                id: row.id.parse().map_err(type_err)?,
                name: row.name,
                owner_id: Owner::from_principal_str(&row.user_id).map_err(type_err)?,
                created_at: row.created_at,
                updated_at: row.updated_at,
                viewed_at: row.viewed_at,
                extra: (),
            }),
            SoupRow::DatabaseRow(row) => {
                let placement = row.database_row.0;
                SoupItem::DatabaseRow(SoupDatabaseRow {
                    id: row.id.parse().map_err(type_err)?,
                    table_id: placement.table_id,
                    database_id: placement.database_id,
                    position: placement.position,
                    owner_id: Owner::from_principal_str(&row.user_id).map_err(type_err)?,
                    created_by: placement.created_by,
                    created_at: row.created_at,
                    updated_at: row.updated_at,
                    extra: (),
                })
            }
            SoupRow::CalendarEvent(event) => SoupItem::CalendarEvent(event),
        })
    }
}

#[derive(Debug)]
pub(crate) struct ExpandedDynamicCursorArgs<'a> {
    /// the user for which we are performing the query
    pub user_id: MacroUserIdStr<'a>,
    /// the limit of items we can return
    pub limit: u16,
    /// the Query that we are attempting to perform
    pub cursor: Query<Uuid, DynamicSort, EntityFilterAst>,
    /// whether or not the query should explicitly remove items that DO have
    /// frecency records
    pub exclude_frecency: bool,
}

/// Opens the transaction a dynamic query runs in. A parallel worker costs
/// this query tens of milliseconds to start, so at the default
/// `parallel_setup_cost` callers with a few thousand grants ran twice as slow
/// in parallel; the raised cost keeps parallel plans for the largest callers.
async fn begin_dynamic_query(db: &PgPool) -> Result<Transaction<'static, Postgres>, sqlx::Error> {
    let mut transaction = db
        .begin()
        .instrument(tracing::info_span!(
            "soup.pool.acquire",
            pool_size = db.size(),
            pool_idle = db.num_idle()
        ))
        .await?;
    sqlx::query!("SET LOCAL parallel_setup_cost = 10000")
        .execute(&mut *transaction)
        .await?;
    Ok(transaction)
}

#[tracing::instrument(skip(db), err)]
async fn expanded_dynamic_cursor_soup_hydrated(
    db: &PgPool,
    args: ExpandedDynamicCursorArgs<'_>,
) -> Result<Vec<SoupProjectionHydration>, sqlx::Error> {
    let ExpandedDynamicCursorArgs {
        user_id,
        limit,
        cursor,
        exclude_frecency,
    } = args;
    let query_limit = limit as i64;
    let sort_method_str = cursor.sort_method().timestamp_method().to_string();
    let (cursor_id, cursor_position) = cursor.vals();
    let cursor_timestamp = cursor_position.map(|position| position.timestamp());
    let cursor_key = cursor_position.and_then(|position| position.key());
    let cursor_id_str = cursor_id.as_ref().map(|u| u.to_string());
    let status_property_id = SystemPropertyKey::STATUS_UUID;
    let assignees_property_id = SystemPropertyKey::ASSIGNEES_UUID;
    let completed_option_id = StatusOption::COMPLETED_UUID.to_string();

    let mut transaction = begin_dynamic_query(db).await?;
    let source_ids = fetch_user_source_ids(&mut transaction, user_id.as_ref()).await?;
    let mut document_ids =
        DocumentIdArrays::starting_at(first_document_id_slot(*cursor.sort_method(), false));
    let mut builder = build_query(
        cursor.filter(),
        exclude_frecency,
        *cursor.sort_method(),
        &mut document_ids,
    );
    let mut query = builder
        .build()
        .bind(user_id.as_ref())
        .bind(sort_method_str)
        .bind(query_limit)
        .bind(cursor_timestamp)
        .bind(cursor_id_str)
        .bind(completed_option_id)
        .bind(status_property_id)
        .bind(assignees_property_id);
    if let DynamicSort::Property {
        property_definition_id,
        ..
    } = *cursor.sort_method()
    {
        query = query.bind(property_definition_id).bind(cursor_key);
    }
    query = query.bind(&source_ids);
    for ids in document_ids.into_binds() {
        query = query.bind(ids);
    }
    let query_span = tracing::info_span!("soup.sql.fetch_decode", rows = tracing::field::Empty);
    let items = query
        // Unnamed statement: the SQL text varies per filter shape and per
        // interpolated literal (dates change daily), so a cached prepared
        // statement is rarely reused but flips to a generic plan after five
        // executions — and the generic plan misestimates the CTEs badly
        // enough to run 10x slower. Planning with the real bind values every
        // time costs ~1ms and keeps the plan stable.
        .persistent(false)
        .try_map(|row| {
            let mut item = SoupRow::from_row(&row)?.into_projection_hydration()?;
            item.sort_key = row.try_get("sort_key")?;
            Ok(item)
        })
        .fetch_all(&mut *transaction)
        .instrument(query_span.clone())
        .await?;
    query_span.record("rows", items.len());
    drop(query_span);
    transaction.commit().await?;

    Ok(items)
}

/// Execute a flat expanded dynamic query and retain document server facts.
#[tracing::instrument(skip(db), err)]
pub(crate) async fn expanded_dynamic_cursor_soup_with_projection(
    db: &PgPool,
    args: ExpandedDynamicCursorArgs<'_>,
) -> Result<Vec<SoupProjectionHydration>, sqlx::Error> {
    expanded_dynamic_cursor_soup_hydrated(db, args).await
}

/// Execute a flat expanded dynamic query without exposing projection metadata.
#[cfg(test)]
#[tracing::instrument(skip(db), err)]
pub(crate) async fn expanded_dynamic_cursor_soup(
    db: &PgPool,
    args: ExpandedDynamicCursorArgs<'_>,
) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
    Ok(expanded_dynamic_cursor_soup_hydrated(db, args)
        .await?
        .into_iter()
        .map(|hydration| hydration.item)
        .collect())
}

// ============================================================================
// Grouped Query Support
// ============================================================================

/// Arguments for grouped dynamic cursor soup queries.
#[derive(Debug)]
pub struct GroupedDynamicCursorArgs<'a> {
    /// The user for which we are performing the query
    pub user_id: MacroUserIdStr<'a>,
    /// The limit of items we can return
    pub limit: u16,
    /// The Query that we are attempting to perform
    pub cursor: Query<Uuid, DynamicSort, EntityFilterAst>,
    /// Whether or not the query should explicitly remove items that DO have frecency records
    pub exclude_frecency: bool,
    /// Grouping configuration
    pub grouping: GroupingConfig,
}

/// Group metadata fields extracted from grouped query rows.
#[derive(FromRow)]
struct GroupFields {
    group_key: String,
    group_order: Option<f64>,
    group_total_count: i64,
    row_in_group: i64,
    sort_key: Option<f64>,
}

/// Grouped row: reuses SoupRow for item data, adds group metadata.
#[derive(FromRow)]
struct GroupedSoupRow {
    #[sqlx(flatten)]
    item: SoupRow,
    #[sqlx(flatten)]
    group: GroupFields,
}

/// Legacy initial grouped queries return ten items per group.
const PER_GROUP_LIMIT: i32 = 10;

/// Build the GroupedItems CTE based on the grouping configuration.
/// Returns the entity_type value to bind at $10, if property grouping with entity_type filter.
fn build_grouped_items_cte(
    builder: &mut QueryBuilder<'_, Postgres>,
    grouping: &GroupingConfig,
    sort: DynamicSort,
) -> Option<String> {
    let date_expression = date_range_group_expression(grouping);
    let select_expr =
        date_expression.unwrap_or_else(|| group_select_expr(&grouping.field).into_owned());
    builder.push("GroupedItems AS (SELECT t.*, (");
    builder.push(&select_expr);
    builder.push(") as group_key, ");
    let group_order = if matches!(grouping.field, GroupByField::Property { .. }) {
        property_value_sql(
            "ep_group",
            "NULL::uuid",
            "ep_group.data_type",
            "ep_group.val",
            "po_group",
        )
        .group_order
    } else {
        "NULL::double precision".to_string()
    };
    builder.push(group_order);
    builder.push(" AS group_order, COUNT(*) OVER (PARTITION BY ");
    builder.push(&select_expr);
    builder.push(") as group_total_count, ROW_NUMBER() OVER (PARTITION BY ");
    builder.push(&select_expr);
    if matches!(sort, DynamicSort::Property { .. }) {
        builder.push(" ORDER BY ");
        builder.push(property_order(sort, "t.sort_key", "t.sort_ts", "t.id"));
        builder.push(") as row_in_group FROM TopItems t ");
    } else {
        builder.push(" ORDER BY t.sort_ts DESC, t.id DESC) as row_in_group FROM TopItems t ");
    }
    let bind = if let Some(GroupJoinClause {
        sql,
        entity_type_bind,
    }) = group_join_clause(&grouping.field)
    {
        builder.push(sql);
        let property_definition_id = match &grouping.field {
            GroupByField::Property {
                property_definition_id,
                ..
            } => Some(*property_definition_id),
            GroupByField::Project => Some(SystemPropertyKey::PROJECT_UUID),
            _ => None,
        };
        if let Some(property_definition_id) = property_definition_id {
            builder.push(format!(
                " LEFT JOIN property_options po_group ON po_group.property_definition_id = '{property_definition_id}' AND po_group.id::text = ep_group.val#>>'{{}}' "
            ));
        }
        entity_type_bind
    } else {
        None
    };
    if grouping.group_key.is_some() {
        builder.push(" WHERE (");
        builder.push(&select_expr);
        builder.push(") = $9 ");
    }
    builder.push("), FilteredGroupedItems AS (SELECT * FROM GroupedItems WHERE ");
    if matches!(sort, DynamicSort::Property { .. }) {
        builder.push(property_keyset(sort, "sort_key", 12, "sort_ts", "id"));
    } else {
        builder.push("($4::timestamptz IS NULL OR (sort_ts, id::text) < ($4, $5))");
    }
    if grouping.group_key.is_none() {
        builder.push(" AND row_in_group <= ");
        builder.push(
            grouping
                .per_group_limit
                .unwrap_or(PER_GROUP_LIMIT as u32)
                .clamp(1, 500)
                .to_string(),
        );
    }
    builder.push("), ");
    bind
}

/// Build the grouped query with grouping CTE.
/// Returns (QueryBuilder, entity_type_bind) where entity_type_bind is Some when
/// property grouping with entity_type filter is used (bind at $10).
fn build_grouped_query<'a>(
    filter_ast: &'a EntityFilterAst,
    exclude_frecency: bool,
    grouping: &'a GroupingConfig,
    sort: DynamicSort,
    document_ids: &mut DocumentIdArrays,
) -> (QueryBuilder<'a, Postgres>, Option<String>) {
    let sort_method = sort.timestamp_method();
    let source_ids = source_ids_slot(sort, true);
    let property_sort = matches!(sort, DynamicSort::Property { .. });
    let mut builder = sqlx::QueryBuilder::new(PREFIX);

    // Determine which entity types to include based on filters (same logic as build_query)
    let include_initiatives = initiative_opted_in(filter_ast.initiative_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Initiative],
        );
    let include_database_rows = database_row_opted_in(filter_ast.database_row_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::DatabaseRow],
        );
    let include_documents = !document_filter_is_impossible(filter_ast.document_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Document, PropertyEntityType::Task],
        );
    let include_chats = !chat_filter_is_impossible(filter_ast.chat_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Chat],
        );
    let include_projects = !project_filter_is_impossible(filter_ast.project_filter.as_deref())
        && properties_filter_can_apply_to(
            filter_ast.properties_filter.as_deref(),
            &[PropertyEntityType::Project],
        );
    // Gated like the other three legs. Beyond skipping a union branch that can
    // never match, this keeps the NIL-id exclusion every Soup query now sends
    // from reaching `push_filter` below — see the bind-numbering note there.
    let include_calendar_events =
        !calendar_event_filter_is_impossible(filter_ast.calendar_event_filter.as_deref())
            && properties_filter_can_apply_to(
                filter_ast.properties_filter.as_deref(),
                &[PropertyEntityType::CalendarEvent],
            );

    // Full-set windows run before the position filter.
    builder.push(if property_sort {
        "TopItems AS MATERIALIZED ("
    } else {
        "TopItems AS ("
    });
    builder.push(
        "SELECT all_items.item_type, all_items.id, all_items.sort_ts, all_items.project_id, all_items.property_entity_type, ",
    );

    builder.push(sort_key_expression(property_sort));
    builder.push(" AS sort_key FROM (");
    let mut needs_separator = false;

    if include_documents {
        push_union_separator(&mut builder, &mut needs_separator);
        let document_filter = filter_ast.document_filter.as_deref();
        let needs_task_property_joins = document_filter_needs_task_property_joins(document_filter);
        builder.push(grouped_document_top_clause(
            sort_method,
            needs_task_property_joins,
            document_filter_is_task_only(document_filter),
        ));
        if needs_task_property_joins {
            builder.push(DOCUMENT_TASK_PROPERTY_JOINS);
        }
        builder.push(document_top_where_clause(sort_method, source_ids));
        builder.push(build_document_filter(
            filter_ast.document_filter.as_deref(),
            Some(document_ids),
        ));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "d.id",
        ));
    }

    if include_chats {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(grouped_chat_top_clause(sort_method));
        builder.push(chat_top_where_clause(source_ids));
        builder.push(build_chat_filter(filter_ast.chat_filter.as_deref()));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "c.id",
        ));
    }

    if include_projects {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(grouped_project_top_clause(sort_method));
        builder.push(project_top_where_clause(source_ids));
        builder.push(build_project_filter(filter_ast.project_filter.as_deref()));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "p.id",
        ));
    }

    if include_calendar_events {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(grouped_calendar_event_top_clause(sort_method));
        builder.push(build_calendar_event_filter(
            filter_ast.calendar_event_filter.as_deref(),
        ));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "event.id::text",
        ));
    }

    if include_initiatives {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(initiative_top_clause(sort_method, true, false));
        builder.push(build_initiative_filter(
            filter_ast.initiative_filter.as_deref(),
        ));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "i.id::text",
        ));
    }

    if include_database_rows {
        push_union_separator(&mut builder, &mut needs_separator);
        builder.push(database_row_top_clause(sort_method, true, false));
        builder.push(build_database_row_filter(
            filter_ast.database_row_filter.as_deref(),
        ));
        builder.push(build_properties_filter(
            filter_ast.properties_filter.as_deref(),
            "r.id::text",
        ));
    }

    // Fallback when all entity types are filtered out
    if !needs_separator {
        builder.push(
            "SELECT 'document'::text as item_type, NULL::text as id, NULL::timestamptz as sort_ts, NULL::text as project_id, NULL::property_entity_type as property_entity_type WHERE false",
        );
    }

    builder.push(") all_items ");
    builder.push(property_sort_join(property_sort, 11));

    // Frecency exclusion join (only when exclude_frecency is true)
    if exclude_frecency {
        builder.push(
            r#"LEFT JOIN frecency_aggregates fa
                ON fa.entity_id = all_items.id
                AND fa.entity_type = all_items.item_type
                AND fa.user_id = $1
            WHERE fa.id IS NULL AND TRUE"#,
        );
    } else {
        builder.push("WHERE TRUE ");
    }

    if matches!(
        &grouping.field,
        GroupByField::Property {
            entity_type: Some(_),
            ..
        }
    ) {
        builder.push(" AND all_items.property_entity_type = $10::property_entity_type ");
    }

    // Ranking windows and the final SELECT provide their own ordering.
    builder.push("), ");

    // Rank/count the full group before applying the cursor and page cap.
    let entity_type_bind = build_grouped_items_cte(&mut builder, grouping, sort);

    // Hydrate only the rows selected for each group's page.
    let source_table = "FilteredGroupedItems";

    builder.push("Combined AS (");

    let mut combined_needs_separator = false;
    if include_documents {
        push_union_separator(&mut builder, &mut combined_needs_separator);
        builder.push(
            GROUPED_DOCUMENT_DETAIL_CLAUSE
                .replace("GroupedItems gi", &format!("{} gi", source_table)),
        );
    }
    if include_chats {
        push_union_separator(&mut builder, &mut combined_needs_separator);
        builder.push(
            GROUPED_CHAT_DETAIL_CLAUSE.replace("GroupedItems gi", &format!("{} gi", source_table)),
        );
    }
    if include_projects {
        push_union_separator(&mut builder, &mut combined_needs_separator);
        builder.push(
            GROUPED_PROJECT_DETAIL_CLAUSE
                .replace("GroupedItems gi", &format!("{} gi", source_table)),
        );
    }
    if include_calendar_events {
        push_union_separator(&mut builder, &mut combined_needs_separator);
        builder.push(
            GROUPED_CALENDAR_EVENT_DETAIL_CLAUSE
                .replace("GroupedItems gi", &format!("{} gi", source_table)),
        );
    }
    if include_initiatives {
        push_union_separator(&mut builder, &mut combined_needs_separator);
        builder.push(
            GROUPED_INITIATIVE_DETAIL_CLAUSE
                .replace("GroupedItems gi", &format!("{} gi", source_table)),
        );
    }
    if include_database_rows {
        push_union_separator(&mut builder, &mut combined_needs_separator);
        builder.push(
            GROUPED_DATABASE_ROW_DETAIL_CLAUSE
                .replace("GroupedItems gi", &format!("{} gi", source_table)),
        );
    }
    if !combined_needs_separator {
        builder.push(GROUPED_EMPTY_COMBINED_CLAUSE);
    }
    builder.push(") ");

    // Final SELECT with group-aware ordering
    builder.push("SELECT * FROM Combined ORDER BY ");

    match &grouping.field {
        GroupByField::Date => {
            builder.push(date_bucket_sql_order("sort_ts"));
        }
        _ => {
            if grouping.field.date_ranges().is_some() {
                builder.push("\"group_key\"::integer");
            } else {
                builder.push(group_order_expr(&grouping.field));
            }
        }
    }
    builder.push(", ");
    if property_sort {
        builder.push(property_order(
            sort,
            "\"sort_key\"",
            "\"sort_ts\"",
            "\"id\"",
        ));
    } else {
        builder.push("\"sort_ts\" DESC, \"id\" DESC");
    }

    // Apply LIMIT only in single-group "load more" mode. In multi-group mode the
    // result is already bounded by the per-group cap inside FilteredGroupedItems
    // (PER_GROUP_LIMIT), so a row-level LIMIT here would silently drop entire
    // groups from the response when there are many groups.
    if grouping.group_key.is_some() {
        builder.push(" LIMIT $3");
    }

    (builder, entity_type_bind)
}

/// Execute a grouped dynamic cursor soup query.
#[tracing::instrument(skip(db), err)]
pub async fn expanded_dynamic_cursor_soup_grouped(
    db: &PgPool,
    args: GroupedDynamicCursorArgs<'_>,
) -> Result<std::vec::IntoIter<ItemGroupingInfo>, sqlx::Error> {
    let GroupedDynamicCursorArgs {
        user_id,
        limit,
        cursor,
        exclude_frecency,
        grouping,
    } = args;

    let query_limit = limit as i64;
    let sort_method_str = cursor.sort_method().timestamp_method().to_string();
    let (cursor_id, cursor_position) = cursor.vals();
    let cursor_timestamp = cursor_position.map(|position| position.timestamp());
    let cursor_key = cursor_position.and_then(|position| position.key());
    let cursor_id_str = cursor_id.as_ref().map(|u| u.to_string());
    let status_property_id = SystemPropertyKey::STATUS_UUID;
    let assignees_property_id = SystemPropertyKey::ASSIGNEES_UUID;
    let completed_option_id = StatusOption::COMPLETED_UUID.to_string();

    let mut transaction = begin_dynamic_query(db).await?;
    let source_ids = fetch_user_source_ids(&mut transaction, user_id.as_ref()).await?;
    let mut document_ids =
        DocumentIdArrays::starting_at(first_document_id_slot(*cursor.sort_method(), true));
    let (mut query_builder, entity_type_bind) = build_grouped_query(
        cursor.filter(),
        exclude_frecency,
        &grouping,
        *cursor.sort_method(),
        &mut document_ids,
    );

    // Keep main's fixed grouping parameters; property sorts append their two values.
    let mut query = query_builder
        .build_query_as::<'_, GroupedSoupRow>()
        .bind(user_id.as_ref())
        .bind(sort_method_str)
        .bind(query_limit)
        .bind(cursor_timestamp)
        .bind(cursor_id_str)
        .bind(completed_option_id)
        .bind(status_property_id)
        .bind(assignees_property_id)
        .bind(grouping.group_key.as_ref().map(|key| {
            if let Some((_, ranges)) = grouping.field.date_ranges() {
                ranges
                    .ranges
                    .iter()
                    .position(|range| &range.key == key)
                    .map(|index| index.to_string())
                    .unwrap_or_else(|| "-1".into())
            } else {
                key.clone()
            }
        }))
        // The entity type of property grouping with an entity type filter.
        .bind(entity_type_bind);
    if let DynamicSort::Property {
        property_definition_id,
        ..
    } = *cursor.sort_method()
    {
        query = query.bind(property_definition_id).bind(cursor_key);
    }
    query = query.bind(&source_ids);
    for ids in document_ids.into_binds() {
        query = query.bind(ids);
    }

    let query_span = tracing::info_span!("soup.sql.fetch_decode", rows = tracing::field::Empty);
    let rows: Vec<GroupedSoupRow> = query
        // Unnamed statement — same rationale as expanded_dynamic_cursor_soup:
        // filter-shaped SQL text gets no reuse from the statement cache but
        // does get generic-plan flips.
        .persistent(false)
        .fetch_all(&mut *transaction)
        .instrument(query_span.clone())
        .await?;
    query_span.record("rows", rows.len());
    drop(query_span);
    transaction.commit().await?;

    let items = tracing::info_span!("soup.rows.convert", rows = rows.len()).in_scope(|| {
        rows.into_iter()
            .map(|row| {
                Ok(ItemGroupingInfo {
                    document_server_facts: row.item.document_server_facts(),
                    sort_key: row.group.sort_key,
                    display_order: row.group.group_order.map(|order| order as i32),
                    item: row.item.into_soup_item()?,
                    key: if let Some((_, ranges)) = grouping.field.date_ranges() {
                        row.group
                            .group_key
                            .parse::<usize>()
                            .ok()
                            .and_then(|index| ranges.ranges.get(index))
                            .map(|range| range.key.clone())
                            .unwrap_or_else(|| ranges.missing_value_bucket_key.clone())
                    } else {
                        row.group.group_key
                    },
                    total_group_count: usize::try_from(row.group.group_total_count)
                        .map_err(|error| sqlx::Error::Decode(Box::new(error)))?,
                    index_in_group: usize::try_from(row.group.row_in_group)
                        .map_err(|error| sqlx::Error::Decode(Box::new(error)))?,
                })
            })
            .collect::<Result<Vec<_>, sqlx::Error>>()
    })?;

    Ok(items.into_iter())
}