Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix fsp for datetime, time and timestamp (#27170) #27243

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5086,6 +5086,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