Skip to content

Commit d035688

Browse files
authored
Merge pull request #6 from grongierisc/main
Fix a bug with pagination and orderby with and without modern pagination
2 parents 2019d6f + 395a4fc commit d035688

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

langchain_iris/vectorstores.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,16 +529,37 @@ def similarity_search_with_score_by_vector(
529529

530530
# Execute the query and fetch the results
531531
with Session(self._conn) as session:
532-
results: Sequence[Row] = (
532+
# Create the distance column explicitly to avoid textual label reference issues
533+
distance_expr = (
534+
self.distance_strategy(embedding)
535+
if self.native_vector
536+
else self.table.c.embedding.func(
537+
self.distance_strategy, embedding
538+
)
539+
)
540+
541+
distance_col = distance_expr.label("distance")
542+
543+
# Note: For IRIS without modern pagination support, using .limit() with .order_by()
544+
# doesn't include ORDER BY in the SQL (it only generates TOP N).
545+
# So we fetch all results ordered and limit in Python.
546+
query = (
533547
session.query(
534548
self.table,
535-
distance_expr,
549+
distance_col,
536550
)
537551
.filter(filter_by)
538552
.order_by(distance_expr)
539-
.limit(k)
540-
.all()
541553
)
554+
555+
# Only add limit to SQL if dialect supports modern pagination
556+
if self._conn.dialect.supports_modern_pagination:
557+
query = query.limit(k)
558+
results: Sequence[Row] = query.all()
559+
else:
560+
# For legacy pagination, fetch all and limit in Python
561+
all_results = query.all()
562+
results = all_results[:k]
542563

543564
documents_with_scores = [
544565
(

requirements-dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
pytest
22
testcontainers-iris>=1.3.0
3+
langchain
4+
langchain-community
5+
iris-embedded-python-wrapper

0 commit comments

Comments
 (0)