Skip to content

Maintain Numeric scale when computing AVG across shards#992

Merged
sgrif merged 1 commit into
mainfrom
sg-maintain-avg-scale
May 20, 2026
Merged

Maintain Numeric scale when computing AVG across shards#992
sgrif merged 1 commit into
mainfrom
sg-maintain-avg-scale

Conversation

@sgrif
Copy link
Copy Markdown
Contributor

@sgrif sgrif commented May 20, 2026

When computing this across shards, our behavior could slightly diverge from PG's. PG will maintain the scale of the column in the resulting average (or the default scale when converting a integer to a numeric). However, when we divide a Rust Decimal, it will potentially increase the scale and track more significant digits.

If these were floats, I would argue we should ignore this and as long as our behavior is within epsilon of what vanilla PG would do that is acceptable. But numeric is a type you use specifically in order to be able to specify the scale, so returning a more precise result isn't acceptable.

When computing this across shards, our behavior could slightly diverge
from PG's. PG will maintain the scale of the column in the resulting
average (or the default scale when converting a integer to a numeric).
However, when we divide a Rust `Decimal`, it will potentially increase
the scale and track more significant digits.

If these were floats, I would argue we should ignore this and as long as
our behavior is within epsilon of what vanilla PG would do that is
acceptable. But numeric is a type you use specifically in order to be
able to specify the scale, so returning a more precise result isn't
acceptable.
@sgrif sgrif requested a review from levkk May 20, 2026 18:34
} else {
Some(Datum::Numeric(Numeric::from(decimal / divisor)))
let mut result = decimal / divisor;
result.rescale(decimal.scale());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This line is the relevant change. Everything else is light cleanup

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

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

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

📢 Thoughts on this report? Let us know!

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.

Image

@levkk
Copy link
Copy Markdown
Collaborator

levkk commented May 20, 2026

The LB test is flaky.

@blacksmith-sh
Copy link
Copy Markdown
Contributor

blacksmith-sh Bot commented May 20, 2026

Found 1 test failure on Blacksmith runners:

Failure

Test View Logs
lb_tests/TestReloadWithAutoRole View Logs

Fix in Cursor

@sgrif sgrif merged commit 1457c4c into main May 20, 2026
23 of 24 checks passed
@sgrif sgrif deleted the sg-maintain-avg-scale branch May 20, 2026 19:14
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