Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#54045
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
tangenta authored and ti-chi-bot committed Jul 11, 2024
1 parent b32b212 commit b4a5a65
Show file tree
Hide file tree
Showing 3 changed files with 507 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/expression/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int, expli
switch x := v.(type) {
case string:
lowerX := strings.ToLower(x)
<<<<<<< HEAD
if lowerX == ast.CurrentTimestamp || lowerX == ast.CurrentDate {
if value, err = getTimeCurrentTimeStamp(ctx, tp, fsp); err != nil {
return d, err
Expand All @@ -103,6 +104,25 @@ func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int, expli
terror.Log(err)
} else {
value, err = types.ParseTime(sc, x, tp, fsp, explicitTz)
=======
switch lowerX {
case ast.CurrentTimestamp:
if value, err = getTimeCurrentTimeStamp(ctx.GetEvalCtx(), tp, fsp); err != nil {
return d, err
}
case ast.CurrentDate:
if value, err = getTimeCurrentTimeStamp(ctx.GetEvalCtx(), tp, fsp); err != nil {
return d, err
}
yy, mm, dd := value.Year(), value.Month(), value.Day()
truncated := types.FromDate(yy, mm, dd, 0, 0, 0, 0)
value.SetCoreTime(truncated)
case types.ZeroDatetimeStr:
value, err = types.ParseTimeFromNum(tc, 0, tp, fsp)
terror.Log(err)
default:
value, err = types.ParseTime(tc, x, tp, fsp)
>>>>>>> 4d8e1d5e485 (expression: truncate time part for current_date columns (#54045))
if err != nil {
return d, err
}
Expand Down
276 changes: 276 additions & 0 deletions tests/integrationtest/r/executor/write.result
Original file line number Diff line number Diff line change
Expand Up @@ -1888,3 +1888,279 @@ update t force index(primary) set b = 10 where a = '2023-06-11 10:00:00';
admin check table t;

drop table if exists t;
<<<<<<< HEAD
=======
drop table if exists t_securities;
create table t_securities(id bigint not null auto_increment primary key, security_id varchar(8), market_id smallint, security_type int, unique key uu(security_id, market_id));
insert into t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type);
replace into t_securities (security_id, market_id, security_type) select security_id+1, 1, security_type from t_securities where security_id="7";
INSERT INTO t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type);
select * from t_securities;
id security_id market_id security_type
1 1 2 7
2 7 1 7
3 8 1 7
insert into t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type);
insert into t_securities (security_id, market_id, security_type) select security_id+2, 1, security_type from t_securities where security_id="7";
INSERT INTO t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type);
select * from t_securities;
id security_id market_id security_type
1 1 2 7
2 7 1 7
3 8 1 7
8 9 1 7
set @@session.tidb_enable_list_partition = ON;
drop table if exists t;
create table t (a bigint key auto_random (3), b int) partition by list (a%5) (partition p0 values in (0,1,2), partition p1 values in (3,4));
set @@allow_auto_random_explicit_insert = true;
replace into t values (1,1);
insert into t (b) values (2);
insert into t (b) values (3);
insert into t (b) values (4);
insert into t (b) values (5);
insert into t (b) values (6);
insert into t (b) values (7);
insert into t (b) values (8);
insert into t (b) values (9);
select b from t order by b;
b
1
2
3
4
5
6
7
8
9
update t set b=b+1 where a=1;
select b from t where a=1;
b
2
update t set b=b+1 where a<2;
select b from t where a<2;
b
3
insert into t values (1, 1) on duplicate key update b=b+1;
select b from t where a=1;
b
4
set @@session.tidb_enable_list_partition = default;
set @@allow_auto_random_explicit_insert = default;
set @@session.tidb_enable_list_partition = ON;
drop table if exists t;
create table t (a bigint key auto_increment, b int) partition by list (a%5) (partition p0 values in (0,1,2), partition p1 values in (3,4));
set @@allow_auto_random_explicit_insert = true;
replace into t values (1,1);
insert into t (b) values (2);
insert into t (b) values (3);
insert into t (b) values (4);
insert into t (b) values (5);
insert into t (b) values (6);
insert into t (b) values (7);
insert into t (b) values (8);
insert into t (b) values (9);
select b from t order by b;
b
1
2
3
4
5
6
7
8
9
update t set b=b+1 where a=1;
select b from t where a=1;
b
2
update t set b=b+1 where a<2;
select b from t where a<2;
b
3
insert into t values (1, 1) on duplicate key update b=b+1;
select b from t where a=1;
b
4
set @@session.tidb_enable_list_partition = default;
set @@allow_auto_random_explicit_insert = default;
drop table if exists replace_test;
create table replace_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1);
replace replace_test (c1) values (1),(2),(NULL);
affected rows: 3
info: Records: 3 Duplicates: 0 Warnings: 0
begin;
replace replace_test (c1) values ();
Error 1136 (21S01): Column count doesn't match value count at row 1
rollback;
begin;
replace replace_test (c1, c2) values (1,2),(1);
Error 1136 (21S01): Column count doesn't match value count at row 2
rollback;
begin;
replace replace_test (xxx) values (3);
Error 1054 (42S22): Unknown column 'xxx' in 'field list'
rollback;
begin;
replace replace_test_xxx (c1) values ();
Error 1146 (42S02): Table 'executor__write.replace_test_xxx' doesn't exist
rollback;
replace replace_test set c1 = 3;
affected rows: 1
info:
begin;
replace replace_test set c1 = 4, c1 = 5;
Error 1110 (42000): Column 'c1' specified twice
rollback;
begin;
replace replace_test set xxx = 6;
Error 1054 (42S22): Unknown column 'xxx' in 'field list'
rollback;
drop table if exists replace_test_1;
create table replace_test_1 (id int, c1 int);
replace replace_test_1 select id, c1 from replace_test;
affected rows: 4
info: Records: 4 Duplicates: 0 Warnings: 0
begin;
replace replace_test_1 select c1 from replace_test;
Error 1136 (21S01): Column count doesn't match value count at row 1
rollback;
create table replace_test_2 (id int, c1 int);
replace replace_test_1 select id, c1 from replace_test union select id * 10, c1 * 10 from replace_test;
affected rows: 8
info: Records: 8 Duplicates: 0 Warnings: 0
drop table if exists replace_test_3;
create table replace_test_3 (c1 int, c2 int, UNIQUE INDEX (c2));
replace into replace_test_3 set c2=1;
affected rows: 1
info:
replace into replace_test_3 set c2=1;
affected rows: 1
info:
replace into replace_test_3 set c1=1, c2=1;
affected rows: 2
info:
replace into replace_test_3 set c2=NULL;
affected rows: 1
info:
replace into replace_test_3 set c2=NULL;
affected rows: 1
info:
drop table if exists replace_test_4;
create table replace_test_4 (c1 int, c2 int, c3 int, UNIQUE INDEX (c1, c2));
replace into replace_test_4 set c2=NULL;
affected rows: 1
info:
replace into replace_test_4 set c2=NULL;
affected rows: 1
info:
drop table if exists replace_test_5;
create table replace_test_5 (c1 int, c2 int, c3 int, PRIMARY KEY (c1, c2));
replace into replace_test_5 set c1=1, c2=2;
affected rows: 1
info:
replace into replace_test_5 set c1=1, c2=2;
affected rows: 1
info:
drop table if exists tIssue989;
CREATE TABLE tIssue989 (a int, b int, PRIMARY KEY(a), UNIQUE KEY(b));
insert into tIssue989 (a, b) values (1, 2);
affected rows: 1
info:
replace into tIssue989(a, b) values (111, 2);
affected rows: 2
info:
select * from tIssue989;
a b
111 2
drop table if exists tIssue1012;
CREATE TABLE tIssue1012 (a int, b int, PRIMARY KEY(a), UNIQUE KEY(b));
insert into tIssue1012 (a, b) values (1, 2);
insert into tIssue1012 (a, b) values (2, 1);
replace into tIssue1012(a, b) values (1, 1);
affected rows: 3
info:
select * from tIssue1012;
a b
1 1
drop table if exists t1;
create table t1(a int primary key, b int);
insert into t1 values(1,1),(2,2),(3,3),(4,4),(5,5);
replace into t1 values(1,1);
affected rows: 1
info:
replace into t1 values(1,1),(2,2);
affected rows: 2
info: Records: 2 Duplicates: 0 Warnings: 0
replace into t1 values(4,14),(5,15),(6,16),(7,17),(8,18);
affected rows: 7
info: Records: 5 Duplicates: 2 Warnings: 0
replace into t1 select * from (select 1, 2) as tmp;
affected rows: 2
info: Records: 1 Duplicates: 1 Warnings: 0
drop table if exists t1, t2;
create table t1 (a int primary key, b int default 20, c int default 30);
insert into t1 value (1, 2, 3);
replace t1 set a=1, b=default;
select * from t1;
a b c
1 20 30
replace t1 set a=2, b=default, c=default;
select * from t1;
a b c
1 20 30
2 20 30
replace t1 set a=2, b=default(c), c=default(b);
select * from t1;
a b c
1 20 30
2 30 20
replace t1 set a=default(b)+default(c);
select * from t1;
a b c
1 20 30
2 30 20
50 20 30
create table t2 (pk int primary key, a int default 1, b int generated always as (-a) virtual, c int generated always as (-a) stored);
replace t2 set pk=1, b=default;
select * from t2;
pk a b c
1 1 -1 -1
replace t2 set pk=2, a=10, b=default;
select * from t2;
pk a b c
1 1 -1 -1
2 10 -10 -10
replace t2 set pk=2, c=default, a=20;
select * from t2;
pk a b c
1 1 -1 -1
2 20 -20 -20
replace t2 set pk=2, a=default, b=default, c=default;
select * from t2;
pk a b c
1 1 -1 -1
2 1 -1 -1
replace t2 set pk=3, a=default(a), b=default, c=default;
select * from t2;
pk a b c
1 1 -1 -1
2 1 -1 -1
3 1 -1 -1
replace t2 set b=default(a);
Error 3105 (HY000): The value specified for generated column 'b' in table 't2' is not allowed.
replace t2 set a=default(b), b=default(b);
Error 3105 (HY000): The value specified for generated column 'a' in table 't2' is not allowed.
replace t2 set a=default(a), c=default(c);
Error 1364 (HY000): Field 'pk' doesn't have a default value
replace t2 set c=default(a);
Error 3105 (HY000): The value specified for generated column 'c' in table 't2' is not allowed.
drop table t1, t2;
drop table if exists t;
create table t (a date default current_date);
insert into t values();
select count(1) from t where a = date(a);
count(1)
1
>>>>>>> 4d8e1d5e485 (expression: truncate time part for current_date columns (#54045))
Loading

0 comments on commit b4a5a65

Please sign in to comment.