Filterable list query for the generic cell_list RPC.
Takes a flat filter shape (single optional clause per dimension; the
cell_list API explicitly does NOT support OR'd alternatives within a
dimension — keep it simple) plus an optional viewer-aware visibility
predicate.
The visibility predicate mirrors can_view_cell in SQL form:
(viewer_is_admin
OR cell.visibility = 'public'
OR (viewer_actor_id IS NOT NULL AND created_by = viewer_actor_id)
OR (viewer_actor_id IS NOT NULL AND <grant admits caller>))The grants branch closes parity with can_view_cell: a SQL EXISTS
over cell_grant, parameterized by the caller's actor_id and the
parallel (role[], scope_id[]) projection of auth.role_grants. The
caller's role_grants are materialized once via a caller_role_grants
CTE so the role-grant unnest isn't re-scanned per outer row. Empty
role_grant arrays are fine: the CTE yields zero rows, the inner EXISTS
returns false, and the actor-grant branch still fires for actor-shaped
grants.
shared_with_caller_only: true (shared_with: 'me' at the wire layer)
takes a different SQL shape: instead of layering an extra
conjunction on the cell-driven scan, it semi-joins through
cell_grant, letting the planner drive from the (typically tiny)
admitted-grant set via idx_cell_grant_actor /
idx_cell_grant_role_scope rather than scanning every cell row. For a
sharee with N grants over a table of M cells, the cost drops from
O(M) to O(N + matched-cells). Owner-is-implicit (a cell's owner never
appears as a grant principal) means the grants branch is itself
owner-excluding, but the explicit created_by IS DISTINCT FROM caller
guards against any future deviation. The shared_with branch does NOT
bypass for admin: an admin asking "what's shared with me" wants their
own grant footprint, not every cell.
Soft-deleted rows are excluded by default; opt-in via include_deleted.