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

Commit b8c6f24

Browse files
committed
pr feedback
1 parent 2691cc5 commit b8c6f24

2 files changed

Lines changed: 20 additions & 35 deletions

File tree

google/cloud/bigtable/data/mutations.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,50 +287,56 @@ def _to_dict(self) -> dict[str, Any]:
287287
@dataclass
288288
class AddToCell(Mutation):
289289
"""
290-
Mutation to add a value to an aggregate cell.
290+
Adds an int64 value to an aggregate cell. The column family must be an
291+
aggregate family and have an "int64" input type or this mutation will be
292+
rejected.
291293
294+
Note: The timestamp values are in microseconds but must match the
295+
granularity of the table (defaults to `MILLIS`). Therefore, the given value
296+
must be a multiple of 1000 (millisecond granularity). For example:
297+
`1571902339435000`.
292298
293299
Args:
294300
family: The name of the column family to which the cell belongs.
295301
qualifier: The column qualifier of the cell.
296302
value: The value to be accumulated into the cell.
297-
timestamp_micros: The timestamp of the cell.
303+
timestamp_micros: The timestamp of the cell. Must be provided for
304+
cell aggregation to work correctly.
305+
298306
299307
Raises:
300308
TypeError: If `qualifier` is not `bytes` or `str`.
301309
TypeError: If `value` is not `int`.
302-
ValueError: If `timestamp_micros` is less than `_SERVER_SIDE_TIMESTAMP`.
310+
TypeError: If `timestamp_micros` is not `int`.
311+
ValueError: If `timestamp_micros` is less than 0.
303312
"""
304313

305314
def __init__(
306315
self,
307316
family: str,
308317
qualifier: bytes | str,
309318
value: int,
310-
timestamp: int | None = None,
319+
timestamp_micros: int,
311320
):
312321
qualifier = qualifier.encode() if isinstance(qualifier, str) else qualifier
313322
if not isinstance(qualifier, bytes):
314323
raise TypeError("qualifier must be bytes or str")
315324
if not isinstance(value, int):
316325
raise TypeError("value must be int")
326+
if not isinstance(timestamp_micros, int):
327+
raise TypeError("value must be int")
317328
if abs(value) > _MAX_INCREMENT_VALUE:
318329
raise ValueError(
319330
"int values must be between -2**63 and 2**63 (64-bit signed int)"
320331
)
321332

322-
if timestamp is None:
323-
# use current timestamp, with milisecond precision
324-
timestamp = time.time_ns() // 1000
325-
timestamp = timestamp - (timestamp % 1000)
326-
327-
if timestamp < 0:
328-
raise ValueError("timestamp must be positive")
333+
if timestamp_micros < 0:
334+
raise ValueError("timestamp must be non-negative")
329335

330336
self.family = family
331337
self.qualifier = qualifier
332338
self.value = value
333-
self.timestamp = timestamp
339+
self.timestamp = timestamp_micros
334340

335341
def _to_dict(self) -> dict[str, Any]:
336342
return {

tests/unit/data/test_mutations.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -744,31 +744,10 @@ def test_ctor(self):
744744
assert instance.timestamp == expected_timestamp
745745

746746
def test_ctor_negative_timestamp(self):
747-
"""Only positive timestamps are valid"""
747+
"""Only non-negative timestamps are valid"""
748748
with pytest.raises(ValueError) as e:
749749
self._make_one("test-family", b"test-qualifier", 1234, -2)
750-
assert "timestamp must be positive" in str(e.value)
751-
752-
@pytest.mark.parametrize(
753-
"timestamp_ns,expected_timestamp_micros",
754-
[
755-
(0, 0),
756-
(1, 0),
757-
(123, 0),
758-
(999, 0),
759-
(999_999, 0),
760-
(1_000_000, 1000),
761-
(1_234_567, 1000),
762-
(1_999_999, 1000),
763-
(2_000_000, 2000),
764-
(1_234_567_890_123, 1_234_567_000),
765-
],
766-
)
767-
def test_ctor_no_timestamp(self, timestamp_ns, expected_timestamp_micros):
768-
"""If no timestamp is given, should use current time with millisecond precision"""
769-
with mock.patch("time.time_ns", return_value=timestamp_ns):
770-
instance = self._make_one("test-family", b"test-qualifier", 1234)
771-
assert instance.timestamp == expected_timestamp_micros
750+
assert "timestamp must be non-negative" in str(e.value)
772751

773752
def test__to_dict(self):
774753
"""ensure dict representation is as expected"""

0 commit comments

Comments
 (0)