File tree Expand file tree Collapse file tree 2 files changed +28
-4
lines changed
Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff line change 11pytest
22testcontainers-iris >= 1.3.0
3+ langchain
4+ langchain-community
5+ iris-embedded-python-wrapper
You can’t perform that action at this time.
0 commit comments