Skip to content

Commit

Permalink
fix(mssql): ensure ibis.random() generates a new value per call
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist committed Sep 19, 2024
1 parent 1c91d65 commit f1d35cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ibis/backends/sql/compilers/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ def to_sqlglot(
def visit_RandomUUID(self, op, **_):
return self.f.newid()

def visit_RandomScalar(self, op, **_):
# By default RAND() will generate the same value for all calls within a
# query. The standard way to work around this is to pass in a unique
# value per call, which `CHECKSUM(NEWID())` provides.
return self.f.rand(self.f.checksum(self.f.newid()))

def visit_StringLength(self, op, *, arg):
"""The MSSQL LEN function doesn't count trailing spaces.
Expand Down
12 changes: 12 additions & 0 deletions ibis/backends/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,18 @@ def test_random(con):
assert 0 <= result <= 1


@pytest.mark.notimpl(["polars"], raises=com.OperationNotDefinedError)
@pytest.mark.notimpl(["druid"], raises=PyDruidProgrammingError)
@pytest.mark.notimpl(
["risingwave"],
raises=PsycoPg2InternalError,
reason="function random() does not exist",
)
def test_random_different_per_row(alltypes):
result = alltypes.select("int_col", rand_col=ibis.random()).execute()
assert result.rand_col.nunique() > 1


@pytest.mark.parametrize(
("ibis_func", "pandas_func"),
[
Expand Down

0 comments on commit f1d35cc

Please sign in to comment.