Skip to content

Skip remote database engines when scanning materialized views - #804

Open
alexsubota wants to merge 2 commits into
ClickHouse:mainfrom
alexsubota:fix/skip-remote-engines-when-scanning-mvs
Open

alexsubota wants to merge 2 commits into
ClickHouse:mainfrom
alexsubota:fix/skip-remote-engines-when-scanning-mvs

Conversation

@alexsubota

@alexsubota alexsubota commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Relation listing no longer opens every MySQL, PostgreSQL, and MaterializedMySQL database engine while looking for materialized views. One unreachable remote database was failing the unfiltered system.tables scan for every model.
  • The associated-view lookup is limited to the model database.
  • source() on a remote schema still queries that schema directly.

Test plan

  • Run dbt against a server that has an unreachable MySQL database engine and confirm relation listing and materialized-view builds succeed for local databases.
  • Confirm a source() on a MySQL or PostgreSQL database engine still reads that schema.
  • Run the materialized view integration tests.

Note

Low Risk
Narrower metadata queries for MV discovery; behavior change is intentional isolation from remote engines with no impact on explicit remote source() access.

Overview
Fixes relation listing and materialized-view maintenance when ClickHouse has unreachable MySQL, PostgreSQL, or MaterializedMySQL database engines.

clickhouse__list_relations_without_caching now loads local database names from system.databases (engines that do not connect remotely) and restricts the materialized-view subquery on system.tables to those databases, so a bad remote engine no longer breaks listing for every schema. source() on a remote schema is unchanged because the outer relation query still filters by the requested schema.

The associated-MV lookup in clickhouse__search_associated_mvs_to_target adds a database = relation_schema predicate so that scan is limited to the model’s database instead of all materialized views cluster-wide.

Reviewed by Cursor Bugbot for commit f9a4fc5. Bugbot is set up for automated code reviews on this repo. Configure here.

An unreachable MySQL database engine makes an unfiltered system.tables scan fail for every model. Limit the associated-view lookup to the model database, and restrict relation listing to local databases so source() on a remote schema still connects only to that schema.
@alexsubota
alexsubota force-pushed the fix/skip-remote-engines-when-scanning-mvs branch from 13b7c9c to f9a4fc5 Compare November 16, 2026 08:32
select name
from system.tables
where engine = 'MaterializedView'
where database = '{{ relation_schema }}'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too limiting, a user may create a MV from another database.

where engine = 'MaterializedView'
and create_table_query like '%TO %'
{%- if ns.local_databases %}
and database in ({% for db_name in ns.local_databases %}'{{ db_name | replace("'", "''") }}'{% if not loop.last %}, {% endif %}{% endfor %})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just add the filtering here somehow by calling system.databases and filtering by other values? feels like adding two sequentials queries should not be the way to go and maybe one is enough

@Flipmonster Flipmonster reopened this Nov 26, 2026
@connectorsec

Copy link
Copy Markdown

ConnectorSec security review — head f9a4fc5

1. Database names from system.databases are put into SQL with incomplete escaping (SQL injection) — dbt/include/clickhouse/macros/adapters.sql:63

  • Scenario: in ClickHouse string literals \ is an escape character ("you need to escape at least ' and \", SQL syntax reference), and a backtick-quoted database name may contain it. This line doubles ' only. A local database whose name ends in \ therefore escapes the closing quote of its literal. The relation-listing query becomes a syntax error, and list_relations_without_caching fails for every model. That is the outage this PR fixes, and now any principal that can CREATE DATABASE on the server can cause it. Two crafted, adjacent database names go further: the second name lands outside any literal, and ClickHouse parses it as SQL inside a query that runs with the dbt user's grants on every dbt run, build and docs generate. Before this PR, the scan put no server-side names into the query text.
  • Fix: escape the backslash before the quote, as the adapter's own escape_str does (dbt/adapters/clickhouse/query.py:13).
-        and database in ({% for db_name in ns.local_databases %}'{{ db_name | replace("'", "''") }}'{% if not loop.last %}, {% endif %}{% endfor %})
+        and database in ({% for db_name in ns.local_databases %}'{{ db_name | replace("\\", "\\\\") | replace("'", "''") }}'{% if not loop.last %}, {% endif %}{% endfor %})

Static review by ConnectorSec (ClickHouse Product Security); no PR code was executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants