mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
Fix Aggregate IndexError on a SQL query that matched nothing across shards (#1139)
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user