Skip to content

Commit 0222427

Browse files
Added support for deleting indexes by hashed ids (#93)
1 parent b0bd678 commit 0222427

File tree

1 file changed

+12
-5
lines changed
  • libs/langchain-db2/langchain_db2

1 file changed

+12
-5
lines changed

libs/langchain-db2/langchain_db2/db2vs.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import logging
77
import os
8+
import re
89
import uuid
910
from typing import (
1011
TYPE_CHECKING,
@@ -707,13 +708,19 @@ def delete(self, ids: Optional[List[str]] = None, **kwargs: Any) -> None:
707708
if ids is None:
708709
raise ValueError("No ids provided to delete.")
709710

710-
# Compute SHA-256 hashes of the ids and truncate them
711-
hashed_ids = [
712-
hashlib.sha256(_id.encode()).hexdigest()[:16].upper() for _id in ids
713-
]
711+
is_hashed = bool(ids) and all(re.fullmatch(r"[A-F0-9]{16}", _id) for _id in ids)
712+
713+
if is_hashed:
714+
hashed_ids = ids # use as-is
715+
else:
716+
# Compute SHA-256 hashes of the raw ids and truncate them
717+
hashed_ids = [
718+
hashlib.sha256(_id.encode("utf-8")).hexdigest()[:16].upper()
719+
for _id in ids
720+
]
714721

715722
# Constructing the SQL statement with individual placeholders
716-
placeholders = ", ".join(["?" for i in range(len(hashed_ids))])
723+
placeholders = ", ".join("?" for _ in hashed_ids)
717724

718725
ddl = f"DELETE FROM {self.table_name} WHERE id IN ({placeholders})"
719726
cursor = self.client.cursor()

0 commit comments

Comments
 (0)