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 cluster group by merge for mixed case columns and NULL group keys (#1381)

Muneeb Ullahd5394118b1fa
diff --git a/src/python/txtai/database/sql/aggregate.py b/src/python/txtai/database/sql/aggregate.py
index e16fc705..2f8d3392 100644
--- a/src/python/txtai/database/sql/aggregate.py
+++ b/src/python/txtai/database/sql/aggregate.py
@@ -154,9 +154,12 @@ class Aggregate(SQL):
results grouped using group by clause
"""
- groupby = [column for column in columns if column.lower() in query["groupby"]]
+ # Group by columns are matched case-insensitively
+ names = {name.lower() for name in query["groupby"]}
+ groupby = [column for column in columns if column.lower() in names]
if groupby:
- results = sorted(results, key=operator.itemgetter(*groupby))
+ # NULLs sort first like in SQLite
+ results = sorted(results, key=lambda row: [(row[column] is not None, row[column]) for column in groupby])
return [list(value) for _, value in itertools.groupby(results, operator.itemgetter(*groupby))]
return [results]
diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py
index 73bac065..89ddbb7d 100644
--- a/test/python/testdatabase/testsql.py
+++ b/test/python/testdatabase/testsql.py
@@ -73,6 +73,35 @@ class TestSQL(unittest.TestCase):
results = [{"COUNT(*)": 1, "Sum(price)": 1.0, "MAX(price)": 1.0}, {"COUNT(*)": 2, "Sum(price)": 5.0, "MAX(price)": 4.0}]
self.assertEqual(aggregate(query, results), [{"COUNT(*)": 3, "Sum(price)": 6.0, "MAX(price)": 4.0}])
+ def testAggregateGroupby(self):
+ """
+ Test Aggregate groups results using mixed case and NULL group by columns
+ """
+
+ aggregate = Aggregate()
+
+ query = "select Category, count(*) from txtai group by Category"
+ results = [{"Category": "A", "count(*)": 2}, {"Category": "A", "count(*)": 1}, {"Category": "B", "count(*)": 4}]
+ self.assertEqual(aggregate(query, results), [{"Category": "A", "count(*)": 3}, {"Category": "B", "count(*)": 4}])
+
+ query = "select category, count(*) from txtai group by category"
+ results = [{"category": None, "count(*)": 2}, {"category": "A", "count(*)": 1}, {"category": None, "count(*)": 3}]
+ self.assertEqual(aggregate(query, results), [{"category": None, "count(*)": 5}, {"category": "A", "count(*)": 1}])
+
+ results = [
+ {"category": "B", "count(*)": 1},
+ {"category": None, "count(*)": 2},
+ {"category": "A", "count(*)": 3},
+ {"category": "B", "count(*)": 4},
+ ]
+ self.assertEqual(
+ aggregate(query, results), [{"category": None, "count(*)": 2}, {"category": "A", "count(*)": 3}, {"category": "B", "count(*)": 5}]
+ )
+
+ query = "select a, B, count(*) from txtai group by A, b"
+ results = [{"a": 1, "B": None, "count(*)": 1}, {"a": 1, "B": "x", "count(*)": 2}, {"a": 1, "B": None, "count(*)": 3}]
+ self.assertEqual(aggregate(query, results), [{"a": 1, "B": None, "count(*)": 4}, {"a": 1, "B": "x", "count(*)": 2}])
+
def testAggregateNull(self):
"""
Test Aggregate skips NULL values returned by shards with no matching rows