Skip to content

Commit

Permalink
sql: do no collect multi-column stats for indexes with only virtual c…
Browse files Browse the repository at this point in the history
…olumns

Fixes #76867

Release justification: This is a low-risk fix for a minor bug.

Release note (bug fix): A bug has been fixed that caused errors when
attempting to create table statistics (with CREATE STATISTICS or
ANALYZE) for a table containing an index which indexed only virtual
computed columns. This bug has been present since version 21.1.0.
  • Loading branch information
mgartner committed Mar 9, 2022
1 parent 5302b9f commit 59814fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/sql/create_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ func createStatsDefaultColumns(
colIDs = append(colIDs, col.GetID())
}

// Do not attempt to create multi-column stats with no columns. This
// can happen when an index contains only virtual computed columns.
if len(colIDs) == 0 {
continue
}

// Check for existing stats and remember the requested stats.
if !trackStatsIfNotExists(colIDs) {
continue
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/distsql_stats
Original file line number Diff line number Diff line change
Expand Up @@ -1156,3 +1156,16 @@ CREATE STATISTICS s ON b FROM t71080;

statement error cannot create statistics on virtual column \"b\"
CREATE STATISTICS s ON a, b FROM t71080;

# Regression test for #76867. Do not attempt to collect empty multi-column stats
# when there are indexes on columns that are all virtual.
statement ok
CREATE TABLE t76867 (
a INT,
b INT AS (a + 1) VIRTUAL,
c INT AS (a + 2) VIRTUAL,
INDEX (b, c)
)

statement ok
ANALYZE t76867

0 comments on commit 59814fa

Please sign in to comment.