From 80788a3462c18d5c603ee4badb2c51e7f87205ae Mon Sep 17 00:00:00 2001 From: unknown <evgen@moonbone.local> Date: Wed, 9 May 2007 00:23:16 +0400 Subject: [PATCH] Bug#27670: LOAD DATA does not set CURRENT_TIMESTAMP default value for a TIMESTAMP field when no value has been provided. The LOAD DATA sets the current time in the TIMESTAMP field with CURRENT_TIMESTAMP default value when the field is detected as a null. But when the LOAD DATA command loads data from a file that doesn't contain enough data for all fields then the rest of fields are simply set to null without any check. This leads to no value being inserted to such TIMESTAMP field. Now the read_sep_field() and the read_fixed_length() functions set current time to the TIMESTAMP field with CURRENT_TIMESTAMP default value in all cases when a NULL value is loaded to the field. mysql-test/t/loaddata.test: Added a test case for the bug#27670: LOAD DATA does not set CURRENT_TIMESTAMP default value for a TIMESTAMP field when no value has been provided. mysql-test/r/loaddata.result: Added a test case for the bug#27670: LOAD DATA does not set CURRENT_TIMESTAMP default value for a TIMESTAMP field when no value has been provided. sql/sql_load.cc: Bug#27670: LOAD DATA does not set CURRENT_TIMESTAMP default value for a TIMESTAMP field when no value has been provided. Now the read_sep_field() and the read_fixed_length() functions set current time to the TIMESTAMP field with CURRENT_TIMESTAMP default value in all cases when a NULL value is loaded to the field. --- mysql-test/r/loaddata.result | 19 +++++++++++++++++++ mysql-test/t/loaddata.test | 26 ++++++++++++++++++++++++++ sql/sql_load.cc | 4 ++++ 3 files changed, 49 insertions(+) diff --git a/mysql-test/r/loaddata.result b/mysql-test/r/loaddata.result index 0478e48025..f66aa78377 100644 --- a/mysql-test/r/loaddata.result +++ b/mysql-test/r/loaddata.result @@ -165,3 +165,22 @@ f2 2 SET @@SQL_MODE=@OLD_SQL_MODE; drop table t1,t2; +create table t1(f1 int, f2 timestamp not null default current_timestamp); +create table t2(f1 int); +insert into t2 values(1),(2); +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 2 doesn't contain data for all columns +select f1 from t1 where f2 <> '0000-00-00 00:00:00'; +f1 +1 +2 +delete from t1; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 2 doesn't contain data for all columns +select f1 from t1 where f2 <> '0000-00-00 00:00:00'; +f1 +1 +2 +drop table t1,t2; diff --git a/mysql-test/t/loaddata.test b/mysql-test/t/loaddata.test index e6788cd779..171479163f 100644 --- a/mysql-test/t/loaddata.test +++ b/mysql-test/t/loaddata.test @@ -152,4 +152,30 @@ select * from t2; --exec rm $MYSQLTEST_VARDIR/tmp/t1 SET @@SQL_MODE=@OLD_SQL_MODE; drop table t1,t2; + +# +# Bug#27670: LOAD DATA does not set CURRENT_TIMESTAMP default value for a +# TIMESTAMP field when no value has been provided. +# +create table t1(f1 int, f2 timestamp not null default current_timestamp); +create table t2(f1 int); +insert into t2 values(1),(2); +disable_query_log; +eval select * into outfile '$MYSQLTEST_VARDIR/tmp/t2' from t2; +eval load data infile '$MYSQLTEST_VARDIR/tmp/t2' into table t1; +enable_query_log; +select f1 from t1 where f2 <> '0000-00-00 00:00:00'; +--exec rm $MYSQLTEST_VARDIR/tmp/t2 +delete from t1; +disable_query_log; +eval SELECT * INTO OUTFILE '$MYSQLTEST_VARDIR/tmp/t2' +FIELDS TERMINATED BY '' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY '\r\n' +FROM t2; +eval load data infile '$MYSQLTEST_VARDIR/tmp/t2' into table t1 +FIELDS TERMINATED BY '' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY '\r\n'; +enable_query_log; +select f1 from t1 where f2 <> '0000-00-00 00:00:00'; +--exec rm $MYSQLTEST_VARDIR/tmp/t2 +drop table t1,t2; + # End of 5.0 tests diff --git a/sql/sql_load.cc b/sql/sql_load.cc index ee6d2d0a57..d14e165a78 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -585,6 +585,8 @@ read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_TOO_FEW_RECORDS, ER(ER_WARN_TOO_FEW_RECORDS), thd->row_count); + if (!field->maybe_null() && field->type() == FIELD_TYPE_TIMESTAMP) + ((Field_timestamp*) field)->set_time(); } else { @@ -770,6 +772,8 @@ read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, thd->row_count); DBUG_RETURN(1); } + if (!field->maybe_null() && field->type() == FIELD_TYPE_TIMESTAMP) + ((Field_timestamp*) field)->set_time(); /* QQ: We probably should not throw warning for each field. But how about intention to always have the same number -- 2.30.9