Skip to content

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

Closed

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

Activity

  1. added
    bugan unexpected problem or unintended behavior
    func trans 🌍Translation of individual functions to SQL
    wipwork in progress
    on Jan 21, 2021
  2. added a commit that references this issue on Jan 22, 2021
    a4872e2
  3. Selbosh commented on Jul 17, 2025

    @Selbosh

    Encountered same issue with MariaDB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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