I cannot run a reprex just now, but the following does not take into account the partition column x which is incorrect.
library(dplyr)
library(duckdb)
library(DBI)
df <- tibble(x = c(1, 1, 2), y = c(1, 1, 2))
con <- DBI::dbConnect(duckdb::duckdb())
duckdb::duckdb_register(con, "df", df)
tbl(con, "df") |> mutate(z = n_distinct(y), .by = x) |> show_query()
I am planning to provide a fix on the upcoming weekend.