Skip to content

Commit

Permalink
MongoDB: Handle too large $date.$numberLong values gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 30, 2024
1 parent adcb2ff commit 9688e9a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
- DynamoDB: Change CrateDB data model to use (`pk`, `data`, `aux`) columns
Attention: This is a breaking change.
- MongoDB: Handle too large `$date.$numberLong` values gracefully

## 2024/09/26 v0.0.19
- DynamoDB CDC: Fix `MODIFY` operation by propagating `NewImage` fully
Expand Down
6 changes: 5 additions & 1 deletion src/commons_codec/transform/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ def decode_extended_json(self, value: t.Dict[str, t.Any]) -> t.Any:
type_ = next(iter(value)) # Get key of first item in dictionary.
is_date_numberlong = type_ == "$date" and "$numberLong" in value["$date"]
if is_date_numberlong:
out = dt.datetime.fromtimestamp(int(value["$date"]["$numberLong"]) / 1000, tz=dt.timezone.utc)
try:
out = dt.datetime.fromtimestamp(int(value["$date"]["$numberLong"]) / 1000, tz=dt.timezone.utc)
except ValueError as ex:
logger.error(f"Decoding legacy timestamp failed: {ex}. value={value}")
out = 0
else:
out = object_hook(value)

Expand Down
8 changes: 6 additions & 2 deletions tests/transform/mongodb/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
"code_bytes": {"$code": "ab\u0000ab\u0000"},
"code_scope": {"$code": "abab", "$scope": {"x": {"$numberInt": "42"}}},
"date_iso8601": {"$date": "2015-09-23T10:32:42.33Z"},
"date_numberlong": {"$date": {"$numberLong": "1356351330000"}},
"date_numberlong_valid": {"$date": {"$numberLong": "1356351330000"}},
"date_numberlong_invalid": {
"$date": {"$numberLong": "-9223372036854775808"}
}, # year -292275055 is out of range
"dbref": {
"$id": {"$oid": "56027fcae4b09385a85f9344"},
"$ref": "foo",
Expand Down Expand Up @@ -169,7 +172,8 @@
},
},
"date_iso8601": 1443004362000,
"date_numberlong": 1356351330000,
"date_numberlong_valid": 1356351330000,
"date_numberlong_invalid": 0,
"dbref": {
"$id": "56027fcae4b09385a85f9344",
"$ref": "foo",
Expand Down

0 comments on commit 9688e9a

Please sign in to comment.