diff --git a/pkg/sql/create_stats.go b/pkg/sql/create_stats.go index 217496594a2e..ea6d189e6baf 100644 --- a/pkg/sql/create_stats.go +++ b/pkg/sql/create_stats.go @@ -247,6 +247,13 @@ func (n *createStatsNode) makeJobRecord(ctx context.Context) (*jobs.Record, erro columnIDs := make([]descpb.ColumnID, len(columns)) for i := range columns { + if columns[i].IsVirtual() { + return nil, pgerror.Newf( + pgcode.InvalidColumnReference, + "cannot create statistics on virtual column %q", + columns[i].ColName(), + ) + } columnIDs[i] = columns[i].GetID() } col, err := tableDesc.FindColumnWithID(columnIDs[0]) @@ -441,9 +448,16 @@ func createStatsDefaultColumns( continue } - colIDs := make([]descpb.ColumnID, j+1) + colIDs := make([]descpb.ColumnID, 0, j+1) for k := 0; k <= j; k++ { - colIDs[k] = idx.GetKeyColumnID(k) + col, err := desc.FindColumnWithID(idx.GetKeyColumnID(k)) + if err != nil { + return nil, err + } + if col.IsVirtual() { + continue + } + colIDs = append(colIDs, col.GetID()) } // Check for existing stats and remember the requested stats. diff --git a/pkg/sql/logictest/testdata/logic_test/distsql_stats b/pkg/sql/logictest/testdata/logic_test/distsql_stats index f2dbb4c26d1c..9ce0693ed1a4 100644 --- a/pkg/sql/logictest/testdata/logic_test/distsql_stats +++ b/pkg/sql/logictest/testdata/logic_test/distsql_stats @@ -824,11 +824,18 @@ upper_bound range_rows distinct_range_rows equal_rows 3 0 0 1 # Test that stats are not collected for virtual columns. +statement ok +SET CLUSTER SETTING sql.stats.multi_column_collection.enabled = true + statement ok CREATE TABLE virt ( a INT, + b INT, v INT AS (a + 10) VIRTUAL, - INDEX (v) + INDEX (v), + INDEX (a, v), + INDEX (a, v, b), + INDEX (a) WHERE v > 0 ) statement ok @@ -850,7 +857,9 @@ ORDER BY column_names::STRING, created ---- statistics_name column_names row_count null_count has_histogram +s {a,b} 3 0 false s {a} 3 0 true +s {b} 3 3 true s {rowid} 3 0 true # Test that stats are not collect for inaccessible virtual columns that @@ -861,7 +870,9 @@ CREATE TABLE expression ( b INT, j JSON, INDEX a_plus_b ((a + b)), - INVERTED INDEX j_a ((j->'a')) + INDEX a_a_plus_b (a, (a + b)), + INVERTED INDEX j_a ((j->'a')), + INVERTED INDEX a_j_a (a, (j->'a')) ); INSERT INTO expression VALUES (1, 1, '{"a": "b"}'), (2, 10, '{"c": "d"}'), (3, 1, '{"e": "f"}'); @@ -1037,6 +1048,7 @@ ORDER BY column_names has_histogram {id} true {j} true +{s,j} false {s} true statement ok @@ -1181,3 +1193,28 @@ CREATE TABLE t63387 ( ); INSERT INTO t63387 VALUES (1, '{}'); CREATE STATISTICS s FROM t63387; + +# Regression test for #71080. Stats collection should succeed on tables with NOT +# NULL virtual columns. +statement ok +SET CLUSTER SETTING sql.stats.multi_column_collection.enabled = true + +statement ok +CREATE TABLE t71080 ( + k INT PRIMARY KEY, + a INT, + b INT NOT NULL AS (a + 10) VIRTUAL, + INDEX (a, b) +); + +statement ok +INSERT INTO t71080 VALUES (1, 2); + +statement ok +CREATE STATISTICS s FROM t71080; + +statement error cannot create statistics on virtual column \"b\" +CREATE STATISTICS s ON b FROM t71080; + +statement error cannot create statistics on virtual column \"b\" +CREATE STATISTICS s ON a, b FROM t71080;