Skip NULL aggregate values from shards with no matching rows (#1331)

This commit is contained in:
Gaurav Chintakunta
2026-10-05 06:17:05 -04:00
committed by GitHub
parent f14c26c4c7
commit ffcbc0b815
2 changed files with 17 additions and 6 deletions
+4 -6
View File
@@ -123,16 +123,14 @@ class Aggregate(SQL):
# Compute column values
rows = []
for result in results:
# Row counts for this group, if a count(*) column was selected
counts = [r[countcolumn] for r in result] if countcolumn else None
# Calculate/copy column values
row = {}
for column in columns:
if column in aggcolumns:
# Calculate aggregate value
values = [r[column] for r in result]
row[column] = self.avg(values, counts) if column.lower().startswith("avg(") else aggcolumns[column](values)
# Calculate aggregate value, skip NULLs from shards with no matching rows
matches = [r for r in result if r[column] is not None]
values, counts = [r[column] for r in matches], [r[countcolumn] for r in matches] if countcolumn else None
row[column] = (self.avg(values, counts) if column.lower().startswith("avg(") else aggcolumns[column](values)) if values else None
else:
# Non aggregate column value repeat, use first value
row[column] = result[0][column]
+13
View File
@@ -62,6 +62,19 @@ class TestSQL(unittest.TestCase):
with self.assertRaises(SQLError):
aggregate("select avg(price) from txtai", [{"avg(price)": 100.0}, {"avg(price)": 10.0}])
def testAggregateNull(self):
"""
Test Aggregate skips NULL values returned by shards with no matching rows
"""
aggregate = Aggregate()
query = "select count(*), max(price), avg(price) from txtai where price > 50"
empty, result = {"count(*)": 0, "max(price)": None, "avg(price)": None}, {"count(*)": 2, "max(price)": 100.0, "avg(price)": 80.0}
self.assertEqual(aggregate(query, [empty, result]), [result])
self.assertEqual(aggregate(query, [empty, empty]), [empty])
def testAlias(self):
"""
Test alias clauses