feat: name a cohort by state/type predicate instead of a 40k-id IN list
cog_spending(), cog_revenue() and cog_balances() gain optional state/type
arguments. Both default to NULL, so every existing govid-based call is
unchanged.
The verbs took a cohort only as a govid vector, which .sql_lit_chr()
rendered into a quoted IN list and .verb_spendrev() embedded into 5-8
separate statements per call: the scope check, the main aggregate, the
per-capita join, the harmonization block, and the suggestion and
suppression queries. For type = "city" that list is 301,589 characters,
parsed and planned from scratch every time it appears.
Passing state/type instead expresses the cohort as a subquery against
canonical_fips_xwalk, so its size never enters the SQL string at all.
Measured on the production corpus, same FY2022 aggregate over the
20,106-government city cohort, DUCKDB_THREADS=2, median of 5:
IN (20,106 literals) -- 0.3.0 432 ms
join against a temp cohort table 132 ms
predicate on canonical_fips_xwalk 102 ms
no cohort filter at all (the floor) 105 ms
The predicate reaches the no-filter floor: the cohort restriction is
now free. End to end through cog_spending(category = "Police"),
1080 ms -> 271 ms, 3.99x -- larger than the single-query saving,
because the repetition across statements is what actually cost.
Design decisions, both made explicitly rather than left implicit:
- govid AND state/type INTERSECT. "These ids, narrowed to that
state/type" is a real query, and an error here could never be
relaxed later without breaking callers.
- A predicate cohort has no id list to report, so
provenance$scope$govids_found/govids_missing stay empty and a new
scope$cohort block carries state, type and n_governments. Resolving
the ids just to report them would put 20,000 govids in every
fleet-scale response body -- the cost this change removes. A
govid-named cohort's provenance is untouched.
state/type are coerced with .coerce_state_to_fips()/.coerce_type(), the
same helpers cog_gov_search() uses. That is load-bearing: the argument
is a postal abbreviation ("WI") while fips_state holds a FIPS code
("55"), and a predicate on the raw parameter matches nothing and returns
an empty result indistinguishable from "reported nothing". cog-api hit
exactly this trap optimizing the same path.
.attach_per_capita() now keys its population lookup on the govids present
in the result rather than the requested cohort. Those are the only ones
its LEFT JOIN can match, so the output is identical -- but it needs no id
list, and on a paginated call it looks up one page instead of the fleet.
Fixes uscogdata#58.
This commit is contained in:
+31
-3
@@ -5,7 +5,7 @@
|
||||
\title{Summarized revenue by category}
|
||||
\usage{
|
||||
cog_revenue(
|
||||
govid,
|
||||
govid = NULL,
|
||||
years,
|
||||
category = NULL,
|
||||
per_capita = FALSE,
|
||||
@@ -15,11 +15,15 @@ cog_revenue(
|
||||
revenue_concept = c("general", "total"),
|
||||
complete = FALSE,
|
||||
limit = NULL,
|
||||
offset = NULL
|
||||
offset = NULL,
|
||||
state = NULL,
|
||||
type = NULL
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
\item{govid}{Character vector of `canonical_govid` values.}
|
||||
\item{govid}{Character vector of `canonical_govid` values, or `NULL` to name
|
||||
the cohort by `state`/`type` instead. One of `govid`, `state`, or `type`
|
||||
is required.}
|
||||
|
||||
\item{years}{Integer vector of years.}
|
||||
|
||||
@@ -126,6 +130,30 @@ exactly as before this parameter existed. Mutually exclusive with
|
||||
|
||||
\item{offset}{Rows to skip before `limit` starts counting (0-based).
|
||||
Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.}
|
||||
|
||||
\item{state, type}{Name the cohort by predicate instead of by id: `state` is
|
||||
a 2-letter USPS abbreviation (or a FIPS code) and `type` is one of
|
||||
`"state"`, `"county"`, `"city"`, `"township"` (or the integer `0:3`) --
|
||||
the same vocabulary, and the same internal coercion, as
|
||||
[cog_gov_search()]. Both default to `NULL`.
|
||||
|
||||
The cohort is then expressed as a subquery against `canonical_fips_xwalk`
|
||||
inside each statement rather than round-tripped through R as a literal id
|
||||
list. For a fleet-scale cohort that is the difference between a
|
||||
301,591-character `IN` list re-parsed in 5--8 statements per call and a
|
||||
constant-size predicate: measured at **94 ms versus 449 ms** for the same
|
||||
FY2022 aggregate over the 20,106-government `type = "city"` cohort, within
|
||||
7% of the no-filter floor.
|
||||
|
||||
Supplying `govid` **and** `state`/`type` INTERSECTS them -- the
|
||||
governments in `govid` that also match the predicate -- rather than one
|
||||
silently taking precedence. Naming no cohort at all (`govid`, `state` and
|
||||
`type` all `NULL`) aborts with class `uscogdata_no_cohort`.
|
||||
|
||||
When the cohort is named by predicate, `provenance$scope$govids_found`
|
||||
and `govids_missing` are empty -- there is no id list to report against --
|
||||
and `provenance$scope$cohort` carries `state`, `type` and
|
||||
`n_governments` instead. A `govid`-named cohort reports exactly as before.}
|
||||
}
|
||||
\value{
|
||||
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||
|
||||
Reference in New Issue
Block a user