Skip to content

Commit

Permalink
types: fix fsp for datetime, time and timestamp (#27170)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored Aug 16, 2021
1 parent ed88b54 commit 5ae87ca
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/explaintest/r/explain_generate_column_substitute.result
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,33 @@ 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` date DEFAULT NULL,
`b` datetime DEFAULT NULL,
`c` time DEFAULT NULL,
`d` timestamp NULL DEFAULT NULL,
`e` year(4) DEFAULT NULL,
KEY `expression_index` ((adddate(`a`, interval 10 microsecond))),
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);
a b c d e
2021-01-02 2021-03-30 08:10:00 12:01:03 2021-08-13 04:10:44 2021
select * from t ignore index(expression_index) where ADDDATE(a, interval 10 MICROSECOND) = ADDDATE('2021-01-02', interval 10 MICROSECOND);
a b c d e
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_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');
a b c d e
2021-01-02 2021-03-30 08:10:00 12:01:03 2021-08-13 04:10:44 2021
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');
a b c d e
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_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
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
20 changes: 20 additions & 0 deletions cmd/explaintest/t/explain_generate_column_substitute.test
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,23 @@ 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` date DEFAULT NULL,
`b` datetime DEFAULT NULL,
`c` time DEFAULT NULL,
`d` timestamp NULL DEFAULT NULL,
`e` year(4) DEFAULT NULL,
KEY `expression_index` ((adddate(`a`, interval 10 microsecond))),
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';
5 changes: 5 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5110,6 +5110,11 @@ func buildHiddenColumnInfo(ctx sessionctx.Context, indexPartSpecifications []*as
Hidden: true,
FieldType: *expr.GetType(),
}
if colInfo.Tp == mysql.TypeDatetime || colInfo.Tp == mysql.TypeDate || colInfo.Tp == mysql.TypeTimestamp || colInfo.Tp == mysql.TypeDuration {
if colInfo.FieldType.Decimal == types.UnspecifiedLength {
colInfo.FieldType.Decimal = int(types.MaxFsp)
}
}
checkDependencies := make(map[string]struct{})
for _, colName := range findColumnNamesInExpr(idxPart.Expr) {
colInfo.Dependences[colName.Name.L] = struct{}{}
Expand Down

0 comments on commit 5ae87ca

Please sign in to comment.