Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Fix LIMIT bind-parameter crashing content SQL search (#1148)

Andrew Chenb246b642c718
diff --git a/src/python/txtai/embeddings/search/base.py b/src/python/txtai/embeddings/search/base.py
index 2c62792e..c126767d 100644
--- a/src/python/txtai/embeddings/search/base.py
+++ b/src/python/txtai/embeddings/search/base.py
@@ -287,12 +287,12 @@ class Search:
# Override limit with largest limit from database queries
qlimit = 0
for query in queries:
- # Parse out qlimit
+ # Parse out qlimit; a non-numeric limit (e.g. a ":n" bind parameter) isn't a
+ # candidate-count override and must not be compared as a str against an int.
l = query.get("limit")
- if l and l.isdigit():
- l = int(l)
+ l = int(l) if l and l.isdigit() else 0
- qlimit = l if l and l > qlimit else qlimit
+ qlimit = l if l > qlimit else qlimit
return qlimit
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index 7834e89d..66b4b76a 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -363,6 +363,21 @@ class TestEmbeddings(unittest.TestCase):
uid = embeddings.search("feel good story", 1)[0][0]
self.assertEqual(uid, 0)
+ def testLimitBindParameter(self):
+ """
+ Test a LIMIT bind parameter in a content SQL query
+ """
+
+ embeddings = Embeddings({"keyword": True, "content": True})
+ embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])
+
+ # A ":n" LIMIT bind parameter must not crash on the candidate-count parse (str vs int)
+ results = embeddings.search("select id from txtai order by id limit :n", parameters={"n": 2})
+ self.assertEqual(len(results), 2)
+
+ # A plain integer limit still works
+ self.assertEqual(len(embeddings.search("select id from txtai order by id limit 3")), 3)
+
def testQuantize(self):
"""
Test scalar quantization