Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: fix overflow causing zero row-count #38036

Merged
merged 1 commit into from
Jun 5, 2019

Conversation

justinj
Copy link
Contributor

@justinj justinj commented Jun 5, 2019

We were subtracting two ints that could overflow and then casting the
result to a float64. There's an easy solution to avoid the overflow:
just cast each integer to a float64 before performing the subtraction.

I will backport this to 19.1 and 2.1.

Release note (bug fix): Previously, due to a bug when estimating result
set sizes in the optimizer, queries involving int ranges that were very
large could result in poor plans being generated.

@justinj justinj requested a review from rytaft June 5, 2019 16:59
@justinj justinj requested a review from a team as a code owner June 5, 2019 16:59
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

Hey @mjibson - check this out! Could we lint for this?

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm: nice find!

Reviewed 2 of 2 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm: Thanks for the quick diagnose and fix!

Reviewed 1 of 2 files at r1.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @justinj)


pkg/sql/opt/memo/statistics_builder.go, line 2530 at r1 (raw file):

					// by constraint.PreferInclusive).
					if c.Columns.Get(col).Ascending() {
						distinctCount += float64(end) - float64(start)

Definitely worth a comment explaining that we're avoiding overflow.

We were subtracting two ints that could overflow and then casting the
result to a float64. There's an easy solution to avoid the overflow:
just cast each integer to a float64 before performing the subtraction.

I will backport this to 19.1 and 2.1.

Release note (bug fix): Previously, due to a bug when estimating result
set sizes in the optimizer, queries involving int ranges that were very
large could result in poor plans being generated.
Copy link
Contributor Author

@justinj justinj left a comment

Choose a reason for hiding this comment

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

TFTRs!

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @RaduBerinde and @rytaft)


pkg/sql/opt/memo/statistics_builder.go, line 2530 at r1 (raw file):

Previously, RaduBerinde wrote…

Definitely worth a comment explaining that we're avoiding overflow.

Done.

craig bot pushed a commit that referenced this pull request Jun 5, 2019
38036: opt: fix overflow causing zero row-count r=justinj a=justinj

We were subtracting two ints that could overflow and then casting the
result to a float64. There's an easy solution to avoid the overflow:
just cast each integer to a float64 *before* performing the subtraction.

I will backport this to 19.1 and 2.1.

Release note (bug fix): Previously, due to a bug when estimating result
set sizes in the optimizer, queries involving int ranges that were very
large could result in poor plans being generated.

Co-authored-by: Justin Jaffray <justin@cockroachlabs.com>
@maddyblue
Copy link
Contributor

I made a linter at one point that looked for unsafe casts and int arithmetic that didn't check for overflows at one point that would have caught this. It's hard because it has a lot of false positives, but this is definitely a thing we could pursue.

@craig
Copy link
Contributor

craig bot commented Jun 5, 2019

Build failed

@justinj
Copy link
Contributor Author

justinj commented Jun 5, 2019

TestComposeGSS seems flaky

bors r+

craig bot pushed a commit that referenced this pull request Jun 5, 2019
38036: opt: fix overflow causing zero row-count r=justinj a=justinj

We were subtracting two ints that could overflow and then casting the
result to a float64. There's an easy solution to avoid the overflow:
just cast each integer to a float64 *before* performing the subtraction.

I will backport this to 19.1 and 2.1.

Release note (bug fix): Previously, due to a bug when estimating result
set sizes in the optimizer, queries involving int ranges that were very
large could result in poor plans being generated.

Co-authored-by: Justin Jaffray <justin@cockroachlabs.com>
@maddyblue
Copy link
Contributor

Filed an issue for the test flake: #38037

@craig
Copy link
Contributor

craig bot commented Jun 5, 2019

Build succeeded

@craig craig bot merged commit 87c2c1d into cockroachdb:master Jun 5, 2019
@justinj
Copy link
Contributor Author

justinj commented Jun 5, 2019

Thanks!

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.

6 participants