Skip to content

Commit

Permalink
expression: check type for virtual column use list (#27275)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 committed Aug 18, 2021
1 parent 84a8b0f commit ed17428
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cmd/explaintest/r/explain_generate_column_substitute.result
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,13 @@ a b c d e
select * from t ignore index(expression_index3) where d+ timestamp'0000-00-00 00:00:00.00001' = timestamp'2021-08-13 04:10:44'+ timestamp'0000-00-00 00:00:00.00001';
a b c d e
2021-01-02 2021-03-30 08:10:00 12:01:03 2021-08-13 04:10:44 2021
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
6 changes: 5 additions & 1 deletion cmd/explaintest/t/explain_generate_column_substitute.test
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,15 @@ CREATE TABLE t (
KEY `expression_index2` ((timediff(`b`, '2021-03-30 08:10:00.000001'))),
KEY `expression_index3` ((`d`+ timestamp'0000-00-00 00:00:00.00001'))
);

insert into t values ('2021-01-02', '2021-03-30 08:10:00', '12:01:03', '2021-08-13 04:10:44', 2021);
select * from t use index(expression_index) where ADDDATE(a, interval 10 MICROSECOND) = ADDDATE('2021-01-02', interval 10 MICROSECOND);
select * from t ignore index(expression_index) where ADDDATE(a, interval 10 MICROSECOND) = ADDDATE('2021-01-02', interval 10 MICROSECOND);
select * from t use index(expression_index2) where timediff(`b`, '2021-03-30 08:10:00.000001') = timediff('2021-03-30 08:10:00', '2021-03-30 08:10:00.000001');
select * from t ignore index(expression_index2) where timediff(`b`, '2021-03-30 08:10:00.000001') = timediff('2021-03-30 08:10:00', '2021-03-30 08:10:00.000001');
select * from t use index(expression_index3) where d+ timestamp'0000-00-00 00:00:00.00001' = timestamp'2021-08-13 04:10:44'+ timestamp'0000-00-00 00:00:00.00001';
select * from t ignore index(expression_index3) where d+ timestamp'0000-00-00 00:00:00.00001' = timestamp'2021-08-13 04:10:44'+ timestamp'0000-00-00 00:00:00.00001';

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;
2 changes: 1 addition & 1 deletion expression/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion expression/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
if !used[j] && j != i && (expr).Equal(nil, colToCompare.VirtualExpr) && col.RetType.Equal(colToCompare.RetType) {
used[j] = true
}
}
Expand Down

0 comments on commit ed17428

Please sign in to comment.