-
Notifications
You must be signed in to change notification settings - Fork 3
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/PyMongo: Amalgamate PyMongo driver to use CrateDB as backend #83
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3bec4b9
MongoDB/PyMongo: Add transparent driver adapter for PyMongo/CrateDB
amotl f375710
MongoDB/PyMongo: Configure dynamic column policy with SQLAlchemy dialect
amotl 362c24e
MongoDB/PyMongo: Add querying capabilities using JessiQL
amotl 8e99d22
MongoDB/PyMongo: Add software tests and CI configuration
amotl 3cd7ac3
MongoDB/PyMongo: Naming things
amotl 746d7c2
MongoDB/PyMongo: Manifest README as documentation page
amotl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
.eggs | ||
.env | ||
.idea | ||
.mypy_cache | ||
*.pyc | ||
.venv* | ||
__pycache__ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .api import PyMongoCrateDBAdapter # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from unittest.mock import patch | ||
|
||
import pymongo.collection | ||
|
||
from cratedb_toolkit.adapter.pymongo.collection import collection_factory | ||
from cratedb_toolkit.sqlalchemy.patch import patch_types_map | ||
from cratedb_toolkit.util import DatabaseAdapter | ||
from cratedb_toolkit.util.pandas import patch_pandas_sqltable_with_extended_mapping | ||
|
||
|
||
class PyMongoCrateDBAdapter: | ||
""" | ||
Patch PyMongo to talk to CrateDB. | ||
""" | ||
|
||
def __init__(self, dburi: str): | ||
self.cratedb = DatabaseAdapter(dburi=dburi) | ||
self.collection_backup = pymongo.collection.Collection | ||
|
||
collection_patched = collection_factory(cratedb=self.cratedb) # type: ignore[misc] | ||
self.patches = [ | ||
# Patch PyMongo's `Collection` implementation. | ||
patch("pymongo.collection.Collection", collection_patched), | ||
patch("pymongo.database.Collection", collection_patched), | ||
# Converge a few low-level functions of PyMongo to no-ops. | ||
patch("pymongo.mongo_client.MongoClient._ensure_session"), | ||
patch("pymongo.mongo_client._ClientConnectionRetryable._get_server"), | ||
] | ||
|
||
def start(self): | ||
self.adjust_sqlalchemy() | ||
self.activate() | ||
|
||
def stop(self): | ||
self.deactivate() | ||
|
||
def __enter__(self): | ||
self.start() | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.stop() | ||
|
||
def adjust_sqlalchemy(self): | ||
""" | ||
Configure CrateDB SQLAlchemy dialect. | ||
|
||
Setting the CrateDB column policy to `dynamic` means that new columns | ||
can be added without needing to explicitly change the table definition | ||
by running corresponding `ALTER TABLE` statements. | ||
|
||
https://cratedb.com/docs/crate/reference/en/latest/general/ddl/column-policy.html#dynamic | ||
""" | ||
# 1. Patch data types for CrateDB dialect. | ||
# TODO: Upstream to `sqlalchemy-cratedb`. | ||
patch_types_map() | ||
|
||
# 2. Prepare pandas. | ||
# TODO: Provide unpatching hook. | ||
# TODO: Use `with table_kwargs(...)`. | ||
from cratedb_toolkit.util.pandas import patch_pandas_sqltable_with_dialect_parameters | ||
|
||
patch_pandas_sqltable_with_dialect_parameters(table_kwargs={"crate_column_policy": "'dynamic'"}) | ||
patch_pandas_sqltable_with_extended_mapping() | ||
|
||
def activate(self): | ||
""" | ||
Swap in the MongoDB -> CrateDB adapter, by patching functions in PyMongo. | ||
""" | ||
for patch_ in self.patches: | ||
patch_.start() | ||
|
||
def deactivate(self): | ||
""" | ||
Swap out the MongoDB -> CrateDB adapter, by restoring patched functions. | ||
""" | ||
for patch_ in self.patches: | ||
patch_.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# PyMongo CrateDB Adapter Backlog | ||
|
||
## Iteration +1 | ||
- Upstream / converge patches. | ||
- `cratedb_toolkit/sqlalchemy/patch.py` | ||
- `cratedb_toolkit/util/pandas.py` | ||
- `cratedb_toolkit/adapter/pymongo/api.py::adjust_sqlalchemy` | ||
- `cratedb_toolkit/adapter/pymongo/collection.py::insert_returning_id` | ||
- Make `jessiql` work with more recent SQLAlchemy 2.x. | ||
- Make adapter work with pandas 2.2. | ||
|
||
## Iteration +2 | ||
- Add documentation. | ||
- Add missing essential querying features: Examples: sort order, skip, limit | ||
- Add missing essential methods. Example: `db.my_collection.drop()`. | ||
|
||
## Iteration +2 | ||
- Make write-synchronization behavior (refresh table) configurable. | ||
- Handle deeply nested documents of various types. | ||
- Unlock more features from canonical examples. | ||
https://pymongo.readthedocs.io/en/stable/examples/ | ||
|
||
## Iteration +3 | ||
- Decoding timestamps are yielding only Decimals instead of datetime objects | ||
when printed on the terminal? See example program. | ||
There is also a warning: | ||
```shell | ||
SAWarning: Dialect crate+crate-python does *not* support Decimal objects natively, | ||
and SQLAlchemy must convert from floating point - rounding errors and other issues | ||
may occur. Please consider storing Decimal numbers as strings or integers on this | ||
platform for lossless storage. | ||
``` | ||
So, why are `Decimal` types used here, at all? | ||
- Mimic MongoDB exceptions. | ||
Example: | ||
```python | ||
jessiql.exc.InvalidColumnError: Invalid column "x" for "Surrogate" specified in sort | ||
``` | ||
|
||
|
||
## Done | ||
|
||
- Make it work | ||
- Using individual columns for fields does not work, because `insert_one` works | ||
iteratively, and needs to evolve the table schema gradually. As a consequence, | ||
we need to use `OBJECT(DYNAMIC)` for storing MongoDB fields. | ||
- Add software tests | ||
|
||
### Research | ||
How to translate a MongoDB query expression? | ||
|
||
- https://github.com/gordonbusyman/mongo-to-sql-converter | ||
- https://github.com/2do2go/json-sql | ||
|
||
- https://github.com/kolypto/py-mongosql | ||
- https://github.com/SY-Xuan/mongo2sql | ||
- https://github.com/nsragow/MongoToSqlParse | ||
- https://github.com/sushmitharao2124/MongoToSQLConverter | ||
- https://github.com/Phomint/MongoSQL |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be generalized / upstreamed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a corresponding note to the backlog.