seungoh-lee commented on code in PR #66013: URL: https://github.com/apache/airflow/pull/66013#discussion_r3325241478
########## providers/google/src/airflow/providers/google/cloud/transfers/mongo_to_gcs.py: ########## @@ -0,0 +1,204 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""MongoDB to GCS operator.""" + +from __future__ import annotations + +import base64 +import json +from collections.abc import Iterator, Sequence +from datetime import date, datetime, time +from decimal import Decimal +from functools import cached_property +from typing import Any + +from bson import ObjectId +from bson.decimal128 import Decimal128 + +from airflow.providers.google.cloud.transfers.sql_to_gcs import BaseSQLToGCSOperator +from airflow.providers.mongo.hooks.mongo import MongoHook + + +class _MongoCursorAdapter: + """ + Wrap a pymongo cursor as a DB-API 2.0 style cursor. + + ``BaseSQLToGCSOperator`` consumes ``cursor.description`` to derive the + BigQuery schema and iterates the cursor expecting tuples. MongoDB documents + are dict-shaped and have no fixed schema; this adapter: + + * Peeks the first document to derive ``description`` (column names and + Python types). + * Re-yields the first document, then iterates the rest. + * Converts each document to a tuple in the derived column order, filling + missing fields with ``None``. + """ + + def __init__(self, cursor: Any) -> None: + self._cursor = iter(cursor) + self._first: dict | None = None + self._description: list[tuple] = [] + try: + self._first = next(self._cursor) + except StopIteration: + return + self._description = [ + (name, type(value), None, None, None, None, True) for name, value in self._first.items() + ] + + @property + def description(self) -> list[tuple]: + return self._description + + def __iter__(self) -> Iterator[tuple]: + if self._first is None: + return + names = [d[0] for d in self._description] + yield tuple(self._first.get(n) for n in names) + for doc in self._cursor: + yield tuple(doc.get(n) for n in names) + + +class MongoToGCSOperator(BaseSQLToGCSOperator): Review Comment: added a note to the class docstring explaining that -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
