From 81298819ab9fa4f0108f0abd5bd9ae93f6844bf5 Mon Sep 17 00:00:00 2001 From: ouyuanning <52741323@qq.com> Date: Tue, 24 Dec 2024 15:48:34 +0800 Subject: [PATCH 1/2] fix opt bug: not ResolveAlias for sk --- pkg/sql/plan/apply_indices.go | 2 +- test/distributed/cases/optimizer/index.result | 5 +++++ test/distributed/cases/optimizer/index.test | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/sql/plan/apply_indices.go b/pkg/sql/plan/apply_indices.go index 4dac70f2a8c74..02d0018d62feb 100644 --- a/pkg/sql/plan/apply_indices.go +++ b/pkg/sql/plan/apply_indices.go @@ -450,7 +450,7 @@ func (builder *QueryBuilder) applyExtraFiltersOnIndex(idxDef *IndexDef, node *pl continue } for k := range idxDef.Parts { - colIdx := node.TableDef.Name2ColIndex[idxDef.Parts[k]] + colIdx := node.TableDef.Name2ColIndex[catalog.ResolveAlias(idxDef.Parts[k])] if colIdx != col.ColPos { continue } diff --git a/test/distributed/cases/optimizer/index.result b/test/distributed/cases/optimizer/index.result index 6d5d94121459e..a21cedd50595b 100644 --- a/test/distributed/cases/optimizer/index.result +++ b/test/distributed/cases/optimizer/index.result @@ -250,4 +250,9 @@ insert into t1 select *,1111,* from generate_series(100001,150000) g; update t1 set c1=c1+1000000, c2=1,c3=c3-1000000 where c2=1; delete from t1; drop table t1; +create table t1 (a timestamp, b varchar(10), key(b)); +insert into t1 select "2024-01-01 10:10:10", "a"||result from generate_series(1,200000)g; +select * from t1 where a >= "2020-01-01 10:10:10" and a <= "2034-01-01 10:10:10" and b in ("a1"); +a b +2024-01-01 10:10:10 a1 drop database d1; diff --git a/test/distributed/cases/optimizer/index.test b/test/distributed/cases/optimizer/index.test index f2efd4e33b14a..3eb099564f412 100644 --- a/test/distributed/cases/optimizer/index.test +++ b/test/distributed/cases/optimizer/index.test @@ -110,4 +110,7 @@ insert into t1 select *,1111,* from generate_series(100001,150000) g; update t1 set c1=c1+1000000, c2=1,c3=c3-1000000 where c2=1; delete from t1; drop table t1; +create table t1 (a timestamp, b varchar(10), key(b)); +insert into t1 select "2024-01-01 10:10:10", "a"||result from generate_series(1,200000)g; +select * from t1 where a >= "2020-01-01 10:10:10" and a <= "2034-01-01 10:10:10" and b in ("a1"); drop database d1; \ No newline at end of file From 0f5d45d7edb8f7dda95c55968a68c64e5fb3069b Mon Sep 17 00:00:00 2001 From: ouyuanning <52741323@qq.com> Date: Tue, 24 Dec 2024 17:10:14 +0800 Subject: [PATCH 2/2] more ResolveAlias for sk --- pkg/sql/plan/apply_indices.go | 8 ++++---- pkg/sql/plan/apply_indices_master.go | 3 ++- pkg/sql/plan/build_dml_util.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/sql/plan/apply_indices.go b/pkg/sql/plan/apply_indices.go index 02d0018d62feb..0e4691af92d2e 100644 --- a/pkg/sql/plan/apply_indices.go +++ b/pkg/sql/plan/apply_indices.go @@ -497,7 +497,7 @@ func tryMatchMoreLeadingFilters(idxDef *IndexDef, node *plan.Node, pos int32) [] if i == 0 { continue //already hit } - currentPos, ok := node.TableDef.Name2ColIndex[idxDef.Parts[i]] + currentPos, ok := node.TableDef.Name2ColIndex[catalog.ResolveAlias(idxDef.Parts[i])] if !ok { break } @@ -714,7 +714,7 @@ func (builder *QueryBuilder) tryIndexOnlyScan(idxDef *IndexDef, node *plan.Node, idxColMap[[2]int32{node.BindingTags[0], colIdx}] = leadingColExpr } else { for i := 0; i < numKeyParts; i++ { - colIdx := node.TableDef.Name2ColIndex[idxDef.Parts[i]] + colIdx := node.TableDef.Name2ColIndex[catalog.ResolveAlias(idxDef.Parts[i])] origType := node.TableDef.Cols[colIdx].Typ mappedExpr, _ := MakeSerialExtractExpr(builder.GetContext(), DeepCopyExpr(leadingColExpr), origType, int64(i)) idxColMap[[2]int32{node.BindingTags[0], colIdx}] = mappedExpr @@ -877,7 +877,7 @@ func (builder *QueryBuilder) getMostSelectiveIndexForPointSelect(indexes []*Inde filterIdx = filterIdx[:0] for j := 0; j < numKeyParts; j++ { - colIdx := node.TableDef.Name2ColIndex[idxDef.Parts[j]] + colIdx := node.TableDef.Name2ColIndex[catalog.ResolveAlias(idxDef.Parts[j])] idx, ok := col2filter[colIdx] if !ok { break @@ -994,7 +994,7 @@ func (builder *QueryBuilder) applyIndicesForJoins(nodeID int32, node *plan.Node, condIdx = condIdx[:0] for i := 0; i < numKeyParts; i++ { - colIdx := leftChild.TableDef.Name2ColIndex[idxDef.Parts[i]] + colIdx := leftChild.TableDef.Name2ColIndex[catalog.ResolveAlias(idxDef.Parts[i])] idx, ok := col2Cond[colIdx] if !ok { break diff --git a/pkg/sql/plan/apply_indices_master.go b/pkg/sql/plan/apply_indices_master.go index a0931eed0e729..6815e4a5a3350 100644 --- a/pkg/sql/plan/apply_indices_master.go +++ b/pkg/sql/plan/apply_indices_master.go @@ -15,6 +15,7 @@ package plan import ( + "github.com/matrixorigin/matrixone/pkg/catalog" "github.com/matrixorigin/matrixone/pkg/common/mpool" "github.com/matrixorigin/matrixone/pkg/container/types" "github.com/matrixorigin/matrixone/pkg/container/vector" @@ -234,7 +235,7 @@ func makeIndexTblScan(builder *QueryBuilder, bindCtx *BindContext, filterExp *pl func isKeyPresentInList(key string, list []string) bool { for _, item := range list { - if key == item { + if key == catalog.ResolveAlias(item) { return true } } diff --git a/pkg/sql/plan/build_dml_util.go b/pkg/sql/plan/build_dml_util.go index ce0379d9c0c97..d7ed666f3cceb 100644 --- a/pkg/sql/plan/build_dml_util.go +++ b/pkg/sql/plan/build_dml_util.go @@ -1216,7 +1216,7 @@ func isPrimaryKey(tableDef *TableDef, colNames []string) bool { if uniqueKeyCount == 1 { for _, col := range tableDef.Cols { for _, colName := range colNames { - if col.Name == colName { + if col.Name == catalog.ResolveAlias(colName) { if col.Default.NullAbility { return false }