Skip to content

Commit

Permalink
add support for arbitrary SETTINGS in CREATE TABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
beda42 committed Oct 3, 2021
1 parent 7c90c1e commit c2fcba8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/infi/clickhouse_orm/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ class MergeTree(Engine):

def __init__(self, date_col=None, order_by=(), sampling_expr=None,
index_granularity=8192, replica_table_path=None, replica_name=None, partition_key=None,
primary_key=None):
primary_key=None, settings=None):
assert type(order_by) in (list, tuple), 'order_by must be a list or tuple'
assert date_col is None or isinstance(date_col, str), 'date_col must be string if present'
assert primary_key is None or type(primary_key) in (list, tuple), 'primary_key must be a list or tuple'
assert partition_key is None or type(partition_key) in (list, tuple),\
'partition_key must be tuple or list if present'
assert (replica_table_path is None) == (replica_name is None), \
'both replica_table_path and replica_name must be specified'
assert settings is None or type(settings) is dict, 'settings must be dict'

# These values conflict with each other (old and new syntax of table engines.
# So let's control only one of them is given.
Expand All @@ -56,6 +57,7 @@ def __init__(self, date_col=None, order_by=(), sampling_expr=None,
self.index_granularity = index_granularity
self.replica_table_path = replica_table_path
self.replica_name = replica_name
self.settings = settings

# I changed field name for new reality and syntax
@property
Expand Down Expand Up @@ -88,6 +90,9 @@ def create_table_sql(self, db):
partition_sql += " SAMPLE BY %s" % self.sampling_expr

partition_sql += " SETTINGS index_granularity=%d" % self.index_granularity
if self.settings:
settings_sql = ", ".join('%s=%s' % (key, value) for key, value in self.settings.items())
partition_sql += ", " + settings_sql

elif not self.date_col:
# Can't import it globally due to circular import
Expand Down Expand Up @@ -124,9 +129,10 @@ class CollapsingMergeTree(MergeTree):

def __init__(self, date_col=None, order_by=(), sign_col='sign', sampling_expr=None,
index_granularity=8192, replica_table_path=None, replica_name=None, partition_key=None,
primary_key=None):
primary_key=None, settings=None):
super(CollapsingMergeTree, self).__init__(date_col, order_by, sampling_expr, index_granularity,
replica_table_path, replica_name, partition_key, primary_key)
replica_table_path, replica_name, partition_key, primary_key,
settings=settings)
self.sign_col = sign_col

def _build_sql_params(self, db):
Expand Down

0 comments on commit c2fcba8

Please sign in to comment.