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

Translate str_starts() to REGEXP_INSTR instead of INSTR for Snowflake #1405

Closed
nathanhaigh opened this issue Nov 10, 2023 · 2 comments
Closed

Comments

@nathanhaigh
Copy link
Contributor

nathanhaigh commented Nov 10, 2023

Snowflake doesn't have a INSTR function. As such, I think the following stringr functions are affected:

  • str_detect() when fixed() is used
  • str_ends()
  • str_starts()
x <- c("XXX", "XXY", "YXXX")

#####
# str_detect()
#####
lazy_frame(TOKEN=x,con = simulate_snowflake()) %>%
  filter(
    stringr::str_detect(TOKEN, fixed("XXX"))
  )
<SQL>
SELECT `df`.*
FROM `df`
WHERE (INSTR(`TOKEN`, 'XXX') > 0)

#####
# str_starts()
#####
lazy_frame(TOKEN=x,con = simulate_snowflake()) %>%
  filter(
    stringr::str_starts(TOKEN, fixed("XXX"))
  )
<SQL>
SELECT `df`.*
FROM `df`
WHERE (INSTR(`TOKEN`, 'XXX') = 1)

#####
# str_ends()
#####
lazy_frame(TOKEN=x,con = simulate_snowflake()) %>%
  filter(
    stringr::str_ends(TOKEN, fixed("XXX"))
  )
<SQL>
SELECT `df`.*
FROM `df`
WHERE (INSTR(`TOKEN`, 'XXX') = ((LENGTH(`TOKEN`) - LENGTH('XXX')) + 1))
@nathanhaigh
Copy link
Contributor Author

I see from R/backend-snowflake.R the str_detect() function is hacking around the fact that REGEXP anchors the start and end by default. A simpler solution is to use CONTAINS() instead.

@hadley
Copy link
Member

hadley commented Dec 21, 2023

Fixed in #1406

@hadley hadley closed this as completed Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants