Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
unittest:
PYTHONPATH=`pwd` python3 -m pytest tests/test_dataset.py::TestDataSet::test_download_small tests/test_seekdb_config.py tests/test_seekdb_client.py -svv
PYTHONPATH=`pwd` python3 -m pytest tests/test_dataset.py::TestDataSet::test_download_small tests/test_seekdb_config.py tests/test_seekdb_client.py tests/test_clickhouse_client.py -svv

format:
PYTHONPATH=`pwd` python3 -m black vectordb_bench
Expand Down
83 changes: 83 additions & 0 deletions tests/test_clickhouse_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Offline unit tests for the Clickhouse search query (no live ClickHouse)."""

import sys
import types

try:
import clickhouse_connect # noqa: F401
except ImportError:
_cc = types.ModuleType("clickhouse_connect")
_driver = types.ModuleType("clickhouse_connect.driver")
_driver.Client = object
_cc.driver = _driver
_cc.get_client = None
sys.modules["clickhouse_connect"] = _cc
sys.modules["clickhouse_connect.driver"] = _driver

import pytest

from vectordb_bench.backend.clients.api import MetricType
from vectordb_bench.backend.clients.clickhouse.clickhouse import Clickhouse
from vectordb_bench.backend.clients.clickhouse.config import ClickhouseHNSWConfig

DB_CONFIG = {"host": "127.0.0.1", "port": 8123, "user": "clickhouse", "password": "pw", "database": "default"}


class FakeConn:
def __init__(self):
self.queries = []

def close(self):
pass

def query(self, sql, parameters=None, settings=None):
self.queries.append((sql, settings))
return types.SimpleNamespace(result_rows=[(3,), (1,)])


@pytest.fixture(autouse=True)
def _no_server(monkeypatch):
monkeypatch.setattr(Clickhouse, "_create_connection", lambda self, settings, **kwargs: FakeConn())


def _client(ef, metric_type=MetricType.L2):
config = ClickhouseHNSWConfig(M=16, efConstruction=128, ef=ef, metric_type=metric_type)
client = Clickhouse(dim=4, db_config=DB_CONFIG, db_case_config=config)
client.conn = FakeConn()
return client


def test_ef_is_sent_as_the_hnsw_search_setting():
client = _client(ef=64)

assert client.search_embedding([0.1, 0.2, 0.3, 0.4], k=10) == [3, 1]
_, settings = client.conn.queries[0]
assert settings == {"hnsw_candidate_list_size_for_search": 64}


def test_no_ef_keeps_the_server_default():
client = _client(ef=None)

client.search_embedding([0.1, 0.2, 0.3, 0.4], k=10)
_, settings = client.conn.queries[0]
assert settings is None


@pytest.mark.parametrize(
("metric_type", "filters", "distance", "has_where"),
[
(MetricType.L2, None, "L2Distance", False),
(MetricType.L2, {"id": 100}, "L2Distance", True),
(MetricType.COSINE, None, "cosineDistance", False),
(MetricType.COSINE, {"id": 100}, "cosineDistance", True),
],
)
def test_search_selects_only_ids(metric_type, filters, distance, has_where):
client = _client(ef=64, metric_type=metric_type)

client.search_embedding([0.1, 0.2, 0.3, 0.4], k=10, filters=filters)
sql, _ = client.conn.queries[0]
assert sql.startswith("SELECT {primary_field:Identifier} FROM")
assert "{vector_field:Identifier}" not in sql
assert f"ORDER BY {distance}(embedding,{{query:Array(Float64)}})" in sql
assert ("WHERE" in sql) == has_where
42 changes: 9 additions & 33 deletions vectordb_bench/backend/clients/clickhouse/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(
self.index_param = self.case_config.index_param()
self.search_param = self.case_config.search_param()
self.session_param = self.case_config.session_param()
ef = self.search_param["params"].get("ef")
self._search_settings = {"hnsw_candidate_list_size_for_search": ef} if ef else None

self._index_name = "clickhouse_index"
self._primary_field = "id"
Expand Down Expand Up @@ -191,42 +193,16 @@ def search_embedding(
"metric_type": self.search_param["metric_type"],
"query": query,
}
if self.case_config.metric_type == "COSINE":
if filters:
result = self.conn.query(
"SELECT {primary_field:Identifier}, {vector_field:Identifier} "
"FROM {schema:Identifier}.{table:Identifier} "
"WHERE {primary_field:Identifier} > {gt:UInt32} "
"ORDER BY cosineDistance(embedding,{query:Array(Float64)}) "
"LIMIT {k:UInt32}",
parameters=parameters,
).result_rows
return [int(row[0]) for row in result]

result = self.conn.query(
"SELECT {primary_field:Identifier}, {vector_field:Identifier} "
"FROM {schema:Identifier}.{table:Identifier} "
"ORDER BY cosineDistance(embedding,{query:Array(Float64)}) "
"LIMIT {k:UInt32}",
parameters=parameters,
).result_rows
return [int(row[0]) for row in result]
if filters:
result = self.conn.query(
"SELECT {primary_field:Identifier}, {vector_field:Identifier} "
"FROM {schema:Identifier}.{table:Identifier} "
"WHERE {primary_field:Identifier} > {gt:UInt32} "
"ORDER BY L2Distance(embedding,{query:Array(Float64)}) "
"LIMIT {k:UInt32}",
parameters=parameters,
).result_rows
return [int(row[0]) for row in result]

distance = "cosineDistance" if self.case_config.metric_type == "COSINE" else "L2Distance"
where = "WHERE {primary_field:Identifier} > {gt:UInt32} " if filters else ""
# Only ids are returned: also selecting the vector column made recall ignore ef.
result = self.conn.query(
"SELECT {primary_field:Identifier}, {vector_field:Identifier} "
"SELECT {primary_field:Identifier} "
"FROM {schema:Identifier}.{table:Identifier} "
"ORDER BY L2Distance(embedding,{query:Array(Float64)}) "
f"{where}"
f"ORDER BY {distance}(embedding,{{query:Array(Float64)}}) "
"LIMIT {k:UInt32}",
parameters=parameters,
settings=self._search_settings,
).result_rows
return [int(row[0]) for row in result]
Loading