From d8144f2a4fa97d314e7d5f08076bdf0241db1de2 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Sun, 4 Feb 2024 13:29:20 +0800 Subject: [PATCH 1/7] add one of ut --- pkg/types/time_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/types/time_test.go b/pkg/types/time_test.go index 263eabfa255f0..c12dba8515aef 100644 --- a/pkg/types/time_test.go +++ b/pkg/types/time_test.go @@ -1902,6 +1902,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) From 68f8f8aedd55b428ce0b93daa6e8d956f5f1b524 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Sun, 4 Feb 2024 15:13:49 +0800 Subject: [PATCH 2/7] fix --- pkg/types/time.go | 5 +++-- tests/integrationtest/r/expression/cast.result | 10 ++++++++++ tests/integrationtest/t/expression/cast.test | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/types/time.go b/pkg/types/time.go index a643e1f77bbec..da5de7b802630 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -576,7 +576,8 @@ 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. func GetFracIndex(s string) (index int) { tzIndex, _, _, _, _ := GetTimezone(s) var end int @@ -587,7 +588,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] != '-' && unicode.IsPunct(rune(s[i])) { if s[i] == '.' { index = i } diff --git a/tests/integrationtest/r/expression/cast.result b/tests/integrationtest/r/expression/cast.result index 6628ec0af24c6..d54c9aea37ea1 100644 --- a/tests/integrationtest/r/expression/cast.result +++ b/tests/integrationtest/r/expression/cast.result @@ -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'); diff --git a/tests/integrationtest/t/expression/cast.test b/tests/integrationtest/t/expression/cast.test index e1681e4d87934..854daad9756b5 100644 --- a/tests/integrationtest/t/expression/cast.test +++ b/tests/integrationtest/t/expression/cast.test @@ -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; From 3bb43c405ba7eb67377c2a37b6e4e8cd29a4cd4d Mon Sep 17 00:00:00 2001 From: SeaRise Date: Sun, 4 Feb 2024 15:14:44 +0800 Subject: [PATCH 3/7] u --- pkg/types/time.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/types/time.go b/pkg/types/time.go index da5de7b802630..f83ae744a59ae 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -576,8 +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' and `2020-01-01 12:00:00.123456-05:00`, the index should be 19. +// related issue https://github.com/pingcap/tidb/issues/49555 func GetFracIndex(s string) (index int) { tzIndex, _, _, _, _ := GetTimezone(s) var end int From 86072f5e07162522cc22ed9f1b55e6e354107c79 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 5 Feb 2024 10:06:56 +0800 Subject: [PATCH 4/7] Update time_test.go --- pkg/types/time_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/types/time_test.go b/pkg/types/time_test.go index c12dba8515aef..422c36514e7b2 100644 --- a/pkg/types/time_test.go +++ b/pkg/types/time_test.go @@ -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 07:00:00.123456"}, } for _, test := range table { From 7aabcdc4b21989a6f4748448f374754e5ea2f5e1 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 5 Feb 2024 10:08:19 +0800 Subject: [PATCH 5/7] Update time.go --- pkg/types/time.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/types/time.go b/pkg/types/time.go index f83ae744a59ae..97cb805c8b379 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -579,7 +579,7 @@ func GetFsp(s string) int { // // 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/49555 +// 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 From 9744f94993a5acf96e38b5a3d329a4990ae32daa Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 5 Feb 2024 10:52:45 +0800 Subject: [PATCH 6/7] Update pkg/types/time_test.go Co-authored-by: Hangjie Mo --- pkg/types/time_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/types/time_test.go b/pkg/types/time_test.go index 422c36514e7b2..c9bc3e7fd2dcb 100644 --- a/pkg/types/time_test.go +++ b/pkg/types/time_test.go @@ -115,7 +115,7 @@ 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 07:00:00.123456"}, + {"2020-01-01 12:00:00.123456-05:00", "2020-01-01 17:00:00.123456"}, } for _, test := range table { From cd038bc6d2ed9178dfd6ab823eed2ccbcd014e7f Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 5 Feb 2024 14:13:45 +0800 Subject: [PATCH 7/7] update --- pkg/types/time.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/types/time.go b/pkg/types/time.go index 97cb805c8b379..e2d915df86489 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -590,7 +590,7 @@ func GetFracIndex(s string) (index int) { } index = -1 for i := end; i >= 0; i-- { - if s[i] != '+' && s[i] != '-' && unicode.IsPunct(rune(s[i])) { + if s[i] != '+' && s[i] != '-' && isPunctuation(s[i]) { if s[i] == '.' { index = i }