From 6d3cb6190719a92175159dcf014ea8aafbcadb81 Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Tue, 17 Aug 2021 11:06:45 +0800 Subject: [PATCH 1/4] done Signed-off-by: wjhuang2016 --- .../r/explain_generate_column_substitute.result | 10 ++++++++++ .../t/explain_generate_column_substitute.test | 5 +++++ expression/schema.go | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/explaintest/r/explain_generate_column_substitute.result b/cmd/explaintest/r/explain_generate_column_substitute.result index a2f3059c06625..1cddf5bd5964b 100644 --- a/cmd/explaintest/r/explain_generate_column_substitute.result +++ b/cmd/explaintest/r/explain_generate_column_substitute.result @@ -456,3 +456,13 @@ HashAgg 6.00 root group by:Column#9, funcs:max(Column#8)->Column#7 └─Projection 6.00 root upper(test.t.b)->Column#8, upper(test.t.b)->Column#9 └─TableReader 6.00 root data:TableFullScan └─TableFullScan 6.00 cop[tikv] table:t keep order:false +drop table if exists t; +create table t(a int, b int as (a+1), key((a+1)), key(b)); +desc format = 'brief' select a+1 from t; +id estRows task access object operator info +IndexReader 10000.00 root index:IndexFullScan +└─IndexFullScan 10000.00 cop[tikv] table:t, index:expression_index(`a` + 1) keep order:false, stats:pseudo +desc format = 'brief' select b from t; +id estRows task access object operator info +IndexReader 10000.00 root index:IndexFullScan +└─IndexFullScan 10000.00 cop[tikv] table:t, index:b(b) keep order:false, stats:pseudo diff --git a/cmd/explaintest/t/explain_generate_column_substitute.test b/cmd/explaintest/t/explain_generate_column_substitute.test index 1ebb37a1dd2df..82b129159c8b5 100644 --- a/cmd/explaintest/t/explain_generate_column_substitute.test +++ b/cmd/explaintest/t/explain_generate_column_substitute.test @@ -192,3 +192,8 @@ desc format = 'brief' select count(upper(b)) from t group by upper(b); desc format = 'brief' select max(upper(b)) from t group by upper(b); desc format = 'brief' select count(upper(b)) from t use index() group by upper(b); desc format = 'brief' select max(upper(b)) from t use index() group by upper(b); + +drop table if exists t; +create table t(a int, b int as (a+1), key((a+1)), key(b)); +desc format = 'brief' select a+1 from t; +desc format = 'brief' select b from t; diff --git a/expression/schema.go b/expression/schema.go index f8ea2af063e9c..7ca7a08723d47 100644 --- a/expression/schema.go +++ b/expression/schema.go @@ -230,7 +230,7 @@ func GetUsedList(usedCols []*Column, schema *Schema) []bool { // When cols are a generated expression col, compare them in terms of virtual expr. if expr, ok := col.VirtualExpr.(*ScalarFunction); ok && used[i] { for j, colToCompare := range schema.Columns { - if !used[j] && j != i && (expr).Equal(nil, colToCompare.VirtualExpr) { + if !used[j] && j != i && (expr).Equal(nil, colToCompare.VirtualExpr) && col.RetType.Equal(colToCompare.GetType()) { used[j] = true } } From 3d7c5df7ff342dfec9a418d4e77ab4422f7eefa7 Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Tue, 17 Aug 2021 11:20:04 +0800 Subject: [PATCH 2/4] fmt Signed-off-by: wjhuang2016 --- expression/schema.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/schema.go b/expression/schema.go index 04cc8d1c9bd26..7cfab1ccb79ed 100644 --- a/expression/schema.go +++ b/expression/schema.go @@ -231,7 +231,7 @@ func GetUsedList(usedCols []*Column, schema *Schema) []bool { // When cols are a generated expression col, compare them in terms of virtual expr. if expr, ok := col.VirtualExpr.(*ScalarFunction); ok && used[i] { for j, colToCompare := range schema.Columns { - if !used[j] && j != i && (expr).Equal(nil, colToCompare.VirtualExpr) && col.RetType.Equal(colToCompare.GetType()) { + if !used[j] && j != i && (expr).Equal(nil, colToCompare.VirtualExpr) && col.RetType.Equal(colToCompare.GetType()) { used[j] = true } } From 8e586fba6bd8a44e33846cda12689e80ac21d511 Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Tue, 17 Aug 2021 16:46:34 +0800 Subject: [PATCH 3/4] done Signed-off-by: wjhuang2016 --- expression/column.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/column.go b/expression/column.go index 2cdc78c29e3e7..29de393425c2f 100644 --- a/expression/column.go +++ b/expression/column.go @@ -236,7 +236,7 @@ func (col *Column) Equal(_ sessionctx.Context, expr Expression) bool { func (col *Column) EqualByExprAndID(_ sessionctx.Context, expr Expression) bool { if newCol, ok := expr.(*Column); ok { expr, isOk := col.VirtualExpr.(*ScalarFunction) - isVirExprMatched := isOk && expr.Equal(nil, newCol.VirtualExpr) && expr.RetType.Equal(newCol.RetType) + isVirExprMatched := isOk && expr.Equal(nil, newCol.VirtualExpr) && col.RetType.Equal(newCol.RetType) return (newCol.UniqueID == col.UniqueID) || isVirExprMatched } return false From 0a1e2d28e784a6e968f3be6bc2cd84f047a110c4 Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Tue, 17 Aug 2021 16:48:03 +0800 Subject: [PATCH 4/4] clean Signed-off-by: wjhuang2016 --- expression/schema.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/schema.go b/expression/schema.go index 7cfab1ccb79ed..1ca8740de5eea 100644 --- a/expression/schema.go +++ b/expression/schema.go @@ -231,7 +231,7 @@ func GetUsedList(usedCols []*Column, schema *Schema) []bool { // When cols are a generated expression col, compare them in terms of virtual expr. if expr, ok := col.VirtualExpr.(*ScalarFunction); ok && used[i] { for j, colToCompare := range schema.Columns { - if !used[j] && j != i && (expr).Equal(nil, colToCompare.VirtualExpr) && col.RetType.Equal(colToCompare.GetType()) { + if !used[j] && j != i && (expr).Equal(nil, colToCompare.VirtualExpr) && col.RetType.Equal(colToCompare.RetType) { used[j] = true } }