diff --git a/cmd/explaintest/r/explain_generate_column_substitute.result b/cmd/explaintest/r/explain_generate_column_substitute.result index a2f3059c06625..5efbd22acfbbd 100644 --- a/cmd/explaintest/r/explain_generate_column_substitute.result +++ b/cmd/explaintest/r/explain_generate_column_substitute.result @@ -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 diff --git a/cmd/explaintest/t/explain_generate_column_substitute.test b/cmd/explaintest/t/explain_generate_column_substitute.test index 1ebb37a1dd2df..eb83f8a5a1bf7 100644 --- a/cmd/explaintest/t/explain_generate_column_substitute.test +++ b/cmd/explaintest/t/explain_generate_column_substitute.test @@ -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'; diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index bef6621958fba..503dc9b1c7136 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -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{}{}