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

feat: Filter collections #36

Merged
merged 13 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions meltano.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ plugins:
description: |
An optional prefix which will be added to each stream name.
value: ''
- name: filter_collections
description: |
Collections to discover (default: all). Useful for improving catalog discovery performance.
- name: start_date
kind: date_iso8601
description: |
Expand Down
13 changes: 11 additions & 2 deletions tap_mongodb/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys
from logging import Logger, getLogger
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, Union

from pymongo import MongoClient
from pymongo.database import Database
Expand Down Expand Up @@ -36,12 +36,14 @@ def __init__( # pylint: disable=too-many-arguments
db_name: str,
datetime_conversion: str,
prefix: Optional[str] = None,
collections: Optional[Union[str, List[str]]] = None,
) -> None:
self._connection_string = connection_string
self._options = options
self._db_name = db_name
self._datetime_conversion: str = datetime_conversion.upper()
self._prefix: Optional[str] = prefix
self._collections = [collections] if isinstance(collections, str) else collections
self._logger: Logger = getLogger(__name__)
self._version: Optional[MongoVersion] = None

Expand Down Expand Up @@ -116,7 +118,14 @@ def discover_catalog_entries(self) -> List[Dict[str, Any]]:
The discovered catalog entries as a list.
"""
result: List[Dict] = []
for collection in self.database.list_collection_names(authorizedCollections=True, nameOnly=True):

collections = self.database.list_collection_names(
authorizedCollections=True,
nameOnly=True,
filter={"$or": [{"name": c} for c in self._collections]} if self._collections else None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filter={"$or": [{"name": c} for c in self._collections]} if self._collections else None,
filter={"name": {"$in": self._collections}} if self._collections else None,

This would be ideal, but I get the following error:

pymongo.errors.OperationFailure: can't get regex from filter doc not a regex, full error: {'ok': 0, 'errmsg': "can't get regex from filter doc not a regex", 'code': 8000, 'codeName': 'AtlasError'}

Can't see any indication from docs that this isn't supported:

)

for collection in collections:
try:
self.database[collection].find_one()
except PyMongoError:
Expand Down
13 changes: 13 additions & 0 deletions tap_mongodb/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ class TapMongoDB(Tap):
default="",
description="An optional prefix which will be added to each stream name.",
),
th.Property(
"filter_collections",
th.OneOf(
th.StringType,
th.ArrayType(th.StringType),
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Meltano doesn't yet support setting union types and that is arguably how most of the time this tap will be used, wdyt of only accepting an array of strings for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel that strongly about the union type - this just started out as a POC for @melgazar9 who wanted to filter one collection at a time, so I thought it made sense to support that pattern as a string also. I think the Meltano setting definition needs to be kind: array as you pointed out, but that shouldn't have any bearing here. If you think it's confusing, I'm all for removing it in favour of a better UX/DX. 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove it. It adds little benefit and complicates things for automatic settings generation from the tap's --about output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required=True,
default=[],
description=(
"Collections to discover (default: all). Useful for improving catalog discovery performance."
),
),
th.Property(
"start_date",
th.DateTimeType,
Expand Down Expand Up @@ -210,6 +222,7 @@ def connector(self) -> MongoDBConnector:
self.config.get("database"),
self.config.get("datetime_conversion"),
prefix=self.config.get("prefix", None),
collections=self.config["filter_collections"],
)

@property
Expand Down