Skip to content

Commit 9c2ad4f

Browse files
wolfgang-auramday-ioclaude
authored
fix(clickhouse): re-raise non-NOT_IMPLEMENTED errors from EXCHANGE TABLES (#6105)
Signed-off-by: wolfgang-aura <169568318+wolfgang-aura@users.noreply.github.com> Co-authored-by: Michael Day <mdaytn@gmail.com> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 9412674 commit 9c2ad4f

2 files changed

Lines changed: 71 additions & 11 deletions

File tree

‎sqlmesh/core/engine_adapter/clickhouse.py‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -861,17 +861,19 @@ def _exchange_tables(
861861
try:
862862
self.execute(f"EXCHANGE TABLES {old_table_sql} AND {new_table_sql}{on_cluster_sql}")
863863
except DatabaseError as e:
864-
if "NOT_IMPLEMENTED" in str(e):
865-
# If someone is using an old Clickhouse version, an OS that doesn't support atomic exchanges,
866-
# or a database engine that doesn't support atomic exchanges, we do a non-atomic rename instead.
867-
#
868-
# Executing multiple renames in one call like `RENAME TABLE a to b, c to a` is supported
869-
# but not an atomic operation. Because it is not atomic, doing it in two calls is equivalent
870-
# and does not require defining an additional method.
871-
throwaway_table_name = self._get_temp_table(old_table_name)
872-
self._rename_table(old_table_name, throwaway_table_name)
873-
self._rename_table(new_table_name, old_table_name)
874-
self.drop_table(throwaway_table_name)
864+
if "NOT_IMPLEMENTED" not in str(e):
865+
raise
866+
867+
# If someone is using an old Clickhouse version, an OS that doesn't support atomic exchanges,
868+
# or a database engine that doesn't support atomic exchanges, we do a non-atomic rename instead.
869+
#
870+
# Executing multiple renames in one call like `RENAME TABLE a to b, c to a` is supported
871+
# but not an atomic operation. Because it is not atomic, doing it in two calls is equivalent
872+
# and does not require defining an additional method.
873+
throwaway_table_name = self._get_temp_table(old_table_name)
874+
self._rename_table(old_table_name, throwaway_table_name)
875+
self._rename_table(new_table_name, old_table_name)
876+
self.drop_table(throwaway_table_name)
875877

876878
def _rename_table(
877879
self,

‎tests/core/engine_adapter/test_clickhouse.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,64 @@ def test_exchange_tables(
14521452
]
14531453

14541454

1455+
def _executed_sql(execute_mock: t.Any) -> t.List[str]:
1456+
return [
1457+
quote_identifiers(call.args[0]).sql("clickhouse")
1458+
if isinstance(call.args[0], exp.Expr)
1459+
else call.args[0]
1460+
for call in execute_mock.call_args_list
1461+
]
1462+
1463+
1464+
def test_insert_overwrite_by_condition_replace_exchange_error_propagates(
1465+
make_mocked_engine_adapter: t.Callable, mocker: MockerFixture, make_temp_table_name: t.Callable
1466+
):
1467+
from clickhouse_connect.driver.exceptions import DatabaseError # type: ignore
1468+
1469+
adapter = make_mocked_engine_adapter(ClickhouseEngineAdapter)
1470+
1471+
temp_table_mock = mocker.patch("sqlmesh.core.engine_adapter.EngineAdapter._get_temp_table")
1472+
temp_table_mock.return_value = make_temp_table_name("target", "abcd")
1473+
1474+
fetchone_mock = mocker.patch("sqlmesh.core.engine_adapter.ClickhouseEngineAdapter.fetchone")
1475+
fetchone_mock.return_value = None
1476+
1477+
insert_table_name = make_temp_table_name("new_records", "abcd")
1478+
existing_table_name = make_temp_table_name("existing_records", "abcd")
1479+
1480+
source_queries, columns_to_types = adapter._get_source_queries_and_columns_to_types(
1481+
parse_one(f"SELECT * FROM {insert_table_name}"),
1482+
{
1483+
"id": exp.DataType.build("Int8", dialect="clickhouse"),
1484+
"ds": exp.DataType.build("Date", dialect="clickhouse"),
1485+
},
1486+
existing_table_name,
1487+
)
1488+
1489+
def execute_side_effect(sql: t.Any, *args: t.Any, **kwargs: t.Any) -> None:
1490+
if str(sql).startswith("EXCHANGE TABLES"):
1491+
raise DatabaseError("DB::Exception: Not enough privileges. (ACCESS_DENIED)")
1492+
1493+
execute_mock = mocker.patch("sqlmesh.core.engine_adapter.ClickhouseEngineAdapter.execute")
1494+
execute_mock.side_effect = execute_side_effect
1495+
1496+
with pytest.raises(DatabaseError, match="ACCESS_DENIED"):
1497+
adapter._insert_overwrite_by_condition(
1498+
existing_table_name.sql(),
1499+
source_queries,
1500+
columns_to_types,
1501+
)
1502+
1503+
executed = _executed_sql(execute_mock)
1504+
assert executed[-2:] == [
1505+
'EXCHANGE TABLES "__temp_existing_records_abcd" AND "__temp_target_abcd"',
1506+
'DROP TABLE IF EXISTS "__temp_target_abcd"',
1507+
]
1508+
assert not any(sql.startswith("RENAME") for sql in executed)
1509+
dropped = [sql for sql in executed if sql.startswith("DROP")]
1510+
assert dropped == ['DROP TABLE IF EXISTS "__temp_target_abcd"']
1511+
1512+
14551513
def test_virtual_catalog_ddl_stripping(make_mocked_engine_adapter: t.Callable):
14561514
"""After inject_virtual_catalog(), create_schema() with the virtual catalog prefix must strip
14571515
the catalog and execute without raising, and with a wrong catalog must raise SQLMeshError."""

0 commit comments

Comments
 (0)