💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Skip NULL aggregate values from shards with no matching rows (#1331)
Gaurav Chintakuntaffcbc0b8156f
diff --git a/src/python/txtai/database/sql/aggregate.py b/src/python/txtai/database/sql/aggregate.pyindex 6a511fb8..e7bb043d 100644--- a/src/python/txtai/database/sql/aggregate.py+++ b/src/python/txtai/database/sql/aggregate.py@@ -123,16 +123,14 @@ class Aggregate(SQL):# Compute column valuesrows = []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 valuesrow = {}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 Noneelse:# Non aggregate column value repeat, use first valuerow[column] = result[0][column]diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.pyindex e8eebf82..e0ed4d15 100644--- a/test/python/testdatabase/testsql.py+++ b/test/python/testdatabase/testsql.py@@ -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