Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB: Improve type mapper discriminating between INTEGER and BIGINT #235

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
## Unreleased
- Dependencies: Unpin commons-codec, to always use the latest version
- Dependencies: Unpin lorrystream, to always use the latest version
- MongoDB: Improve type mapper by discriminating between
`INTEGER` and `BIGINT`

## 2024/08/19 v0.0.17
- Processor: Updated Kinesis Lambda processor to understand AWS DMS
Expand Down
16 changes: 14 additions & 2 deletions cratedb_toolkit/io/mongodb/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,17 @@ def extract_schema_from_array(array: list, schema: dict):
}


def get_type(o):
return TYPES_MAP.get(type(o), "UNKNOWN")
def get_type(value):
"""
Resolve value type via type map, with special treatment for integer types.

INTEGER: -2^31 to 2^31-1
BIGINT: -2^63 to 2^63-1
"""
type_ = type(value)
surister marked this conversation as resolved.
Show resolved Hide resolved
if type_ is int:
if -(2**31) <= value <= 2**31 - 1:
return "INTEGER"
else:
return "BIGINT"
return TYPES_MAP.get(type_, "UNKNOWN")
15 changes: 15 additions & 0 deletions tests/io/mongodb/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ def test_primitive_types(self):
schema = trim_schema(extract.extract_schema_from_document(data, {}))
self.assertDictEqual(schema, expected)

def test_integer_types(self):
"""
Validate extraction of numeric types INTEGER vs. BIGINT.
"""
data = {
"integer": 2147483647,
"bigint": 1563051934000,
}
expected = {
"integer": "INTEGER",
"bigint": "BIGINT",
}
schema = trim_schema(extract.extract_schema_from_document(data, {}))
self.assertDictEqual(schema, expected)

def test_bson_types(self):
data = {
"a": bson.ObjectId("55153a8014829a865bbf700d"),
Expand Down