Skip to content

slice_* function issue with data masking #1294

Closed
@bairdj

Description

@bairdj

There is an issue when slice_* calls are translated to SQL when the order_by parameter uses .data$ notation rather than a bare symbol name. The generated SQL erroneously prefixes .data to the order by clause, which causes the query to fail.

# Expected behaviour (using local df)
mtcars %>%
  group_by(.data$cyl) %>%
  slice_max(.data$mpg)

# Returns 1 row per group, ordered by MPG

# Observed behaviour (fails)
rem_mtcars <- copy_to(connection, mtcars)
rem_mtcars %>%
  group_by(.data$cyl) %>%
  slice_max(.data$mpg)

# This works
rem_mtcars %>%
  group_by(.data$cyl) %>%
  slice_max(mpg)

An example of the generated SQL is

ROW_NUMBER() OVER (PARTITION BY "cyl" ORDER BY ".data"."mpg" DESC)

The translation works fine when using arrange, it is only for the window functions I have observed the issue.

I've tested this with Redshift and SQLite with the same output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions