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

LAG and LEAD do not have default argument in Redshift #548

Closed
hdplsa opened this issue Nov 26, 2020 · 0 comments · Fixed by #549
Closed

LAG and LEAD do not have default argument in Redshift #548

hdplsa opened this issue Nov 26, 2020 · 0 comments · Fixed by #549
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL wip work in progress

Comments

@hdplsa
Copy link
Contributor

hdplsa commented Nov 26, 2020

The LAG and LEAD functions do not have the default argument in Redshift. The dbplyr translation always inserts the default argument into the LEAD/LAG functions and this leads to an error in Redshift.

Please see below an example:

library(dbplyr)
library(DBI)
library(reprex)

con <- dbConnect(RPostgres::Redshift(),
                 host = "",
                 dbname = "",
                 port = ,
                 user = "",
                 password = )

# Create a simple table in the DB
table.sql <- sql("SELECT 1 AS YR, 1 AS MON, 123456 AS SALES
                  UNION ALL
                  SELECT 1 AS YR, 2 AS MON, 234567 AS SALES
                  UNION ALL
                  SELECT 1 AS YR, 3 AS MON, 345678 AS SALES
                  UNION ALL
                  SELECT 1 AS YR, 4 AS MON, NULL AS SALES
                  UNION ALL
                  SELECT 2 AS YR, 1 AS MON, 123456 AS SALES
                  UNION ALL
                  SELECT 2 AS YR, 2 AS MON, 234567 AS SALES
                  UNION ALL
                  SELECT 2 AS YR, 3 AS MON, 345678 AS SALES
                  UNION ALL
                  SELECT 3 AS YR, 1 AS MON, 123456 AS SALES
                  ")

# Send query to DB creating the simple table
new.table <- con %>% dplyr::tbl(table.sql)

# Test lag and lead
new.table %>%
  dbplyr::window_order(yr, mon) %>%
  dplyr::mutate(LG = lag(sales), LD = lead(sales)) %>%
  dplyr::arrange(yr, mon)
#> Error: Failed to prepare query: ERROR:  Default parameter not be supported for window function lag

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

Redshift documentation links:
LAG: https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LAG.html
LEAD: https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LEAD.html

@hadley hadley added bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL wip work in progress labels Jan 21, 2021
hadley pushed a commit that referenced this issue Jan 22, 2021
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 wip work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants