Skip to content
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

* `across(everything())` doesn't select grouping columns created via `.by` in
`summarise()` (@mgirlich, #1493).
* Use `COUNT_BIG` instead of `COUNT` for SQL server so that `tally()` and
`count()` work regardless of size of the data (@edward-burn, #1498).

* New translations of clock function `date_count_between()` for SQL server, Redshift, Snowflake, Postgres, and Spark (@edward-burn, #1495).

Expand Down
2 changes: 2 additions & 0 deletions R/backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ simulate_mssql <- function(version = "15.0") {
sql_variant(
mssql_scalar,
sql_translator(.parent = base_odbc_agg,
# https://learn.microsoft.com/en-us/sql/t-sql/functions/count-big-transact-sql?view=sql-server-ver17
n = function() sql("COUNT_BIG(*)"),
Copy link
Member

Choose a reason for hiding this comment

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

Could you please include a comment with a link to the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, now added!

sd = sql_aggregate("STDEV", "sd"),
var = sql_aggregate("VAR", "var"),
str_flatten = function(x, collapse = "") sql_expr(string_agg(!!x, !!collapse)),
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/backend-mssql.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@
FROM `df`
ORDER BY `y`

# count_big
Copy link
Member

Choose a reason for hiding this comment

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

Where did these snapshots come from? I don't see a corresponding test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, not sure how that happened. Have now added test and updated snapshots


Code
count(mf)
Output
<SQL>
SELECT COUNT_BIG(*) AS `n`
FROM `df`

# add prefix to temporary table

Code
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ test_that("row_number() with and without group_by() and arrange(): unordered def
expect_snapshot(mf %>% arrange(y) %>% mutate(rown = row_number()))
})

test_that("count_big", {
mf <- lazy_frame(x = c(1:5), y = c(rep("A", 5)), con = simulate_mssql())
expect_snapshot(count(mf))
})

# Live database -----------------------------------------------------------

test_that("can copy_to() and compute() with temporary tables (#438)", {
Expand Down
Loading