Skip to content

Commit 3d7e98e

Browse files
committed
Ensure sql_runif() actually returns random numbers
Manual merge of PR by by @thomashulst. Fixes #1503. Closes #1504
1 parent ccb218b commit 3d7e98e

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# dbplyr (development version)
22

3+
* SQL server: `slice_sample()` returns different results each run (@thomashulst, #1503)
4+
* Corrected translation of `stringr::str_like()` to use case-sensitive `LIKE` when argument `ignore_case` is set as `FALSE` (@edward-burn, #1488).
35
* Corrected translation of `stringr::str_like()` to use case-sensitive `LIKE` when argument `ignore_case` is set as `FALSE` (@edward-burn, #1488).
46
* Fixed overwrite flag in `copy_to()` to work when source is in the same DB as destination (@liudvikasakelis, #1535)
57
* Snowflake correctly translates `$` to `:` (@jsowder, #1608)

R/backend-mssql.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ simulate_mssql <- function(version = "15.0") {
322322
is.na = mssql_is_null,
323323

324324
runif = function(n = n(), min = 0, max = 1) {
325-
sql_runif(RAND(), n = {{ n }}, min = min, max = max)
325+
# https://stackoverflow.com/a/9039661
326+
sql_runif(RAND(CHECKSUM(NEWID())), n = {{ n }}, min = min, max = max)
326327
},
327328

328329
# string functions ------------------------------------------------
@@ -747,5 +748,7 @@ utils::globalVariables(c(
747748
"BIGINT",
748749
"INT",
749750
"%AND%",
750-
"%BETWEEN%"
751+
"%BETWEEN%",
752+
"CHECKSUM",
753+
"NEWID"
751754
))

tests/testthat/_snaps/backend-mssql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
<SQL>
276276
SELECT `x`
277277
FROM (
278-
SELECT `df`.*, ROW_NUMBER() OVER (ORDER BY RAND()) AS `col01`
278+
SELECT `df`.*, ROW_NUMBER() OVER (ORDER BY RAND(CHECKSUM(NEWID()))) AS `col01`
279279
FROM `df`
280280
) AS `q01`
281281
WHERE (`col01` <= 1)

0 commit comments

Comments
 (0)