Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runif(n()) translation #526

Merged
merged 6 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading