[MDEV-19655] Data truncated for column 'date_1' for alter ADD PERIOD FOR date_period command Created: 2019-05-31  Updated: 2021-02-10  Resolved: 2021-02-10

Status: Closed
Project: MariaDB Server
Component/s: Documentation, Versioned Tables
Affects Version/s: 10.4.6
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Shahriyar Rzayev (Inactive) Assignee: Ian Gilfillan
Resolution: Fixed Votes: 0
Labels: versioned-table

Issue Links:
Relates
relates to MDEV-24824 Can't add non-null column to versione... Closed

 Description   

Reading Application-time period tables doc:
https://mariadb.com/kb/en/library/temporal-data-tables/#creating-tables-with-time-periods
and
https://mariadb.com/kb/en/library/temporal-data-tables/#adding-and-removing-time-periods

It is clear that there is alter statement for adding time_period, but if we also have date_period why not to add it using alter as well? If yes it can be added to the DOC as well.
If this step is not supported, then it should be prohibited by proper error message or syntax error:

Adding columns to table:

MariaDB [sbtest]> alter table sbtest1 add column date_1 date;
Query OK, 0 rows affected (0.047 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [sbtest]> alter table sbtest1 add column date_2 date;
Query OK, 0 rows affected (0.040 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [sbtest]> show create table sbtest1\G
*************************** 1. row ***************************
       Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `k` int(11) NOT NULL DEFAULT 0,
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  `date_1` date DEFAULT NULL,
  `date_2` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=220753785 DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING

Trying to add date_period:

MariaDB [sbtest]> ALTER TABLE sbtest1 ADD PERIOD FOR date_period(date_1, date_2);
ERROR 1265 (01000): Data truncated for column 'date_1' at row 1

Why? It is not clear.



 Comments   
Comment by Elena Stepanova [ 2019-05-31 ]

It is supported. But when you add the period, ALTER attempts to convert the period columns into NOT NULL and, in your case, fails because they already have NULL values inside.

I'm not sure what can and should be done about it (or what the standard says about it), leaving it to nikitamalyavin to decide.

Comment by Shahriyar Rzayev (Inactive) [ 2019-05-31 ]

Yes indeed it makes null columns not null. Should it altered back to default null if we drop the date_period?

MariaDB [test]> alter table t2 drop period for date_period;
Query OK, 0 rows affected (0.048 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table t2\G
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `name` varchar(50) DEFAULT NULL,
  `date_1` date NOT NULL,
  `date_2` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.001 sec)

Or it is even better to leave it.
In fact it is not a big deal - just mention about this in the DOC and clarify a bit how people are going to use this feature.
Another thing is mentioning clearly about constraint as well:

 
MariaDB [sbtest]> alter table sbtest1 add date_2 date not null;
Query OK, 0 rows affected (0.057 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [sbtest]> alter table sbtest1 add date_1 date not null;
Query OK, 0 rows affected (0.039 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [sbtest]> select date_1, id from sbtest1 limit 1;
+------------+----+
| date_1     | id |
+------------+----+
| 0000-00-00 |  1 |
+------------+----+
1 row in set (0.002 sec)
 
MariaDB [sbtest]> select date_2, id from sbtest1 limit 1;
+------------+----+
| date_2     | id |
+------------+----+
| 0000-00-00 |  1 |
+------------+----+
1 row in set (0.001 sec)
 
MariaDB [sbtest]> alter table sbtest1 add period for date_period(date_1, date_2);
ERROR 4025 (23000): CONSTRAINT `date_period` failed for `sbtest`.`sbtest1`

Comment by Elena Stepanova [ 2019-05-31 ]

> Should it altered back to default null if we drop the date_period?
Hopefully not. It would mean having to store the information about the initial NULL-ability and what not, it will get real complicated real soon.

> In fact it is not a big deal - just mention about this in the DOC and clarify a bit how people are going to use this feature.
Technically, it is already mentioned in the doc:

Using time periods implicitly defines the two columns as NOT NULL.

Suggestions for improving the docs are always welcome, of course.

> Another thing is mentioning clearly about constraint as well
As discussed in another issue, it is mentioned in the doc:

It also adds a constraint to check whether the first value is less than the second value.

Comment by Ian Gilfillan [ 2021-02-10 ]

Extended the documentation with an example.

Generated at Thu Feb 08 08:53:20 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.