From 035f4a92339d7cfeed562fa3904a735238a29643 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Tue, 7 Nov 2023 14:13:00 +0100 Subject: [PATCH] Add `runif(n())` translation (#526) --- DESCRIPTION | 4 ++-- NEWS.md | 3 +++ R/dplyr.R | 6 +++++- tests/testthat/test-dplyr.R | 7 +++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e890c851..927be4a1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,8 +40,8 @@ Imports: Suggests: blob, covr, - dbplyr (>= 2.2.1), - dplyr (>= 0.7.0), + dbplyr (>= 2.4.0), + dplyr (>= 1.1.0), hms, readr, sodium, diff --git a/NEWS.md b/NEWS.md index 235a5005..7fe1c7e0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # bigrquery (development version) +* Added a translation for `runif(n())`. This fixes the translation for + `slice_sample()` (@mgirlich, #448). + * Add a `dbQuoteLiteral()` method for logicals to revert breaking change introduced by DBI 1.1.2 (@meztez, #478). diff --git a/R/dplyr.R b/R/dplyr.R index 14333dee..371a224b 100644 --- a/R/dplyr.R +++ b/R/dplyr.R @@ -253,7 +253,11 @@ sql_translation.BigQueryConnection <- function(x) { pmin = sql_prefix("LEAST"), # Median - median = function(x) dbplyr::build_sql("APPROX_QUANTILES(", x, ", 2)[SAFE_ORDINAL(2)]") + median = function(x) dbplyr::build_sql("APPROX_QUANTILES(", x, ", 2)[SAFE_ORDINAL(2)]"), + + runif = function(n = n(), min = 0, max = 1) { + dbplyr::sql_runif(RAND(), n = {{ n }}, min = min, max = max) + }, ), dbplyr::sql_translator(.parent = dbplyr::base_agg, n = function() dplyr::sql("count(*)"), diff --git a/tests/testthat/test-dplyr.R b/tests/testthat/test-dplyr.R index 33793fd5..0bc812fa 100644 --- a/tests/testthat/test-dplyr.R +++ b/tests/testthat/test-dplyr.R @@ -130,6 +130,13 @@ test_that("suffixes use _", { expect_equal(dbplyr::sql_join_suffix(simulate_bigrquery()), c("_x", "_y")) }) +test_that("runif is correctly translated", { + expect_equal( + dbplyr::translate_sql(runif(n()), con = simulate_bigrquery()), + dbplyr::sql("RAND()") + ) +}) + test_that("can correctly print a lazy query", { con <- DBI::dbConnect( bigquery(),