Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
122313: stats: set the type resolver when dealing with virtual comp cols r=yuzefovich a=yuzefovich

We forgot to set the type resolver on the sema context that we create when dealing with virtual computed columns, and as a result stats collection on tables with such columns now might fail altogether. In a separate change I'll audit all callers of `MakeSemaContext` to prevent such a mistake in the future.

Fixes: cockroachdb#122312.

Release note (bug fix): CockroachDB could fail to collect table statistics on tables that have virtual computed columns of user-defined type if `sql.stats.virtual_computed_columns.enabled` cluster setting is enabled. The setting is introduced in 23.2.4 version and is disabled by default, so only this particular version is affected with non-default setting.

Co-authored-by: Yahor Yuzefovich <[email protected]>
  • Loading branch information
craig[bot] and yuzefovich committed Apr 13, 2024
2 parents cc182eb + 108990d commit f117eea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/sql/create_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func createStatsDefaultColumns(
cannotDistribute := make([]bool, len(desc.PublicColumns()))
if virtColEnabled {
semaCtx := tree.MakeSemaContext()
semaCtx.TypeResolver = evalCtx.Planner
exprs, _, err := schemaexpr.MakeComputedExprs(
ctx,
desc.PublicColumns(),
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/distsql_plan_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ func (dsp *DistSQLPlanner) createStatsPlan(
if len(virtComputedCols) != 0 {
// Resolve names and types.
semaCtx := tree.MakeSemaContext()
semaCtx.TypeResolver = planCtx.planner
virtComputedExprs, _, err := schemaexpr.MakeComputedExprs(
ctx,
virtComputedCols,
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/stats
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ SELECT CASE
END
----
true

# Regression for not setting the TypeResolver on the SemaContext when dealing
# with stats on virtual computed columns (#122312).
statement ok
CREATE TYPE greeting AS ENUM ('hello', 'hi', 'yo');

statement ok
CREATE TABLE t122312 (s STRING, g greeting AS (s::greeting) STORED);

statement ok
ANALYZE t122312;

0 comments on commit f117eea

Please sign in to comment.