Files
Alexey MilovidovandClaude Opus 4.8 295ced78d1 Enable more Ruff checks (F401/F403/F405/F541/F841) and clean up tests
Follow-up to #105171, which added `ruff` with a deliberately narrow,
bug-class-only rule set and left the unused-name and star-import rules
off pending a dedicated cleanup pass. This enables that second batch in
`pyproject.toml` and fixes every violation across the tree (the bulk in
integration tests):

- `F401` unused-import and `F841` unused-variable: removed, preserving
  any side-effecting right-hand side (e.g. `result = node.query(...)`
  becomes `node.query(...)`). Two `try: import ...` feature probes are
  kept with `# noqa: F401`.
- `F541` f-string-missing-placeholders: dropped the redundant `f` prefix.
- `F403`/`F405` star imports: every `from module import *` is expanded
  into explicit imports of the names actually used, taken from each
  name's real origin, so stdlib/third-party names import from their real
  source rather than through a test "convenience bundle". The now-empty
  `tests/integration/helpers/kafka/common_direct.py` bundle is removed and
  its three remaining consumers import `_VarintBytes`/`KafkaAdminClient`
  directly.

Eliminating the star imports surfaced a real bug the wildcard had masked
from `F821`: `generate_presets.py` in `test_prometheus_protocols` called
an undefined `execute_instant_query_with_http_api`; fixed to the existing
`execute_query_via_http_api`.

No `black`/`isort` reformatting was applied (neither is enforced in the
style check), so the diff is limited to the lint fixes themselves.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
2026-06-18 23:22:25 +00:00

212 lines
6.9 KiB
Python

import logging
import pytest
from helpers.cluster import ClickHouseCluster
from helpers.test_tools import TSV
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler())
@pytest.fixture(scope="module")
def started_cluster():
try:
cluster = ClickHouseCluster(__file__)
cluster.add_instance(
"s0_0_0",
main_configs=["configs/cluster.xml"],
user_configs=["configs/users.xml"],
macros={"replica": "node1", "shard": "shard1"},
with_zookeeper=True,
)
cluster.add_instance(
"s0_0_1",
main_configs=["configs/cluster.xml"],
user_configs=["configs/users.xml"],
macros={"replica": "replica2", "shard": "shard1"},
with_zookeeper=True,
)
cluster.add_instance(
"s0_1_0",
main_configs=["configs/cluster.xml"],
user_configs=["configs/users.xml"],
macros={"replica": "replica1", "shard": "shard2"},
with_zookeeper=True,
)
logging.info("Starting cluster...")
cluster.start()
logging.info("Cluster started")
for node_name in ("s0_0_0", "s0_0_1", "s0_1_0"):
for i in range(1, 3):
cluster.instances[node_name].query(
f"""
INSERT INTO TABLE FUNCTION file(
'file{i}.csv', 'CSV', 's String, i UInt32') VALUES ('file{i}',{i})
"""
)
yield cluster
finally:
cluster.shutdown()
def get_query(select: str, cluster: bool, files_nums: str, order_by="ORDER BY (i, s)"):
if cluster:
return f"SELECT {select} from fileCluster('my_cluster', 'file{{{files_nums}}}.csv', 'CSV', 's String, i UInt32') {order_by}"
else:
return f"SELECT {select} from file('file{{{files_nums}}}.csv', 'CSV', 's String, i UInt32') {order_by}"
def test_select_all(started_cluster):
node = started_cluster.instances["s0_0_0"]
local = node.query(get_query("*", False, "1,2"))
distributed = node.query(get_query("*", True, "1,2"))
assert TSV(local) == TSV(distributed)
def test_count(started_cluster):
node = started_cluster.instances["s0_0_0"]
local = node.query(get_query("count(*)", False, "1,2", ""))
distributed = node.query(get_query("count(*)", True, "1,2", ""))
assert TSV(local) == TSV(distributed)
def test_non_existent_cluster(started_cluster):
node = started_cluster.instances["s0_0_0"]
error = node.query_and_get_error(
"""
SELECT count(*) from fileCluster(
'non_existent_cluster', 'file{1,2}.csv', 'CSV', 's String, i UInt32')
UNION ALL
SELECT count(*) from fileCluster(
'non_existent_cluster', 'file{1,2}.csv', 'CSV', 's String, i UInt32')
"""
)
assert "not found" in error
def test_missing_file(started_cluster):
"""
Select from a list of files, _some_ of them don't exist
"""
node = started_cluster.instances["s0_0_0"]
local_with_missing_file = node.query(get_query("*", False, "1,2,3"))
local_wo_missing_file = node.query(get_query("*", False, "1,2"))
distributed_with_missing_file = node.query(get_query("*", True, "1,2,3"))
distributed_wo_missing_file = node.query(get_query("*", True, "1,2"))
assert TSV(local_with_missing_file) == TSV(distributed_with_missing_file)
assert TSV(local_wo_missing_file) == TSV(distributed_wo_missing_file)
assert TSV(local_with_missing_file) == TSV(distributed_wo_missing_file)
assert TSV(local_wo_missing_file) == TSV(distributed_with_missing_file)
def test_no_such_files(started_cluster):
"""
Select from a list of files, _none_ of them don't exist
"""
node = started_cluster.instances["s0_0_0"]
local = node.query(get_query("*", False, "3,4"))
distributed = node.query(get_query("*", True, "3,4"))
assert TSV(local) == TSV(distributed)
def test_schema_inference(started_cluster):
node = started_cluster.instances["s0_0_0"]
expected_result = node.query(
"select * from file('file*.csv', 'CSV', 's String, i UInt32') ORDER BY (i, s)"
)
result = node.query(
"select * from fileCluster('my_cluster', 'file*.csv') ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file*.csv', auto) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file*.csv', CSV) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file*.csv', auto, auto) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file*.csv', CSV, auto) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file*.csv', auto, auto, auto) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file*.csv', CSV, auto, auto) ORDER BY (c1, c2)"
)
assert result == expected_result
def test_format_detection(started_cluster):
for node_name in ("s0_0_0", "s0_0_1", "s0_1_0"):
for i in range(1, 3):
started_cluster.instances[node_name].query(
f"""
INSERT INTO TABLE FUNCTION file(
'file_for_format_detection_{i}', 'CSV', 's String, i UInt32') VALUES ('file{i}',{i})
"""
)
node = started_cluster.instances["s0_0_0"]
expected_result = node.query(
"select * from file('file_for_format_detection*', 'CSV', 's String, i UInt32') ORDER BY (i, s)"
)
result = node.query(
"select * from fileCluster('my_cluster', 'file_for_format_detection*') ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file_for_format_detection*', auto) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file_for_format_detection*', auto, auto) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file_for_format_detection*', auto, 's String, i UInt32') ORDER BY (i, s)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file_for_format_detection*', auto, auto, auto) ORDER BY (c1, c2)"
)
assert result == expected_result
result = node.query(
"select * from fileCluster('my_cluster', 'file_for_format_detection*', auto, 's String, i UInt32', auto) ORDER BY (i, s)"
)
assert result == expected_result