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

planner: show warnings when analyzing multi-valued indexes #40827

Merged
merged 2 commits into from
Jan 29, 2023
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
38 changes: 38 additions & 0 deletions planner/core/indexmerge_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,44 @@ import (
"github.com/stretchr/testify/require"
)

func TestAnalyzeMVIndex(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t(a int, b int, c int, j json,
index(a), index(b),
index idx(a, b, (cast(j as signed array)), c),
index idx2(a, b, (cast(j->'$.str' as char(10) array)), c))`)

tk.MustExec("set tidb_analyze_version=2")
tk.MustExec("analyze table t")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Note 1105 Analyze use auto adjusted sample rate 1.000000 for table test.t",
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx",
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx2"))
tk.MustExec("analyze table t index idx")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Note 1105 Analyze use auto adjusted sample rate 1.000000 for table test.t",
"Warning 1105 The version 2 would collect all statistics not only the selected indexes",
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx",
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx2"))

tk.MustExec("set tidb_analyze_version=1")
tk.MustExec("analyze table t")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx",
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx2"))
tk.MustExec("analyze table t index idx")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx"))
tk.MustExec("analyze table t index a")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows())
tk.MustExec("analyze table t index a, idx, idx2")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx",
"Warning 1105 analyzing multi-valued indexes is not supported, skip idx2"))
}

func TestIndexMergeJSONMemberOf(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
21 changes: 19 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2320,12 +2320,16 @@ func getColOffsetForAnalyze(colsInfo []*model.ColumnInfo, colID int64) int {
// in tblInfo.Indices, index.Columns[i].Offset is set according to tblInfo.Columns. Since we decode row samples according to colsInfo rather than tbl.Columns
// in the execution phase of ANALYZE, we need to modify index.Columns[i].Offset according to colInfos.
// TODO: find a better way to find indexed columns in ANALYZE rather than use IndexColumn.Offset
func getModifiedIndexesInfoForAnalyze(tblInfo *model.TableInfo, allColumns bool, colsInfo []*model.ColumnInfo) []*model.IndexInfo {
func getModifiedIndexesInfoForAnalyze(sctx sessionctx.Context, tblInfo *model.TableInfo, allColumns bool, colsInfo []*model.ColumnInfo) []*model.IndexInfo {
idxsInfo := make([]*model.IndexInfo, 0, len(tblInfo.Indices))
for _, originIdx := range tblInfo.Indices {
if originIdx.State != model.StatePublic {
continue
}
if originIdx.MVIndex {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("analyzing multi-valued indexes is not supported, skip %s", originIdx.Name.L))
continue
}
if allColumns {
// If all the columns need to be analyzed, we don't need to modify IndexColumn.Offset.
idxsInfo = append(idxsInfo, originIdx)
Expand Down Expand Up @@ -2401,7 +2405,7 @@ func (b *PlanBuilder) buildAnalyzeFullSamplingTask(
execColsInfo = colsInfo
}
allColumns := len(tbl.TableInfo.Columns) == len(execColsInfo)
indexes := getModifiedIndexesInfoForAnalyze(tbl.TableInfo, allColumns, execColsInfo)
indexes := getModifiedIndexesInfoForAnalyze(b.ctx, tbl.TableInfo, allColumns, execColsInfo)
handleCols := BuildHandleColsForAnalyze(b.ctx, tbl.TableInfo, allColumns, execColsInfo)
newTask := AnalyzeColumnsTask{
HandleCols: handleCols,
Expand Down Expand Up @@ -2631,6 +2635,10 @@ func (b *PlanBuilder) buildAnalyzeTable(as *ast.AnalyzeTableStmt, opts map[ast.A
commonHandleInfo = idx
continue
}
if idx.MVIndex {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("analyzing multi-valued indexes is not supported, skip %s", idx.Name.L))
continue
}
for i, id := range physicalIDs {
if id == tbl.TableInfo.ID {
id = -1
Expand Down Expand Up @@ -2724,6 +2732,10 @@ func (b *PlanBuilder) buildAnalyzeIndex(as *ast.AnalyzeTableStmt, opts map[ast.A
if idx == nil || idx.State != model.StatePublic {
return nil, ErrAnalyzeMissIndex.GenWithStackByArgs(idxName.O, tblInfo.Name.O)
}
if idx.MVIndex {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("analyzing multi-valued indexes is not supported, skip %s", idx.Name.L))
continue
}
for i, id := range physicalIDs {
if id == tblInfo.ID {
id = -1
Expand Down Expand Up @@ -2766,6 +2778,11 @@ func (b *PlanBuilder) buildAnalyzeAllIndex(as *ast.AnalyzeTableStmt, opts map[as
}
for _, idx := range tblInfo.Indices {
if idx.State == model.StatePublic {
if idx.MVIndex {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("analyzing multi-valued indexes is not supported, skip %s", idx.Name.L))
continue
}

for i, id := range physicalIDs {
if id == tblInfo.ID {
id = -1
Expand Down