From 59814faa6b0266ded7672e5096c068c0444080e2 Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Tue, 8 Mar 2022 15:50:38 -0500 Subject: [PATCH] sql: do no collect multi-column stats for indexes with only virtual columns 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. --- pkg/sql/create_stats.go | 6 ++++++ pkg/sql/logictest/testdata/logic_test/distsql_stats | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/sql/create_stats.go b/pkg/sql/create_stats.go index aae704326690..e3404c40ba29 100644 --- a/pkg/sql/create_stats.go +++ b/pkg/sql/create_stats.go @@ -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 diff --git a/pkg/sql/logictest/testdata/logic_test/distsql_stats b/pkg/sql/logictest/testdata/logic_test/distsql_stats index 44aa546c448d..77254e5f2fd1 100644 --- a/pkg/sql/logictest/testdata/logic_test/distsql_stats +++ b/pkg/sql/logictest/testdata/logic_test/distsql_stats @@ -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