77from clickhouse_connect .driver import Client
88
99from .. import IndexType
10+ from .config import ClickhouseConfigDict , ClickhouseIndexConfig
1011from ..api import VectorDB , DBCaseConfig
1112
1213log = logging .getLogger (__name__ )
@@ -16,8 +17,8 @@ class Clickhouse(VectorDB):
1617 def __init__ (
1718 self ,
1819 dim : int ,
19- db_config : dict ,
20- db_case_config : DBCaseConfig ,
20+ db_config : ClickhouseConfigDict ,
21+ db_case_config : ClickhouseIndexConfig ,
2122 collection_name : str = "CHVectorCollection" ,
2223 drop_old : bool = False ,
2324 ** kwargs ,
@@ -29,16 +30,17 @@ def __init__(
2930
3031 self .index_param = self .case_config .index_param ()
3132 self .search_param = self .case_config .search_param ()
33+ self .session_param = self .case_config .session_param ()
3234
3335 self ._index_name = "clickhouse_index"
3436 self ._primary_field = "id"
3537 self ._vector_field = "embedding"
3638
3739 # construct basic units
38- self .conn = self ._create_connection (** self .db_config )
40+ self .conn = self ._create_connection (** self .db_config , settings = self . session_param )
3941
4042 # set this param to enable ANN search
41- self ._set_experimental_param ()
43+ # self._set_experimental_param()
4244
4345 if drop_old :
4446 log .info (f"Clickhouse client drop table : { self .table_name } " )
@@ -59,39 +61,43 @@ def init(self) -> None:
5961 >>> self.search_embedding()
6062 """
6163
62- self .conn = self ._create_connection (** self .db_config )
64+ self .conn = self ._create_connection (** self .db_config , settings = self . session_param )
6365
6466 try :
6567 yield
6668 finally :
6769 self .conn .close ()
6870 self .conn = None
6971
70- def _create_connection (self , ** kwargs ) -> Client :
71- connection = clickhouse_connect .get_client (** self .db_config )
72+ def _create_connection (self , settings : dict | None , ** kwargs ) -> Client :
73+ connection = clickhouse_connect .get_client (** self .db_config , settings = settings )
7274 return connection
7375
74- def _drop_table (self ):
75- assert self .conn is not None , "Connection is not initialized"
76-
77- self .conn .command (f'DROP TABLE IF EXISTS { self .db_config ["database" ]} .{ self .table_name } ' )
7876
79- def _perfomance_tuning (self ):
80- self .conn .command (f'SET materialize_skip_indexes_on_insert = 1' )
77+ def _drop_index (self ):
78+ assert self .conn is not None , "Connection is not initialized"
79+ try :
80+ self .conn .command (f'ALTER TABLE { self .db_config ["database" ]} .{ self .table_name } DROP INDEX { self ._index_name } ' )
81+ except Exception as e :
82+ log .warning (
83+ f"Failed to drop index on table { self .db_config ['database' ]} .{ self .table_name } : { e } "
84+ )
85+ raise e from None
8186
82- def _set_experimental_param (self ):
87+ def _drop_table (self ):
8388 assert self .conn is not None , "Connection is not initialized"
8489
8590 try :
86- self .conn .command (
87- "SET allow_experimental_vector_similarity_index = 1"
88- )
91+ self .conn .command (f'DROP TABLE IF EXISTS { self .db_config ["database" ]} .{ self .table_name } ' )
8992 except Exception as e :
9093 log .warning (
91- f"Failed to set allow_experimental_vector_similarity_index error : { e } "
94+ f"Failed to drop table { self . db_config [ 'database' ] } . { self . table_name } : { e } "
9295 )
9396 raise e from None
9497
98+ def _perfomance_tuning (self ):
99+ self .conn .command (f'SET materialize_skip_indexes_on_insert = 1' )
100+
95101 def _create_index (self ):
96102 assert self .conn is not None , "Connection is not initialized"
97103 try :
0 commit comments