Skip to content

Malfunctioning SQL generation with dbplyr 2.3.0 #1101

Description

@lschneiderbauer

Good day,

with dbplyr 2.3.0 I managed to construct a statement which does not correctly translate to SQL.

Consider the following example:

library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)

table1 <- tbl_lazy(tibble(key = 1), dbplyr::simulate_hana()) %>%
  rename_with(~ paste0("table1.", .))
table2 <- tbl_lazy(tibble(key = 2, parent = 3), dbplyr::simulate_hana()) %>%
  rename_with(~ paste0("table2.", .))
table3 <- tbl_lazy(tibble(key = 3, parent = 4), dbplyr::simulate_hana()) %>%
  rename_with(~ paste0("table3.", .))
table4 <- tbl_lazy(tibble(key = 4, parent = 5), dbplyr::simulate_hana()) %>%
  rename_with(~ paste0("table4.", .))

table1 %>%
  inner_join(table2, by = c("table1.key" = "table2.parent")) %>%
  inner_join(table3, by = c("table2.key" = "table3.parent")) %>%
  inner_join(table4, by = c("table3.key" = "table4.parent"))
#> <SQL>
#> SELECT
#>   `df...1`.`key` AS `table1.key`,
#>   `df...2`.`key` AS `table2.key`,
#>   `df...3`.`key` AS `table3.key`,
#>   `df...3`.`key` AS `table4.key`
#> FROM `df` AS `df...1`
#> INNER JOIN `df` AS `df...2`
#>   ON (`df...1`.`key` = `df...2`.`parent`)
#> INNER JOIN `df` AS `df...3`
#>   ON (`df...2`.`key` = `df...3`.`parent`)
#> INNER JOIN `df` AS `df...4`
#>   ON (`df...3`.`key` = `df...4`.`parent`)

Created on 2023-01-18 with reprex v2.0.2

Notice the fourth line after SELECT: df...3.key AS table4.key. I would expect it to read df...4.key AS table4.key (4 instead of 3), i.e.

#> SELECT
#>   `df...1`.`key` AS `table1.key`,
#>   `df...2`.`key` AS `table2.key`,
#>   `df...3`.`key` AS `table3.key`,
#>   `df...4`.`key` AS `table4.key`
#> FROM `df` AS `df...1`

Somehow the same column is selected twice with different names.
But I really want "table4.key" from "table4" to be selected.

Unfortunately this behavior seems to break some of my queries which were working before.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions