Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion backend/apps/db/db_sql.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
79 changes: 79 additions & 0 deletions backend/tests/test_ck_table_sql_version.py
Original file line number Diff line number Diff line change
@@ -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
Loading