|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from typing import IO, Any, Dict, Iterator, List, Sequence, TYPE_CHECKING, Optional |
2 | 4 | import math |
3 | 5 |
|
4 | 6 | import sqlalchemy as sa |
5 | 7 |
|
| 8 | +from dlt.common import logger |
6 | 9 | from dlt.common.destination.client import ( |
7 | 10 | RunnableLoadJob, |
8 | 11 | HasFollowupJobs, |
9 | 12 | PreparedTableSchema, |
10 | 13 | ) |
11 | 14 | from dlt.common.storages import FileStorage |
12 | 15 | from dlt.common.json import json, PY_DATETIME_DECODERS |
13 | | -from dlt.destinations.sql_jobs import SqlFollowupJob |
14 | 16 |
|
| 17 | +from dlt.destinations._adbc_jobs import AdbcParquetCopyJob |
| 18 | +from dlt.destinations.sql_jobs import SqlFollowupJob |
15 | 19 | from dlt.destinations.impl.sqlalchemy.db_api_client import SqlalchemyClient |
16 | 20 | from dlt.destinations.impl.sqlalchemy.merge_job import SqlalchemyMergeFollowupJob |
17 | 21 |
|
@@ -74,6 +78,90 @@ def run(self) -> None: |
74 | 78 | _sql_client.execute_sql(table.insert(), chunk) |
75 | 79 |
|
76 | 80 |
|
| 81 | +class SqlalchemyParquetADBCJob(AdbcParquetCopyJob): |
| 82 | + """ADBC Parquet copy job for SQLAlchemy (sqlite, mysql) with query param handling.""" |
| 83 | + |
| 84 | + def __init__(self, file_path: str, table: sa.Table) -> None: |
| 85 | + super().__init__(file_path) |
| 86 | + self._job_client: "SqlalchemyJobClient" = None |
| 87 | + self.table = table |
| 88 | + |
| 89 | + if TYPE_CHECKING: |
| 90 | + from adbc_driver_manager.dbapi import Connection |
| 91 | + |
| 92 | + def _connect(self) -> Connection: |
| 93 | + from adbc_driver_manager import dbapi |
| 94 | + |
| 95 | + engine = self._job_client.config.credentials.engine |
| 96 | + dialect = engine.dialect.name.lower() |
| 97 | + url = engine.url |
| 98 | + |
| 99 | + query = dict(url.query or {}) |
| 100 | + |
| 101 | + if dialect == "sqlite": |
| 102 | + # disable schema and catalog when ingest |
| 103 | + self._connect_schema_name = "" |
| 104 | + self._connect_catalog_name = "" |
| 105 | + |
| 106 | + # attach directly to dataset sqlite file as "main" |
| 107 | + if self._job_client.sql_client.dataset_name == "main": |
| 108 | + db_path = url.database |
| 109 | + else: |
| 110 | + db_path = self._job_client.sql_client._sqlite_dataset_filename( |
| 111 | + self._job_client.sql_client.dataset_name |
| 112 | + ) |
| 113 | + conn_str = f"file:{db_path}" |
| 114 | + |
| 115 | + if query: |
| 116 | + qs = "&".join(f"{k}={v}" for k, v in query.items()) |
| 117 | + conn_str = f"{conn_str}?{qs}" |
| 118 | + |
| 119 | + logger.info(f"ADBC connect to {conn_str}") |
| 120 | + return dbapi.connect(driver="sqlite", db_kwargs={"uri": conn_str}) |
| 121 | + |
| 122 | + elif dialect == "mysql": |
| 123 | + # disable schema and catalog when ingest |
| 124 | + self._connect_schema_name = "" |
| 125 | + self._connect_catalog_name = "" |
| 126 | + |
| 127 | + # mysql: convert SSL params into go-mysql ADBC parameters |
| 128 | + mapped = {} |
| 129 | + for k, v in query.items(): |
| 130 | + lk = k.lower() |
| 131 | + if lk == "ssl_ca": |
| 132 | + mapped["tls-ca"] = v |
| 133 | + elif lk == "ssl_cert": |
| 134 | + mapped["tls-cert"] = v |
| 135 | + elif lk == "ssl_key": |
| 136 | + mapped["tls-key"] = v |
| 137 | + elif lk == "ssl_mode": |
| 138 | + mapped["tls"] = v |
| 139 | + else: |
| 140 | + mapped[k] = v |
| 141 | + |
| 142 | + username = url.username or "" |
| 143 | + password = url.password or "" |
| 144 | + auth = f"{username}:{password}@" if username or password else "" |
| 145 | + |
| 146 | + host = url.host or "localhost" |
| 147 | + port = url.port or 3306 |
| 148 | + # dataset name is schema name is database name. each database is a schema in mysql |
| 149 | + database = self._job_client.sql_client.dataset_name # url.database or "" |
| 150 | + |
| 151 | + base = f"{auth}tcp({host}:{port})/{database}" |
| 152 | + if mapped: |
| 153 | + qs = "&".join(f"{k}={v}" for k, v in mapped.items()) |
| 154 | + conn_str = f"{base}?{qs}" |
| 155 | + else: |
| 156 | + conn_str = base |
| 157 | + |
| 158 | + logger.info(f"ADBC connect to {conn_str}") |
| 159 | + return dbapi.connect(driver="mysql", db_kwargs={"uri": conn_str}) |
| 160 | + |
| 161 | + else: |
| 162 | + raise NotImplementedError(f"ADBC not supported for sqlalchemy dialect {dialect}") |
| 163 | + |
| 164 | + |
77 | 165 | class SqlalchemyParquetInsertJob(SqlalchemyJsonLInsertJob): |
78 | 166 | def _iter_data_item_chunks(self) -> Iterator[Sequence[Dict[str, Any]]]: |
79 | 167 | from dlt.common.libs.pyarrow import ParquetFile |
|
0 commit comments