atquery 2.17.0__tar.gz → 2.18.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.
Files changed (76) hide show
  1. {atquery-2.17.0 → atquery-2.18.0}/PKG-INFO +7 -5
  2. {atquery-2.17.0 → atquery-2.18.0}/pyproject.toml +10 -10
  3. {atquery-2.17.0 → atquery-2.18.0}/pyproject.toml.orig +10 -11
  4. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/aio/__init__.py +8 -0
  5. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/aio/api.py +95 -0
  6. atquery-2.18.0/src/atquery/cli/iceberg.py +241 -0
  7. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/main.py +13 -0
  8. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/aio.py +138 -0
  9. atquery-2.18.0/src/atquery/iceberg/__init__.py +6 -0
  10. atquery-2.18.0/src/atquery/iceberg/_catalog.py +478 -0
  11. atquery-2.18.0/src/atquery/iceberg/_schema.py +87 -0
  12. atquery-2.18.0/src/atquery/iceberg/cutoff.py +162 -0
  13. atquery-2.18.0/src/atquery/iceberg/exporter.py +252 -0
  14. {atquery-2.17.0 → atquery-2.18.0}/LICENSE +0 -0
  15. {atquery-2.17.0 → atquery-2.18.0}/README.md +0 -0
  16. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/__init__.py +0 -0
  17. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/aio/engine.py +0 -0
  18. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/aio/query.py +0 -0
  19. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/api_v2.py +0 -0
  20. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/atquery2.py +0 -0
  21. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/__init__.py +0 -0
  22. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/columns.py +0 -0
  23. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/csv.py +0 -0
  24. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/ddl.py +0 -0
  25. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/diff.py +0 -0
  26. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/ecd.py +0 -0
  27. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/migrate.py +0 -0
  28. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/routines.py +0 -0
  29. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/scenario.py +0 -0
  30. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/schemas.py +0 -0
  31. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/cli/tables.py +0 -0
  32. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/__init__.py +0 -0
  33. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/duckdb.py +0 -0
  34. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/__init__.py +0 -0
  35. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/core.py +0 -0
  36. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/dump/__init__.py +0 -0
  37. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/dump/__main__.py +0 -0
  38. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/dump/core.py +0 -0
  39. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/meta.py +0 -0
  40. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/pooling.py +0 -0
  41. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/restore/__init__.py +0 -0
  42. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/restore/__main__.py +0 -0
  43. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/mysql/restore/core.py +0 -0
  44. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/dialect/sqlite.py +0 -0
  45. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/diff/__init__.py +0 -0
  46. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/diff/comparator.py +0 -0
  47. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/diff/generator.py +0 -0
  48. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/diff/git_integration.py +0 -0
  49. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/diff/models.py +0 -0
  50. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/diff/parser.py +0 -0
  51. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/__init__.py +0 -0
  52. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/_toml.py +0 -0
  53. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/catalog.py +0 -0
  54. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/ddl.py +0 -0
  55. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/diff.py +0 -0
  56. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/erd.py +0 -0
  57. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/errors.py +0 -0
  58. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/model.py +0 -0
  59. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/parser.py +0 -0
  60. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/resolver.py +0 -0
  61. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/seed.py +0 -0
  62. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/ecd/skeleton.py +0 -0
  63. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/engine.py +0 -0
  64. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/factory.py +0 -0
  65. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/migration/__init__.py +0 -0
  66. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/migration/_migrator.py +0 -0
  67. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/migration/_protocol.py +0 -0
  68. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/migration/_standard.py +0 -0
  69. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/migration/_types.py +0 -0
  70. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/py.typed +0 -0
  71. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/testing/__init__.py +0 -0
  72. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/testing/creator.py +0 -0
  73. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/testing/loader.py +0 -0
  74. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/testing/schema.py +0 -0
  75. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/testing/validator.py +0 -0
  76. {atquery-2.17.0 → atquery-2.18.0}/src/atquery/type.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: atquery
3
- Version: 2.17.0
3
+ Version: 2.18.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
@@ -14,21 +14,23 @@ Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
15
  Classifier: Programming Language :: Python :: 3.12
16
16
  Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
17
18
  Classifier: Topic :: Database
18
19
  Requires-Dist: tomli>=2.0.1 ; python_full_version < '3.11'
19
- Requires-Dist: mysql-connector-python ; extra == 'all'
20
- Requires-Dist: duckdb ; extra == 'all'
21
- Requires-Dist: json5 ; extra == 'all'
22
- Requires-Dist: sqlglot ; extra == 'all'
23
20
  Requires-Dist: python-dotenv>=1.2.2 ; extra == 'all'
21
+ Requires-Dist: atquery[mysql,duckdb,testing,diff,iceberg-export] ; extra == 'all'
24
22
  Requires-Dist: sqlglot>=30.4.2 ; extra == 'diff'
25
23
  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'
26
27
  Requires-Dist: mysql-connector-python>=8.0,<10 ; extra == 'mysql'
27
28
  Requires-Dist: json5>=0.12.1 ; extra == 'testing'
28
29
  Requires-Python: >=3.10
29
30
  Provides-Extra: all
30
31
  Provides-Extra: diff
31
32
  Provides-Extra: duckdb
33
+ Provides-Extra: iceberg-export
32
34
  Provides-Extra: mysql
33
35
  Provides-Extra: testing
34
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.17.0"
7
+ version = "2.18.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"
@@ -28,6 +28,7 @@ classifiers = [
28
28
  "Programming Language :: Python :: 3.11",
29
29
  "Programming Language :: Python :: 3.12",
30
30
  "Programming Language :: Python :: 3.13",
31
+ "Programming Language :: Python :: 3.14",
31
32
  "Topic :: Database",
32
33
  ]
33
34
 
@@ -40,23 +41,21 @@ atquery = "atquery.cli.main:main"
40
41
  [project.optional-dependencies]
41
42
  mysql = ["mysql-connector-python>=8.0,<10"]
42
43
  duckdb = ["duckdb>=0.10.0"]
44
+ iceberg-export = [
45
+ "mysql-connector-python>=8.0,<10",
46
+ "pyiceberg>=0.12.0",
47
+ "pyarrow>=15.0.0",
48
+ ]
43
49
  testing = ["json5>=0.12.1"]
44
50
  diff = ["sqlglot>=30.4.2"]
45
51
  all = [
46
- "mysql-connector-python",
47
- "duckdb",
48
- "json5",
49
- "sqlglot",
50
52
  "python-dotenv>=1.2.2",
53
+ "atquery[mysql,duckdb,testing,diff,iceberg-export]",
51
54
  ]
52
55
 
53
56
  [dependency-groups]
54
57
  dev = [
55
- "mysql-connector-python",
56
- "duckdb",
57
- "json5",
58
- "python-dotenv",
59
- "sqlglot",
58
+ "atquery[all]",
60
59
  "ruff",
61
60
  "pyright",
62
61
  "pytest>=7.0.0",
@@ -86,6 +85,7 @@ select = [
86
85
 
87
86
  [tool.ruff.lint.per-file-ignores]
88
87
  "docker/*.py" = ["INP"]
88
+ "examples/**/*.py" = ["INP"]
89
89
  "scripts/*.py" = ["INP"]
90
90
  "tests/*.py" = ["INP"]
91
91
  "var/*.py" = ["INP"]
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "atquery"
7
- version = "2.17.0"
7
+ version = "2.18.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"
@@ -26,6 +26,7 @@ classifiers = [
26
26
  "Programming Language :: Python :: 3.11",
27
27
  "Programming Language :: Python :: 3.12",
28
28
  "Programming Language :: Python :: 3.13",
29
+ "Programming Language :: Python :: 3.14",
29
30
  "Topic :: Database",
30
31
  ]
31
32
 
@@ -39,6 +40,11 @@ mysql = [
39
40
  duckdb = [
40
41
  "duckdb>=0.10.0",
41
42
  ]
43
+ iceberg-export = [
44
+ "mysql-connector-python>=8.0,<10",
45
+ "pyiceberg>=0.12.0",
46
+ "pyarrow>=15.0.0",
47
+ ]
42
48
  testing = [
43
49
  "json5>=0.12.1",
44
50
  ]
@@ -46,21 +52,13 @@ diff = [
46
52
  "sqlglot>=30.4.2",
47
53
  ]
48
54
  all = [
49
- "mysql-connector-python",
50
- "duckdb",
51
- "json5",
52
- "sqlglot",
53
55
  "python-dotenv>=1.2.2",
56
+ "atquery[mysql,duckdb,testing,diff,iceberg-export]"
54
57
  ]
55
58
 
56
59
  [dependency-groups]
57
60
  dev = [
58
- # -- optional dependencies --
59
- "mysql-connector-python",
60
- "duckdb",
61
- "json5",
62
- "python-dotenv",
63
- "sqlglot",
61
+ "atquery[all]",
64
62
  # -- ruff --
65
63
  "ruff",
66
64
  # -- pyright --
@@ -105,6 +103,7 @@ select = [
105
103
 
106
104
  [tool.ruff.lint.per-file-ignores]
107
105
  "docker/*.py" = ["INP"]
106
+ "examples/**/*.py" = ["INP"]
108
107
  "scripts/*.py" = ["INP"]
109
108
  "tests/*.py" = ["INP"]
110
109
  "var/*.py" = ["INP"]
@@ -10,8 +10,12 @@ from .api import (
10
10
  create_datasource,
11
11
  execute,
12
12
  execute_script,
13
+ export_csv,
14
+ insert_csv,
13
15
  load,
16
+ replace_csv,
14
17
  sql,
18
+ upsert_csv,
15
19
  )
16
20
 
17
21
  __all__ = [
@@ -26,6 +30,10 @@ __all__ = [
26
30
  "create_datasource",
27
31
  "execute",
28
32
  "execute_script",
33
+ "export_csv",
34
+ "insert_csv",
29
35
  "load",
36
+ "replace_csv",
30
37
  "sql",
38
+ "upsert_csv",
31
39
  ]
@@ -11,7 +11,9 @@ Usage:
11
11
  users = await aat("SELECT__users.sql").getall(conn, {"status": "active"})
12
12
  """
13
13
  from importlib import import_module
14
+ from pathlib import Path
14
15
  from textwrap import dedent
16
+ from typing import Union
15
17
 
16
18
  from atquery.aio.engine import (
17
19
  ATAsyncConnection,
@@ -37,8 +39,12 @@ __all__ = [
37
39
  "create_datasource",
38
40
  "execute",
39
41
  "execute_script",
42
+ "export_csv",
43
+ "insert_csv",
40
44
  "load",
45
+ "replace_csv",
41
46
  "sql",
47
+ "upsert_csv",
42
48
  ]
43
49
 
44
50
  ASYNC_DATASOURCE_TYPES = {
@@ -145,6 +151,95 @@ async def execute_script(conn: ATAsyncConnection, script: str, params: dict | No
145
151
  await ATAsyncQuery._from_atquery(ATQuery(dedent(script).strip())).execute_script(conn, params)
146
152
 
147
153
 
154
+ async def insert_csv(conn: ATAsyncConnection, table: str, file: Union[str, Path]) -> int:
155
+ """CSVファイルをテーブルにINSERTします。PKが既に存在するデータは更新しません。
156
+
157
+ Args:
158
+ conn (ATAsyncConnection): 非同期コネクション
159
+ table (str): テーブル名
160
+ file (Union[str, Path]): CSVファイルのパス
161
+
162
+ Raises:
163
+ NotImplementedError: 接続中のDBに対しては機能が実装されていません
164
+
165
+ Returns:
166
+ int: 挿入した行数
167
+ """
168
+ if conn.dialect == "mysql":
169
+ from atquery.dialect.mysql import aio as mysql_aio
170
+ return await mysql_aio.load_data_csv(conn, table, str(file), ifconflict="IGNORE")
171
+ raise NotImplementedError(f"insert_csv is not supported for dialect '{conn.dialect}'. Supported dialects: mysql")
172
+
173
+
174
+ async def upsert_csv(conn: ATAsyncConnection, table: str, file: Union[str, Path]) -> int:
175
+ """CSVファイルの内容でテーブルの行をUPSERTします(列保持、キー衝突時は指定列のみ更新)。
176
+
177
+ テーブルのPRIMARY KEY(UNIQUE制約も可)に基づいてキー衝突を判定し、
178
+ CSVヘッダーに含まれる列だけをSETします。CSVに含まれない列の既存値は変更されません
179
+ (行全体を置き換える `replace_csv()` とは異なります)。
180
+
181
+ Args:
182
+ conn (ATAsyncConnection): 非同期コネクション
183
+ table (str): テーブル名
184
+ file (Union[str, Path]): CSVファイルのパス
185
+
186
+ Raises:
187
+ ValueError: テーブルにPRIMARY KEY(またはUNIQUE制約)が無い場合
188
+ NotImplementedError: 接続中のDBに対しては機能が実装されていません
189
+
190
+ Returns:
191
+ int: 更新・挿入した行数
192
+ """
193
+ if conn.dialect == "mysql":
194
+ from atquery.dialect.mysql import aio as mysql_aio
195
+ return await mysql_aio.upsert_data_csv(conn, table, str(file))
196
+ raise NotImplementedError(f"upsert_csv is not supported for dialect '{conn.dialect}'. Supported dialects: mysql")
197
+
198
+
199
+ async def replace_csv(conn: ATAsyncConnection, table: str, file: Union[str, Path]) -> int:
200
+ """CSVファイルの内容でテーブルの行を REPLACE します。
201
+
202
+ キーが衝突した行は一旦DELETEしてから改めてINSERTされるため、CSVに含まれていない
203
+ 列はテーブルのDEFAULT値(またはNULL)にリセットされます。一部の列だけを更新したい
204
+ 場合はこの関数を使わず、列を保持する `upsert_csv()` を使ってください。
205
+
206
+ Args:
207
+ conn (ATAsyncConnection): 非同期コネクション
208
+ table (str): テーブル名
209
+ file (Union[str, Path]): CSVファイルのパス
210
+
211
+ Raises:
212
+ NotImplementedError: 接続中のDBに対しては機能が実装されていません
213
+
214
+ Returns:
215
+ int: 更新した行数
216
+ """
217
+ if conn.dialect == "mysql":
218
+ from atquery.dialect.mysql import aio as mysql_aio
219
+ return await mysql_aio.load_data_csv(conn, table, str(file), ifconflict="REPLACE")
220
+ raise NotImplementedError(f"replace_csv is not supported for dialect '{conn.dialect}'. Supported dialects: mysql")
221
+
222
+
223
+ async def export_csv(conn: ATAsyncConnection, table: str, file: Union[str, Path]) -> int:
224
+ """テーブルの内容をCSVファイルにエクスポートします。
225
+
226
+ Args:
227
+ conn (ATAsyncConnection): 非同期コネクション
228
+ table (str): テーブル名
229
+ file (Union[str, Path]): CSVファイルのパス
230
+
231
+ Raises:
232
+ NotImplementedError: 接続中のDBに対しては機能が実装されていません
233
+
234
+ Returns:
235
+ int: エクスポートした行数
236
+ """
237
+ if conn.dialect == "mysql":
238
+ from atquery.dialect.mysql import aio as mysql_aio
239
+ return await mysql_aio.export_csv(conn, table, str(file))
240
+ raise NotImplementedError(f"export_csv is not supported for dialect '{conn.dialect}'. Supported dialects: mysql")
241
+
242
+
148
243
  class Resource(_SyncResource):
149
244
  """__file__ をアンカーとした非同期リソース読み込みヘルパー。
150
245
 
@@ -0,0 +1,241 @@
1
+ """atquery iceberg サブコマンド - MySQLテーブルのIcebergエクスポート(requires atquery[iceberg-export])"""
2
+ import argparse
3
+ import os
4
+ import sys
5
+ from datetime import datetime
6
+
7
+ from atquery.cli.main import EXIT_ERROR, EXIT_SUCCESS, format_error_for_cli, resolve_datasource_uri
8
+ from atquery.dialect import create_datasource
9
+
10
+ _IMPORT_ERROR_MESSAGE = "Error: iceberg サブコマンドには atquery[iceberg-export] が必要です: pip install 'atquery[iceberg-export]'"
11
+
12
+
13
+ def _add_common_args(parser: argparse.ArgumentParser, *, warehouse_required: bool = True) -> None:
14
+ """各リーフコマンド共通のオプションを追加する。各オプションは対応する環境変数を default として参照する。"""
15
+ parser.add_argument(
16
+ "-d",
17
+ "--datasource",
18
+ default=argparse.SUPPRESS,
19
+ help="MySQL connection string (env: ATQUERY_DATASOURCE)",
20
+ )
21
+ parser.add_argument(
22
+ "-s",
23
+ "--schema",
24
+ default=argparse.SUPPRESS,
25
+ help="Override the schema/database segment of the resolved connection URI (applies to this command only)",
26
+ )
27
+ parser.add_argument(
28
+ "--warehouse",
29
+ default=os.environ.get("ATQUERY_ICEBERG_WAREHOUSE"),
30
+ metavar="URI",
31
+ help="Data file location, e.g. s3://bucket/warehouse (env: ATQUERY_ICEBERG_WAREHOUSE"
32
+ + (")" if warehouse_required else ", optional)"),
33
+ )
34
+ parser.add_argument(
35
+ "--catalog-name",
36
+ default=os.environ.get("ATQUERY_ICEBERG_CATALOG_NAME", "atquery"),
37
+ dest="catalog_name",
38
+ metavar="NAME",
39
+ help="Catalog name that identifies which registered tables are visible "
40
+ "(env: ATQUERY_ICEBERG_CATALOG_NAME, default: atquery)",
41
+ )
42
+ parser.add_argument(
43
+ "--property",
44
+ action="append",
45
+ dest="properties",
46
+ default=[],
47
+ metavar="KEY=VALUE",
48
+ help="Storage property, e.g. s3.endpoint=http://localhost:9000 (repeatable)",
49
+ )
50
+
51
+
52
+ def _add_namespace_arg(parser: argparse.ArgumentParser, *, schema_default: bool) -> None:
53
+ """`schema_default=True`: 未指定時はテーブル名のschema(無ければdefault)。False: 全namespace。"""
54
+ help_text = "Iceberg namespace (env: ATQUERY_ICEBERG_NAMESPACE, default: "
55
+ help_text += (
56
+ "schema of table_name if given as schema.table, otherwise 'default')"
57
+ if schema_default
58
+ else "all namespaces)"
59
+ )
60
+ parser.add_argument("--namespace", default=os.environ.get("ATQUERY_ICEBERG_NAMESPACE"), help=help_text)
61
+
62
+
63
+ def setup_parser(parser: argparse.ArgumentParser) -> None:
64
+ """iceberg サブコマンドのパーサーを設定する。"""
65
+ parser.formatter_class = argparse.RawDescriptionHelpFormatter
66
+ parser.epilog = """
67
+ Examples:
68
+ # Export a whole table (the Iceberg table is created automatically)
69
+ atquery iceberg export access_logs -d mysql://root@localhost/mydb --warehouse s3://bucket/warehouse
70
+
71
+ # Export a table in another schema (namespace defaults to the schema name: sails)
72
+ atquery iceberg export sails.activity_logs -d mysql://root@localhost/mydb --warehouse s3://bucket/warehouse
73
+
74
+ # Export the result of a query
75
+ atquery iceberg export access_logs --query "SELECT * FROM access_logs WHERE created_datetime < '2026-01-01'"
76
+
77
+ # Export under a different Iceberg table name
78
+ atquery iceberg export access_logs --output-name access_logs_2026
79
+
80
+ # Export rows older than the cutoff, then delete them from MySQL (safe to re-run)
81
+ atquery iceberg cutoff run access_logs --time-column created_datetime --cutoff-at 2026-01-01T00:00:00
82
+
83
+ # List tables in the catalog
84
+ atquery iceberg catalog list-tables
85
+
86
+ Notes:
87
+ Times are interpreted in the session time zone of the MySQL connection
88
+ (e.g. -d "mysql://.../mydb?time_zone=+09:00"). --cutoff-at must not carry a UTC offset.
89
+ Environment variables: ATQUERY_DATASOURCE, ATQUERY_ICEBERG_WAREHOUSE, ATQUERY_ICEBERG_NAMESPACE,
90
+ ATQUERY_ICEBERG_CATALOG_NAME
91
+ """
92
+
93
+ sub = parser.add_subparsers(dest="iceberg_command", help="iceberg commands")
94
+
95
+ export_parser = sub.add_parser("export", help="Export a MySQL table (or query result) to an Iceberg table")
96
+ export_parser.add_argument("table_name", help="Source MySQL table (also the Iceberg table name)")
97
+ export_parser.add_argument("--query", help="SELECT statement to export (default: all rows of table_name)")
98
+ export_parser.add_argument(
99
+ "--output-name",
100
+ dest="output_name",
101
+ metavar="NAME",
102
+ help="Iceberg table name to write to, without namespace (default: table_name)",
103
+ )
104
+ _add_common_args(export_parser)
105
+ _add_namespace_arg(export_parser, schema_default=True)
106
+
107
+ cutoff_parser = sub.add_parser("cutoff", help="Cutoff management for periodic log offloading")
108
+ cutoff_sub = cutoff_parser.add_subparsers(dest="cutoff_command", help="cutoff commands")
109
+ cutoff_run_parser = cutoff_sub.add_parser(
110
+ "run", help="Export rows older than the cutoff to Iceberg, then delete them from MySQL"
111
+ )
112
+ cutoff_run_parser.add_argument("table_name", help="Source MySQL table (also the Iceberg table name)")
113
+ cutoff_run_parser.add_argument(
114
+ "--cutoff-at",
115
+ required=True,
116
+ dest="cutoff_at",
117
+ metavar="DATETIME",
118
+ help="Rows with time_column < DATETIME are offloaded (ISO 8601, e.g. 2026-01-01T00:00:00)",
119
+ )
120
+ cutoff_run_parser.add_argument(
121
+ "--time-column",
122
+ required=True,
123
+ dest="time_column",
124
+ metavar="COLUMN",
125
+ help="DATETIME/TIMESTAMP column used for the cutoff",
126
+ )
127
+ cutoff_run_parser.add_argument(
128
+ "--purge-batch-size",
129
+ type=int,
130
+ default=10_000,
131
+ dest="purge_batch_size",
132
+ metavar="N",
133
+ help="Max rows per DELETE statement (default: 10000)",
134
+ )
135
+ _add_common_args(cutoff_run_parser)
136
+ _add_namespace_arg(cutoff_run_parser, schema_default=True)
137
+
138
+ catalog_parser = sub.add_parser("catalog", help="Inspect the catalog")
139
+ catalog_sub = catalog_parser.add_subparsers(dest="catalog_command", help="catalog commands")
140
+ list_tables_parser = catalog_sub.add_parser("list-tables", help="List tables registered in the catalog")
141
+ _add_common_args(list_tables_parser, warehouse_required=False)
142
+ _add_namespace_arg(list_tables_parser, schema_default=False)
143
+
144
+
145
+ def execute_iceberg(args: argparse.Namespace) -> int:
146
+ """iceberg コマンドをディスパッチする。"""
147
+ command = args.iceberg_command
148
+ if command == "export":
149
+ return _run(args, _execute_export)
150
+ if command == "cutoff" and getattr(args, "cutoff_command", None) == "run":
151
+ return _run(args, _execute_cutoff_run)
152
+ if command == "catalog" and getattr(args, "catalog_command", None) == "list-tables":
153
+ return _run(args, _execute_list_tables)
154
+ print("usage: atquery iceberg {export,cutoff run,catalog list-tables} [options]", file=sys.stderr)
155
+ print("Run 'atquery iceberg --help' for more information.", file=sys.stderr)
156
+ return EXIT_ERROR
157
+
158
+
159
+ def _run(args: argparse.Namespace, func) -> int:
160
+ uri = resolve_datasource_uri(getattr(args, "datasource", None), getattr(args, "schema", None))
161
+ if not uri:
162
+ print("Error: -d/--datasource または ATQUERY_DATASOURCE 環境変数を指定してください", file=sys.stderr)
163
+ return EXIT_ERROR
164
+ try:
165
+ properties = _parse_properties(args.properties)
166
+ with create_datasource(uri) as datasource:
167
+ func(args, datasource, properties)
168
+ return EXIT_SUCCESS
169
+ except ImportError as e:
170
+ print(f"{_IMPORT_ERROR_MESSAGE} ({e})", file=sys.stderr)
171
+ return EXIT_ERROR
172
+ except Exception as e:
173
+ print(f"Error: {format_error_for_cli(e)}", file=sys.stderr)
174
+ return EXIT_ERROR
175
+
176
+
177
+ def _parse_properties(values: list[str]) -> dict[str, str]:
178
+ properties: dict[str, str] = {}
179
+ for value in values:
180
+ key, sep, val = value.partition("=")
181
+ if not sep or not key:
182
+ raise ValueError(f"--property は KEY=VALUE 形式で指定してください: {value!r}")
183
+ properties[key] = val
184
+ return properties
185
+
186
+
187
+ def _require_warehouse(args: argparse.Namespace) -> str:
188
+ if not args.warehouse:
189
+ raise ValueError("--warehouse または ATQUERY_ICEBERG_WAREHOUSE 環境変数を指定してください")
190
+ return args.warehouse
191
+
192
+
193
+ def _execute_export(args: argparse.Namespace, datasource, properties: dict[str, str]) -> None:
194
+ from atquery.iceberg import IcebergExporter
195
+
196
+ exporter = IcebergExporter.from_datasource(
197
+ datasource,
198
+ _require_warehouse(args),
199
+ namespace=args.namespace,
200
+ catalog_name=args.catalog_name,
201
+ properties=properties,
202
+ )
203
+ exporter.export(args.table_name, query=args.query, output_name=args.output_name)
204
+ print("exported: " + ".".join(exporter.table_identifier(args.table_name, args.output_name)))
205
+
206
+
207
+ def _execute_cutoff_run(args: argparse.Namespace, datasource, properties: dict[str, str]) -> None:
208
+ from atquery.iceberg import IcebergCutoffOffloader, IcebergExporter
209
+
210
+ cutoff_at = _parse_datetime(args.cutoff_at)
211
+ exporter = IcebergExporter.from_datasource(
212
+ datasource,
213
+ _require_warehouse(args),
214
+ namespace=args.namespace,
215
+ catalog_name=args.catalog_name,
216
+ properties=properties,
217
+ )
218
+ IcebergCutoffOffloader(
219
+ exporter, time_column=args.time_column, purge_batch_size=args.purge_batch_size
220
+ ).run(args.table_name, cutoff_at)
221
+ print(f"offloaded: {'.'.join(exporter.table_identifier(args.table_name))} (cutoff_at < {args.cutoff_at})")
222
+
223
+
224
+ def _execute_list_tables(args: argparse.Namespace, datasource, properties: dict[str, str]) -> None:
225
+ from atquery.iceberg._catalog import MySQLCatalog
226
+
227
+ catalog_properties = dict(properties)
228
+ if args.warehouse:
229
+ catalog_properties["warehouse"] = args.warehouse
230
+ catalog = MySQLCatalog(args.catalog_name, datasource, **catalog_properties)
231
+ namespaces = [(args.namespace,)] if args.namespace else sorted(catalog.list_namespaces())
232
+ for namespace in namespaces:
233
+ for identifier in sorted(catalog.list_tables(namespace)):
234
+ print(".".join(identifier))
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
@@ -716,6 +716,15 @@ Examples:
716
716
  )
717
717
  setup_ecd_parser(ecd_parser)
718
718
 
719
+ # iceberg サブコマンドの追加
720
+ from atquery.cli.iceberg import setup_parser as setup_iceberg_parser
721
+ iceberg_parser = subparsers.add_parser(
722
+ "iceberg",
723
+ help="Export MySQL tables to Iceberg: export/cutoff/catalog (requires atquery[iceberg-export])",
724
+ description="Export MySQL tables to Iceberg: export/cutoff/catalog",
725
+ )
726
+ setup_iceberg_parser(iceberg_parser)
727
+
719
728
  parser.add_argument(
720
729
  "-d",
721
730
  "--datasource",
@@ -855,6 +864,10 @@ Examples:
855
864
  from atquery.cli.ecd import execute_ecd
856
865
  return execute_ecd(args)
857
866
 
867
+ if args.subcommand == "iceberg":
868
+ from atquery.cli.iceberg import execute_iceberg
869
+ return execute_iceberg(args)
870
+
858
871
  # データベース接続文字列の取得(優先順位: 引数 > 環境変数)、-s/--schemaがあればschema部分を上書き
859
872
  database_uri = resolve_datasource_uri(args.datasource, args.schema)
860
873