mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
Skip NULL aggregate values from shards with no matching rows (#1331)
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user