Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions UPDATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ assists people when migrating to a new version.
destinations. Both policies ignore environment proxies and preserve budgets and
TLS hostname verification. See the [provider transport guide](docs/developer_docs/extensions/semantic-provider-transport.md).

- Saved ClickHouse connections on the legacy `clickhouse://` (clickhouse-sqlalchemy)
driver are migrated to `clickhousedb+connect://` (clickhouse-connect) by
`superset db upgrade`, so they use the dynamic connection form. Install
`clickhouse-connect>=0.13.0` before upgrading. HTTP connections keep their port;
Comment on lines +279 to +280

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Small one, 0.13.0 is well below what the clickhouse extra in pyproject.toml pins on master (>=1.11.0, <2.0), so pointing folks at the extra seems safer:

Suggested change
`superset db upgrade`, so they use the dynamic connection form. Install
`clickhouse-connect>=0.13.0` before upgrading. HTTP connections keep their port;
`superset db upgrade`, so they use the dynamic connection form. Install the
`clickhouse` extra (`clickhouse-connect>=1.11.0, <2.0`) before upgrading. HTTP connections keep their port;

`?protocol=https` becomes `?secure=true` (port 8123 is pinned when none was set).
Native-protocol connections (`clickhouse+native://`, `clickhouse+asynch://`) move
from the native default port 9000 to the HTTP port 8123, because
clickhouse-connect speaks HTTP only — confirm the server's HTTP interface is
reachable. Converted connections open in the dynamic form
(`configuration_method` becomes `dynamic_form`). Connections whose settings
cannot be carried over exactly stay on the legacy driver and are logged by
database id during the upgrade for manual migration: other URI parameters, a
non-default native port, native TLS, or any `extra.engine_params.connect_args`
(clickhouse-connect would send unknown keys such as `timeout` to the server as
settings, and turns a CA-bundle path in `verify` into `False`). A downgrade
restores the original URI and `configuration_method` of every converted
connection that has not been edited since. Custom grains configured in
`TIME_GRAIN_ADDON_EXPRESSIONS` under `clickhouse` keep applying to converted
connections; entries under `clickhousedb` take precedence.
`DISALLOWED_SQL_FUNCTIONS` gains a `clickhousedb` entry matching `clickhouse`;
deployments that override `DISALLOWED_SQL_FUNCTIONS` must add a `clickhousedb`
entry too, or converted connections run without a function denylist.
- Semantic-view filters with an empty IN or NOT IN selection no longer fail with a
query validation error. They follow SQL semantics where the filter sits: an
empty IN matches no rows (grouped charts return no rows; an ungrouped total
Expand Down
4 changes: 4 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2737,6 +2737,10 @@ def engine_context_manager( # pylint: disable=unused-argument
"httpuritype",
},
}
# The lookup is keyed by engine spec ``engine``: ClickHouse connections use
# ``clickhouse`` (clickhouse-sqlalchemy) or ``clickhousedb`` (clickhouse-connect),
# and both drivers reach the same server functions.
DISALLOWED_SQL_FUNCTIONS["clickhousedb"] = set(DISALLOWED_SQL_FUNCTIONS["clickhouse"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This line is the only thing keeping rewritten connections under the ClickHouse function denylist, but nothing fails if it is dropped: the existing denylist tests either patch the config or use PostgreSQL. Could a test run an enforcement path with ClickHouseConnectEngineSpec and the default config and assert that SELECT version() is rejected while SELECT 1 passes?


# Per-engine blocklist of system catalog tables/views that should not be queried.
# Prevents information disclosure through system catalog access.
Expand Down
23 changes: 22 additions & 1 deletion superset/connectors/sqla/partition_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,23 @@ def equality_mirrors_safely(
}


@lru_cache(maxsize=None)
def _grain_addon_engines(engine: str) -> tuple[str, ...]:
"""
The ``TIME_GRAIN_ADDON_EXPRESSIONS`` keys that apply to ``engine``.

An engine spec can inherit custom grains configured for a predecessor's
engine name through ``time_grain_addon_aliases``. Specs are fixed for the
life of the process, so the lookup is resolved once per engine name.
"""
# pylint: disable=import-outside-toplevel
from superset.db_engine_specs import get_engine_spec

spec = get_engine_spec(engine)
aliases = spec.time_grain_addon_aliases if spec.engine == engine else ()
return (*aliases, engine)


def grain_bucket_width(grain: str | None, engine: str) -> relativedelta | None:
"""
How far a grain's truncation can move a timestamp, or ``None`` if unknown.
Expand All @@ -753,7 +770,11 @@ def grain_bucket_width(grain: str | None, engine: str) -> relativedelta | None:
# claim to know, so both fall back to not mirroring.
if grain in app.config["TIME_GRAIN_ADDONS"]:
return None
if grain in app.config["TIME_GRAIN_ADDON_EXPRESSIONS"].get(engine, {}):
addon_expressions = app.config["TIME_GRAIN_ADDON_EXPRESSIONS"]
if any(
grain in addon_expressions.get(name, {})
for name in _grain_addon_engines(engine)
):
return None

return GRAIN_BUCKET_WIDTHS.get(grain)
Expand Down
24 changes: 22 additions & 2 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
engine_aliases: set[str] = set()
drivers: dict[str, str] = {}
default_driver: str | None = None

# Older ``engine`` names whose ``TIME_GRAIN_ADDON_EXPRESSIONS`` entries also
# apply to this spec, so custom grains survive connections being moved onto
# it from a predecessor spec. Entries under ``engine`` take precedence.
time_grain_addon_aliases: tuple[str, ...] = ()
# Historical default names that must continue to match DBS_AVAILABLE_DENYLIST.
default_driver_aliases: tuple[str, ...] = ()

Expand Down Expand Up @@ -1737,6 +1742,22 @@ def sort_interval() -> float:

return plist.get(index, 0)

@classmethod
def get_time_grain_addon_expressions(cls) -> dict[str | None, str]:
"""
Return the operator-configured time grain expressions for this engine.

Entries keyed by any of ``time_grain_addon_aliases`` are included, with
entries keyed by ``engine`` taking precedence.

:return: The ``TIME_GRAIN_ADDON_EXPRESSIONS`` that apply to the engine
"""
grain_addon_expressions = app.config["TIME_GRAIN_ADDON_EXPRESSIONS"]
expressions: dict[str | None, str] = {}
for engine in (*cls.time_grain_addon_aliases, cls.engine):
expressions.update(grain_addon_expressions.get(engine, {}))
return expressions

@classmethod
def get_time_grain_expressions(cls) -> dict[str | None, str]:
"""
Expand All @@ -1747,8 +1768,7 @@ def get_time_grain_expressions(cls) -> dict[str | None, str]:
"""
# TODO: use @memoize decorator or similar to avoid recomputation on every call
time_grain_expressions = cls._time_grain_expressions.copy()
grain_addon_expressions = app.config["TIME_GRAIN_ADDON_EXPRESSIONS"]
time_grain_expressions.update(grain_addon_expressions.get(cls.engine, {}))
time_grain_expressions.update(cls.get_time_grain_addon_expressions())
denylist: list[str] = app.config["TIME_GRAIN_DENYLIST"]
for key in denylist:
time_grain_expressions.pop(key, None)
Expand Down
3 changes: 3 additions & 0 deletions superset/db_engine_specs/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ class ClickHouseConnectEngineSpec(BasicParametersMixin, ClickHouseEngineSpec):
engine_name = "ClickHouse"

default_driver = "connect"
# Connections migrated from the legacy ``clickhouse`` spec keep the custom
# grains operators configured under that engine name.
time_grain_addon_aliases = ("clickhouse",)
_function_names: list[str] = []

sqlalchemy_uri_placeholder = (
Expand Down
Loading
Loading