-
Notifications
You must be signed in to change notification settings - Fork 178
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
Poor substr() translation #577
Comments
Minimal reprex: library(dbplyr)
library(dplyr, warn.conflicts = FALSE)
db <- memdb_frame(x = c("abc", "defsg", "highs"))
db %>%
mutate(y = substr(x, 2, nchar(x) - 1)) %>%
show_query()
#> Warning in pmax(as.integer(stop) - start + 1L, 0L): NAs introduced by coercion
#> <SQL>
#> SELECT `x`, SUBSTR(`x`, 2, NULL) AS `y`
#> FROM `dbplyr_001` Created on 2021-01-21 by the reprex package (v0.3.0.9001) |
Hmmm, this is going to be hard to support, I think. The easiest work around is to use library(dbplyr)
library(dplyr, warn.conflicts = FALSE)
db <- memdb_frame(x = c("abc", "defsg", "highs"))
db %>%
mutate(y = str_sub(x, 2, -2))
#> # Source: lazy query [?? x 2]
#> # Database: sqlite 3.34.0 [:memory:]
#> x y
#> <chr> <chr>
#> 1 abc b
#> 2 defsg efs
#> 3 highs igh Created on 2021-01-21 by the reprex package (v0.3.0.9001) |
Thanks for the reply. library(dbplyr)
library(stringr)
translate_sql(str_sub(Test, 1, -2))
#> <SQL> SUBSTR(`Test`, 1, LENGTH(`Test`) - 1)
translate_sql(str_sub(Test, 1, length(Test)))
#> Warning in str_sub(Test, 1, length(Test)): NAs introduced by coercion
#> Error in if (end == -1L) {: missing value where TRUE/FALSE needed Created on 2021-01-22 by the reprex package (v0.3.0) Interestingly, |
Use -1 if you want to include the last character. |
Yep, that'd do it, thanks. |
The This library(dbplyr)
library(stringr)
translate_sql(str_sub(Test, -10, -1),
con = simulate_mssql())
#> <SQL> SUBSTRING(`Test`, -10) Created on 2021-01-25 by the reprex package (v0.3.0) |
Ah bummer, it seems like SQL server doesn't have an option third argument, unlike pretty much every other database (and, as far as I can tell, the SQL spec). |
That said, there's clearly a bug because no database is going to accept a negative start value. |
No, that's not right — |
Looks like all the backends that use |
The handling of negative start seems to be inconsistent from database to database, so I think it's safer to just always use LENGTH(). New optional_length parameter allows for MS SQL compatibility. Fixes #577
dbplyr
doesn't handle substr
with dynamic start
and stop
Improving handling of negative values, and new optional_length parameter allows for MS SQL compatibility. Fixes #577
I've just tested it on the SQL server and can confirm it works perfectly. Thank you for solving it! |
Hi,
I was trying to create an SQL query which would grab the end of a string. In this example it was supposed to be the last five characters. However, the
show_query
function shows it returnsnull
rather than calculated integers.Is that a correct behaviour?
Created on 2021-01-21 by the reprex package (v0.3.0)
I've tried using
nchar
andlen
instead of Rlength
but with the same effect (My actual database useslen
rather thanlength
).Thanks
Edit: Provided a better example showing that using native SQL code gets desired result
The text was updated successfully, but these errors were encountered: