Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mssql sql_runif() to return random number for each row in result set #1504

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions R/backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ simulate_mssql <- function(version = "15.0") {
is.na = mssql_is_null,

runif = function(n = n(), min = 0, max = 1) {
sql_runif(RAND(), n = {{ n }}, min = min, max = max)
# https://stackoverflow.com/a/9039661
sql_runif(RAND(CHECKSUM(NEWID())), n = {{ n }}, min = min, max = max)
},

# string functions ------------------------------------------------
Expand Down Expand Up @@ -659,4 +660,4 @@ bit_to_boolean <- function(x_expr) {
}
}

utils::globalVariables(c("BIT", "CAST", "%AS%", "%is%", "convert", "DATE", "DATEADD", "DATEFROMPARTS", "DATEDIFF", "DATENAME", "DATEPART", "IIF", "NOT", "SUBSTRING", "LTRIM", "RTRIM", "CHARINDEX", "SYSDATETIME", "SECOND", "MINUTE", "HOUR", "DAY", "DAYOFWEEK", "DAYOFYEAR", "MONTH", "QUARTER", "YEAR", "BIGINT", "INT", "%AND%", "%BETWEEN%"))
utils::globalVariables(c("BIT", "CAST", "%AS%", "%is%", "convert", "DATE", "DATEADD", "DATEFROMPARTS", "DATEDIFF", "DATENAME", "DATEPART", "IIF", "NOT", "SUBSTRING", "LTRIM", "RTRIM", "CHARINDEX", "SYSDATETIME", "SECOND", "MINUTE", "HOUR", "DAY", "DAYOFWEEK", "DAYOFYEAR", "MONTH", "QUARTER", "YEAR", "BIGINT", "INT", "%AND%", "%BETWEEN%", "CHECKSUM", "NEWID"))
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/backend-mssql.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
<SQL>
SELECT `x`
FROM (
SELECT `df`.*, ROW_NUMBER() OVER (ORDER BY RAND()) AS `col01`
SELECT `df`.*, ROW_NUMBER() OVER (ORDER BY RAND(CHECKSUM(NEWID()))) AS `col01`
FROM `df`
) AS `q01`
WHERE (`col01` <= 1)
Expand Down
Loading