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

270 lines
7.4 KiB
Python

import csv
import logging
import os
import shutil
import pytest
from helpers.cluster import ClickHouseCluster
from helpers.config_cluster import minio_secret_key
from helpers.mock_servers import start_mock_servers
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler())
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
S3_DATA = []
generated_rows = 0
def create_buckets_s3(cluster):
global generated_rows
minio = cluster.minio_client
for file_number in range(100):
file_name = f"data/generated/file_{file_number}.csv"
os.makedirs(os.path.join(SCRIPT_DIR, "data/generated/"), exist_ok=True)
S3_DATA.append(file_name)
with open(os.path.join(SCRIPT_DIR, file_name), "w+", encoding="utf-8") as f:
# a String, b UInt64
data = []
# Make all files a bit different
for number in range(100 + file_number):
data.append(
["str_" + str(number + file_number) * 10, number + file_number]
)
generated_rows += 1
writer = csv.writer(f)
writer.writerows(data)
for file in S3_DATA:
minio.fput_object(
bucket_name=cluster.minio_bucket,
object_name=file,
file_path=os.path.join(SCRIPT_DIR, file),
)
for obj in minio.list_objects(cluster.minio_bucket, recursive=True):
print(obj.object_name)
def run_s3_mocks(started_cluster):
script_dir = os.path.join(os.path.dirname(__file__), "s3_mocks")
start_mock_servers(
started_cluster,
script_dir,
[
("s3_mock.py", "resolver", "8080"),
],
)
cluster = ClickHouseCluster(__file__)
node1 = cluster.add_instance(
"node1",
main_configs=["configs/cluster.xml"],
user_configs=["configs/users.xml"],
with_minio=True,
with_zookeeper=True,
macros={"replica": "node1"},
)
node2 = cluster.add_instance(
"node2",
main_configs=["configs/cluster.xml"],
user_configs=["configs/users.xml"],
stay_alive=True,
with_zookeeper=True,
macros={"replica": "node2"},
)
node3 = cluster.add_instance(
"node3",
main_configs=["configs/cluster.xml"],
user_configs=["configs/users.xml"],
stay_alive=True,
with_zookeeper=True,
macros={"replica": "node3"},
)
@pytest.fixture(scope="module")
def started_cluster():
try:
logging.info("Starting cluster...")
cluster.start()
logging.info("Cluster started")
create_buckets_s3(cluster)
run_s3_mocks(cluster)
yield cluster
finally:
shutil.rmtree(os.path.join(SCRIPT_DIR, "data/generated/"))
cluster.shutdown()
def test_distributed_insert_select_to_rmt(started_cluster):
table = "t_rmt_target"
cluster_name = "cluster_1_shard_3_replicas"
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)
node1.query(
f"""
CREATE TABLE {table} ON CLUSTER {cluster_name} (a String, b UInt64)
ENGINE=ReplicatedMergeTree('/clickhouse/tables/b0612a53-5f58-4df0-a28f-a154e0cdb797/{table}', '{{replica}}')
ORDER BY (a, b);
"""
)
node1.query(
f"""
INSERT INTO {table} SELECT * FROM s3Cluster(
'{cluster_name}',
'http://minio1:9001/root/data/generated/*.csv', 'minio', '{minio_secret_key}', 'CSV','a String, b UInt64'
) SETTINGS parallel_distributed_insert_select=2;
"""
)
node1.query(f"SYSTEM SYNC REPLICA ON CLUSTER {cluster_name} {table}")
assert (
int(
node1.query(
f"SELECT count(*) FROM {table};"
).strip()
) == generated_rows
)
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)
def test_distributed_insert_select_to_rmt_limit(started_cluster):
table = "t_rmt_target"
cluster_name = "cluster_1_shard_3_replicas"
limit = generated_rows - 1000
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)
node1.query(
f"""
CREATE TABLE {table} ON CLUSTER {cluster_name} (a String, b UInt64)
ENGINE=ReplicatedMergeTree('/clickhouse/tables/32c614a9-13af-43c5-848c-a3f62a78e390/{table}', '{{replica}}')
ORDER BY (a, b);
"""
)
node1.query(
f"""
INSERT INTO {table} SELECT * FROM s3Cluster(
'{cluster_name}',
'http://minio1:9001/root/data/generated/*.csv', 'minio', '{minio_secret_key}', 'CSV','a String, b UInt64'
) LIMIT {limit} SETTINGS parallel_distributed_insert_select=2;
"""
)
node1.query(f"SYSTEM SYNC REPLICA ON CLUSTER {cluster_name} {table}")
assert (
int(
node1.query(
f"SELECT count(*) FROM {table};"
).strip()
) == limit
)
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)
def test_distributed_insert_select_to_rmt_where(started_cluster):
table = "t_rmt_target"
cluster_name = "cluster_1_shard_3_replicas"
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)
node1.query(
f"""
CREATE TABLE {table} ON CLUSTER {cluster_name} (a String, b UInt64)
ENGINE=ReplicatedMergeTree('/clickhouse/tables/32c614a9-13af-43c5-848c-a3f62a78e390/{table}', '{{replica}}')
ORDER BY (a, b);
"""
)
node1.query(
f"""
INSERT INTO {table} SELECT * FROM s3Cluster(
'{cluster_name}',
'http://minio1:9001/root/data/generated/*.csv', 'minio', '{minio_secret_key}', 'CSV','a String, b UInt64'
) WHERE b = 100
SETTINGS
parallel_distributed_insert_select=2,
-- disable deduplication since all rows identical, and if the batch size will be the same size on different nodes it will be deduplicated
insert_deduplicate=0;
"""
)
node1.query(f"SYSTEM SYNC REPLICA {table}")
assert (
int(
node1.query(
f"SELECT count(*) FROM {table};"
).strip()
) == 99
)
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)
def test_distributed_insert_select_to_rmt_cte_const(started_cluster):
table = "t_rmt_target"
cluster_name = "cluster_1_shard_3_replicas"
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)
node1.query(
f"""
CREATE TABLE {table} ON CLUSTER {cluster_name} (a String, b UInt64)
ENGINE=ReplicatedMergeTree('/clickhouse/tables/178ed413-b055-46eb-9657-442811aef552/{table}', '{{replica}}')
ORDER BY (a, b);
"""
)
node1.query(
f"""
INSERT INTO {table} WITH 1 + 1 as two SELECT a, b + (select sum(number) from numbers(10) where number < 2) FROM s3Cluster(
'{cluster_name}',
'http://minio1:9001/root/data/generated/*.csv', 'minio', '{minio_secret_key}', 'CSV','a String, b UInt64'
) SETTINGS parallel_distributed_insert_select=2;
"""
)
node1.query(f"SYSTEM SYNC REPLICA ON CLUSTER {cluster_name} {table}")
assert (
int(
node1.query(
f"SELECT count(*) FROM {table};"
).strip()
) == generated_rows
)
node1.query(
f"""DROP TABLE IF EXISTS {table} ON CLUSTER '{cluster_name}' SYNC;"""
)