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

Update grepl SQL translation to match REGEXP_CONTAINS signature #416

Merged
merged 3 commits into from
Nov 8, 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
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)

* `grepl(pattern, x)` is now correctly translated to
`REGEXP_CONTAINS(x, pattern)` (#416).

* `median()` gets a translation that works in `summarise()` and a clear
error if you use it in `mutate()` (#419).

Expand Down
10 changes: 7 additions & 3 deletions R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,13 @@ sql_translation.BigQueryConnection <- function(x) {
Sys.time = sql_prefix("current_time"),

# Regular expressions
grepl = sql_prefix("REGEXP_CONTAINS", 2),
gsub = function(match, replace, x) {
dbplyr::build_sql("REGEXP_REPLACE", list(x, match, replace))
grepl = function(pattern, x) {
# https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#regexp_contains
dbplyr::build_sql("REGEXP_CONTAINS", list(x, pattern))
},
gsub = function(pattern, replace, x) {
# https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#regexp_replace
dbplyr::build_sql("REGEXP_REPLACE", list(x, pattern, replace))
},

# Other scalar functions
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/dplyr.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@
Error in `db_copy_to()`:
! BigQuery does not support temporary tables

# string functions correctly

Code
dbplyr::translate_sql(grepl("a.c", x), con = con)
Output
<SQL> REGEXP_CONTAINS(`x`, 'a.c')
Code
dbplyr::translate_sql(gsub("a.c", "", x), con = con)
Output
<SQL> REGEXP_REPLACE(`x`, 'a.c', '')

9 changes: 9 additions & 0 deletions tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ test_that("runif is correctly translated", {
)
})

test_that("string functions correctly", {
con <- simulate_bigrquery()

expect_snapshot({
dbplyr::translate_sql(grepl("a.c", x), con = con)
dbplyr::translate_sql(gsub("a.c", "", x), con = con)
})
})

test_that("median is correctly translated", {
expect_equal(
dbplyr::translate_sql(median(x), con = simulate_bigrquery(), window = FALSE),
Expand Down