lyft / lyft/pynamodb-attributes

`range_key_condition` Doesn't work with TimestampMSAttribute

Open
#51 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
62
Forks
12
PR merge metrics
No merged PRs in 30d

Description

The table:

```py
class EventTypeIndex(GlobalSecondaryIndex):
class Meta:
index_name = "EventTypeIndex"
projection = AllProjection()

event_type = UnicodeEnumAttribute(
LogEventTypeEnum,
hash_key=True,
unknown_value=LogEventTypeEnum.GENERIC,
default_for_new=LogEventTypeEnum.GENERIC,
)

class Logs(Model):
class Meta:
table_name = "Logs"
region = Session().get_config_variable("region")
billing_mode = PAY_PER_REQUEST_BILLING_MODE

user_id = NumberAttribute(hash_key=True)
timestamp = TimestampMsAttribute(range_key=True)

event_type = UnicodeEnumAttribute(
LogEventTypeEnum, unknown_value=LogEventTypeEnum.GENERIC, default_for_new=LogEventTypeEnum.GENERIC
)
event_type_index = EventTypeIndex()
content = UnicodeAttribute(null=True)

```

The crash :
```python
[i for i in Logs.query(hash_key=9152, range_key_condition=Logs.timestamp.between(0, 9999999999999999))]
Traceback (most recent call last):
File "/Users/seluj78/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
coro = func()
^^^^^^
File "", line 1, in
File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/attributes.py", line 176, in between
return Path(self).between(lower, upper)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 79, in between
return Between(self, self._to_operand(lower), self._to_operand(upper))
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 45, in _to_operand
return Path(value) if isinstance(value, Attribute) else self._to_value(value)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 328, in _to_value
return Value(value, attribute=self.attribute)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 200, in __init__
(self.attr_type, value) = Value.__serialize(value, attribute)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 221, in __serialize
return attribute.attr_type, attribute.serialize(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb_attributes/timestamp.py", line 26, in serialize
return str(int(value.timestamp() * self._multiplier))
^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'timestamp'
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the query using Logs.timestamp.between(0, 9999999999999999), then inspect TimestampMsAttribute.serialize and the range_key_condition/between expression path shown in the traceback. Done means the range condition no longer raises for the supported timestamp bounds and a regression test covers this query.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.