Skip to content
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
7 changes: 5 additions & 2 deletions pkg/types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,10 @@ func GetFsp(s string) int {

// GetFracIndex finds the last '.' for get fracStr, index = -1 means fracStr not found.
// but for format like '2019.01.01 00:00:00', the index should be -1.
// It will not be affected by the time zone suffix. For format like '2020-01-01 12:00:00.123456+05:00', the index should be 19.
//
// It will not be affected by the time zone suffix.
// For format like '2020-01-01 12:00:00.123456+05:00' and `2020-01-01 12:00:00.123456-05:00`, the index should be 19.
// related issue https://github.com/pingcap/tidb/issues/35291 and https://github.com/pingcap/tidb/issues/49555
func GetFracIndex(s string) (index int) {
tzIndex, _, _, _, _ := GetTimezone(s)
var end int
Expand All @@ -587,7 +590,7 @@ func GetFracIndex(s string) (index int) {
}
index = -1
for i := end; i >= 0; i-- {
if unicode.IsPunct(rune(s[i])) {
if s[i] != '+' && s[i] != '-' && isPunctuation(s[i]) {
if s[i] == '.' {
index = i
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/types/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func TestDateTime(t *testing.T) {

// For issue 35291
{"2020-01-01 12:00:00.123456+05:00", "2020-01-01 07:00:00.123456"},
// For issue 49555
{"2020-01-01 12:00:00.123456-05:00", "2020-01-01 17:00:00.123456"},
}

for _, test := range table {
Expand Down Expand Up @@ -1902,6 +1904,8 @@ func TestGetFracIndex(t *testing.T) {
{"2019.01.01 00:00:00", -1},
{"2019.01.01 00:00:00.1", 19},
{"12345.6", 5},
{"2020-01-01 12:00:00.123456 +0600 PST", 19},
{"2020-01-01 12:00:00.123456 -0600 PST", 19},
}
for _, testCase := range testCases {
index := types.GetFracIndex(testCase.str)
Expand Down
10 changes: 10 additions & 0 deletions tests/integrationtest/r/expression/cast.result
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ select cast(col4 as time(31)) from t where col1 is null;
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
select cast(col5 as time(31)) from t where col1 is null;
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
drop table if exists t;
create table t(a varchar(50));
insert into t values ('2020-01-01 12:00:00.123456 +0600 PST');
insert into t values ('2020-01-01 12:00:00.123456 -0600 PST');
insert into t values ('2020-01-01 12:00:00.123456');
select cast(a as datetime(3)) from t;
cast(a as datetime(3))
2020-01-01 12:00:00.123
2020-01-01 12:00:00.123
2020-01-01 12:00:00.123
drop table if exists t1;
create table t1 (c1 text);
insert into t1 values ('a');
Expand Down
6 changes: 6 additions & 0 deletions tests/integrationtest/t/expression/cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ select cast(col3 as time(31)) from t where col1 is null;
select cast(col4 as time(31)) from t where col1 is null;
-- error 1426
select cast(col5 as time(31)) from t where col1 is null;
drop table if exists t;
create table t(a varchar(50));
insert into t values ('2020-01-01 12:00:00.123456 +0600 PST');
insert into t values ('2020-01-01 12:00:00.123456 -0600 PST');
insert into t values ('2020-01-01 12:00:00.123456');
select cast(a as datetime(3)) from t;

# TestCastErrMsg
drop table if exists t1;
Expand Down