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

Redshift round() can't return decimal places #1033

Closed
owenjonesuob opened this issue Oct 26, 2022 · 0 comments · Fixed by #1034
Closed

Redshift round() can't return decimal places #1033

owenjonesuob opened this issue Oct 26, 2022 · 0 comments · Fixed by #1034

Comments

@owenjonesuob
Copy link
Contributor

owenjonesuob commented Oct 26, 2022

Redshift's numeric (== decimal) type has a default scale (number of decimal places) of 0, so naively casting values to numeric can lead to surprising results.

The Redshift backend already guards against this, by instead casting to float:

as.numeric = sql_cast("FLOAT"),

However the Postgres backend, which the Redshift backend inherits from, uses a custom function for its translation of round(), which contains an explicit cast to numeric:

postgres_round <- function(x, digits = 0L) {
  digits <- as.integer(digits)
  sql_expr(round(((!!x)) %::% numeric, !!digits))
}

This means we end up with the following SQL translation:

translate_sql(round(1.234, 1), con = simulate_redshift())
#> <SQL> ROUND((1.234) :: numeric, 1)

This always:

  • Implicitly rounds to 0 decimal places, via the cast to numeric
  • Then, explicitly rounds the result to the specified number of decimal places, meaning we end up with at best 0dp

Hopefully an easy fix by overriding round in the Redshift translator with a small round_redshift() function - I'll add a pull request shortly!

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

Successfully merging a pull request may close this issue.

1 participant