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:
+11
-4
@@ -184,11 +184,18 @@ cog_gov_search <- function(name = NULL, state = NULL, type = NULL) {
|
||||
if (!is.character(state) || length(state) != 1L) {
|
||||
cli::cli_abort("`state` must be a 2-letter USPS abbrev or a FIPS integer.")
|
||||
}
|
||||
fips <- .state_abbrev_to_fips[[toupper(state)]]
|
||||
if (is.null(fips)) {
|
||||
cli::cli_abort("Unknown state abbreviation: {state}.")
|
||||
# Membership tested before the lookup, not after: `.state_abbrev_to_fips` is
|
||||
# a named CHARACTER vector, and `[[` on a name it does not carry throws
|
||||
# base R's "subscript out of bounds" rather than returning NULL -- which
|
||||
# made the curated message below unreachable dead code. Reported as a bare
|
||||
# subscript error, `cog_gov_search(state = "ZZ")` gave no hint that the
|
||||
# argument wants a postal abbreviation.
|
||||
key <- toupper(state)
|
||||
if (!key %in% names(.state_abbrev_to_fips)) {
|
||||
cli::cli_abort("Unknown state abbreviation: {state}.",
|
||||
class = "uscogdata_unknown_state")
|
||||
}
|
||||
fips
|
||||
.state_abbrev_to_fips[[key]]
|
||||
}
|
||||
|
||||
# USPS state / territory abbreviation -> 2-digit FIPS code.
|
||||
|
||||
Reference in New Issue
Block a user