Skip to content

Commit 4b05402

Browse files
fix(clickhouse): respect user-provided insert_quorum in cluster mode
In cluster/cloud mode the ClickHouse connection always overwrote insert_quorum with "auto", ignoring a value set in connection_settings. It also mutated the user's connection_settings dict in place. Use setdefault so a user-provided insert_quorum wins, and work on a copy of the settings dict. Fixes #6171 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
1 parent c4dbac4 commit 4b05402

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎sqlmesh/core/config/connection.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ def _static_connection_kwargs(self) -> t.Dict[str, t.Any]:
23912391
# https://clickhouse.com/docs/en/operations/settings/settings
23922392
# - below are set to align with dbt-clickhouse
23932393
# - https://github.com/ClickHouse/dbt-clickhouse/blob/44d26308ea6a3c8ead25c280164aa88191f05f47/dbt/adapters/clickhouse/dbclient.py#L77
2394-
settings = self.connection_settings or {}
2394+
settings = dict(self.connection_settings or {})
23952395
# mutations_sync = 2: "The query waits for all mutations [ALTER statements] to complete on all replicas (if they exist)"
23962396
settings["mutations_sync"] = "2"
23972397
# insert_distributed_sync = 1: "INSERT operation succeeds only after all the data is saved on all shards"
@@ -2405,7 +2405,8 @@ def _static_connection_kwargs(self) -> t.Dict[str, t.Any]:
24052405
# - "INSERT succeeds only when ClickHouse manages to correctly write data to the insert_quorum of replicas during
24062406
# the insert_quorum_timeout"
24072407
# - "use majority number (number_of_replicas / 2 + 1) as quorum number"
2408-
settings["insert_quorum"] = "auto"
2408+
# - a value set by the user in connection_settings takes precedence
2409+
settings.setdefault("insert_quorum", "auto")
24092410

24102411
return {
24112412
"compress": compress,

‎tests/core/test_connection_config.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,33 @@ def test_clickhouse(make_config):
12871287
assert not config3._static_connection_kwargs["compress"]
12881288

12891289

1290+
def test_clickhouse_insert_quorum_user_setting(make_config):
1291+
user_settings = {"insert_quorum": "0"}
1292+
config = make_config(
1293+
type="clickhouse",
1294+
host="localhost",
1295+
username="default",
1296+
password="default",
1297+
cluster="default",
1298+
connection_settings=user_settings,
1299+
)
1300+
1301+
# A user-provided insert_quorum is respected in cluster mode
1302+
assert config._static_connection_kwargs["insert_quorum"] == "0"
1303+
# The user's dict is not mutated by building the connection kwargs
1304+
assert user_settings == {"insert_quorum": "0"}
1305+
1306+
# Without a user value, cluster mode still defaults to "auto"
1307+
config_default = make_config(
1308+
type="clickhouse",
1309+
host="localhost",
1310+
username="default",
1311+
password="default",
1312+
cluster="default",
1313+
)
1314+
assert config_default._static_connection_kwargs["insert_quorum"] == "auto"
1315+
1316+
12901317
def test_athena(make_config):
12911318
config = make_config(type="athena", work_group="primary")
12921319
assert isinstance(config, AthenaConnectionConfig)

0 commit comments

Comments
 (0)