Repository navigation
fix(clickhouse): suppress ON CLUSTER for object DDL in a Replicated database - #6003
Conversation
f68f12f to
b6c3e72
Compare
7cf6528 to
80d884f
Compare
…atabase
Inside a `Replicated` database Keeper already propagates object DDL, so adding
`ON CLUSTER` asks for a second, redundant fan-out and ClickHouse rejects the
statement outright:
Code: 80. DB::Exception: It's not initial query.
ON CLUSTER is not allowed for Replicated database. (INCORRECT_QUERY)
The ClickHouse adapter had no way to avoid this: `_on_cluster_sql()` took no
argument and keyed only on `engine_run_mode.is_cluster`. The practical effect is
total -- with a cluster configured, no table or view can be created in a
`Replicated` database at all. Reproduced end to end against a 1x3 cluster on
26.6.3.62: `create_table` into such a database fails with the error above before
this change and succeeds after it, propagating to all three replicas under one
shared UUID.
This cannot be a connection-level flag. One connection can hold both an Atomic
and a `Replicated` database, and objects in the Atomic one still need
`ON CLUSTER`, so the decision is made per target database by
`_should_use_on_cluster()` and threaded through all 14 emission sites.
The policy fails open throughout. An unknown target, an unresolvable database,
or any introspection failure keeps the previous behaviour, and a single cached
probe short-circuits the whole path when the server hosts no `Replicated`
database at all -- so this is a no-op, and costs no extra round-trips, for any
deployment that does not have one.
Two carve-outs are deliberate. `CREATE DATABASE` is how a `Replicated` database
comes into existence, so there is no engine to consult, and a database's own DDL
log cannot propagate its own removal; both stay cluster-wide unconditionally.
`alter_table` decides per expression rather than per call, because one call can
carry alters against both kinds of database, and `RENAME` / `EXCHANGE` across a
Replicated and a non-Replicated database are refused rather than rendered
silently half-correct.
`_build_table_properties_exp` and `_build_view_properties_exp` receive no
reference to the object being created, so the base class now passes the target
table down. The parameter is additive and every override across the nine adapter
files already accepts `**kwargs`, so no other adapter changes; Athena already
declares and receives the same `table` parameter via its own override.
This is a correctness fix independent of managed models: any SQLMesh user
running against a `Replicated` database is affected today for ordinary tables
and views.
Signed-off-by: mday-io <mdaytn@gmail.com>
Signed-off-by: mday-io <mdaytn@gmail.com>
80d884f to
f1cfe4c
Compare
Signed-off-by: Michael Day <mdaytn@gmail.com>
|
ready for review |
|
Will look into this one |
|
Hi @mday-io — thanks for tackling the error 80 on Replicated databases. Checking the target database's engine per statement, and keeping Two small things: The three sqlmesh/sqlmesh/core/engine_adapter/clickhouse.py Line 1141 in 3f397d7 system.databases, the fix quietly turns off and error 80 comes back with no clue why. Could you add a logger.debug or logger.warning there?
The ClickHouse docs say SQLMesh injects Replicated databases?
One question, and this is already on sqlmesh/sqlmesh/core/engine_adapter/clickhouse.py Lines 776 to 787 in 3f397d7 REPLACE PARTITION and DROP PARTITION in one ALTER. As far as I can tell from shouldReplicateQuery in the ClickHouse source (https://github.com/ClickHouse/ClickHouse/blob/master/src/Databases/DatabaseReplicated.cpp), the exemption only applies when every command in the ALTER is the same kind, so a mixed one is still replicated by the database. On a multi-shard Replicated database that should still hit error 80. Was your live test single-shard? If so, it may be worth a note, or a follow-up.
Thanks again for taking the time on this. |
Signed-off-by: Michael Day <mdaytn@gmail.com>
|
Added warnings for all three metadata lookup failures in 92864ad76. Each warning identifies the failed lookup and includes the exception. The existing ON CLUSTER fallback and subsequent retry remain unchanged. Both docs claims now explain that metadata DDL targeting Replicated databases skips ON CLUSTER. They also clarify that database creation/removal and partition operations retain it. On the topology question, the original commit describes the 26.6.3.62 run as one shard with three replicas. The retained PR result records three replicas without explicitly recording the shard count. Neither provides multi-shard mixed-partition validation. Your concern is supported by ClickHouse's source and predates this PR. The 26.6.3.62 implementation also lacks the newer REPLACE-only exemption, so splitting the mixed ALTER alone is not an established fix on that version. I added a documentation caveat and recorded a version-aware partition validation follow-up. No live multi-shard reproduction was performed for this update. Validation passed. All three diagnostic regression cases failed before the fix and passed afterward. The complete ClickHouse adapter module passed 68 tests. make style passed, and all four make fast-test batches passed with 2,887 tests and 13 skips. New-head CI is separate from these local results. |
|
Thanks @mday-io, the warnings and the docs caveat look good, and the test checking the exact message per failure is a nice touch. Appreciate the honest answer on the multi-shard case and the follow-up. One small thing: could you switch the three new warnings to f-strings? The engine adapters mostly use them, for example sqlmesh/sqlmesh/core/engine_adapter/clickhouse.py Line 1141 in 939da1f logger.warning(f"Failed to determine engine for ClickHouse database '{database}'; keeping ON CLUSTER: {ex}")The tests compare the exact message text, so they should still pass once the text matches. The repo mixes both styles today, so this isn't something you could have known. I'll put up a separate PR to add a lint rule so only f-strings are allowed from here on. Everything else looks good to me. Thanks again! |
Signed-off-by: Michael Day <mdaytn@gmail.com>
|
Switched all three warnings to f-strings in 658ab8b8f, preserving the exact message text. The existing three message/recovery cases, make style, and all four make fast-test batches passed (2,891 tests, 13 skips). Independent review found no issues. |
Description
Fix ClickHouse error 80 when SQLMesh creates or alters objects in a
Replicateddatabase with a cluster configured.ON CLUSTERwhere the database already propagates metadata changes.Test Plan
make stylepasses.make fast-test: 2,691 tests passed in the main batch; all 196 isolation tests passed (5 isolated, 1 registry, 190 dialect). Skips are recorded per batch in the local logs.Checklist
make styleand fixed any issuesmake fast-test)git commit -s) per the DCO