allow expressions in MergeTree params

This commit is contained in:
Itai Shirav 2020-05-16 12:15:01 +03:00
parent ce784d7af7
commit 9697157d6b
3 changed files with 11 additions and 7 deletions

View File

@ -77,11 +77,12 @@ class MergeTree(Engine):
# https://clickhouse.yandex/docs/en/table_engines/custom_partitioning_key/ # https://clickhouse.yandex/docs/en/table_engines/custom_partitioning_key/
# Let's check version and use new syntax if available # Let's check version and use new syntax if available
if db.server_version >= (1, 1, 54310): if db.server_version >= (1, 1, 54310):
partition_sql = "PARTITION BY %s ORDER BY %s" \ partition_sql = "PARTITION BY (%s) ORDER BY (%s)" \
% ('(%s)' % comma_join(self.partition_key), '(%s)' % comma_join(self.order_by)) % (comma_join(self.partition_key, stringify=True),
comma_join(self.order_by, stringify=True))
if self.primary_key: if self.primary_key:
partition_sql += " PRIMARY KEY (%s)" % comma_join(self.primary_key) partition_sql += " PRIMARY KEY (%s)" % comma_join(self.primary_key, stringify=True)
if self.sampling_expr: if self.sampling_expr:
partition_sql += " SAMPLE BY %s" % self.sampling_expr partition_sql += " SAMPLE BY %s" % self.sampling_expr
@ -113,7 +114,7 @@ class MergeTree(Engine):
params.append(self.date_col) params.append(self.date_col)
if self.sampling_expr: if self.sampling_expr:
params.append(self.sampling_expr) params.append(self.sampling_expr)
params.append('(%s)' % comma_join(self.order_by)) params.append('(%s)' % comma_join(self.order_by, stringify=True))
params.append(str(self.index_granularity)) params.append(str(self.index_granularity))
return params return params

View File

@ -68,7 +68,7 @@ def parametric(func):
def inner(*args, **kwargs): def inner(*args, **kwargs):
f = func(*args, **kwargs) f = func(*args, **kwargs)
# Append the parameter to the function name # Append the parameter to the function name
parameters_str = comma_join([str(p) for p in parameters]) parameters_str = comma_join(parameters, stringify=True)
f.name = '%s(%s)' % (f.name, parameters_str) f.name = '%s(%s)' % (f.name, parameters_str)
return f return f
return inner return inner

View File

@ -96,10 +96,13 @@ def import_submodules(package_name):
} }
def comma_join(items): def comma_join(items, stringify=False):
""" """
Joins an iterable of strings with commas. Joins an iterable of strings with commas.
""" """
if stringify:
return ', '.join(str(item) for item in items)
else:
return ', '.join(items) return ', '.join(items)