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 Aggregate IndexError on a SQL query that matched nothing across shards (#1139)

Andrew Chend6f696d7953e
diff --git a/src/python/txtai/database/sql/aggregate.py b/src/python/txtai/database/sql/aggregate.py
index 5e73db41..e21d5ae8 100644
--- a/src/python/txtai/database/sql/aggregate.py
+++ b/src/python/txtai/database/sql/aggregate.py
@@ -32,8 +32,9 @@ class Aggregate(SQL):
# Parse query
query = super().__call__(query)
- # Check if this is a SQL query
- if "select" in query:
+ # Check if this is a SQL query with results. A sharded query that matches
+ # nothing across all shards yields empty results, so guard before indexing.
+ if "select" in query and results:
# Get list of unique and aggregate columns. If no aggregate columns or order by found, skip
columns = list(results[0].keys())
aggcolumns = self.aggcolumns(columns)
diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py
index 9739ce1c..39085b01 100644
--- a/test/python/testdatabase/testsql.py
+++ b/test/python/testdatabase/testsql.py
@@ -5,6 +5,7 @@ SQL module tests
import unittest
from txtai.database import DatabaseFactory, SQL, SQLError
+from txtai.database.sql import Aggregate
class TestSQL(unittest.TestCase):
@@ -312,6 +313,21 @@ class TestSQL(unittest.TestCase):
self.assertSql("where", prefix + "where f(id) = b(id)", "f(s.id) = b(s.id)")
self.assertSql("where", prefix + "WHERE f(id)", "f(s.id)")
+ def testAggregateEmptyResults(self):
+ """
+ Test Aggregate returns empty results for a SQL query that matched nothing
+ (e.g. a sharded query with zero hits across all shards) instead of crashing.
+ """
+
+ aggregate = Aggregate()
+
+ # SQL query, no results across shards - must not raise IndexError
+ self.assertEqual(aggregate("select id, text, score from txtai where similar('x')", []), [])
+ # Non-SQL query with no results stays consistent
+ self.assertEqual(aggregate("plain text query", []), [])
+ # SQL query with results still aggregates as before
+ self.assertEqual(aggregate("select count(*) from txtai", [{"count(*)": 1}, {"count(*)": 2}]), [{"count(*)": 3}])
+
def assertSql(self, clause, query, expected):
"""
Helper method to assert a query clause is as expected.