Skip to content

[sqlite3] Converter Not Called with Aggregate Functions #131002

@DLu

Description

@DLu

Bug report

Bug description:

The converter functions are correctly called when using the raw field name in a query, but when you use an aggregate function like MIN(fieldname) the value is returned as a string.

Minimal Example

import sqlite3


sqlite3.register_converter('faketype', lambda s: int(s.rstrip(b';')))
con = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.execute('CREATE TABLE awesome(f faketype)')
cur.execute('INSERT INTO awesome(f) VALUES("5;")')

queries = ['SELECT f FROM awesome', 'SELECT MIN(f) FROM awesome']
for query in queries:
    cur.execute(query)
    result = cur.fetchone()[0]
    print(f'{query}\n{type(result)} {result}\n')

Expected Output

SELECT f FROM awesome
<class 'int'> 5

SELECT MIN(f) FROM awesome
<class 'int'> 5

Both queries return an integer

Actual Output

SELECT f FROM awesome
<class 'int'> 5

SELECT MIN(f) FROM awesome
<class 'str'> 5;

The query with just the fieldname f returns the proper type (int). The query using the aggregate function MIN(f) returns a string, indicating the converter function has not been called.

Additional information

A slightly expanded example is available as gist

Python 3.12.3 (main, Feb 4 2025, 14:48:35) [GCC 13.3.0] on linux / Ubuntu 24.04

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirtopic-sqlite3type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions