Skip to content

Commit

Permalink
Add runif(n()) translation (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich authored Nov 7, 2023
1 parent 9c828cb commit 035f4a9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
6 changes: 5 additions & 1 deletion R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(*)"),
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 035f4a9

Please sign in to comment.