Skip to content

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

@hdplsa

Description

@hdplsa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorfunc trans 🌍Translation of individual functions to SQLwipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions