From d74fb627c287ac3d51804a1a79a38886050ae0f8 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 14:18:24 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01Qbe6u2h83wgZdTgJgE4Xh5 --- NEWS.md | 9 +++++++++ R/suggestions.R | 10 ++++++++-- tests/testthat/test-suggestions.R | 31 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test-suggestions.R diff --git a/NEWS.md b/NEWS.md index ea1282e..04d4e71 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# uscogdata (development version) + +## Suggestions come back in a stable order + +`cog_spending()` and `cog_revenue()` now order suggestions by `recipe_id`. The query +that picks candidate recipes used `SELECT DISTINCT` with no `ORDER BY`, so DuckDB +returned them in whatever order its parallel aggregate finished, and two identical +calls could list the same suggestions in different orders. The query now sorts them. + # uscogdata 0.4.0 ## Cohorts can be named by predicate, not just by id diff --git a/R/suggestions.R b/R/suggestions.R index ca7c40d..a57a13c 100644 --- a/R/suggestions.R +++ b/R/suggestions.R @@ -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 } diff --git a/tests/testthat/test-suggestions.R b/tests/testthat/test-suggestions.R new file mode 100644 index 0000000..1280c95 --- /dev/null +++ b/tests/testthat/test-suggestions.R @@ -0,0 +1,31 @@ +# tests/testthat/test-suggestions.R + +verb_suggestions <- function(r) attr(r, "provenance")$suggestions + +suggestion_ids <- function(sugg) { + vapply(sugg, function(s) s$recipe_id, character(1)) +} + +# --- stable order ------------------------------------------------------------ + +test_that("cog_spending() returns suggestions ordered by recipe_id", { + # On the fixture, the unsorted candidate query lists + # corrections_other_capital_combined before corrections_combined, so this + # fails without the ORDER BY in .query_candidate_recipes(). + skip_if_no_corpus() + r <- suppressMessages( + cog_spending("010000226085", 2011L, category = "All Categories")) + ids <- suggestion_ids(verb_suggestions(r)) + expect_gt(length(ids), 1L) + expect_identical(ids, sort(ids, method = "radix")) +}) + +test_that(".query_candidate_recipes() returns recipe ids in sorted order", { + con <- uscogdata:::.ensure_session() + ids <- uscogdata:::.query_candidate_recipes( + con, category = NULL, flow_prefixes = c("E", "F", "G"), + all_categories = TRUE, subtype_col = "spend_subtype", + subtype_scope = c("operations", "capital", "assistance")) + expect_gt(length(ids), 1L) + expect_identical(ids, sort(ids, method = "radix")) +})