Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.

Commit 4f02d1d

Browse files
Add bigframes.bigquery.rand() function
This change adds `bigframes.bigquery.rand()` which wraps the BigQuery `RAND()` function. It accepts a Series or DataFrame as input to determine the shape and index of the output Series. It includes a warning about non-determinism in the docstring. Additionally, `SqlScalarOp` has been updated to accept an `is_deterministic` argument, which is set to `False` for `rand()`. Tests are added in `tests/unit/bigquery/test_mathematical.py` and `tests/system/small/bigquery/test_mathematical.py`.
1 parent ca4aa76 commit 4f02d1d

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

bigframes/bigquery/_operations/mathematical.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from typing import Union
1818

19-
from bigframes import dataframe, dtypes
19+
from bigframes import dataframe
20+
from bigframes import dtypes
2021
from bigframes import operations as ops
2122
from bigframes import series
2223

@@ -62,5 +63,6 @@ def rand(input_data: Union[series.Series, dataframe.DataFrame]) -> series.Series
6263
op = ops.SqlScalarOp(
6364
_output_type=dtypes.FLOAT_DTYPE,
6465
sql_template="RAND()",
66+
is_deterministic=False,
6567
)
6668
return anchor._apply_nary_op(op, [])

bigframes/operations/generic_ops.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,15 @@ class SqlScalarOp(base_ops.NaryOp):
443443
name: typing.ClassVar[str] = "sql_scalar"
444444
_output_type: dtypes.ExpressionType
445445
sql_template: str
446+
is_deterministic: bool = True
446447

447448
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
448449
return self._output_type
449450

451+
@property
452+
def deterministic(self) -> bool:
453+
return self.is_deterministic
454+
450455

451456
@dataclasses.dataclass(frozen=True)
452457
class PyUdfOp(base_ops.NaryOp):

tests/unit/bigquery/test_mathematical.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_rand_calls_apply_nary_op():
3232
assert isinstance(op, ops.SqlScalarOp)
3333
assert op.sql_template == "RAND()"
3434
assert op._output_type == dtypes.FLOAT_DTYPE
35+
assert op.deterministic is False
3536
assert args[1] == []
3637

3738

0 commit comments

Comments
 (0)