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

cast "as.double" and "as.numeric" to DOUBLE PRECISION or FLOAT instead of NUMERIC for Redshift/PostgreSQL #408

Closed
kmishra9 opened this issue Apr 2, 2020 · 3 comments
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL

Comments

@kmishra9
Copy link

kmishra9 commented Apr 2, 2020

Very similar issue to #379 and (I assume) the associated fix at #388. Casting a string, using as.numeric() and as.double() in Redshift coerces to an integer.

Given a column x, with the string "0.88", the output value is 0. The SQL translation is CAST("x" AS NUMERIC) in both cases.

Using sql('cast(x AS DOUBLE PRECISION)') or sql('cast(x AS FLOAT)') directly outputs the expected 0.88

@kmishra9 kmishra9 changed the title cast "as.double" and "as.numeric" to DOUBLE instead of NUMERIC for Redshift/PostgreSQL cast "as.double" and "as.numeric" to DOUBLE PRECISION or FLOAT instead of NUMERIC for Redshift/PostgreSQL Apr 2, 2020
@hadley
Copy link
Member

hadley commented Apr 14, 2020

Could you please create a reprex, following the advice at https://dbplyr.tidyverse.org/articles/reprex.html?

@hadley hadley added the reprex needs a minimal reproducible example label Apr 14, 2020
@kmishra9
Copy link
Author

Here you go:

library("dplyr", warn.conflicts = FALSE)

lf <- dbplyr::lazy_frame(x = "0.88", con = dbplyr::simulate_postgres())

# Does not work as expected
lf %>% mutate(x = as.double(x))
#> <SQL>
#> SELECT CAST(`x` AS NUMERIC) AS `x`
#> FROM `df`
lf %>% mutate(x = as.numeric(x))
#> <SQL>
#> SELECT CAST(`x` AS NUMERIC) AS `x`
#> FROM `df`

# Works as expected
lf %>% mutate(x = sql('cast(x AS DOUBLE PRECISION)'))
#> <SQL>
#> SELECT cast(x AS DOUBLE PRECISION) AS `x`
#> FROM `df`
lf %>% mutate(x = sql('cast(x AS FLOAT)'))
#> <SQL>
#> SELECT cast(x AS FLOAT) AS `x`
#> FROM `df`

Created on 2020-04-26 by the reprex package (v0.3.0)

@hadley hadley added bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL and removed reprex needs a minimal reproducible example labels Sep 22, 2020
@hadley
Copy link
Member

hadley commented Sep 22, 2020

Fixed in ccdda24

@hadley hadley closed this as completed Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL
Projects
None yet
Development

No branches or pull requests

2 participants