Repository navigation
Return the database value from Avg on integer fields - #2314
Open
Shaisolaris wants to merge 3 commits into
Open
Shaisolaris wants to merge 3 commits into
Shaisolaris wants to merge 3 commits into
Conversation
waketzheng
reviewed
Oct 12, 2026
waketzheng
reviewed
Oct 12, 2026
Merging this PR will not alter performance
|
Avg kept the type of the averaged field, so the average of an IntField column was truncated by IntField.to_python_value. Only decimal and float fields keep their own type now; for other fields the database value is returned as is.
Shaisolaris
force-pushed
the
fix-avg-integer-field
branch
from
October 16, 2026 13:07
6ea577c to
12f724b
Compare
Shaisolaris
force-pushed
the
fix-avg-integer-field
branch
from
October 31, 2026 12:41
12f724b to
b5e18c1
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Avgkeeps the type of the field it averages, so the average of anIntField,BigIntFieldorSmallIntFieldcolumn goes throughIntField.to_python_valueand loses its fraction:The same happened for
values_list(),group_by(...).values()andAvg(F("score") + 1).Change
Avg.resolvenow keeps the field type only forDecimalFieldandFloatField. For any other field the database value is returned as is, so nothing is truncated and nothing is converted through a lossy cast (aBigIntFieldaverage on PostgreSQL stays an exactDecimal).Tests
test_avg_of_integer_field_is_not_truncatedintests/test_aggregation.py(values,values_list,group_by, and anFexpression). It fails ondevelop(assert 1.0 == 1.667) and passes with the change.Fexpressions. Only the integer-family averages change.AvgonFloatFieldandDecimalField,Sum,Max,MinandCountreturn identical values and types.tests/test_aggregation.py,tests/test_group_by.py,tests/fields/test_decimal.pyandtests/test_f.py: 67 passed, 9 skipped (SQLite).testsdirectory on SQLite: 1 failed, 1847 passed, 145 skipped, 4 collection errors. The same failure (test_psycopg_connection_params) and the same 4 collection errors occur on unmodifieddevelopin the same environment (missing optional dependencies and a missing time zone database), so they are not caused by this change. Only SQLite was used for the test suite; MySQL, Oracle and MSSQL were not run.Arithmetic with other fields
Avg.resolveclearsfield_object, but theResolveResultit returns still carries the integer field asoutput_field, andCombinedExpressioncompares operand field types. I compared a grouped query on SQLite with and without this change, using a model with integer, float and decimal columns:F("f") + Avg("i"),Avg("i") + F("f")andF("d") + Avg("i")raiseFieldError: Cannot use arithmetic expression between different field type, identically ondevelopand with this change. It is the same errorF("i") + F("f")gives, so it comes from the existing rule on mixed field types and not from this change.F("i") + Avg("i"),Avg("i") + F("i"),F("f") + Avg("f")andF("d") + Avg("d")give identical results before and after.So the stale
output_fieldcauses no new error and no result differs, and I left it unchanged. This was checked on SQLite only; arithmetic between an integer column and an integer average was not run on PostgreSQL.Follow-up validation — October 16
Follow-up validation (October 16): rebased onto current upstream, narrowed the conversion exception to IntField and its BigInt/SmallInt/IntEnum subclasses, and parameterized the regressions as requested. The assertion allows MySQL AVG(integer) rounding to four decimal places while rejecting integer truncation. Aggregation suite on SQLite: 21 passed, 1 skipped. Hosted database jobs still require verification.