Skip to content

sql: reject equal bounds in width_bucket#171264

Merged
rail merged 1 commit into
cockroachdb:masterfrom
virajchogle:width-bucket-equal-bounds-171263
Jun 4, 2026
Merged

sql: reject equal bounds in width_bucket#171264
rail merged 1 commit into
cockroachdb:masterfrom
virajchogle:width-bucket-equal-bounds-171263

Conversation

@virajchogle
Copy link
Copy Markdown
Contributor

@virajchogle virajchogle commented May 31, 2026

Fixes #171263

PostgreSQL rejects width_bucket(operand, b1, b2, count) when the lower and upper bounds are equal with the message "lower bound cannot equal upper bound" (pgcode invalid_argument_for_width_bucket_function, 2201G). CockroachDB silently returned garbage: the helper computes width = (b2 - b1) / count = 0, then int(math.Floor(diff/0) + 1) casts +Inf, -Inf, or NaN to int, producing INT64_MAX, INT64_MIN, or 0 depending on operand position.

This adds the guard to both the numeric and int overloads, using the same pgcode that the sibling NaN and finite-bound checks already raise. In the numeric overload, the guard is placed after the existing NaN/finite-bound guards and before the +/-Infinity operand shortcuts, matching PostgreSQL's check precedence.

This is a sibling of #171234 (count <= 0), already addressed by #171259, but independent and self-contained.

Epic: none

Release note (bug fix): width_bucket(operand, b1, b2, count) now returns "lower bound cannot equal upper bound" when the two bounds are equal, matching PostgreSQL.

@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented May 31, 2026

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented May 31, 2026

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

I was unable to automatically find a reviewer. You can try CCing one of the following members:

  • A person you worked with closely on this PR.
  • The person who created the ticket, or a CRDB organization member involved with the ticket (author, commenter, etc.).
  • Join our community slack channel and ask on #contributors.
  • Try find someone else from here.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added O-community Originated from the community X-blathers-untriaged blathers was unable to find an owner labels May 31, 2026
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@virajchogle virajchogle marked this pull request as ready for review May 31, 2026 01:56
@virajchogle virajchogle requested a review from a team as a code owner May 31, 2026 01:56
@virajchogle virajchogle requested review from bghal and removed request for a team May 31, 2026 01:56
@virajchogle
Copy link
Copy Markdown
Contributor Author

Thanks for the review @rafiss! Could you please /trunk merge when you have a moment?

@rafiss
Copy link
Copy Markdown
Collaborator

rafiss commented Jun 4, 2026

Could you please /trunk merge when you have a moment?

Thanks for your contribution. You need to rebase this change and resolve merge conflicts first.

@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Jun 4, 2026

invalid

An error occurred while handling your Trunk command: Unexpected argument 'when'. This command does not take positional arguments

PostgreSQL rejects width_bucket(operand, b1, b2, count) when the
lower and upper bounds are equal with the message "lower bound
cannot equal upper bound". CockroachDB silently returned garbage
in both overloads: the helper computes width = (b2-b1)/count = 0,
then casts +/-Inf or NaN to int, producing INT64_MAX, INT64_MIN,
or 0 depending on the operand.

Add the guard to both the numeric and int overloads, using the
same pgcode (2201G) that the sibling NaN and finite-bound checks
already raise.

Fixes cockroachdb#171263

Release note (bug fix): width_bucket(operand, b1, b2, count) now
returns an error "lower bound cannot equal upper bound" when the
two bounds are equal, matching PostgreSQL.
@virajchogle virajchogle force-pushed the width-bucket-equal-bounds-171263 branch from f666f90 to f0f6601 Compare June 4, 2026 16:35
@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Jun 4, 2026

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@virajchogle
Copy link
Copy Markdown
Contributor Author

@rafiss rebased onto master. Resolved conflicts. Ready to retry the merge.

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Jun 4, 2026

Detected infrastructure failure (matched: self-hosted runner lost communication with the server). Automatically rerunning failed jobs. (run link)

@rail rail merged commit 2fb40b6 into cockroachdb:master Jun 4, 2026
34 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community target-release-26.3.0 X-blathers-untriaged blathers was unable to find an owner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: width_bucket returns garbage when lower bound equals upper bound

4 participants