From 9c718f78767061d211d438068263d4b54b1d4772 Mon Sep 17 00:00:00 2001 From: Raashish Aggarwal <94279692+raashish1601@users.noreply.github.com> Date: Wed, 7 Oct 2026 18:30:39 +0530 Subject: [PATCH] Merge aggregate columns written in uppercase across shards (#1352) --- src/python/txtai/database/sql/aggregate.py | 11 ++++++----- test/python/testdatabase/testsql.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/python/txtai/database/sql/aggregate.py b/src/python/txtai/database/sql/aggregate.py index e7bb043d..dee362dd 100644 --- a/src/python/txtai/database/sql/aggregate.py +++ b/src/python/txtai/database/sql/aggregate.py @@ -63,14 +63,15 @@ class Aggregate(SQL): aggregates = {} for column in columns: - column = column.lower() - if column.startswith(("count(", "sum(", "total(")): + # Match function names case-insensitively but keep the column name as returned + name = column.lower() + if name.startswith(("count(", "sum(", "total(")): aggregates[column] = sum - elif column.startswith("max("): + elif name.startswith("max("): aggregates[column] = max - elif column.startswith("min("): + elif name.startswith("min("): aggregates[column] = min - elif column.startswith("avg("): + elif name.startswith("avg("): aggregates[column] = self.avg return aggregates diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py index e0ed4d15..1b141101 100644 --- a/test/python/testdatabase/testsql.py +++ b/test/python/testdatabase/testsql.py @@ -62,6 +62,17 @@ class TestSQL(unittest.TestCase): with self.assertRaises(SQLError): aggregate("select avg(price) from txtai", [{"avg(price)": 100.0}, {"avg(price)": 10.0}]) + def testAggregateCase(self): + """ + Test Aggregate combines aggregate columns written in any case + """ + + aggregate = Aggregate() + + query = "select COUNT(*), Sum(price), MAX(price) from txtai" + 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 testAggregateNull(self): """ Test Aggregate skips NULL values returned by shards with no matching rows