Skip to content

Normalize integer numerics when performing aggregate queries#993

Open
sgrif wants to merge 1 commit into
mainfrom
sg-normalize-integer-numerics
Open

Normalize integer numerics when performing aggregate queries#993
sgrif wants to merge 1 commit into
mainfrom
sg-normalize-integer-numerics

Conversation

@sgrif
Copy link
Copy Markdown
Contributor

@sgrif sgrif commented May 20, 2026

The AVG function in PG will always convert any integer types to numeric. The scale of that result does not appear to be clearly defined, but appears to be a scale of 16 for small numbers and 0 for larger ones. Unfortunately, since this behavior isn't documented or defined, it's difficult for us to match.

That said, normalizing integers in this case will prevent our values from diverging with larger values (e.g. very small fractional parts that a single shrad query would not return), while only causing us to diverge from PG by not including trailing zeroes on small integers (e.g. we will now return 85 instead of 85.0000000000000000), which I believe is a net win overall

The AVG function in PG will always convert any integer types to numeric.
The scale of that result does not appear to be clearly defined, but
appears to be a scale of 16 for small numbers and 0 for larger ones.
Unfortunately, since this behavior isn't documented or defined, it's
difficult for us to match.

That said, normalizing integers in this case will prevent our values
from diverging with larger values (e.g. very small fractional parts that
a single shrad query would not return), while only causing us to diverge
from PG by not including trailing zeroes on small integers (e.g. we will
now return `85` instead of `85.0000000000000000`), which I believe is a
net win overall
@sgrif sgrif requested a review from levkk May 20, 2026 19:35
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pgdog/src/backend/pool/connection/aggregate.rs 0.00% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@blacksmith-sh

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

@levkk levkk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hell yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants