Skip to content

Comments

Support both dbplyr < 2.6.0 and >= 2.6.0 with backwards compatibility#2032

Draft
Copilot wants to merge 11 commits intomainfrom
copilot/fix-glue-sql2-error
Draft

Support both dbplyr < 2.6.0 and >= 2.6.0 with backwards compatibility#2032
Copilot wants to merge 11 commits intomainfrom
copilot/fix-glue-sql2-error

Conversation

Copy link
Contributor

Copilot AI commented Feb 15, 2026

glue_sql2 is removed in dbplyr 2.6.0+, breaking n_distinct() translation.

Changes

  • Add memoised has_sql_glue() function to detect dbplyr version
  • Use dbplyr::sql_glue() when available (dbplyr >= 2.6.0)
  • Fall back to glue_sql2() for older versions (< 2.6.0)
  • No version constraint in DESCRIPTION - works with all dbplyr versions
  • Add TODO(2031) comments for removing compatibility code

Implementation

The code now conditionally uses the appropriate function based on dbplyr version:

  • dbplyr >= 2.6.0: Uses exported sql_glue() and sql()
  • dbplyr < 2.6.0: Uses glue_sql2() via pkg_method()

Version detection is memoised for performance, checking only once per R session.

Example

library(dplyr)
library(dbplyr)
library(duckdb)

con <- dbConnect(duckdb())
lazy_frame(x = c(1, 2), con = con) %>%
  summarise(n = n_distinct(x)) %>%
  show_query()
# <SQL> SELECT COUNT(DISTINCT row(x)) AS n FROM df

Works with all dbplyr versions.

Original prompt

This section details on the original issue you should resolve

<issue_title>glue_sql2 not a method in development version of dbplyr</issue_title>
<issue_description>I get the below error when using the development version of dbplyr.

I believe it is because glue_sql2 is no longer a method in the development version of dbplyr, breaking

glue_sql2 <- pkg_method("glue_sql2", "dbplyr")

library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)
packageVersion("dbplyr")
#> [1] '2.5.1.9000'
library(duckdb, , warn.conflicts = FALSE)
#> Loading required package: DBI
packageVersion("duckdb")
#> [1] '1.4.3.9002'

lazy_frame(x = c(1, 2), con = simulate_duckdb()) |>
  summarise(
    n_unique = n_distinct(x)
  ) |>
  show_query()
#> Error in get(x, envir = ns, inherits = FALSE): object 'glue_sql2' not found

Created on 2026-01-04 with reprex v2.1.1</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 15, 2026 17:19
- glue_sql2 no longer exists in dbplyr 2.5.1+
- Use build_sql with manual comma insertion for column lists
- All n_distinct tests passing (165 tests pass, 1 unrelated clock test fails)

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
- Pre-allocate col_parts list for better performance
- Simplify nested paste0 calls for better readability
- All tests still pass (165 tests pass, 1 unrelated clock test fails)

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix glue_sql2 method error in dbplyr development version Replace glue_sql2 with build_sql for dbplyr 2.5.1+ compatibility Feb 15, 2026
Copilot AI requested a review from krlmlr February 15, 2026 17:22
@krlmlr
Copy link
Collaborator

krlmlr commented Feb 15, 2026

@copilot: Target specifically dbplyr 2.5.2, fall back to the existing implementation if dbplyr 2.5.1 or earlier is detected. Memoise the detection. In dbplyr 2.5.2, sql_glue() is an exported function, no need to look up unexported functions.

- Add memoised has_sql_glue() detection function
- Use exported sql_glue() for dbplyr >= 2.5.2
- Fallback to glue_sql2() for dbplyr <= 2.5.1
- All 168 tests pass

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 15, 2026

@copilot: Target specifically dbplyr 2.5.2, fall back to the existing implementation if dbplyr 2.5.1 or earlier is detected. Memoise the detection. In dbplyr 2.5.2, sql_glue() is an exported functio...

Implemented in commit 3908bf7:

  • Added memoised has_sql_glue() function that detects if sql_glue is exported
  • Uses sql_glue() for dbplyr >= 2.5.2
  • Falls back to glue_sql2() for dbplyr <= 2.5.1
  • All 168 tests pass with dbplyr 2.5.1 (using glue_sql2 path)

Copilot AI changed the title Replace glue_sql2 with build_sql for dbplyr 2.5.1+ compatibility Use sql_glue for dbplyr 2.5.2+, fallback to glue_sql2 for compatibility Feb 15, 2026
Copy link
Collaborator

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot: Address comments.

})

duckdb_n_distinct <- function(..., na.rm = FALSE) {
sql <- pkg_method("sql", "dbplyr")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be dbplyr::sql, is this an exported function? Can we inline it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use dbplyr::sql directly in commit 5f0cd3e. The function is inlined where used.

return(glue_sql2(con, "COUNT(DISTINCT {.col {list(...)}*})"))
if (has_sql_glue()) {
# dbplyr >= 2.5.2: use exported sql_glue()
sql_glue <- pkg_method("sql_glue", "dbplyr")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, can this be dbplyr::sql_glue ? Can we inline it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use dbplyr::sql_glue directly in commit 5f0cd3e. The function is inlined where used.

- Use dbplyr::sql and dbplyr::sql_glue directly (exported functions)
- Remove version detection and fallback to glue_sql2
- Require dbplyr >= 2.5.2 in DESCRIPTION
- Clear error message if dbplyr is too old

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 15, 2026

@copilot: Address comments.

All comments addressed in commit 5f0cd3e:

  • Use dbplyr::sql and dbplyr::sql_glue directly
  • Removed version detection and fallback code
  • Added dbplyr (>= 2.5.2) requirement in DESCRIPTION

Copilot AI changed the title Use sql_glue for dbplyr 2.5.2+, fallback to glue_sql2 for compatibility Use dbplyr::sql_glue directly, require dbplyr >= 2.5.2 Feb 15, 2026
Copilot AI requested a review from krlmlr February 15, 2026 17:51
Copy link
Collaborator

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot: This still needs a clear error message if dbplyr is too old. Add and use a memoised function that checks for the availability of dbplyr::sql_glue() .

- Add check_sql_glue() function with memoisation
- Provides clear, actionable error when dbplyr is too old
- Error message: "This version of duckdb requires dbplyr >= 2.5.2"
- Memoisation makes repeated checks ~1000x faster

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 15, 2026

@copilot: This still needs a clear error message if dbplyr is too old. Add and use a memoised function that checks for the availability of dbplyr::sql_glue() .

Added in commit 06dc487. The check_sql_glue() function:

  • Checks for dbplyr::sql_glue() availability
  • Provides clear error: "This version of duckdb requires dbplyr >= 2.5.2. Please update dbplyr: install.packages('dbplyr')"
  • Memoised for performance (0.33ms on subsequent calls vs 294ms on first call)

Copilot AI changed the title Use dbplyr::sql_glue directly, require dbplyr >= 2.5.2 Use dbplyr::sql_glue directly with clear error message, require dbplyr >= 2.5.2 Feb 15, 2026
Copilot AI requested a review from krlmlr February 15, 2026 18:03
@krlmlr
Copy link
Collaborator

krlmlr commented Feb 16, 2026

@copilot: I was mistaken, the functionality is only available for dbplyr >= 2.6.0 . Remove the version dependency, implement functionality for both cases, depending on whether sql_glue() exists or not. Add comments to remove compat code in 2031.

krlmlr and others added 2 commits February 16, 2026 06:13
- Rename check_sql_glue to has_sql_glue, returns TRUE/FALSE
- Conditionally use sql_glue (dbplyr >= 2.6.0) or glue_sql2 (older)
- Remove dbplyr version constraint from DESCRIPTION
- Add TODO(2031) comments for removing compat code
- All 168 tests pass with dbplyr 2.5.2 (glue_sql2 path)

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 16, 2026

@copilot: I was mistaken, the functionality is only available for dbplyr >= 2.6.0 . Remove the version dependency, implement functionality for both cases, depending on whether sql_glue() exists or n...

Implemented in commit 1104cd8:

  • Renamed check_sql_glue to has_sql_glue, returns TRUE/FALSE
  • Conditionally uses sql_glue() (dbplyr >= 2.6.0) or glue_sql2() (older versions)
  • Removed dbplyr (>= 2.5.2) version constraint from DESCRIPTION
  • Added TODO(2031) comments for removing compat code
  • All 168 tests pass with dbplyr 2.5.2 (using glue_sql2 path)

Copilot AI changed the title Use dbplyr::sql_glue directly with clear error message, require dbplyr >= 2.5.2 Support both dbplyr < 2.6.0 and >= 2.6.0 with backwards compatibility Feb 16, 2026
@krlmlr
Copy link
Collaborator

krlmlr commented Feb 16, 2026

@hadley: Would this work with both dbplyr 2.5.x and 2.6.x?

@hadley
Copy link

hadley commented Feb 16, 2026

I already added sql_glue() back and I'm not seeing any revdep issues for duckdb on my end.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

glue_sql2 not a method in development version of dbplyr

3 participants