Skip to content

Commit

Permalink
WIP: try to work around the SQL error for lubridate::quarter
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmlft committed Nov 19, 2024
1 parent 09e2fbb commit 8f69031
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
17 changes: 16 additions & 1 deletion preprocessing/R/monthly_counts.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,22 @@ summarise_counts <- function(omop_table, concept_col, date_col, level) {

if (level == "quarterly") {
# NOTE: lubridate::quarter is supported for postgres back-ends, but not sqlite
omop_table$date_quarter <- as.integer(lubridate::quarter(.data[[date_col]]))
tryCatch(
{
omop_table_with_quarter <- omop_table |>
dplyr::mutate(date_quarter = as.integer(lubridate::quarter(.data[[date_col]])))
dplyr::compute(omop_table_with_quarter) # Force execution of the query to check for the error
},
error = function(cnd) {
warning(
sprintf("Extracting quarter from `%s` failed with\n%s", date_col, cnd),
"\nCollecting table first and trying again.",
call. = FALSE
)
omop_table_with_quarter <- dplyr::collect(omop_table) |>
dplyr::mutate(date_quarter = as.integer(lubridate::quarter(.data[[date_col]])))
}
)
}

omop_table |>
Expand Down
22 changes: 17 additions & 5 deletions preprocessing/tests/testthat/test-monthly_counts.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ mock_measurement <- data.frame(
value_as_concept_id = 0
)

test_that("calculate_monthly_counts produces the expected results", {
res <- calculate_monthly_counts(mock_measurement, measurement_concept_id, measurement_date)
test_that("summarise_counts produces the expected results at monthly level", {
res <- summarise_counts(mock_measurement, "measurement_concept_id", "measurement_date", level = "monthly")
expect_s3_class(res, "data.frame")
expect_named(res, c("concept_id", "date_year", "date_month", "record_count", "person_count", "records_per_person"))
expect_equal(nrow(res), 1)
Expand All @@ -41,9 +41,21 @@ test_that("calculate_monthly_counts produces the expected results", {

db <- dbplyr::src_memdb()
db_measurement <- dplyr::copy_to(db, mock_measurement, name = "measurement", overwrite = TRUE)
test_that("calculate_monthly_counts works on Database-stored tables", {
ref <- calculate_monthly_counts(mock_measurement, measurement_concept_id, measurement_date)
db_res <- calculate_monthly_counts(db_measurement, measurement_concept_id, measurement_date)
test_that("summarise_counts works on Database-stored tables at monthly level", {
ref <- summarise_counts(mock_measurement, "measurement_concept_id", "measurement_date", level = "monthly")
db_res <- summarise_counts(db_measurement, "measurement_concept_id", "measurement_date", level = "monthly")

expect_s3_class(db_res, "data.frame")
expect_named(db_res, c("concept_id", "date_year", "date_month", "record_count", "person_count", "records_per_person"))
expect_type(db_res$record_count, "integer")
expect_type(db_res$person_count, "integer")
expect_type(db_res$records_per_person, "double")
expect_identical(db_res, ref)
})

test_that("summarise_counts works on Database-stored tables at quarterly level", {
ref <- summarise_counts(mock_measurement, "measurement_concept_id", "measurement_date", level = "quarterly")
db_res <- summarise_counts(db_measurement, "measurement_concept_id", "measurement_date", level = "quarterly")

expect_s3_class(db_res, "data.frame")
expect_named(db_res, c("concept_id", "date_year", "date_month", "record_count", "person_count", "records_per_person"))
Expand Down

0 comments on commit 8f69031

Please sign in to comment.