Skip to content

Commit 79ea0e1

Browse files
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.
1 parent 287935a commit 79ea0e1

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎backend/apps/db/db_sql.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Author: Junjun
22
# Date: 2025/8/20
3+
import re
4+
35
from apps.datasource.models.datasource import CoreDatasource, DatasourceConf
46
from common.utils.utils import equals_ignore_case
57

@@ -102,7 +104,12 @@ def get_table_sql(ds: CoreDatasource, conf: DatasourceConf, db_version: str = ''
102104
ORDER BY t.TABLE_NAME
103105
""", conf.dbSchema
104106
elif equals_ignore_case(ds.type, "ck"):
105-
version = int(db_version.split('.')[0])
107+
# get_version() returns '' whenever the version query fails (no
108+
# permission, timeout), and some forks prefix the string, so parse the
109+
# leading number and fall back to the pre-22 query instead of raising:
110+
# '' as comment is valid on every version, and the failure used to abort
111+
# the whole table listing.
112+
version = leading_version(db_version)
106113
if version < 22:
107114
return """
108115
SELECT name, '' as comment
@@ -321,3 +328,8 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
321328
elif equals_ignore_case(ds.type, "hive", "impala"):
322329
sql1 = f"DESCRIBE {table_name}"
323330
return sql1, None, None
331+
332+
333+
def leading_version(value) -> int:
334+
match = re.match(r"\s*(\d+)", "" if value is None else str(value))
335+
return int(match.group(1)) if match else 0
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""ClickHouse table listing must survive a server version it cannot parse.
2+
3+
``get_version()`` returns ``''`` whenever the version query fails (limited
4+
permissions, timeout, pool error), and the ClickHouse branch used to call
5+
``int(db_version.split('.')[0])`` directly, so table listing raised ValueError
6+
instead of falling back to the pre-22 query. Follow the other isolated tests
7+
and load only the functions under test, so no driver stack is imported.
8+
"""
9+
10+
import ast
11+
import re
12+
from pathlib import Path
13+
14+
import pytest
15+
16+
SOURCE = Path(__file__).resolve().parents[1] / "apps/db/db_sql.py"
17+
18+
19+
def _load():
20+
tree = ast.parse(SOURCE.read_text(encoding="utf-8"))
21+
wanted = {
22+
node.name for node in tree.body if isinstance(node, ast.FunctionDef)
23+
} & {"get_table_sql", "leading_version"}
24+
module = ast.Module(
25+
body=[node for node in tree.body if getattr(node, "name", None) in wanted],
26+
type_ignores=[],
27+
)
28+
namespace = {
29+
"re": re,
30+
# Same contract as common.utils.utils.equals_ignore_case.
31+
"equals_ignore_case": lambda value, *candidates: str(value or "").lower()
32+
in {str(candidate).lower() for candidate in candidates},
33+
"CoreDatasource": object,
34+
"DatasourceConf": object,
35+
}
36+
exec(compile(module, str(SOURCE), "exec"), namespace)
37+
return namespace
38+
39+
40+
class _ClickHouseDatasource:
41+
type = "ck"
42+
43+
44+
class _Conf:
45+
database = "default"
46+
dbSchema = ""
47+
48+
49+
@pytest.mark.parametrize("version", ["", None, "v23.8", "unknown", " "])
50+
def test_unparsable_versions_fall_back_to_the_pre_22_query(version):
51+
namespace = _load()
52+
53+
sql, param = namespace["get_table_sql"](_ClickHouseDatasource(), _Conf(), version)
54+
55+
assert "'' as comment" in sql
56+
assert param == "default"
57+
58+
59+
@pytest.mark.parametrize(
60+
("version", "uses_comment_column"),
61+
[("22.8.1.2993", True), (" 23.3", True), ("21.8.1.1", False), ("9.9", False)],
62+
)
63+
def test_numeric_versions_still_choose_the_comment_column(version, uses_comment_column):
64+
namespace = _load()
65+
66+
sql, _ = namespace["get_table_sql"](_ClickHouseDatasource(), _Conf(), version)
67+
68+
assert ("'' as comment" not in sql) is uses_comment_column
69+
70+
71+
def test_leading_version_reads_the_first_number_only():
72+
namespace = _load()
73+
74+
leading_version = namespace["leading_version"]
75+
76+
assert leading_version("23.8.1.2993") == 23
77+
assert leading_version("v23.8") == 0
78+
assert leading_version(None) == 0
79+
assert leading_version("") == 0

0 commit comments

Comments
 (0)