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 error due to distinct and/or null count must be non-zero #37913

Merged
merged 1 commit into from
May 29, 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: 7 additions & 0 deletions pkg/sql/opt/memo/statistics_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,13 @@ func (sb *statisticsBuilder) updateNullCountsFromProps(
colStat.ApplySelectivity(s.Selectivity, inputRowCount)
}
}
if colStat.DistinctCount == 0 {
// Transfer the null count to the distinct count since we now know that
// the column is not null. We want to make sure that either the null
// count or the distinct count is non-zero, since we always ensure that
// the row count is non-zero to avoid creating inefficient plans.
colStat.DistinctCount = min(colStat.NullCount, 1)
}
colStat.NullCount = 0
})
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/sql/opt/memo/testdata/stats/select
Original file line number Diff line number Diff line change
Expand Up @@ -1360,3 +1360,51 @@ select
│ └── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(2)=20, null(2)=5]
└── filters
└── b = 1 [type=bool, outer=(2), constraints=(/2: [/1 - /1]; tight), fd=()-->(2)]

# Regression test for #37754.
norm
SELECT
1
FROM
(
VALUES
(true, NULL, B'001000101101110'),
(true, e'19\x1e':::STRING, NULL)
)
AS t (_bool, _string, _bit)
GROUP BY
_string, _bit
HAVING
min(_bool)
----
project
├── columns: "?column?":5(int!null)
├── cardinality: [0 - 2]
├── stats: [rows=1]
├── fd: ()-->(5)
├── select
│ ├── columns: column2:2(string) column3:3(varbit) min:4(bool!null)
│ ├── cardinality: [0 - 2]
│ ├── stats: [rows=1, distinct(4)=1, null(4)=0]
│ ├── key: (2,3)
│ ├── fd: ()-->(4)
│ ├── group-by
│ │ ├── columns: column2:2(string) column3:3(varbit) min:4(bool)
│ │ ├── grouping columns: column2:2(string) column3:3(varbit)
│ │ ├── cardinality: [1 - 2]
│ │ ├── stats: [rows=1, distinct(4)=0, null(4)=1, distinct(2,3)=0, null(2,3)=1]
│ │ ├── key: (2,3)
│ │ ├── fd: (2,3)-->(4)
│ │ ├── values
│ │ │ ├── columns: column1:1(bool) column2:2(string) column3:3(varbit)
│ │ │ ├── cardinality: [2 - 2]
│ │ │ ├── stats: [rows=2, distinct(2,3)=0, null(2,3)=2]
│ │ │ ├── (true, NULL, B'001000101101110') [type=tuple{bool, string, varbit}]
│ │ │ └── (true, e'19\x1e', NULL) [type=tuple{bool, string, varbit}]
│ │ └── aggregations
│ │ └── min [type=bool, outer=(1)]
│ │ └── variable: column1 [type=bool]
│ └── filters
│ └── variable: min [type=bool, outer=(4), constraints=(/4: [/true - /true]; tight), fd=()-->(4)]
└── projections
└── const: 1 [type=int]