From 1ad36eb0ef293a7a6ec70c7a6783e0096b96ea4e Mon Sep 17 00:00:00 2001 From: Zhou Kunqin <25057648+time-and-fate@users.noreply.github.com> Date: Thu, 11 Jan 2024 19:28:54 +0800 Subject: [PATCH] statistics: fix missing nil check for mv index (#50313) close pingcap/tidb#50298 --- pkg/statistics/table.go | 2 +- .../r/planner/core/indexmerge_path.result | 10 ++++++++++ .../t/planner/core/indexmerge_path.test | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/statistics/table.go b/pkg/statistics/table.go index e556b4f234..e488dfff04 100644 --- a/pkg/statistics/table.go +++ b/pkg/statistics/table.go @@ -448,7 +448,7 @@ func (coll *HistColl) GetAnalyzeRowCount() float64 { func (coll *HistColl) GetScaledRealtimeAndModifyCnt(idxStats *Index) (realtimeCnt, modifyCnt int64) { // In theory, we can apply this scale logic on all indexes. But currently, we only apply it on the mv index to avoid // any unexpected changes caused by factors like precision difference. - if idxStats.Info == nil || !idxStats.Info.MVIndex || !idxStats.IsFullLoad() { + if idxStats == nil || idxStats.Info == nil || !idxStats.Info.MVIndex || !idxStats.IsFullLoad() { return coll.RealtimeCount, coll.ModifyCount } analyzeRowCount := coll.GetAnalyzeRowCount() diff --git a/tests/integrationtest/r/planner/core/indexmerge_path.result b/tests/integrationtest/r/planner/core/indexmerge_path.result index 6bfa6c0d93..09d8780510 100644 --- a/tests/integrationtest/r/planner/core/indexmerge_path.result +++ b/tests/integrationtest/r/planner/core/indexmerge_path.result @@ -704,3 +704,13 @@ select * from t ignore index (mvi) where json_contains(j, '[]'); j [] ["abc"] +drop table if exists t; +create table t(a int, b json); +insert into t value (1, '{"a":[1,2,3], "b": [2,3,4]}'); +analyze table t; +alter table t add index ibb( (cast(b->'$.b' as signed array)) ); +explain select /*+ use_index_merge(t) */ * from t where 10 member of (b->'$.b'); +id estRows task access object operator info +IndexMerge_7 1.00 root type: union +├─IndexRangeScan_5(Build) 1.00 cop[tikv] table:t, index:ibb(cast(json_extract(`b`, _utf8mb4'$.b') as signed array)) range:[10,10], keep order:false, stats:partial[ibb:missing, b:unInitialized] +└─TableRowIDScan_6(Probe) 1.00 cop[tikv] table:t keep order:false, stats:partial[ibb:missing, b:unInitialized] diff --git a/tests/integrationtest/t/planner/core/indexmerge_path.test b/tests/integrationtest/t/planner/core/indexmerge_path.test index 96e92db1fd..07eaab9935 100644 --- a/tests/integrationtest/t/planner/core/indexmerge_path.test +++ b/tests/integrationtest/t/planner/core/indexmerge_path.test @@ -245,3 +245,11 @@ insert into t values ('[]'); insert into t values ('["abc"]'); select * from t use index (mvi) where json_contains(j, '[]'); select * from t ignore index (mvi) where json_contains(j, '[]'); + +# TestIssue50298 +drop table if exists t; +create table t(a int, b json); +insert into t value (1, '{"a":[1,2,3], "b": [2,3,4]}'); +analyze table t; +alter table t add index ibb( (cast(b->'$.b' as signed array)) ); +explain select /*+ use_index_merge(t) */ * from t where 10 member of (b->'$.b');