Skip to content

Commit 979b23c

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43258: Don't allocate sqlite3 aggregate context for empty queries (GH-24569)
1 parent e92d67d commit 979b23c

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/sqlite3/test/userfunctions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ def test_aggr_check_aggr_sum(self):
429429
val = cur.fetchone()[0]
430430
self.assertEqual(val, 60)
431431

432+
def test_aggr_no_match(self):
433+
cur = self.con.execute("select mysum(i) from (select 1 as i) where i == 0")
434+
val = cur.fetchone()[0]
435+
self.assertIsNone(val)
436+
432437
class AuthorizerTests(unittest.TestCase):
433438
@staticmethod
434439
def authorizer_cb(action, arg1, arg2, dbname, source):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prevent needless allocation of :mod:`sqlite3` aggregate function context
2+
when no rows match an aggregate query. Patch by Erlend E. Aasland.

Modules/_sqlite/connection.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,12 @@ void _pysqlite_final_callback(sqlite3_context* context)
708708

709709
threadstate = PyGILState_Ensure();
710710

711-
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
712-
if (!*aggregate_instance) {
711+
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
712+
if (aggregate_instance == NULL) {
713+
/* No rows matched the query; the step handler was never called. */
714+
goto error;
715+
}
716+
else if (!*aggregate_instance) {
713717
/* this branch is executed if there was an exception in the aggregate's
714718
* __init__ */
715719

0 commit comments

Comments
 (0)