Skip to content

bpo-43258: Prevent needless allocation of sqlite3 aggregate context for empty queries #24569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/sqlite3/test/userfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ def test_aggr_check_aggr_sum(self):
val = cur.fetchone()[0]
self.assertEqual(val, 60)

def test_aggr_no_match(self):
cur = self.con.execute("select mysum(i) from (select 1 as i) where i == 0")
val = cur.fetchone()[0]
self.assertIsNone(val)

class AuthorizerTests(unittest.TestCase):
@staticmethod
def authorizer_cb(action, arg1, arg2, dbname, source):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Prevent needless allocation of :mod:`sqlite3` aggregate function context
when no rows match an aggregate query. Patch by Erlend E. Aasland.
8 changes: 6 additions & 2 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,12 @@ void _pysqlite_final_callback(sqlite3_context* context)

threadstate = PyGILState_Ensure();

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

Expand Down