atquery 2.19.0__tar.gz → 2.20.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {atquery-2.19.0 → atquery-2.20.1}/PKG-INFO +6 -6
- {atquery-2.19.0 → atquery-2.20.1}/pyproject.toml +4 -4
- {atquery-2.19.0 → atquery-2.20.1}/pyproject.toml.orig +4 -4
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/aio/query.py +10 -2
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/atquery2.py +15 -2
- atquery-2.20.1/src/atquery/cli/delta.py +253 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/main.py +10 -10
- atquery-2.20.1/src/atquery/delta/__init__.py +6 -0
- atquery-2.20.1/src/atquery/delta/_schema.py +70 -0
- {atquery-2.19.0/src/atquery/iceberg → atquery-2.20.1/src/atquery/delta}/cutoff.py +30 -43
- atquery-2.20.1/src/atquery/delta/exporter.py +331 -0
- atquery-2.19.0/src/atquery/cli/iceberg.py +0 -241
- atquery-2.19.0/src/atquery/iceberg/__init__.py +0 -6
- atquery-2.19.0/src/atquery/iceberg/_catalog.py +0 -478
- atquery-2.19.0/src/atquery/iceberg/_schema.py +0 -87
- atquery-2.19.0/src/atquery/iceberg/exporter.py +0 -252
- {atquery-2.19.0 → atquery-2.20.1}/LICENSE +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/README.md +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/aio/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/aio/api.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/aio/engine.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/api_v2.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/columns.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/csv.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/ddl.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/diff.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/ecd.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/migrate.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/patch.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/routines.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/scenario.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/schemas.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/cli/tables.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/duckdb.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/aio.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/core.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/dump/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/dump/__main__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/dump/core.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/meta.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/pooling.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/restore/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/restore/__main__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/mysql/restore/core.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/dialect/sqlite.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/diff/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/diff/comparator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/diff/generator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/diff/git_integration.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/diff/models.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/diff/parser.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/_toml.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/catalog.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/ddl.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/diff.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/erd.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/errors.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/model.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/parser.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/resolver.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/seed.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/ecd/skeleton.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/engine.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/factory.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/migration/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/migration/_migrator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/migration/_protocol.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/migration/_standard.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/migration/_types.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/_apply.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/_convert.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/_errors.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/_log.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/_meta.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/_model.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/patch/_sql.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/py.typed +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/testing/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/testing/creator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/testing/loader.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/testing/schema.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/testing/validator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.1}/src/atquery/type.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: atquery
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.20.1
|
|
4
4
|
Summary: A Python module for simplifying DBAPI usage with one-query-per-file SQL management.
|
|
5
5
|
Keywords: sql,database,dbapi,mysql,sqlite,duckdb
|
|
6
6
|
Author: alpine
|
|
@@ -18,19 +18,19 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
18
18
|
Classifier: Topic :: Database
|
|
19
19
|
Requires-Dist: tomli>=2.0.1 ; python_full_version < '3.11'
|
|
20
20
|
Requires-Dist: python-dotenv>=1.2.2 ; extra == 'all'
|
|
21
|
-
Requires-Dist: atquery[mysql,duckdb,testing,diff,
|
|
21
|
+
Requires-Dist: atquery[mysql,duckdb,testing,diff,delta-export] ; extra == 'all'
|
|
22
|
+
Requires-Dist: mysql-connector-python>=8.0,<10 ; extra == 'delta-export'
|
|
23
|
+
Requires-Dist: deltalake>=1.6.6 ; extra == 'delta-export'
|
|
24
|
+
Requires-Dist: pyarrow>=15.0.0 ; extra == 'delta-export'
|
|
22
25
|
Requires-Dist: sqlglot>=30.4.2 ; extra == 'diff'
|
|
23
26
|
Requires-Dist: duckdb>=0.10.0 ; extra == 'duckdb'
|
|
24
|
-
Requires-Dist: mysql-connector-python>=8.0,<10 ; extra == 'iceberg-export'
|
|
25
|
-
Requires-Dist: pyiceberg>=0.12.0 ; extra == 'iceberg-export'
|
|
26
|
-
Requires-Dist: pyarrow>=15.0.0 ; extra == 'iceberg-export'
|
|
27
27
|
Requires-Dist: mysql-connector-python>=8.0,<10 ; extra == 'mysql'
|
|
28
28
|
Requires-Dist: json5>=0.12.1 ; extra == 'testing'
|
|
29
29
|
Requires-Python: >=3.10
|
|
30
30
|
Provides-Extra: all
|
|
31
|
+
Provides-Extra: delta-export
|
|
31
32
|
Provides-Extra: diff
|
|
32
33
|
Provides-Extra: duckdb
|
|
33
|
-
Provides-Extra: iceberg-export
|
|
34
34
|
Provides-Extra: mysql
|
|
35
35
|
Provides-Extra: testing
|
|
36
36
|
Description-Content-Type: text/markdown
|
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "atquery"
|
|
7
|
-
version = "2.
|
|
7
|
+
version = "2.20.1"
|
|
8
8
|
description = "A Python module for simplifying DBAPI usage with one-query-per-file SQL management."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -41,16 +41,16 @@ atquery = "atquery.cli.main:main"
|
|
|
41
41
|
[project.optional-dependencies]
|
|
42
42
|
mysql = ["mysql-connector-python>=8.0,<10"]
|
|
43
43
|
duckdb = ["duckdb>=0.10.0"]
|
|
44
|
-
|
|
44
|
+
delta-export = [
|
|
45
45
|
"mysql-connector-python>=8.0,<10",
|
|
46
|
-
"
|
|
46
|
+
"deltalake>=1.6.6",
|
|
47
47
|
"pyarrow>=15.0.0",
|
|
48
48
|
]
|
|
49
49
|
testing = ["json5>=0.12.1"]
|
|
50
50
|
diff = ["sqlglot>=30.4.2"]
|
|
51
51
|
all = [
|
|
52
52
|
"python-dotenv>=1.2.2",
|
|
53
|
-
"atquery[mysql,duckdb,testing,diff,
|
|
53
|
+
"atquery[mysql,duckdb,testing,diff,delta-export]",
|
|
54
54
|
]
|
|
55
55
|
|
|
56
56
|
[dependency-groups]
|
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "atquery"
|
|
7
|
-
version = "2.
|
|
7
|
+
version = "2.20.1"
|
|
8
8
|
description = "A Python module for simplifying DBAPI usage with one-query-per-file SQL management."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -40,9 +40,9 @@ mysql = [
|
|
|
40
40
|
duckdb = [
|
|
41
41
|
"duckdb>=0.10.0",
|
|
42
42
|
]
|
|
43
|
-
|
|
43
|
+
delta-export = [
|
|
44
44
|
"mysql-connector-python>=8.0,<10",
|
|
45
|
-
"
|
|
45
|
+
"deltalake>=1.6.6",
|
|
46
46
|
"pyarrow>=15.0.0",
|
|
47
47
|
]
|
|
48
48
|
testing = [
|
|
@@ -53,7 +53,7 @@ diff = [
|
|
|
53
53
|
]
|
|
54
54
|
all = [
|
|
55
55
|
"python-dotenv>=1.2.2",
|
|
56
|
-
"atquery[mysql,duckdb,testing,diff,
|
|
56
|
+
"atquery[mysql,duckdb,testing,diff,delta-export]"
|
|
57
57
|
]
|
|
58
58
|
|
|
59
59
|
[dependency-groups]
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
SQLの組み立てには ATQuery を再利用し、実行メソッドのみ非同期で実装する。
|
|
4
4
|
"""
|
|
5
5
|
import csv
|
|
6
|
+
import json
|
|
6
7
|
import logging
|
|
7
8
|
from io import StringIO
|
|
8
9
|
from typing import (
|
|
@@ -150,10 +151,17 @@ class ATAsyncQuery:
|
|
|
150
151
|
return ATQuery._to_attrdict(row, json_decodes)
|
|
151
152
|
|
|
152
153
|
async def scalar(
|
|
153
|
-
self, conn: ATAsyncConnection, params: dict | None = None
|
|
154
|
+
self, conn: ATAsyncConnection, params: dict | None = None, json_decode: bool | None = None
|
|
154
155
|
) -> Any:
|
|
155
156
|
result = await self.execute(conn, params, buffered=True)
|
|
156
|
-
|
|
157
|
+
value = await result.scalar()
|
|
158
|
+
if value is None:
|
|
159
|
+
return None
|
|
160
|
+
if json_decode is None:
|
|
161
|
+
json_decode = result.column_types[:1] == ("JSON",)
|
|
162
|
+
if json_decode:
|
|
163
|
+
return json.loads(value, object_hook=AttrDict)
|
|
164
|
+
return value
|
|
157
165
|
|
|
158
166
|
async def count(
|
|
159
167
|
self,
|
|
@@ -370,17 +370,30 @@ class ATQuery:
|
|
|
370
370
|
json_decodes = [column_name for column_name, column_type in zip(result.column_names, result.column_types) if column_type == "JSON"]
|
|
371
371
|
return self.__class__._to_attrdict(row, json_decodes)
|
|
372
372
|
|
|
373
|
-
def scalar(self, conn: ATConnection, params: dict | None = None) -> object:
|
|
373
|
+
def scalar(self, conn: ATConnection, params: dict | None = None, json_decode: bool | None = None) -> object:
|
|
374
374
|
""" SQLを実行し、最初の値を取得します。
|
|
375
|
+
取得したカラムの型がJSONの場合、自動的にデコードされます(get/getallと同様の挙動)。
|
|
375
376
|
|
|
376
377
|
Args:
|
|
377
378
|
conn (ATConnection): コネクション
|
|
378
379
|
params (dict, optional): SQLに定義したパラメータ. Defaults to None.
|
|
380
|
+
json_decode (bool, optional):
|
|
381
|
+
JSONデコードの要否を明示的に指定します。
|
|
382
|
+
Noneの場合、カラム型がJSONであれば自動的にデコードします。
|
|
383
|
+
Defaults to None.
|
|
379
384
|
|
|
380
385
|
Returns:
|
|
381
386
|
object: 結果セットの最初の行の最初のカラムの値。結果セットが0件の場合はNone。
|
|
382
387
|
"""
|
|
383
|
-
|
|
388
|
+
result = self.execute(conn, params, buffered=True)
|
|
389
|
+
value = result.scalar()
|
|
390
|
+
if value is None:
|
|
391
|
+
return None
|
|
392
|
+
if json_decode is None:
|
|
393
|
+
json_decode = result.column_types[:1] == ("JSON",)
|
|
394
|
+
if json_decode:
|
|
395
|
+
return json.loads(value, object_hook=AttrDict)
|
|
396
|
+
return value
|
|
384
397
|
|
|
385
398
|
def count(self, conn: ATConnection, params: dict | None = None, limit: int | None = None) -> int:
|
|
386
399
|
"""SELECT句をCOUNT(*)に置き換えたSQLを実行します。
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""atquery delta サブコマンド - MySQLテーブルのDelta Lakeエクスポート(requires atquery[delta-export])"""
|
|
2
|
+
import argparse
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
import sys
|
|
6
|
+
from datetime import datetime, timedelta
|
|
7
|
+
|
|
8
|
+
from atquery.cli.main import (
|
|
9
|
+
EXIT_ERROR,
|
|
10
|
+
EXIT_SUCCESS,
|
|
11
|
+
format_error_for_cli,
|
|
12
|
+
resolve_datasource_uri,
|
|
13
|
+
)
|
|
14
|
+
from atquery.dialect import create_datasource
|
|
15
|
+
|
|
16
|
+
_IMPORT_ERROR_MESSAGE = "Error: delta サブコマンドには atquery[delta-export] が必要です: pip install 'atquery[delta-export]'"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _add_common_args(parser: argparse.ArgumentParser) -> None:
|
|
20
|
+
"""各リーフコマンド共通のオプションを追加する。各オプションは対応する環境変数を default として参照する。"""
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
"-d",
|
|
23
|
+
"--datasource",
|
|
24
|
+
default=argparse.SUPPRESS,
|
|
25
|
+
help="MySQL connection string (env: ATQUERY_DATASOURCE)",
|
|
26
|
+
)
|
|
27
|
+
parser.add_argument(
|
|
28
|
+
"-s",
|
|
29
|
+
"--schema",
|
|
30
|
+
default=argparse.SUPPRESS,
|
|
31
|
+
help="Override the schema/database segment of the resolved connection URI (applies to this command only)",
|
|
32
|
+
)
|
|
33
|
+
parser.add_argument(
|
|
34
|
+
"--warehouse",
|
|
35
|
+
default=os.environ.get("ATQUERY_DELTA_WAREHOUSE"),
|
|
36
|
+
metavar="URI",
|
|
37
|
+
help="Table location, e.g. s3://bucket/warehouse (env: ATQUERY_DELTA_WAREHOUSE)",
|
|
38
|
+
)
|
|
39
|
+
parser.add_argument(
|
|
40
|
+
"--namespace",
|
|
41
|
+
default=os.environ.get("ATQUERY_DELTA_NAMESPACE"),
|
|
42
|
+
help="Directory under the warehouse (env: ATQUERY_DELTA_NAMESPACE, "
|
|
43
|
+
"default: schema of table_name if given as schema.table, otherwise 'default')",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"--storage-option",
|
|
47
|
+
action="append",
|
|
48
|
+
dest="storage_options",
|
|
49
|
+
default=[],
|
|
50
|
+
metavar="KEY=VALUE",
|
|
51
|
+
help="Storage option, e.g. AWS_ENDPOINT_URL=http://localhost:9000 (repeatable)",
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _add_output_name_arg(parser: argparse.ArgumentParser, help_text: str) -> None:
|
|
56
|
+
parser.add_argument("--output-name", dest="output_name", metavar="NAME", help=help_text)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def setup_parser(parser: argparse.ArgumentParser) -> None:
|
|
60
|
+
"""delta サブコマンドのパーサーを設定する。"""
|
|
61
|
+
parser.formatter_class = argparse.RawDescriptionHelpFormatter
|
|
62
|
+
parser.epilog = """
|
|
63
|
+
Examples:
|
|
64
|
+
# Export a whole table (the Delta table is created automatically)
|
|
65
|
+
atquery delta export access_logs -d mysql://root@localhost/mydb --warehouse s3://bucket/warehouse
|
|
66
|
+
|
|
67
|
+
# Export a table in another schema (namespace defaults to the schema name: sails)
|
|
68
|
+
atquery delta export sails.activity_logs -d mysql://root@localhost/mydb --warehouse s3://bucket/warehouse
|
|
69
|
+
|
|
70
|
+
# Export the result of a query
|
|
71
|
+
atquery delta export access_logs --query "SELECT * FROM access_logs WHERE created_datetime < '2026-01-01'"
|
|
72
|
+
|
|
73
|
+
# Export under a different Delta table name
|
|
74
|
+
atquery delta export access_logs --output-name access_logs_2026
|
|
75
|
+
|
|
76
|
+
# Export rows older than the cutoff, then delete them from MySQL (safe to re-run)
|
|
77
|
+
atquery delta cutoff run access_logs --time-column created_datetime --cutoff-at 2026-01-01T00:00:00
|
|
78
|
+
|
|
79
|
+
# Rewrite small data files into fewer, larger ones
|
|
80
|
+
atquery delta compact access_logs
|
|
81
|
+
|
|
82
|
+
# Physically delete data files removed more than 7 days ago
|
|
83
|
+
atquery delta vacuum access_logs --older-than 7d
|
|
84
|
+
|
|
85
|
+
Notes:
|
|
86
|
+
Times are interpreted in the session time zone of the MySQL connection
|
|
87
|
+
(e.g. -d "mysql://.../mydb?time_zone=+09:00"). --cutoff-at must not carry a UTC offset.
|
|
88
|
+
Environment variables: ATQUERY_DATASOURCE, ATQUERY_DELTA_WAREHOUSE, ATQUERY_DELTA_NAMESPACE
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
sub = parser.add_subparsers(dest="delta_command", help="delta commands")
|
|
92
|
+
|
|
93
|
+
export_parser = sub.add_parser("export", help="Export a MySQL table (or query result) to a Delta Lake table")
|
|
94
|
+
export_parser.add_argument("table_name", help="Source MySQL table (also the Delta table name)")
|
|
95
|
+
export_parser.add_argument("--query", help="SELECT statement to export (default: all rows of table_name)")
|
|
96
|
+
_add_output_name_arg(export_parser, "Delta table name to write to, without namespace (default: table_name)")
|
|
97
|
+
_add_common_args(export_parser)
|
|
98
|
+
|
|
99
|
+
cutoff_parser = sub.add_parser("cutoff", help="Cutoff management for periodic log offloading")
|
|
100
|
+
cutoff_sub = cutoff_parser.add_subparsers(dest="cutoff_command", help="cutoff commands")
|
|
101
|
+
cutoff_run_parser = cutoff_sub.add_parser(
|
|
102
|
+
"run", help="Export rows older than the cutoff to Delta Lake, then delete them from MySQL"
|
|
103
|
+
)
|
|
104
|
+
cutoff_run_parser.add_argument("table_name", help="Source MySQL table (also the Delta table name)")
|
|
105
|
+
cutoff_run_parser.add_argument(
|
|
106
|
+
"--cutoff-at",
|
|
107
|
+
required=True,
|
|
108
|
+
dest="cutoff_at",
|
|
109
|
+
metavar="DATETIME",
|
|
110
|
+
help="Rows with time_column < DATETIME are offloaded (ISO 8601, e.g. 2026-01-01T00:00:00)",
|
|
111
|
+
)
|
|
112
|
+
cutoff_run_parser.add_argument(
|
|
113
|
+
"--time-column",
|
|
114
|
+
required=True,
|
|
115
|
+
dest="time_column",
|
|
116
|
+
metavar="COLUMN",
|
|
117
|
+
help="DATETIME/TIMESTAMP column used for the cutoff",
|
|
118
|
+
)
|
|
119
|
+
cutoff_run_parser.add_argument(
|
|
120
|
+
"--purge-batch-size",
|
|
121
|
+
type=int,
|
|
122
|
+
default=10_000,
|
|
123
|
+
dest="purge_batch_size",
|
|
124
|
+
metavar="N",
|
|
125
|
+
help="Max rows per DELETE statement (default: 10000)",
|
|
126
|
+
)
|
|
127
|
+
_add_common_args(cutoff_run_parser)
|
|
128
|
+
|
|
129
|
+
compact_parser = sub.add_parser("compact", help="Rewrite small data files into fewer, larger ones")
|
|
130
|
+
compact_parser.add_argument("table_name", help="Source MySQL table (also the Delta table name)")
|
|
131
|
+
_add_output_name_arg(compact_parser, "Delta table name to compact, without namespace (default: table_name)")
|
|
132
|
+
_add_common_args(compact_parser)
|
|
133
|
+
|
|
134
|
+
vacuum_parser = sub.add_parser("vacuum", help="Physically delete data files no longer referenced")
|
|
135
|
+
vacuum_parser.add_argument("table_name", help="Source MySQL table (also the Delta table name)")
|
|
136
|
+
vacuum_parser.add_argument(
|
|
137
|
+
"--older-than",
|
|
138
|
+
required=True,
|
|
139
|
+
dest="older_than",
|
|
140
|
+
metavar="VALUE",
|
|
141
|
+
help="Delete files removed before this (at least 24h ago). Relative (7d, 24h) or ISO 8601 datetime",
|
|
142
|
+
)
|
|
143
|
+
_add_output_name_arg(vacuum_parser, "Delta table name to target, without namespace (default: table_name)")
|
|
144
|
+
vacuum_parser.add_argument(
|
|
145
|
+
"--dry-run",
|
|
146
|
+
action="store_true",
|
|
147
|
+
dest="dry_run",
|
|
148
|
+
help="Only report what would be deleted, without making changes",
|
|
149
|
+
)
|
|
150
|
+
_add_common_args(vacuum_parser)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def execute_delta(args: argparse.Namespace) -> int:
|
|
154
|
+
"""delta コマンドをディスパッチする。"""
|
|
155
|
+
command = args.delta_command
|
|
156
|
+
if command == "export":
|
|
157
|
+
return _run(args, _execute_export)
|
|
158
|
+
if command == "cutoff" and getattr(args, "cutoff_command", None) == "run":
|
|
159
|
+
return _run(args, _execute_cutoff_run)
|
|
160
|
+
if command == "compact":
|
|
161
|
+
return _run(args, _execute_compact)
|
|
162
|
+
if command == "vacuum":
|
|
163
|
+
return _run(args, _execute_vacuum)
|
|
164
|
+
print("usage: atquery delta {export,cutoff run,compact,vacuum} [options]", file=sys.stderr)
|
|
165
|
+
print("Run 'atquery delta --help' for more information.", file=sys.stderr)
|
|
166
|
+
return EXIT_ERROR
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _run(args: argparse.Namespace, func) -> int:
|
|
170
|
+
uri = resolve_datasource_uri(getattr(args, "datasource", None), getattr(args, "schema", None))
|
|
171
|
+
if not uri:
|
|
172
|
+
print("Error: -d/--datasource または ATQUERY_DATASOURCE 環境変数を指定してください", file=sys.stderr)
|
|
173
|
+
return EXIT_ERROR
|
|
174
|
+
try:
|
|
175
|
+
storage_options = _parse_storage_options(args.storage_options)
|
|
176
|
+
with create_datasource(uri) as datasource:
|
|
177
|
+
func(args, _make_exporter(args, datasource, storage_options))
|
|
178
|
+
return EXIT_SUCCESS
|
|
179
|
+
except ImportError as e:
|
|
180
|
+
print(f"{_IMPORT_ERROR_MESSAGE} ({e})", file=sys.stderr)
|
|
181
|
+
return EXIT_ERROR
|
|
182
|
+
except Exception as e:
|
|
183
|
+
print(f"Error: {format_error_for_cli(e)}", file=sys.stderr)
|
|
184
|
+
return EXIT_ERROR
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _parse_storage_options(values: list[str]) -> dict[str, str]:
|
|
188
|
+
options: dict[str, str] = {}
|
|
189
|
+
for value in values:
|
|
190
|
+
key, sep, val = value.partition("=")
|
|
191
|
+
if not sep or not key:
|
|
192
|
+
raise ValueError(f"--storage-option は KEY=VALUE 形式で指定してください: {value!r}")
|
|
193
|
+
options[key] = val
|
|
194
|
+
return options
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _make_exporter(args: argparse.Namespace, datasource, storage_options: dict[str, str]):
|
|
198
|
+
from atquery.delta import DeltaExporter
|
|
199
|
+
|
|
200
|
+
if not args.warehouse:
|
|
201
|
+
raise ValueError("--warehouse または ATQUERY_DELTA_WAREHOUSE 環境変数を指定してください")
|
|
202
|
+
return DeltaExporter(args.warehouse, datasource, namespace=args.namespace, storage_options=storage_options)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _execute_export(args: argparse.Namespace, exporter) -> None:
|
|
206
|
+
exporter.export(args.table_name, query=args.query, output_name=args.output_name)
|
|
207
|
+
print("exported: " + ".".join(exporter.table_identifier(args.table_name, args.output_name)))
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _execute_cutoff_run(args: argparse.Namespace, exporter) -> None:
|
|
211
|
+
from atquery.delta import DeltaCutoffOffloader
|
|
212
|
+
|
|
213
|
+
cutoff_at = _parse_datetime(args.cutoff_at)
|
|
214
|
+
DeltaCutoffOffloader(
|
|
215
|
+
exporter, time_column=args.time_column, purge_batch_size=args.purge_batch_size
|
|
216
|
+
).run(args.table_name, cutoff_at)
|
|
217
|
+
print(f"offloaded: {'.'.join(exporter.table_identifier(args.table_name))} (cutoff_at < {args.cutoff_at})")
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _execute_compact(args: argparse.Namespace, exporter) -> None:
|
|
221
|
+
result = exporter.compact(args.table_name, output_name=args.output_name)
|
|
222
|
+
identifier = ".".join(exporter.table_identifier(args.table_name, args.output_name))
|
|
223
|
+
print(
|
|
224
|
+
f"compacted: {identifier} "
|
|
225
|
+
f"(files_added={result.get('numFilesAdded', 0)}, files_removed={result.get('numFilesRemoved', 0)})"
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _execute_vacuum(args: argparse.Namespace, exporter) -> None:
|
|
230
|
+
older_than = _parse_older_than(args.older_than)
|
|
231
|
+
deleted = exporter.vacuum(args.table_name, older_than=older_than, output_name=args.output_name, dry_run=args.dry_run)
|
|
232
|
+
identifier = ".".join(exporter.table_identifier(args.table_name, args.output_name))
|
|
233
|
+
prefix = "would delete" if args.dry_run else "deleted"
|
|
234
|
+
print(f"{prefix}: {identifier} (older_than={older_than.isoformat()}, files={len(deleted)})")
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _parse_datetime(value: str) -> datetime:
|
|
238
|
+
try:
|
|
239
|
+
return datetime.fromisoformat(value)
|
|
240
|
+
except ValueError:
|
|
241
|
+
raise ValueError(f"--cutoff-at は ISO 8601 形式で指定してください: {value!r}") from None
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _parse_older_than(value: str) -> datetime:
|
|
245
|
+
"""'7d'/'24h'/'30m' の相対指定、またはISO 8601文字列をdatetimeへ変換する。"""
|
|
246
|
+
if m := re.fullmatch(r"(\d+)([dhm])", value):
|
|
247
|
+
amount, unit = int(m.group(1)), m.group(2)
|
|
248
|
+
delta = {"d": timedelta(days=amount), "h": timedelta(hours=amount), "m": timedelta(minutes=amount)}[unit]
|
|
249
|
+
return datetime.now().astimezone() - delta
|
|
250
|
+
try:
|
|
251
|
+
return datetime.fromisoformat(value)
|
|
252
|
+
except ValueError:
|
|
253
|
+
raise ValueError(f"--older-than は '7d'/'24h'/'30m' もしくはISO 8601 形式で指定してください: {value!r}") from None
|
|
@@ -725,14 +725,14 @@ Examples:
|
|
|
725
725
|
)
|
|
726
726
|
setup_ecd_parser(ecd_parser)
|
|
727
727
|
|
|
728
|
-
#
|
|
729
|
-
from atquery.cli.
|
|
730
|
-
|
|
731
|
-
"
|
|
732
|
-
help="Export MySQL tables to
|
|
733
|
-
description="Export MySQL tables to
|
|
728
|
+
# delta サブコマンドの追加
|
|
729
|
+
from atquery.cli.delta import setup_parser as setup_delta_parser
|
|
730
|
+
delta_parser = subparsers.add_parser(
|
|
731
|
+
"delta",
|
|
732
|
+
help="Export MySQL tables to Delta Lake: export/cutoff/compact/vacuum (requires atquery[delta-export])",
|
|
733
|
+
description="Export MySQL tables to Delta Lake: export/cutoff/compact/vacuum",
|
|
734
734
|
)
|
|
735
|
-
|
|
735
|
+
setup_delta_parser(delta_parser)
|
|
736
736
|
|
|
737
737
|
parser.add_argument(
|
|
738
738
|
"-d",
|
|
@@ -877,9 +877,9 @@ Examples:
|
|
|
877
877
|
from atquery.cli.ecd import execute_ecd
|
|
878
878
|
return execute_ecd(args)
|
|
879
879
|
|
|
880
|
-
if args.subcommand == "
|
|
881
|
-
from atquery.cli.
|
|
882
|
-
return
|
|
880
|
+
if args.subcommand == "delta":
|
|
881
|
+
from atquery.cli.delta import execute_delta
|
|
882
|
+
return execute_delta(args)
|
|
883
883
|
|
|
884
884
|
# データベース接続文字列の取得(優先順位: 引数 > 環境変数)、-s/--schemaがあればschema部分を上書き
|
|
885
885
|
database_uri = resolve_datasource_uri(args.datasource, args.schema)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""MySQLの列定義からArrowスキーマを導く(非公開)。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterable, Sequence
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import pyarrow as pa
|
|
9
|
+
|
|
10
|
+
_MYSQL_TYPE_TO_ARROW: dict[str, pa.DataType] = {
|
|
11
|
+
"char": pa.string(),
|
|
12
|
+
"varchar": pa.string(),
|
|
13
|
+
"text": pa.string(),
|
|
14
|
+
"tinytext": pa.string(),
|
|
15
|
+
"mediumtext": pa.string(),
|
|
16
|
+
"longtext": pa.string(),
|
|
17
|
+
"json": pa.string(),
|
|
18
|
+
"enum": pa.string(),
|
|
19
|
+
"binary": pa.binary(),
|
|
20
|
+
"varbinary": pa.binary(),
|
|
21
|
+
"smallint": pa.int32(),
|
|
22
|
+
"mediumint": pa.int32(),
|
|
23
|
+
"int": pa.int32(),
|
|
24
|
+
"bigint": pa.int64(),
|
|
25
|
+
"float": pa.float32(),
|
|
26
|
+
"double": pa.float64(),
|
|
27
|
+
"timestamp": pa.timestamp("us"),
|
|
28
|
+
"datetime": pa.timestamp("us"),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def arrow_type_for_mysql_column(data_type: str, column_type: str) -> pa.DataType:
|
|
33
|
+
"""MySQLの列型をArrowの型へ変換する。対応外の型は`ValueError`。
|
|
34
|
+
|
|
35
|
+
- `tinyint(1)`はbool、それ以外の`tinyint`はint32
|
|
36
|
+
- `json`/`enum`/`char`/`varchar`/`text`系はstring(jsonはJSON文字列のまま保持)
|
|
37
|
+
- `binary`/`varbinary`は幅に関わらず可変長binary
|
|
38
|
+
- `datetime`/`timestamp`はタイムゾーン無しのtimestamp(Delta Lakeでは`timestampNtz`)
|
|
39
|
+
"""
|
|
40
|
+
data_type = data_type.lower()
|
|
41
|
+
if data_type == "tinyint":
|
|
42
|
+
return pa.bool_() if column_type.lower() == "tinyint(1)" else pa.int32()
|
|
43
|
+
try:
|
|
44
|
+
return _MYSQL_TYPE_TO_ARROW[data_type]
|
|
45
|
+
except KeyError:
|
|
46
|
+
raise ValueError(f"未対応のMySQL型です: {column_type}(`{data_type}`)") from None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def infer_arrow_schema(columns: Iterable[Sequence[str]]) -> pa.Schema:
|
|
50
|
+
"""`(column_name, data_type, column_type)`の列定義(定義順)からArrowスキーマを作る。"""
|
|
51
|
+
fields = [
|
|
52
|
+
pa.field(name, arrow_type_for_mysql_column(data_type, column_type), nullable=True)
|
|
53
|
+
for name, data_type, column_type in columns
|
|
54
|
+
]
|
|
55
|
+
if not fields:
|
|
56
|
+
raise ValueError("列が1つもありません")
|
|
57
|
+
return pa.schema(fields)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def convert_value(value: Any, arrow_type: pa.DataType) -> Any:
|
|
61
|
+
"""MySQLドライバが返したPython値を、Arrow配列へ詰められる値へ揃える。"""
|
|
62
|
+
if value is None:
|
|
63
|
+
return None
|
|
64
|
+
if pa.types.is_boolean(arrow_type):
|
|
65
|
+
return bool(value)
|
|
66
|
+
if pa.types.is_binary(arrow_type) or pa.types.is_large_binary(arrow_type):
|
|
67
|
+
return bytes(value)
|
|
68
|
+
if pa.types.is_string(arrow_type) or pa.types.is_large_string(arrow_type):
|
|
69
|
+
return bytes(value).decode("utf-8") if isinstance(value, (bytes, bytearray)) else str(value)
|
|
70
|
+
return value
|