Commit 303eec57 authored by Sergei Golubchik's avatar Sergei Golubchik

MDEV-6880 Can't define CURRENT_TIMESTAMP as default value for added column

ALTER TABLE: don't fill default values per row, do it once.
And do it in two places - for copy_data_between_tables() and for online ALTER.

Also, run function_defaults test both for MyISAM and for InnoDB.
parent 5cfc62f9
......@@ -1050,7 +1050,7 @@ SET TIME_ZONE = "+03:00";
--echo # 1970-01-01 03:16:40
SET TIMESTAMP = 1000.123456;
eval CREATE TABLE t1 ( a INT, b $timestamp NOT NULL DEFAULT $current_timestamp ON UPDATE $current_timestamp) ENGINE = INNODB;
eval CREATE TABLE t1 ( a INT, b $timestamp NOT NULL DEFAULT $current_timestamp ON UPDATE $current_timestamp);
SHOW CREATE TABLE t1;
......@@ -1107,10 +1107,10 @@ eval CREATE TABLE t1 (
b INT,
ts $timestamp NOT NULL DEFAULT $current_timestamp ON UPDATE $current_timestamp,
PRIMARY KEY ( a, ts )
) ENGINE = INNODB;
);
INSERT INTO t1( a, b, ts ) VALUES ( 1, 0, '2000-09-28 17:44:34' );
eval CREATE TABLE t2 ( a INT ) ENGINE = INNODB;
eval CREATE TABLE t2 ( a INT );
INSERT INTO t2 VALUES ( 1 );
UPDATE t1 STRAIGHT_JOIN t2
......@@ -1146,8 +1146,7 @@ eval ALTER TABLE t1 ADD COLUMN c4 $datetime ON UPDATE $now AFTER c3;
eval ALTER TABLE t1 ADD COLUMN c5 $datetime DEFAULT $now AFTER c4;
eval ALTER TABLE t1 ADD COLUMN c6 $datetime DEFAULT $now ON UPDATE $now AFTER c5;
SELECT * FROM t1;
query_vertical SELECT * FROM t1;
DROP TABLE t1;
......
......@@ -1434,13 +1434,13 @@ drop table t1;
SET TIME_ZONE = "+03:00";
# 1970-01-01 03:16:40
SET TIMESTAMP = 1000.123456;
CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE = INNODB;
CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 ( a ) VALUES ( 1 );
SELECT * FROM t1;
a b
......@@ -1490,9 +1490,9 @@ a INT,
b INT,
ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ( a, ts )
) ENGINE = INNODB;
);
INSERT INTO t1( a, b, ts ) VALUES ( 1, 0, '2000-09-28 17:44:34' );
CREATE TABLE t2 ( a INT ) ENGINE = INNODB;
CREATE TABLE t2 ( a INT );
INSERT INTO t2 VALUES ( 1 );
UPDATE t1 STRAIGHT_JOIN t2
SET t1.b = t1.b + 1
......@@ -1521,8 +1521,19 @@ ALTER TABLE t1 ADD COLUMN c4 DATETIME ON UPDATE NOW() AFTER c3;
ALTER TABLE t1 ADD COLUMN c5 DATETIME DEFAULT NOW() AFTER c4;
ALTER TABLE t1 ADD COLUMN c6 DATETIME DEFAULT NOW() ON UPDATE NOW() AFTER c5;
SELECT * FROM t1;
a1 a2 a3 a4 a5 a6 b c1 c2 c3 c4 c5 c6
0000-00-00 00:00:00 1970-01-01 03:16:40 1970-01-01 03:16:40 NULL 1970-01-01 03:16:40 1970-01-01 03:16:40 1 0000-00-00 00:00:00 1970-01-01 03:16:40 1970-01-01 03:16:40 NULL 1970-01-01 03:16:40 1970-01-01 03:16:40
a1 0000-00-00 00:00:00
a2 1970-01-01 03:16:40
a3 1970-01-01 03:16:40
a4 NULL
a5 1970-01-01 03:16:40
a6 1970-01-01 03:16:40
b 1
c1 0000-00-00 00:00:00
c2 1970-01-01 03:16:40
c3 1970-01-01 03:16:40
c4 NULL
c5 1970-01-01 03:16:40
c6 1970-01-01 03:16:40
DROP TABLE t1;
CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP, b DATETIME DEFAULT NOW() );
INSERT INTO t1 VALUES ();
......@@ -2979,13 +2990,13 @@ drop table t1;
SET TIME_ZONE = "+03:00";
# 1970-01-01 03:16:40
SET TIMESTAMP = 1000.123456;
CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)) ENGINE = INNODB;
CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 ( a ) VALUES ( 1 );
SELECT * FROM t1;
a b
......@@ -3035,9 +3046,9 @@ a INT,
b INT,
ts TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY ( a, ts )
) ENGINE = INNODB;
);
INSERT INTO t1( a, b, ts ) VALUES ( 1, 0, '2000-09-28 17:44:34' );
CREATE TABLE t2 ( a INT ) ENGINE = INNODB;
CREATE TABLE t2 ( a INT );
INSERT INTO t2 VALUES ( 1 );
UPDATE t1 STRAIGHT_JOIN t2
SET t1.b = t1.b + 1
......@@ -3066,8 +3077,19 @@ ALTER TABLE t1 ADD COLUMN c4 DATETIME(6) ON UPDATE NOW(6) AFTER c3;
ALTER TABLE t1 ADD COLUMN c5 DATETIME(6) DEFAULT NOW(6) AFTER c4;
ALTER TABLE t1 ADD COLUMN c6 DATETIME(6) DEFAULT NOW(6) ON UPDATE NOW(6) AFTER c5;
SELECT * FROM t1;
a1 a2 a3 a4 a5 a6 b c1 c2 c3 c4 c5 c6
0000-00-00 00:00:00.000000 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000 NULL 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000 1 0000-00-00 00:00:00.000000 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000 NULL 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000
a1 0000-00-00 00:00:00.000000
a2 1970-01-01 03:16:40.000000
a3 1970-01-01 03:16:40.000000
a4 NULL
a5 1970-01-01 03:16:40.000000
a6 1970-01-01 03:16:40.000000
b 1
c1 0000-00-00 00:00:00.000000
c2 1970-01-01 03:16:40.000000
c3 1970-01-01 03:16:40.000000
c4 NULL
c5 1970-01-01 03:16:40.000000
c6 1970-01-01 03:16:40.000000
DROP TABLE t1;
CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE CURRENT_TIMESTAMP(6), b DATETIME(6) DEFAULT NOW(6) );
INSERT INTO t1 VALUES ();
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -2,8 +2,6 @@
--echo # Test of function defaults for any server, including embedded.
--echo #
--source include/have_innodb.inc
--echo #
--echo # Function defaults run 1. No microsecond precision.
--echo #
......
--echo #
--echo # Test of function defaults for any server, including embedded.
--echo #
--source include/have_innodb.inc
set default_storage_engine=innodb;
--echo #
--echo # Function defaults run 1. No microsecond precision.
--echo #
let $current_timestamp=CURRENT_TIMESTAMP;
let $now=NOW();
let $timestamp=TIMESTAMP;
let $datetime=DATETIME;
source 'include/function_defaults.inc';
--echo #
--echo # Function defaults run 2. Six digits scale on seconds precision.
--echo #
let $current_timestamp=CURRENT_TIMESTAMP(6);
let $now=NOW(6);
let $timestamp=TIMESTAMP(6);
let $datetime=DATETIME(6);
source 'include/function_defaults.inc';
......@@ -8723,6 +8723,9 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
*/
altered_table->column_bitmaps_set_no_signal(&altered_table->s->all_set,
&altered_table->s->all_set);
restore_record(altered_table, s->default_values); // Create empty record
if (altered_table->default_field && altered_table->update_default_fields())
goto err_new_table_cleanup;
// Ask storage engine whether to use copy or in-place
enum_alter_inplace_result inplace_supported=
......@@ -9365,14 +9368,14 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to,
to->use_all_columns();
to->mark_virtual_columns_for_write(TRUE);
if (init_read_record(&info, thd, from, (SQL_SELECT *) 0, 1, 1, FALSE))
{
error= 1;
goto err;
}
if (ignore && !alter_ctx->fk_error_if_delete_row)
to->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
thd->get_stmt_da()->reset_current_row_for_warning();
restore_record(to, s->default_values); // Create empty record
if (to->default_field && to->update_default_fields())
goto err;
thd->progress.max_counter= from->file->records();
time_to_report_progress= MY_HOW_OFTEN_TO_WRITE/10;
......@@ -9415,11 +9418,6 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to,
prev_insert_id= to->file->next_insert_id;
if (to->vfield)
update_virtual_fields(thd, to, VCOL_UPDATE_FOR_WRITE);
if (to->default_field && to->update_default_fields())
{
error= 1;
break;
}
if (thd->is_error())
{
error= 1;
......
......@@ -2460,7 +2460,7 @@ innobase_build_col_map(
innobase_build_col_map_add(
heap, dtuple_get_nth_field(add_cols, i),
altered_table->s->field[sql_idx],
altered_table->field[sql_idx],
dict_table_is_comp(new_table));
found_col:
i++;
......
......@@ -2461,7 +2461,7 @@ innobase_build_col_map(
innobase_build_col_map_add(
heap, dtuple_get_nth_field(add_cols, i),
altered_table->s->field[sql_idx],
altered_table->field[sql_idx],
dict_table_is_comp(new_table));
found_col:
i++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment