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

date_build sql translation on redshift is incorrect #1512

Open
ablack3 opened this issue Jun 5, 2024 · 0 comments
Open

date_build sql translation on redshift is incorrect #1512

ablack3 opened this issue Jun 5, 2024 · 0 comments

Comments

@ablack3
Copy link
Contributor

ablack3 commented Jun 5, 2024

The translation of date_build is slightly incorrect on redshift.

expect_equal(test_translate_sql(date_build(2020, 1, 1)), sql("TO_DATE(CAST(2020.0 AS TEXT) || '-' CAST(1.0 AS TEXT) || '-' || CAST(1.0 AS TEXT)), 'YYYY-MM-DD')"))

con <- DBI::dbConnect(RPostgres::Redshift(),
                      dbname   = Sys.getenv("CDM5_REDSHIFT_DBNAME"),
                      host     = Sys.getenv("CDM5_REDSHIFT_HOST"),
                      port     = Sys.getenv("CDM5_REDSHIFT_PORT"),
                      user     = Sys.getenv("CDM5_REDSHIFT_USER"),
                      password = Sys.getenv("CDM5_REDSHIFT_PASSWORD"))

# current translation
sql <- "SELECT TO_DATE(CAST(2020.0 AS TEXT) || '-' CAST(1.0 AS TEXT) || '-' || CAST(1.0 AS TEXT)), 'YYYY-MM-DD') AS dt"
cat(sql)
#> SELECT TO_DATE(CAST(2020.0 AS TEXT) || '-' CAST(1.0 AS TEXT) || '-' || CAST(1.0 AS TEXT)), 'YYYY-MM-DD') AS dt
DBI::dbGetQuery(con, sql)
#> Error: Failed to prepare query : ERROR:  syntax error at or near "CAST"
#> LINE 1: SELECT TO_DATE(CAST(2020.0 AS TEXT) || '-' CAST(1.0 AS TEXT)...
#>                                                    ^

# corrected sql
sql <- "SELECT TO_DATE(CAST(2020.0 AS TEXT) || '-' || CAST(1.0 AS TEXT) || '-' || CAST(1.0 AS TEXT), 'YYYY-MM-DD') AS dt"
cat(sql)
#> SELECT TO_DATE(CAST(2020.0 AS TEXT) || '-' || CAST(1.0 AS TEXT) || '-' || CAST(1.0 AS TEXT), 'YYYY-MM-DD') AS dt
DBI::dbGetQuery(con, sql)
#>           dt
#> 1 2020-01-01

DBI::dbDisconnect(con)

Created on 2024-06-05 with reprex v2.1.0

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

1 participant