I'm confused at the behaviour of persistent generated columns in Mariadb when it comes to IF statements and dates. The following works:
CREATE TABLE `test1` (
`date` datetime(3),
`generated` datetime(3) AS (
`date` - INTERVAL 1 SECOND
) STORED
);
As does this:
CREATE TABLE `test2` (
`date` datetime(3),
`generated` datetime(3) AS (
IF(`date` IS NOT NULL, `date` - INTERVAL 1 SECOND, `date`)
) STORED
);
As does this:
CREATE TABLE `test3` (
`date` datetime(3),
`generated` datetime(3) AS (
'2000-01-01 00:00:00'
) STORED
);
But this doesn't:
CREATE TABLE `test4` (
`date` datetime(3),
`generated` datetime(3) AS (
IF(`date` IS NOT NULL, `date` - INTERVAL 1 SECOND, '2000-01-01 00:00:00')
) STORED
);
It throws the following error:
ERROR 1901 (HY000) at line 16: Function or expression 'if(`date` is not null,`date` - interval 1 second,'2000-01-01 00:00:00')' cannot be used in the GENERATED ALWAYS AS clause of `generated`
I've put together these examples here: https://onecompiler.com/mariadb/43hnm9j7r, but have also tested locally with mariadb 10.11, and the behaviour in mysql seems to be the same.
Making the generated column VIRTUAL rather than STORED fixes the problem, as expected, but in my use case I'd like to be able to add an index on the generated column.
The docs say:
Non-deterministic built-in functions are not supported in expressions for PERSISTENT or indexed VIRTUAL generated columns.
So what part of the final example there is non-deterministic, when all of its component parts work fine?
test4works without error in MySQL 8.4.