feat(api): return coverage suggestions in recipe_id order (#75)

The candidate-recipe query in .query_candidate_recipes() used SELECT
DISTINCT with no ORDER BY, and .build_suggestions() emits suggestions in
candidate order. DuckDB returns DISTINCT rows in whatever order its
parallel hash aggregate finishes, so two identical cog_spending() or
cog_revenue() calls could list the same suggestions in different orders.
The query now ends in ORDER BY recipe_id.

On the bundled fixture, cog_spending("010000226085", 2011, category =
"All Categories") listed corrections_other_capital_combined before
corrections_combined; both new tests fail against the previous code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qbe6u2h83wgZdTgJgE4Xh5
This commit is contained in:
Claude committed 2026-11-04 14:18:24 +00:00
1 parent ba57f9cb27
commit d74fb627c2
3 files changed
+48 -2

No files matched your search

+8 -2
View File
@@ -241,7 +241,12 @@
#' scope by when `all_categories = TRUE`; ignored otherwise.
#' @param subtype_scope Character vector of subtype values to scope by
#' when `all_categories = TRUE`; ignored otherwise.
#' @return Character vector of recipe IDs (possibly empty).
#' @return Character vector of recipe IDs (possibly empty), sorted by
#' `recipe_id`. `.build_suggestions()` emits suggestions in this order, so
#' the sort is what makes two identical calls return the same list in the
#' same order (uscogdata#75). Without the `ORDER BY`, DuckDB returns
#' `SELECT DISTINCT` rows in whatever order its parallel hash aggregate
#' finishes in.
#' @noRd
.query_candidate_recipes <- function(con, category, flow_prefixes,
all_categories = FALSE,
@@ -279,7 +284,8 @@
AND recipe_id NOT IN (
SELECT DISTINCT recipe_id FROM harmonization_recipes
WHERE LEFT(component_code, 1) IN ('M', 'L')
)",
)
ORDER BY recipe_id",
candidate_scope_sql
))$recipe_id
}