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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/sql/opt/memo/statistics_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2525,11 +2525,12 @@ func (sb *statisticsBuilder) updateDistinctCountsFromConstraint(
end := int(*endVal.(*tree.DInt))
// We assume that both start and end boundaries are inclusive. This
// should be the case for integer valued columns (due to normalization
// by constraint.PreferInclusive).
// by constraint.PreferInclusive). We must cast each end to a float
// *before* performing the subtraction to avoid overflow.
if c.Columns.Get(col).Ascending() {
distinctCount += float64(end - start)
distinctCount += float64(end) - float64(start)
} else {
distinctCount += float64(start - end)
distinctCount += float64(start) - float64(end)
}
} else {
// We can't determine the distinct count for this column. For example,
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/opt/memo/testdata/stats/scan
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,16 @@ SELECT * FROM empty
scan empty
├── columns: x:1(int)
└── stats: [rows=1]

# Regression test: previously, overflow when computing estimated distinct count
# here resulted in a row count of zero being estimated.
opt
SELECT x FROM a WHERE x >= -9223372036854775808 AND x <= 0 ORDER BY x LIMIT 10
----
scan a
├── columns: x:1(int!null)
├── constraint: /1: [/-9223372036854775808 - /0]
├── limit: 10
├── stats: [rows=10]
├── key: (1)
└── ordering: +1