[MDEV-8605] MariaDB not use DEFAULT value even when inserted NULL for NOT NULLABLE column. Created: 2015-08-12 Updated: 2019-06-17 Resolved: 2015-12-22 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Triggers |
| Affects Version/s: | 5.1.67, 5.2.14, 5.3.12, 5.5, 10.0, 10.1 |
| Fix Version/s: | 10.1.10 |
| Type: | Bug | Priority: | Major |
| Reporter: | Mikhail Gavrilov | Assignee: | Sergei Golubchik |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | upstream-fixed | ||
| Environment: |
Linux |
||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||
| Sprint: | 10.1.9-3, 10.1.10 | ||||||||||||||||||||||||||||||||||||
| Description |
|
MariaDB not use DEFAULT value even when inserted NULL for NOT NULLABLE column. Expected similar behavior as for AUTOINCREMENT and would be usefull for generation right value in trigger. Of course in described example much better if MariaDB would support UUID data type and generate automaticaly UUID for AUTOINCREMENT fields. But I think it would be good idea if MariaDB would allow use any function for default value it would be good replacement of generators. For example:
And it would here appropriate analogy with the behavior of AUTOINCREMENT And if make possible to work with property table autoincrement into functions would be generally be a bomb. It would be possible create complicated ID with concatenation static identifier if schema and autoincrement. For example: and on another schema for same table |
| Comments |
| Comment by Elena Stepanova [ 2015-08-12 ] | ||||||||||||||||||||||||||||||||||||
|
Default specifies a value which should be inserted when no value is provided explicitly. When you attempt to insert NULL, it's not an absence of a value, it's a value NULL. So, naturally the default value is not used. It is so both for MySQL and MariaDB. AUTO_INCREMENT is essentially different in this regard. Possibly a good solution for your case would have been using virtual columns, only they don't seem to support UUID. What you describe in the last part, however, is easy to implement via virtual columns:
I can convert your report into a feature request, but please specify what exactly you are after, as you have several suggestions here:
| ||||||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-08-13 ] | ||||||||||||||||||||||||||||||||||||
|
The primary problem why i wrote this because my application server for add new records or update existence uses templates such as The best solution would be to use UUID data type, and database automaticaly generates UUID values for NULL, as this happens with AUTOINCREMENT PRIMARY KEY. But this feature request already has been written by me. And I look forward to solutions for more than two years. I am don't know what can be done more quickly, probably due to all the above would be enough to fill in the values of DEFAULT NULL. So working CURRENT_TIMESTAMP. CREATE TABLE `b` ( INSERT INTO `test1`.`b` (`b`) VALUES (NULL) ; SELECT * FROM b b It would suit me if it appeared in the version: 10.0.22 Ideas about the use of functions is a reserve for future developments, which would allow to opt out of the trigger. Virtual columns do not help in described case | ||||||||||||||||||||||||||||||||||||
| Comment by VAROQUI Stephane [ 2015-08-15 ] | ||||||||||||||||||||||||||||||||||||
|
I can give a quick state overview of some of my past experience with sequences : UUID as primary key already proved to be bad solution
Holding a global lock at every inserted row would make the sequence very slow
But some generic default call to external function would still require to be serialized like triggers
Can you investigate into microsecond date time, probably feet all requirement ? | ||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-08-18 ] | ||||||||||||||||||||||||||||||||||||
Found it,
If you want, I can convert it to a feature request like that, but I can say right away that there is no chance at all it would possibly appear in any 10.0 release, it is not a feature that can be added to a post-GA release. | ||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-08-19 ] | ||||||||||||||||||||||||||||||||||||
|
We have two tasks related to UUID type: | ||||||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-08-19 ] | ||||||||||||||||||||||||||||||||||||
|
Stephane, I am agree with you that integer's much better for PK than UUID. I try described how I want solve problem without UUID and why can't solve it.
1) For example we have 3 different database schema. 1,2,3 we will store id schema as `s` filed. Function get_current_schema must return for 1 schema 1, for 2 schema 2 and for 3 schema 3 We consider the example on scheme 1 so I simplified function code (hardcoded return 1) 1) schema 1 receive 3 record from scheme 2: INSERT INTO `vvv` (`s`, `id`) VALUES (2, 20) ; Ok. s id Next 3 record from scheme 3: INSERT INTO `vvv` (`s`, `id`) VALUES (3, 5) ; s id And then create own records: INSERT INTO `vvv` (`s`, `id`) VALUES (1, NULL) ; s id Worked as expected, but: 1) I don't think that it really good solution for get next id: 2) I can't create PK for field's `s` and `id`
Because if I do it I trigger stop work's because I try insert NULL value. If above problem would be solved I can created distributed system without UUID. I am afraid that microsecond date time may have collisions. | ||||||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-08-19 ] | ||||||||||||||||||||||||||||||||||||
|
Elena, if before trigger would be called before NULL checking, I not need set DEFAULT 0 also for UUID generation, because I can handle NULL value inside trigger and replace NULL with correct value. So, it may be more easy to do? | ||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-08-19 ] | ||||||||||||||||||||||||||||||||||||
|
Yes, it's very reasonable. Moreover, it is actually an ancient upstream bug http://bugs.mysql.com/bug.php?id=6295 which was fixed in 5.7. We will need to merge the bugfix. I don't know if it's possible to do it in 10.0 (from the upstream bug history I got an impression that the bugfix is very intrusive); for now, I will set it for 10.0 so it will be considered, but no promises here. | ||||||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2015-10-23 ] | ||||||||||||||||||||||||||||||||||||
|
My fix is not nearly as intrusive as what 5.7 has. Still, it changes a very common code path, so I'd rather not put it into 10.0 to avoid possible regressions. I'm going to push it into 10.1 only | ||||||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-10-24 ] | ||||||||||||||||||||||||||||||||||||
|
Sergei, I don't seen your fix, but expected sequence of checks looks for me so: For INSERT: For UPDATE: 3. check duplication for PK and unique index | ||||||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2015-10-24 ] | ||||||||||||||||||||||||||||||||||||
|
mikhail, right. The difficulty is in the fact that NOT NULL columns in MariaDB/MySQL cannot store NULL values. Not even temporarily, not even in memory. There is no place allocated for that. That's why NOT NULL constraint is checked very early, when the value is stored in the field in memory. To fix it we need to be able to store NULL values in the NOT NULL columns. Temporarily, only in memory, not in the table. But still, this is a big change that can potentially introduce regressions. Especially because it will affect every INSERT and UPDATE statements, not only those that store NULL and later change it in the BEFORE trigger. | ||||||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-10-24 ] | ||||||||||||||||||||||||||||||||||||
|
I understood that this could trigger a regression. I would like you to pay attention to the moment I placed the check for null after checking DUPLICATE this in order to be able to do mass update without repeating all the columns in the query. Example from real life:
I can not remove '13', '212516' because they are NOT NULLable. if checking for NULL would be after checking on duplication
this is much cleaner than previous variant. | ||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-12-26 ] | ||||||||||||||||||||||||||||||||||||
|
For the record, the actual change was this:
It is important to have it here to avoid confusion, because it has very little to do with the initial bug summary. |