atquery 2.19.0__tar.gz → 2.20.0__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.0}/PKG-INFO +6 -6
- {atquery-2.19.0 → atquery-2.20.0}/pyproject.toml +4 -4
- {atquery-2.19.0 → atquery-2.20.0}/pyproject.toml.orig +4 -4
- atquery-2.20.0/src/atquery/cli/delta.py +253 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/main.py +10 -10
- atquery-2.20.0/src/atquery/delta/__init__.py +6 -0
- atquery-2.20.0/src/atquery/delta/_schema.py +70 -0
- {atquery-2.19.0/src/atquery/iceberg → atquery-2.20.0/src/atquery/delta}/cutoff.py +30 -43
- atquery-2.20.0/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.0}/LICENSE +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/README.md +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/aio/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/aio/api.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/aio/engine.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/aio/query.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/api_v2.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/atquery2.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/columns.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/csv.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/ddl.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/diff.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/ecd.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/migrate.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/patch.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/routines.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/scenario.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/schemas.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/cli/tables.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/duckdb.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/aio.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/core.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/dump/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/dump/__main__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/dump/core.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/meta.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/pooling.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/restore/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/restore/__main__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/mysql/restore/core.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/dialect/sqlite.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/diff/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/diff/comparator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/diff/generator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/diff/git_integration.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/diff/models.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/diff/parser.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/_toml.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/catalog.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/ddl.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/diff.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/erd.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/errors.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/model.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/parser.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/resolver.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/seed.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/ecd/skeleton.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/engine.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/factory.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/migration/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/migration/_migrator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/migration/_protocol.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/migration/_standard.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/migration/_types.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/_apply.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/_convert.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/_errors.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/_log.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/_meta.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/_model.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/patch/_sql.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/py.typed +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/testing/__init__.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/testing/creator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/testing/loader.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/testing/schema.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/src/atquery/testing/validator.py +0 -0
- {atquery-2.19.0 → atquery-2.20.0}/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.0
|
|
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.0"
|
|
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.0"
|
|
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]
|
|
@@ -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
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
"""定期的なログオフロード向けのカットオフ管理(`
|
|
1
|
+
"""定期的なログオフロード向けのカットオフ管理(`DeltaExporter`のプラスアルファ機能)。"""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from datetime import datetime
|
|
6
|
-
|
|
7
|
-
from pyiceberg.exceptions import NoSuchTableError
|
|
5
|
+
from datetime import datetime, timedelta
|
|
8
6
|
|
|
7
|
+
from atquery.delta.exporter import DeltaExporter, open_connection
|
|
9
8
|
from atquery.dialect.mysql.core import bquote_identifier
|
|
10
|
-
from atquery.iceberg.exporter import IcebergExporter, open_connection
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
"""export
|
|
10
|
+
CUTOFF_AT_APP_ID = "atquery.cutoff_at"
|
|
11
|
+
"""exportのコミットへ`txn`アクションとして記録するapp_id。versionは`cutoff_at`のミリ秒エポック。"""
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
_EPOCH = datetime(1970, 1, 1)
|
|
14
|
+
_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f"
|
|
16
15
|
|
|
17
16
|
_DDL_CUTOFF_BOUNDARIES = """
|
|
18
17
|
CREATE TABLE IF NOT EXISTS cutoff_boundaries (
|
|
19
18
|
table_name VARCHAR(64) NOT NULL PRIMARY KEY
|
|
20
19
|
, cutoff_at TIMESTAMP(3) NOT NULL
|
|
21
20
|
, status ENUM('PENDING', 'EXPORTED', 'PURGED') NOT NULL DEFAULT 'PENDING'
|
|
22
|
-
,
|
|
21
|
+
, delta_version BIGINT
|
|
23
22
|
, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
24
23
|
, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
|
25
24
|
)
|
|
@@ -27,22 +26,27 @@ CREATE TABLE IF NOT EXISTS cutoff_boundaries (
|
|
|
27
26
|
|
|
28
27
|
|
|
29
28
|
def _format_cutoff(cutoff_at: datetime) -> str:
|
|
30
|
-
return cutoff_at.strftime(
|
|
29
|
+
return cutoff_at.strftime(_DATETIME_FORMAT)[:-3]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def cutoff_to_epoch_ms(cutoff_at: datetime) -> int:
|
|
33
|
+
"""タイムゾーン無しのdatetimeをUTCとして解釈したミリ秒エポックへ変換する(浮動小数を経由しない)。"""
|
|
34
|
+
return (cutoff_at - _EPOCH) // timedelta(milliseconds=1)
|
|
31
35
|
|
|
32
36
|
|
|
33
|
-
class
|
|
34
|
-
"""`time_column < cutoff_at`の行を
|
|
37
|
+
class DeltaCutoffOffloader:
|
|
38
|
+
"""`time_column < cutoff_at`の行をDelta Lakeへexportし、成功を確認した上でMySQLから削除する。
|
|
35
39
|
|
|
36
|
-
`run()`は、
|
|
37
|
-
export済みかを判定するため、途中で失敗しても同じ引数で再実行できる。
|
|
40
|
+
`run()`は、Delta Lakeテーブルにコミットとして記録された`cutoff_at`(`txn`アクション)を
|
|
41
|
+
根拠にexport済みかを判定するため、途中で失敗しても同じ引数で再実行できる。
|
|
38
42
|
時刻はMySQL接続のセッションタイムゾーンで解釈する(`cutoff_at`はタイムゾーン無しの
|
|
39
43
|
datetimeで、`time_column`と同じ基準の値を渡す)。
|
|
40
44
|
"""
|
|
41
45
|
|
|
42
|
-
def __init__(self, exporter:
|
|
46
|
+
def __init__(self, exporter: DeltaExporter, *, time_column: str, purge_batch_size: int = 10_000) -> None:
|
|
43
47
|
"""
|
|
44
48
|
Args:
|
|
45
|
-
exporter: export先を担う`
|
|
49
|
+
exporter: export先を担う`DeltaExporter`。
|
|
46
50
|
time_column: カットオフ判定に使うDATETIME/TIMESTAMP列名。
|
|
47
51
|
purge_batch_size: MySQLからの削除を1回のDELETEで行う最大行数。
|
|
48
52
|
"""
|
|
@@ -57,7 +61,7 @@ class IcebergCutoffOffloader:
|
|
|
57
61
|
if cutoff_at.tzinfo is not None:
|
|
58
62
|
# ドライバはtzinfoを無視して時刻文字列だけを送るため、黙って誤った時刻で比較してしまう
|
|
59
63
|
raise ValueError("cutoff_atにはタイムゾーン無しのdatetimeを指定してください(time_columnと同じ基準の値)")
|
|
60
|
-
# cutoff_boundaries.cutoff_atはTIMESTAMP(3)
|
|
64
|
+
# cutoff_boundaries.cutoff_atはTIMESTAMP(3)のため、記録値との比較も含めミリ秒に揃える
|
|
61
65
|
cutoff_at = cutoff_at.replace(microsecond=cutoff_at.microsecond // 1000 * 1000)
|
|
62
66
|
self._ensure_boundaries_table()
|
|
63
67
|
|
|
@@ -66,7 +70,7 @@ class IcebergCutoffOffloader:
|
|
|
66
70
|
self._exporter.export(
|
|
67
71
|
table_name,
|
|
68
72
|
query=self._cutoff_query(table_name, cutoff_at),
|
|
69
|
-
|
|
73
|
+
app_transactions={CUTOFF_AT_APP_ID: cutoff_to_epoch_ms(cutoff_at)},
|
|
70
74
|
)
|
|
71
75
|
self._mark_exported(table_name)
|
|
72
76
|
self._purge(table_name, cutoff_at)
|
|
@@ -86,54 +90,37 @@ class IcebergCutoffOffloader:
|
|
|
86
90
|
f"WHERE {bquote_identifier(self._time_column)} < '{_format_cutoff(cutoff_at)}'"
|
|
87
91
|
)
|
|
88
92
|
|
|
89
|
-
def _current_snapshot(self, table_name: str) -> tuple[int, datetime | None] | None:
|
|
90
|
-
"""Icebergテーブルの現在のsnapshotの(snapshot_id, 記録されたcutoff_at)。snapshotが無ければNone。"""
|
|
91
|
-
try:
|
|
92
|
-
table = self._exporter.load_table(table_name)
|
|
93
|
-
except NoSuchTableError:
|
|
94
|
-
return None
|
|
95
|
-
snapshot = table.current_snapshot()
|
|
96
|
-
if snapshot is None:
|
|
97
|
-
return None
|
|
98
|
-
recorded = None
|
|
99
|
-
if snapshot.summary is not None:
|
|
100
|
-
raw = snapshot.summary.additional_properties.get(CUTOFF_AT_SNAPSHOT_PROPERTY)
|
|
101
|
-
if raw:
|
|
102
|
-
recorded = datetime.strptime(raw, _SNAPSHOT_FORMAT)
|
|
103
|
-
return snapshot.snapshot_id, recorded
|
|
104
|
-
|
|
105
93
|
def _already_exported(self, table_name: str, cutoff_at: datetime) -> bool:
|
|
106
|
-
"""
|
|
107
|
-
|
|
108
|
-
return
|
|
94
|
+
"""記録されたcutoff_atが今回以降なら、export済みとみなす。"""
|
|
95
|
+
recorded = self._exporter.transaction_version(table_name, CUTOFF_AT_APP_ID)
|
|
96
|
+
return recorded is not None and recorded >= cutoff_to_epoch_ms(cutoff_at)
|
|
109
97
|
|
|
110
98
|
def _upsert_pending(self, table_name: str, cutoff_at: datetime) -> None:
|
|
111
99
|
with open_connection(self._exporter.datasource, transaction=True) as conn:
|
|
112
100
|
conn.execute(
|
|
113
101
|
"""
|
|
114
102
|
INSERT INTO cutoff_boundaries
|
|
115
|
-
(table_name, cutoff_at, status,
|
|
103
|
+
(table_name, cutoff_at, status, delta_version)
|
|
116
104
|
VALUES
|
|
117
105
|
(:table_name, :cutoff_at, 'PENDING', NULL)
|
|
118
106
|
ON DUPLICATE KEY UPDATE
|
|
119
107
|
cutoff_at = :cutoff_at
|
|
120
108
|
, status = 'PENDING'
|
|
121
|
-
,
|
|
109
|
+
, delta_version = NULL
|
|
122
110
|
""",
|
|
123
111
|
{"table_name": table_name, "cutoff_at": cutoff_at},
|
|
124
112
|
).close()
|
|
125
113
|
|
|
126
114
|
def _mark_exported(self, table_name: str) -> None:
|
|
127
|
-
|
|
128
|
-
snapshot_id = current[0] if current is not None else None
|
|
115
|
+
version = self._exporter.table_version(table_name)
|
|
129
116
|
with self._exporter.datasource.begin() as conn:
|
|
130
117
|
conn.execute(
|
|
131
118
|
"""
|
|
132
119
|
UPDATE cutoff_boundaries
|
|
133
|
-
SET status = 'EXPORTED',
|
|
120
|
+
SET status = 'EXPORTED', delta_version = :version
|
|
134
121
|
WHERE table_name = :table_name
|
|
135
122
|
""",
|
|
136
|
-
{"table_name": table_name, "
|
|
123
|
+
{"table_name": table_name, "version": version},
|
|
137
124
|
).close()
|
|
138
125
|
|
|
139
126
|
def _purge(self, table_name: str, cutoff_at: datetime) -> None:
|