From 79ea0e1d849b5cc8448722ac0e3885686e782a71 Mon Sep 17 00:00:00 2001 From: Borealis Date: Sun, 6 Dec 2026 17:08:38 +0800 Subject: [PATCH] fix(db): don't abort ClickHouse table listing on an unparsable version get_version() returns '' whenever the version query fails (limited permissions, timeout, pool error), and the ClickHouse branch called int(db_version.split('.')[0]) on it, so table listing raised ValueError before any SQL was built. A non-numeric prefix behaved the same way. Parse the leading number and fall back to the pre-22 query when there is none; '"" as comment' is valid on every ClickHouse version, and the branch that needs the real comment column only runs when a version is actually known. --- backend/apps/db/db_sql.py | 14 +++- backend/tests/test_ck_table_sql_version.py | 79 ++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 backend/tests/test_ck_table_sql_version.py diff --git a/backend/apps/db/db_sql.py b/backend/apps/db/db_sql.py index d2b261e7..8aa52ad9 100644 --- a/backend/apps/db/db_sql.py +++ b/backend/apps/db/db_sql.py @@ -1,5 +1,7 @@ # Author: Junjun # Date: 2025/8/20 +import re + from apps.datasource.models.datasource import CoreDatasource, DatasourceConf from common.utils.utils import equals_ignore_case @@ -102,7 +104,12 @@ def get_table_sql(ds: CoreDatasource, conf: DatasourceConf, db_version: str = '' ORDER BY t.TABLE_NAME """, conf.dbSchema elif equals_ignore_case(ds.type, "ck"): - version = int(db_version.split('.')[0]) + # get_version() returns '' whenever the version query fails (no + # permission, timeout), and some forks prefix the string, so parse the + # leading number and fall back to the pre-22 query instead of raising: + # '' as comment is valid on every version, and the failure used to abort + # the whole table listing. + version = leading_version(db_version) if version < 22: return """ SELECT name, '' as comment @@ -321,3 +328,8 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No elif equals_ignore_case(ds.type, "hive", "impala"): sql1 = f"DESCRIBE {table_name}" return sql1, None, None + + +def leading_version(value) -> int: + match = re.match(r"\s*(\d+)", "" if value is None else str(value)) + return int(match.group(1)) if match else 0 diff --git a/backend/tests/test_ck_table_sql_version.py b/backend/tests/test_ck_table_sql_version.py new file mode 100644 index 00000000..d87c69e9 --- /dev/null +++ b/backend/tests/test_ck_table_sql_version.py @@ -0,0 +1,79 @@ +"""ClickHouse table listing must survive a server version it cannot parse. + +``get_version()`` returns ``''`` whenever the version query fails (limited +permissions, timeout, pool error), and the ClickHouse branch used to call +``int(db_version.split('.')[0])`` directly, so table listing raised ValueError +instead of falling back to the pre-22 query. Follow the other isolated tests +and load only the functions under test, so no driver stack is imported. +""" + +import ast +import re +from pathlib import Path + +import pytest + +SOURCE = Path(__file__).resolve().parents[1] / "apps/db/db_sql.py" + + +def _load(): + tree = ast.parse(SOURCE.read_text(encoding="utf-8")) + wanted = { + node.name for node in tree.body if isinstance(node, ast.FunctionDef) + } & {"get_table_sql", "leading_version"} + module = ast.Module( + body=[node for node in tree.body if getattr(node, "name", None) in wanted], + type_ignores=[], + ) + namespace = { + "re": re, + # Same contract as common.utils.utils.equals_ignore_case. + "equals_ignore_case": lambda value, *candidates: str(value or "").lower() + in {str(candidate).lower() for candidate in candidates}, + "CoreDatasource": object, + "DatasourceConf": object, + } + exec(compile(module, str(SOURCE), "exec"), namespace) + return namespace + + +class _ClickHouseDatasource: + type = "ck" + + +class _Conf: + database = "default" + dbSchema = "" + + +@pytest.mark.parametrize("version", ["", None, "v23.8", "unknown", " "]) +def test_unparsable_versions_fall_back_to_the_pre_22_query(version): + namespace = _load() + + sql, param = namespace["get_table_sql"](_ClickHouseDatasource(), _Conf(), version) + + assert "'' as comment" in sql + assert param == "default" + + +@pytest.mark.parametrize( + ("version", "uses_comment_column"), + [("22.8.1.2993", True), (" 23.3", True), ("21.8.1.1", False), ("9.9", False)], +) +def test_numeric_versions_still_choose_the_comment_column(version, uses_comment_column): + namespace = _load() + + sql, _ = namespace["get_table_sql"](_ClickHouseDatasource(), _Conf(), version) + + assert ("'' as comment" not in sql) is uses_comment_column + + +def test_leading_version_reads_the_first_number_only(): + namespace = _load() + + leading_version = namespace["leading_version"] + + assert leading_version("23.8.1.2993") == 23 + assert leading_version("v23.8") == 0 + assert leading_version(None) == 0 + assert leading_version("") == 0 \ No newline at end of file