Skip to content

Commit 785daa6

Browse files
guo-shaogeti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#53590
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 9541258 commit 785daa6

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

expression/builtin_compare.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,14 @@ func RefineComparedConstant(ctx sessionctx.Context, targetFieldType types.FieldT
14811481
targetFieldType = *types.NewFieldType(mysql.TypeLonglong)
14821482
}
14831483
var intDatum types.Datum
1484+
<<<<<<< HEAD:expression/builtin_compare.go
14841485
intDatum, err = dt.ConvertTo(sc, &targetFieldType)
1486+
=======
1487+
// Disable AllowNegativeToUnsigned to make sure return 0 when underflow happens.
1488+
oriTypeCtx := evalCtx.TypeCtx()
1489+
newTypeCtx := oriTypeCtx.WithFlags(oriTypeCtx.Flags().WithAllowNegativeToUnsigned(false))
1490+
intDatum, err = dt.ConvertTo(newTypeCtx, &targetFieldType)
1491+
>>>>>>> 68d12954fe4 (expression: fix wrong result when convert float to unsigned (#53590)):pkg/expression/builtin_compare.go
14851492
if err != nil {
14861493
if terror.ErrorEqual(err, types.ErrOverflow) {
14871494
return &Constant{
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
select cast('' as signed);
2+
cast('' as signed)
3+
0
4+
Level Code Message
5+
Warning 1292 Truncated incorrect INTEGER value: ''
6+
select cast('12345abcde' as signed);
7+
cast('12345abcde' as signed)
8+
12345
9+
Level Code Message
10+
Warning 1292 Truncated incorrect INTEGER value: '12345abcde'
11+
select cast('123e456' as signed);
12+
cast('123e456' as signed)
13+
123
14+
Level Code Message
15+
Warning 1292 Truncated incorrect INTEGER value: '123e456'
16+
select cast('-12345abcde' as signed);
17+
cast('-12345abcde' as signed)
18+
-12345
19+
Level Code Message
20+
Warning 1292 Truncated incorrect INTEGER value: '-12345abcde'
21+
select cast('-123e456' as signed);
22+
cast('-123e456' as signed)
23+
-123
24+
Level Code Message
25+
Warning 1292 Truncated incorrect INTEGER value: '-123e456'
26+
select coercibility(binary('a'));
27+
coercibility(binary('a'))
28+
2
29+
select coercibility(cast('a' as char(10)));
30+
coercibility(cast('a' as char(10)))
31+
2
32+
select coercibility(convert('abc', char(10)));
33+
coercibility(convert('abc', char(10)))
34+
2
35+
drop table if exists t;
36+
create table t(d1 double, f float, d2 decimal(24,8));
37+
insert into t values(0, 0, 0);
38+
select cast(111.1 as datetime) from t;
39+
cast(111.1 as datetime)
40+
2000-01-11 00:00:00
41+
select cast(1311.1 as datetime) from t;
42+
cast(1311.1 as datetime)
43+
NULL
44+
insert into t values(111.1, 1122.1, 31212.111);
45+
insert into t values(121212.1111, 1121212.111111, 11121212.111111);
46+
insert into t values(99991111.1111111, 101.1111111, 20121212121212.1111111);
47+
insert into t values(NULL, NULL, NULL);
48+
insert into t values(1.1, 48.1, 100.1);
49+
insert into t values(1301.11, 1131.111, 100001111.111);
50+
insert into t values(20121212121260.1111111, 20121212126012.1111111, 20121212241212.1111111);
51+
select cast(d1 as datetime), cast(f as datetime), cast(d2 as datetime) from t;
52+
cast(d1 as datetime) cast(f as datetime) cast(d2 as datetime)
53+
NULL NULL NULL
54+
NULL NULL NULL
55+
NULL NULL NULL
56+
NULL NULL NULL
57+
0000-00-00 00:00:00 0000-00-00 00:00:00 0000-00-00 00:00:00
58+
2000-01-11 00:00:00 2000-11-22 00:00:00 2003-12-12 00:00:00
59+
2012-12-12 00:00:00 0112-12-12 00:00:00 1112-12-12 00:00:00
60+
9999-11-11 00:00:00 2000-01-01 00:00:00 2012-12-12 12:12:12
61+
drop table if exists t;
62+
create table t (col1 bigint, col2 double, col3 decimal, col4 varchar(20), col5 json);
63+
insert into t values (1, 1, 1, "1", "1");
64+
insert into t values (null, null, null, null, null);
65+
select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 = 1;
66+
cast(col1 as time) cast(col2 as time) cast(col3 as time) cast(col4 as time) cast(col5 as time)
67+
00:00:01 00:00:01 00:00:01 00:00:01 NULL
68+
select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 is null;
69+
cast(col1 as time) cast(col2 as time) cast(col3 as time) cast(col4 as time) cast(col5 as time)
70+
NULL NULL NULL NULL NULL
71+
select cast(col1 as time(31)) from t where col1 is null;
72+
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
73+
select cast(col2 as time(31)) from t where col1 is null;
74+
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
75+
select cast(col3 as time(31)) from t where col1 is null;
76+
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
77+
select cast(col4 as time(31)) from t where col1 is null;
78+
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
79+
select cast(col5 as time(31)) from t where col1 is null;
80+
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
81+
drop table if exists t;
82+
create table t(a varchar(50));
83+
insert into t values ('2020-01-01 12:00:00.123456 +0600 PST');
84+
insert into t values ('2020-01-01 12:00:00.123456 -0600 PST');
85+
insert into t values ('2020-01-01 12:00:00.123456');
86+
select cast(a as datetime(3)) from t;
87+
cast(a as datetime(3))
88+
2020-01-01 12:00:00.123
89+
2020-01-01 12:00:00.123
90+
2020-01-01 12:00:00.123
91+
drop table if exists t1;
92+
create table t1 (c1 text);
93+
insert into t1 values ('a');
94+
update t1 set c1 = cast('61qw' as decimal);
95+
Error 1292 (22007): Truncated incorrect DECIMAL value: '61qw'
96+
select cast('61qw' as decimal);
97+
cast('61qw' as decimal)
98+
61
99+
Level Code Message
100+
Warning 1292 Truncated incorrect DECIMAL value: '61qw'
101+
drop table if exists t;
102+
create table t (y year);
103+
insert into t values (cast('14:15' as time));
104+
select 1 from t where y = YEAR(CURDATE());
105+
1
106+
1
107+
select cast(cast('14:15' as time) as year) = YEAR(CURDATE());
108+
cast(cast('14:15' as time) as year) = YEAR(CURDATE())
109+
1
110+
explain select null as a union all select 'a' as a;
111+
id estRows task access object operator info
112+
Union_8 2.00 root
113+
├─Projection_10 1.00 root <nil>->Column#3
114+
│ └─TableDual_11 1.00 root rows:1
115+
└─Projection_12 1.00 root a->Column#3
116+
└─TableDual_13 1.00 root rows:1
117+
select null as a union all select 'a' as a;
118+
a
119+
NULL
120+
a
121+
drop table if exists t0;
122+
create table t0(c0 tinyint(1) unsigned not null );
123+
insert into t0 values (1);
124+
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
125+
c0
126+
1
127+
select t0.c0 > -1.194192591e9 from t0;
128+
t0.c0 > -1.194192591e9
129+
1
130+
select t0.c0 < -1.194192591e9 from t0;
131+
t0.c0 < -1.194192591e9
132+
0
133+
select -1.194192591e9 > t0.c0 from t0;
134+
-1.194192591e9 > t0.c0
135+
0
136+
select -1.194192591e9 < t0.c0 from t0;
137+
-1.194192591e9 < t0.c0
138+
1
139+
select t0.c0 > 1.194192591e9 from t0;
140+
t0.c0 > 1.194192591e9
141+
0
142+
select t0.c0 < 1.194192591e9 from t0;
143+
t0.c0 < 1.194192591e9
144+
1
145+
select 1.194192591e9 > t0.c0 from t0;
146+
1.194192591e9 > t0.c0
147+
1
148+
select 1.194192591e9 < t0.c0 from t0;
149+
1.194192591e9 < t0.c0
150+
0
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# TestCastStrToInt
2+
--enable_warnings
3+
select cast('' as signed);
4+
select cast('12345abcde' as signed);
5+
select cast('123e456' as signed);
6+
select cast('-12345abcde' as signed);
7+
select cast('-123e456' as signed);
8+
--disable_warnings
9+
10+
# TestCastCoer
11+
select coercibility(binary('a'));
12+
select coercibility(cast('a' as char(10)));
13+
select coercibility(convert('abc', char(10)));
14+
15+
# TestCastRealAsTime
16+
drop table if exists t;
17+
create table t(d1 double, f float, d2 decimal(24,8));
18+
insert into t values(0, 0, 0);
19+
select cast(111.1 as datetime) from t;
20+
select cast(1311.1 as datetime) from t;
21+
insert into t values(111.1, 1122.1, 31212.111);
22+
insert into t values(121212.1111, 1121212.111111, 11121212.111111);
23+
insert into t values(99991111.1111111, 101.1111111, 20121212121212.1111111);
24+
insert into t values(NULL, NULL, NULL);
25+
insert into t values(1.1, 48.1, 100.1);
26+
insert into t values(1301.11, 1131.111, 100001111.111);
27+
insert into t values(20121212121260.1111111, 20121212126012.1111111, 20121212241212.1111111);
28+
-- sorted_result
29+
select cast(d1 as datetime), cast(f as datetime), cast(d2 as datetime) from t;
30+
31+
# TestCastAsTime
32+
drop table if exists t;
33+
create table t (col1 bigint, col2 double, col3 decimal, col4 varchar(20), col5 json);
34+
insert into t values (1, 1, 1, "1", "1");
35+
insert into t values (null, null, null, null, null);
36+
select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 = 1;
37+
select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 is null;
38+
-- error 1426
39+
select cast(col1 as time(31)) from t where col1 is null;
40+
-- error 1426
41+
select cast(col2 as time(31)) from t where col1 is null;
42+
-- error 1426
43+
select cast(col3 as time(31)) from t where col1 is null;
44+
-- error 1426
45+
select cast(col4 as time(31)) from t where col1 is null;
46+
-- error 1426
47+
select cast(col5 as time(31)) from t where col1 is null;
48+
drop table if exists t;
49+
create table t(a varchar(50));
50+
insert into t values ('2020-01-01 12:00:00.123456 +0600 PST');
51+
insert into t values ('2020-01-01 12:00:00.123456 -0600 PST');
52+
insert into t values ('2020-01-01 12:00:00.123456');
53+
select cast(a as datetime(3)) from t;
54+
55+
# TestCastErrMsg
56+
drop table if exists t1;
57+
create table t1 (c1 text);
58+
insert into t1 values ('a');
59+
--error 1292
60+
update t1 set c1 = cast('61qw' as decimal);
61+
--enable_warnings
62+
select cast('61qw' as decimal);
63+
--disable_warnings
64+
65+
# TestCastTimeAsYear
66+
drop table if exists t;
67+
create table t (y year);
68+
insert into t values (cast('14:15' as time));
69+
select 1 from t where y = YEAR(CURDATE());
70+
select cast(cast('14:15' as time) as year) = YEAR(CURDATE());
71+
72+
# TestIssue49526
73+
explain select null as a union all select 'a' as a;
74+
--sorted_result
75+
select null as a union all select 'a' as a;
76+
77+
# TestNegFloatConvertToUnsigned
78+
drop table if exists t0;
79+
create table t0(c0 tinyint(1) unsigned not null );
80+
insert into t0 values (1);
81+
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
82+
select t0.c0 > -1.194192591e9 from t0;
83+
select t0.c0 < -1.194192591e9 from t0;
84+
select -1.194192591e9 > t0.c0 from t0;
85+
select -1.194192591e9 < t0.c0 from t0;
86+
select t0.c0 > 1.194192591e9 from t0;
87+
select t0.c0 < 1.194192591e9 from t0;
88+
select 1.194192591e9 > t0.c0 from t0;
89+
select 1.194192591e9 < t0.c0 from t0;

0 commit comments

Comments
 (0)