Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Ilchuk committed Dec 20, 2023
1 parent 562e367 commit f1ae311
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
15 changes: 15 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,18 @@ def test_partition_at_keys(
ydb_partition_at_keys=partition_at_keys,
)
assert desc.partitioning_settings.min_partitions_count == res

def test_several_keys(self, connection, metadata):
desc = self._create_table_and_get_desc(
connection,
metadata,
ydb_auto_partitioning_by_size=True,
ydb_auto_partitioning_by_load=True,
ydb_auto_partitioning_min_partitions_count=3,
ydb_auto_partitioning_max_partitions_count=5,
)
assert desc.partitioning_settings.partitioning_by_size == 1
assert desc.partitioning_settings.partitioning_by_load == 1
assert desc.partitioning_settings.min_partitions_count == 3
assert desc.partitioning_settings.max_partitions_count == 5

31 changes: 17 additions & 14 deletions ydb_sqlalchemy/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,36 +343,39 @@ def get_bind_types(


class YqlDDLCompiler(DDLCompiler):
def post_create_table(self, table):
def post_create_table(self, table: sa.Table) -> str:
ydb_opts = table.dialect_options["ydb"]
with_content = []
with_clause_list = self._render_table_partitioning_settings(ydb_opts)
if with_clause_list:
with_clause_text = ",\n".join(with_clause_list)
return f"\nWITH (\n\t{with_clause_text}\n)"
return ""

def _render_table_partitioning_settings(self, ydb_opts: Dict[str, Any]) -> List[str]:
table_partitioning_settings = []
if ydb_opts["auto_partitioning_by_size"] is not None:
auto_partitioning_by_size = "ENABLED" if ydb_opts["auto_partitioning_by_size"] else "DISABLED"
with_content.append(f"AUTO_PARTITIONING_BY_SIZE = {auto_partitioning_by_size}")
table_partitioning_settings.append(f"AUTO_PARTITIONING_BY_SIZE = {auto_partitioning_by_size}")
if ydb_opts["auto_partitioning_by_load"] is not None:
auto_partitioning_by_load = "ENABLED" if ydb_opts["auto_partitioning_by_load"] else "DISABLED"
with_content.append(f"AUTO_PARTITIONING_BY_LOAD = {auto_partitioning_by_load}")
table_partitioning_settings.append(f"AUTO_PARTITIONING_BY_LOAD = {auto_partitioning_by_load}")
if ydb_opts["auto_partitioning_partition_size_mb"] is not None:
with_content.append(
table_partitioning_settings.append(
f"AUTO_PARTITIONING_PARTITION_SIZE_MB = {ydb_opts['auto_partitioning_partition_size_mb']}"
)
if ydb_opts["auto_partitioning_min_partitions_count"] is not None:
with_content.append(
table_partitioning_settings.append(
f"AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = {ydb_opts['auto_partitioning_min_partitions_count']}"
)
if ydb_opts["auto_partitioning_max_partitions_count"] is not None:
with_content.append(
table_partitioning_settings.append(
f"AUTO_PARTITIONING_MAX_PARTITIONS_COUNT = {ydb_opts['auto_partitioning_max_partitions_count']}"
)
if ydb_opts["uniform_partitions"] is not None:
with_content.append(f"UNIFORM_PARTITIONS = {ydb_opts['uniform_partitions']}")
table_partitioning_settings.append(f"UNIFORM_PARTITIONS = {ydb_opts['uniform_partitions']}")
if ydb_opts["partition_at_keys"] is not None:
with_content.append(f"PARTITION_AT_KEYS = {ydb_opts['partition_at_keys']}")
if with_content:
with_content = ",\n".join(with_content)
return f"\nWITH (\n\t{with_content}\n)"
return ""

table_partitioning_settings.append(f"PARTITION_AT_KEYS = {ydb_opts['partition_at_keys']}")
return table_partitioning_settings

def upsert(table):
return sa.sql.Insert(table)
Expand Down

0 comments on commit f1ae311

Please sign in to comment.