clickhouse-sqlalchemy-ng 0.4.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 (65) hide show
  1. clickhouse_sqlalchemy_ng-0.4.0/LICENSE +21 -0
  2. clickhouse_sqlalchemy_ng-0.4.0/PKG-INFO +170 -0
  3. clickhouse_sqlalchemy_ng-0.4.0/README.rst +119 -0
  4. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/__init__.py +15 -0
  5. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/alembic/__init__.py +0 -0
  6. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/alembic/comparators.py +208 -0
  7. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/alembic/dialect.py +92 -0
  8. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/alembic/operations.py +170 -0
  9. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/alembic/renderers.py +114 -0
  10. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/alembic/toimpl.py +79 -0
  11. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/__init__.py +4 -0
  12. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/asynch/__init__.py +0 -0
  13. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/asynch/base.py +74 -0
  14. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/asynch/connector.py +206 -0
  15. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/base.py +578 -0
  16. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/compilers/__init__.py +0 -0
  17. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/compilers/ddlcompiler.py +305 -0
  18. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/compilers/sqlcompiler.py +525 -0
  19. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/compilers/typecompiler.py +181 -0
  20. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/http/__init__.py +0 -0
  21. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/http/base.py +61 -0
  22. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/http/connector.py +256 -0
  23. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/http/escaper.py +85 -0
  24. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/http/exceptions.py +3 -0
  25. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/http/transport.py +180 -0
  26. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/http/utils.py +14 -0
  27. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/native/__init__.py +0 -0
  28. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/native/base.py +88 -0
  29. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/native/connector.py +271 -0
  30. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/reflection.py +53 -0
  31. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/drivers/util.py +45 -0
  32. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/engines/__init__.py +40 -0
  33. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/engines/base.py +66 -0
  34. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/engines/mergetree.py +225 -0
  35. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/engines/misc.py +150 -0
  36. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/engines/replicated.py +176 -0
  37. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/engines/util.py +52 -0
  38. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/exceptions.py +9 -0
  39. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/ext/__init__.py +0 -0
  40. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/ext/clauses.py +65 -0
  41. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/ext/declarative.py +46 -0
  42. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/orm/__init__.py +5 -0
  43. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/orm/query.py +136 -0
  44. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/orm/session.py +14 -0
  45. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/sql/__init__.py +6 -0
  46. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/sql/ddl.py +80 -0
  47. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/sql/functions.py +61 -0
  48. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/sql/schema.py +129 -0
  49. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/sql/selectable.py +84 -0
  50. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/types/__init__.py +79 -0
  51. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/types/common.py +256 -0
  52. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/types/ip.py +155 -0
  53. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy/types/nested.py +64 -0
  54. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy_ng.egg-info/PKG-INFO +170 -0
  55. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy_ng.egg-info/SOURCES.txt +64 -0
  56. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy_ng.egg-info/dependency_links.txt +1 -0
  57. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy_ng.egg-info/entry_points.txt +5 -0
  58. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy_ng.egg-info/requires.txt +4 -0
  59. clickhouse_sqlalchemy_ng-0.4.0/clickhouse_sqlalchemy_ng.egg-info/top_level.txt +1 -0
  60. clickhouse_sqlalchemy_ng-0.4.0/setup.cfg +21 -0
  61. clickhouse_sqlalchemy_ng-0.4.0/setup.py +108 -0
  62. clickhouse_sqlalchemy_ng-0.4.0/tests/test_compiler.py +78 -0
  63. clickhouse_sqlalchemy_ng-0.4.0/tests/test_ddl.py +548 -0
  64. clickhouse_sqlalchemy_ng-0.4.0/tests/test_reflection.py +245 -0
  65. clickhouse_sqlalchemy_ng-0.4.0/tests/testcase.py +172 -0
@@ -0,0 +1,21 @@
1
+ This is the MIT license: http://www.opensource.org/licenses/mit-license.php
2
+
3
+ Copyright (c) 2017 by Konstantin Lebedev.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: clickhouse-sqlalchemy-ng
3
+ Version: 0.4.0
4
+ Summary: Simple ClickHouse SQLAlchemy Dialect
5
+ Home-page: https://github.com/akurdyukov/clickhouse-sqlalchemy
6
+ Author: Konstantin Lebedev
7
+ Author-email: kostyan.lebedev@gmail.com
8
+ License: MIT
9
+ Project-URL: Documentation, https://clickhouse-sqlalchemy.readthedocs.io
10
+ Project-URL: Changes, https://github.com/akurdyukov/clickhouse-sqlalchemy/blob/master/CHANGELOG.md
11
+ Keywords: ClickHouse db database cloud analytics
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Information Technology
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: SQL
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: 3.14
24
+ Classifier: Programming Language :: Python :: 3.15
25
+ Classifier: Topic :: Database
26
+ Classifier: Topic :: Software Development
27
+ Classifier: Topic :: Software Development :: Libraries
28
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
29
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
30
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
31
+ Requires-Python: >=3.11, <4
32
+ Description-Content-Type: text/x-rst
33
+ License-File: LICENSE
34
+ Requires-Dist: sqlalchemy<2.1.0,>=2.0.0
35
+ Requires-Dist: requests
36
+ Requires-Dist: clickhouse-driver
37
+ Requires-Dist: asynch>=0.2.5
38
+ Dynamic: author
39
+ Dynamic: author-email
40
+ Dynamic: classifier
41
+ Dynamic: description
42
+ Dynamic: description-content-type
43
+ Dynamic: home-page
44
+ Dynamic: keywords
45
+ Dynamic: license
46
+ Dynamic: license-file
47
+ Dynamic: project-url
48
+ Dynamic: requires-dist
49
+ Dynamic: requires-python
50
+ Dynamic: summary
51
+
52
+ ClickHouse SQLAlchemy
53
+ =====================
54
+
55
+ ClickHouse dialect for SQLAlchemy to `ClickHouse database <https://clickhouse.com/>`_.
56
+
57
+ This is ``clickhouse-sqlalchemy-ng``, a maintained fork of
58
+ `clickhouse-sqlalchemy <https://github.com/xzkostyan/clickhouse-sqlalchemy>`_
59
+ by Konstantin Lebedev, which has had no release since 0.3.2. It keeps the
60
+ ``clickhouse_sqlalchemy`` import name and the ``clickhouse://`` URL schemes,
61
+ so it is a drop-in replacement.
62
+
63
+
64
+ .. image:: https://img.shields.io/pypi/v/clickhouse-sqlalchemy-ng.svg
65
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
66
+
67
+ .. image:: https://coveralls.io/repos/github/akurdyukov/clickhouse-sqlalchemy/badge.svg?branch=master
68
+ :target: https://coveralls.io/github/akurdyukov/clickhouse-sqlalchemy?branch=master
69
+
70
+ .. image:: https://img.shields.io/pypi/l/clickhouse-sqlalchemy-ng.svg
71
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
72
+
73
+ .. image:: https://img.shields.io/pypi/pyversions/clickhouse-sqlalchemy-ng.svg
74
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
75
+
76
+ .. image:: https://img.shields.io/pypi/dm/clickhouse-sqlalchemy-ng.svg
77
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
78
+
79
+ .. image:: https://github.com/akurdyukov/clickhouse-sqlalchemy/actions/workflows/actions.yml/badge.svg
80
+ :target: https://github.com/akurdyukov/clickhouse-sqlalchemy/actions/workflows/actions.yml
81
+
82
+
83
+ Installation
84
+ ============
85
+
86
+ .. code-block:: bash
87
+
88
+ pip install clickhouse-sqlalchemy-ng
89
+
90
+ Both packages install the same ``clickhouse_sqlalchemy`` module, so uninstall
91
+ the original first: ``pip uninstall clickhouse-sqlalchemy``.
92
+
93
+
94
+ Documentation
95
+ =============
96
+
97
+ Documentation of the original project is available at
98
+ https://clickhouse-sqlalchemy.readthedocs.io and applies to this fork; see
99
+ `CHANGELOG.md <https://github.com/akurdyukov/clickhouse-sqlalchemy/blob/master/CHANGELOG.md>`_ for what changed since 0.3.2.
100
+
101
+
102
+ Usage
103
+ =====
104
+
105
+ Supported interfaces:
106
+
107
+ - **native** [recommended] (TCP) via `clickhouse-driver <https://github.com/mymarilyn/clickhouse-driver>`
108
+ - **async native** (TCP) via `asynch <https://github.com/long2ice/asynch>`
109
+ - **http** via requests
110
+
111
+ Define table
112
+
113
+ .. code-block:: python
114
+
115
+ from sqlalchemy import create_engine, Column, MetaData
116
+
117
+ from clickhouse_sqlalchemy import (
118
+ Table, make_session, get_declarative_base, types, engines
119
+ )
120
+
121
+ uri = 'clickhouse+native://localhost/default'
122
+
123
+ engine = create_engine(uri)
124
+ session = make_session(engine)
125
+ metadata = MetaData(bind=engine)
126
+
127
+ Base = get_declarative_base(metadata=metadata)
128
+
129
+ class Rate(Base):
130
+ day = Column(types.Date, primary_key=True)
131
+ value = Column(types.Int32)
132
+
133
+ __table_args__ = (
134
+ engines.Memory(),
135
+ )
136
+
137
+ Rate.__table__.create()
138
+
139
+
140
+ Insert some data
141
+
142
+ .. code-block:: python
143
+
144
+ from datetime import date, timedelta
145
+
146
+ from sqlalchemy import func
147
+
148
+ today = date.today()
149
+ rates = [
150
+ {'day': today - timedelta(i), 'value': 200 - i}
151
+ for i in range(100)
152
+ ]
153
+
154
+
155
+ And query inserted data
156
+
157
+ .. code-block:: python
158
+
159
+ session.execute(Rate.__table__.insert(), rates)
160
+
161
+ session.query(func.count(Rate.day)) \
162
+ .filter(Rate.day > today - timedelta(20)) \
163
+ .scalar()
164
+
165
+
166
+ License
167
+ =======
168
+
169
+ ClickHouse SQLAlchemy is distributed under the `MIT license
170
+ <http://www.opensource.org/licenses/mit-license.php>`_.
@@ -0,0 +1,119 @@
1
+ ClickHouse SQLAlchemy
2
+ =====================
3
+
4
+ ClickHouse dialect for SQLAlchemy to `ClickHouse database <https://clickhouse.com/>`_.
5
+
6
+ This is ``clickhouse-sqlalchemy-ng``, a maintained fork of
7
+ `clickhouse-sqlalchemy <https://github.com/xzkostyan/clickhouse-sqlalchemy>`_
8
+ by Konstantin Lebedev, which has had no release since 0.3.2. It keeps the
9
+ ``clickhouse_sqlalchemy`` import name and the ``clickhouse://`` URL schemes,
10
+ so it is a drop-in replacement.
11
+
12
+
13
+ .. image:: https://img.shields.io/pypi/v/clickhouse-sqlalchemy-ng.svg
14
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
15
+
16
+ .. image:: https://coveralls.io/repos/github/akurdyukov/clickhouse-sqlalchemy/badge.svg?branch=master
17
+ :target: https://coveralls.io/github/akurdyukov/clickhouse-sqlalchemy?branch=master
18
+
19
+ .. image:: https://img.shields.io/pypi/l/clickhouse-sqlalchemy-ng.svg
20
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
21
+
22
+ .. image:: https://img.shields.io/pypi/pyversions/clickhouse-sqlalchemy-ng.svg
23
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
24
+
25
+ .. image:: https://img.shields.io/pypi/dm/clickhouse-sqlalchemy-ng.svg
26
+ :target: https://pypi.org/project/clickhouse-sqlalchemy-ng
27
+
28
+ .. image:: https://github.com/akurdyukov/clickhouse-sqlalchemy/actions/workflows/actions.yml/badge.svg
29
+ :target: https://github.com/akurdyukov/clickhouse-sqlalchemy/actions/workflows/actions.yml
30
+
31
+
32
+ Installation
33
+ ============
34
+
35
+ .. code-block:: bash
36
+
37
+ pip install clickhouse-sqlalchemy-ng
38
+
39
+ Both packages install the same ``clickhouse_sqlalchemy`` module, so uninstall
40
+ the original first: ``pip uninstall clickhouse-sqlalchemy``.
41
+
42
+
43
+ Documentation
44
+ =============
45
+
46
+ Documentation of the original project is available at
47
+ https://clickhouse-sqlalchemy.readthedocs.io and applies to this fork; see
48
+ `CHANGELOG.md <https://github.com/akurdyukov/clickhouse-sqlalchemy/blob/master/CHANGELOG.md>`_ for what changed since 0.3.2.
49
+
50
+
51
+ Usage
52
+ =====
53
+
54
+ Supported interfaces:
55
+
56
+ - **native** [recommended] (TCP) via `clickhouse-driver <https://github.com/mymarilyn/clickhouse-driver>`
57
+ - **async native** (TCP) via `asynch <https://github.com/long2ice/asynch>`
58
+ - **http** via requests
59
+
60
+ Define table
61
+
62
+ .. code-block:: python
63
+
64
+ from sqlalchemy import create_engine, Column, MetaData
65
+
66
+ from clickhouse_sqlalchemy import (
67
+ Table, make_session, get_declarative_base, types, engines
68
+ )
69
+
70
+ uri = 'clickhouse+native://localhost/default'
71
+
72
+ engine = create_engine(uri)
73
+ session = make_session(engine)
74
+ metadata = MetaData(bind=engine)
75
+
76
+ Base = get_declarative_base(metadata=metadata)
77
+
78
+ class Rate(Base):
79
+ day = Column(types.Date, primary_key=True)
80
+ value = Column(types.Int32)
81
+
82
+ __table_args__ = (
83
+ engines.Memory(),
84
+ )
85
+
86
+ Rate.__table__.create()
87
+
88
+
89
+ Insert some data
90
+
91
+ .. code-block:: python
92
+
93
+ from datetime import date, timedelta
94
+
95
+ from sqlalchemy import func
96
+
97
+ today = date.today()
98
+ rates = [
99
+ {'day': today - timedelta(i), 'value': 200 - i}
100
+ for i in range(100)
101
+ ]
102
+
103
+
104
+ And query inserted data
105
+
106
+ .. code-block:: python
107
+
108
+ session.execute(Rate.__table__.insert(), rates)
109
+
110
+ session.query(func.count(Rate.day)) \
111
+ .filter(Rate.day > today - timedelta(20)) \
112
+ .scalar()
113
+
114
+
115
+ License
116
+ =======
117
+
118
+ ClickHouse SQLAlchemy is distributed under the `MIT license
119
+ <http://www.opensource.org/licenses/mit-license.php>`_.
@@ -0,0 +1,15 @@
1
+
2
+ from .ext.declarative import get_declarative_base
3
+ from .orm.session import make_session
4
+ from .sql import Table, MaterializedView, select
5
+
6
+
7
+ VERSION = (0, 4, 0)
8
+ __version__ = '.'.join(str(x) for x in VERSION)
9
+
10
+
11
+ __all__ = (
12
+ 'get_declarative_base',
13
+ 'make_session',
14
+ 'Table', 'MaterializedView', 'select'
15
+ )
@@ -0,0 +1,208 @@
1
+ import logging
2
+
3
+ from alembic import __version__ as alembic_version
4
+ from alembic.autogenerate import comparators
5
+ from alembic.operations.ops import ModifyTableOps
6
+ from sqlalchemy import schema as sa_schema
7
+ from sqlalchemy import text
8
+
9
+ from clickhouse_sqlalchemy.sql.schema import Table
10
+ from . import operations
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+ alembic_version = tuple(
15
+ (int(x) if x.isdigit() else x) for x in alembic_version.split('.')
16
+ )
17
+ _alembic_118 = alembic_version >= (1, 18, 0)
18
+
19
+ if _alembic_118:
20
+ from alembic.autogenerate.compare.tables import _compare_columns
21
+ else:
22
+ from alembic.autogenerate.compare import _compare_columns
23
+
24
+
25
+ def _extract_to_table_name(create_table_query):
26
+ query = create_table_query
27
+ # Naive inner name detection
28
+ brace = query.index('(')
29
+ inner_name = query[query.index(' TO ', 0, brace) + 4:brace - 1].strip(' `')
30
+ return inner_name.split('.')[1] if '.' in inner_name else inner_name
31
+
32
+
33
+ # Direct call .dispatch_for('schema', 'clickhouse') override an Alembic
34
+ # default ('schema', 'default') comparator. To avoid it (as we have own
35
+ # implementation only for a materialized views) register default "schema"
36
+ # comparators as "clickhouse" comparators too.
37
+ if _alembic_118:
38
+ for (target, qualifier, priority), fns in comparators._registry.items():
39
+ if target == 'schema' and qualifier == 'default':
40
+ for fn, _ in fns:
41
+ comparators.dispatch_for(
42
+ 'schema', qualifier='clickhouse', priority=priority
43
+ )(fn)
44
+ else:
45
+ for fn in comparators._registry[('schema', 'default')]:
46
+ comparators.dispatch_for('schema', qualifier='clickhouse')(fn)
47
+
48
+
49
+ @comparators.dispatch_for('schema', qualifier='clickhouse')
50
+ def compare_mat_view(autogen_context, upgrade_ops, schemas):
51
+ connection = autogen_context.connection
52
+ dialect = autogen_context.dialect
53
+ metadata = autogen_context.metadata
54
+
55
+ database_engine = dialect._execute(
56
+ connection,
57
+ text(
58
+ 'SELECT engine '
59
+ 'FROM system.databases '
60
+ 'WHERE database = currentDatabase()'
61
+ ), scalar=True
62
+ )
63
+
64
+ is_atomic = database_engine.lower() == 'atomic'
65
+ logger.info('Database engine: %s', database_engine)
66
+ if is_atomic:
67
+ logger.info('Using "TO table" for materialized views storage')
68
+ else:
69
+ logger.info('Using ".inner" for materialized views storage by default')
70
+
71
+ all_mat_views = set(dialect.get_view_names(connection))
72
+
73
+ metadata_mat_views = metadata.info.setdefault('mat_views', set())
74
+
75
+ statement_compiler = dialect.statement_compiler(dialect, None)
76
+ ddl_compiler = dialect.ddl_compiler(dialect, None)
77
+
78
+ for name in sorted(metadata_mat_views.difference(all_mat_views)):
79
+ view = metadata.mat_views[name]
80
+
81
+ selectable = statement_compiler.process(
82
+ view.mv_selectable, literal_binds=True
83
+ )
84
+
85
+ logger.info('Detected added materialized view %s', name)
86
+
87
+ if is_atomic or view.to:
88
+ create = operations.CreateMatViewToTableOp(
89
+ view.name, selectable, view.inner_table.name
90
+ )
91
+
92
+ else:
93
+ inner_table = view.inner_table
94
+ engine = ddl_compiler.process(inner_table.engine)
95
+
96
+ create = operations.CreateMatViewOp(
97
+ view.name, selectable, engine, *inner_table.columns
98
+ )
99
+
100
+ upgrade_ops.ops.append(create)
101
+
102
+ existing_metadata = sa_schema.MetaData()
103
+ inspector = autogen_context.inspector
104
+
105
+ removed_mat_views = all_mat_views.difference(metadata_mat_views)
106
+ existing_mat_views = all_mat_views.intersection(metadata_mat_views)
107
+
108
+ mat_view_params_by_name = {}
109
+ if removed_mat_views | existing_mat_views:
110
+ rv = dialect._execute(
111
+ connection,
112
+ text(
113
+ 'SELECT name, as_select, engine_full, create_table_query '
114
+ 'FROM system.tables '
115
+ 'WHERE database = currentDatabase() AND name IN :names'
116
+ ), names=list(removed_mat_views | existing_mat_views)
117
+ )
118
+ mat_view_params_by_name = {x.name: x for x in rv}
119
+
120
+ for name in sorted(removed_mat_views):
121
+ logger.info('Detected removed materialized view %s', name)
122
+ params = mat_view_params_by_name[name]
123
+
124
+ try:
125
+ inner_name = _extract_to_table_name(params.create_table_query)
126
+ except ValueError:
127
+ inner_name = None
128
+
129
+ if inner_name:
130
+ drop = operations.DropMatViewToTableOp(
131
+ name, params.as_select, inner_name
132
+ )
133
+ else:
134
+ table = Table(name, existing_metadata)
135
+ inspector.reflect_table(table, include_columns=None)
136
+
137
+ drop = operations.DropMatViewOp(
138
+ name, params.as_select, params.engine_full, *table.columns
139
+ )
140
+
141
+ upgrade_ops.ops.append(drop)
142
+
143
+ for name in sorted(existing_mat_views):
144
+ view = metadata.mat_views[name]
145
+ params = mat_view_params_by_name[name]
146
+ if is_atomic or view.to:
147
+ inner_name = _extract_to_table_name(params.create_table_query)
148
+ else:
149
+ inner_name = '.inner.' + name
150
+
151
+ conn_table = Table(inner_name, existing_metadata)
152
+ inspector.reflect_table(conn_table, include_columns=None)
153
+
154
+ if not autogen_context.run_object_filters(
155
+ view, name, 'mat_view', False, conn_table
156
+ ):
157
+ return
158
+
159
+ metadata_table = metadata.mat_views[name].inner_table
160
+ modify_table_ops = ModifyTableOps(name, [])
161
+ schema = None
162
+ with _compare_columns(
163
+ schema,
164
+ inner_name,
165
+ conn_table,
166
+ metadata_table,
167
+ modify_table_ops,
168
+ autogen_context,
169
+ inspector,
170
+ ):
171
+ comparators.dispatch('table')(
172
+ autogen_context,
173
+ modify_table_ops,
174
+ schema,
175
+ inner_name,
176
+ conn_table,
177
+ metadata_table,
178
+ )
179
+
180
+ if not modify_table_ops.is_empty():
181
+ selectable = statement_compiler.process(
182
+ view.mv_selectable, literal_binds=True
183
+ )
184
+ engine = ddl_compiler.process(metadata_table.engine)
185
+
186
+ if is_atomic or view.to:
187
+ ops = [
188
+ operations.DropMatViewToTableOp(
189
+ name, params.as_select, metadata_table.name
190
+ ),
191
+ modify_table_ops,
192
+ operations.CreateMatViewToTableOp(
193
+ view.name, selectable, metadata_table.name
194
+ )
195
+ ]
196
+ else:
197
+ ops = [
198
+ operations.DetachMatViewOp(
199
+ name, params.as_select, engine,
200
+ *metadata_table.columns
201
+ ),
202
+ modify_table_ops,
203
+ operations.AttachMatViewOp(
204
+ name, selectable, engine, *metadata_table.columns
205
+ )
206
+ ]
207
+
208
+ upgrade_ops.ops.extend(ops)
@@ -0,0 +1,92 @@
1
+ from sqlalchemy import func, Column, types as sqltypes
2
+
3
+ try:
4
+ from alembic.ddl import impl
5
+ from alembic.ddl.base import (
6
+ compiles, ColumnComment, format_table_name, format_column_name
7
+ )
8
+ except ImportError:
9
+ raise RuntimeError('alembic must be installed')
10
+
11
+ from clickhouse_sqlalchemy import types, engines
12
+ from clickhouse_sqlalchemy.sql.ddl import DropTable
13
+ from .comparators import compare_mat_view
14
+ from .renderers import (
15
+ render_attach_mat_view, render_detach_mat_view,
16
+ render_create_mat_view, render_drop_mat_view
17
+ )
18
+ from .toimpl import (
19
+ create_mat_view, attach_mat_view
20
+ )
21
+
22
+
23
+ class ClickHouseDialectImpl(impl.DefaultImpl):
24
+ __dialect__ = 'clickhouse'
25
+ transactional_ddl = False
26
+
27
+ def drop_table(self, table):
28
+ table.dispatch.before_drop(
29
+ table, self.connection, checkfirst=False, _ddl_runner=self
30
+ )
31
+
32
+ self._exec(DropTable(table))
33
+
34
+ table.dispatch.after_drop(
35
+ table, self.connection, checkfirst=False, _ddl_runner=self
36
+ )
37
+
38
+
39
+ def patch_alembic_version(context, **kwargs):
40
+ migration_context = context._proxy._migration_context
41
+ version = migration_context._version
42
+
43
+ dt = Column('dt', types.DateTime, server_default=func.now())
44
+ version_num = Column('version_num', types.String, primary_key=True)
45
+ version.append_column(dt)
46
+ version.append_column(version_num, replace_existing=True)
47
+
48
+ if 'cluster' in kwargs:
49
+ cluster = kwargs['cluster']
50
+ version.engine = engines.ReplicatedReplacingMergeTree(
51
+ kwargs['table_path'], kwargs['replica_name'],
52
+ version=dt, order_by=func.tuple()
53
+ )
54
+ version.kwargs['clickhouse_cluster'] = cluster
55
+ else:
56
+ version.engine = engines.ReplacingMergeTree(
57
+ version=dt, order_by=func.tuple()
58
+ )
59
+
60
+
61
+ def include_object(object, name, type_, reflected, compare_to):
62
+ # skip inner matview tables in autogeneration.
63
+ if type_ == 'table' and object.info.get('mv_storage'):
64
+ return False
65
+
66
+ return True
67
+
68
+
69
+ @compiles(ColumnComment, 'clickhouse')
70
+ def visit_column_comment(element, compiler, **kw):
71
+ ddl = "ALTER TABLE {table_name} COMMENT COLUMN {column_name} {comment}"
72
+ comment = (
73
+ compiler.sql_compiler.render_literal_value(
74
+ element.comment or '', sqltypes.String()
75
+ )
76
+ )
77
+
78
+ return ddl.format(
79
+ table_name=format_table_name(
80
+ compiler, element.table_name, element.schema
81
+ ),
82
+ column_name=format_column_name(compiler, element.column_name),
83
+ comment=comment,
84
+ )
85
+
86
+
87
+ __all__ = (
88
+ 'ClickHouseDialectImpl', 'compare_mat_view',
89
+ 'render_attach_mat_view', 'render_detach_mat_view',
90
+ 'render_create_mat_view', 'render_drop_mat_view',
91
+ 'create_mat_view', 'attach_mat_view'
92
+ )