Description:
MySQL produces inconsistent results for `LAST_INSERT_ID(expr)` when `expr` evaluates to the same `LONGTEXT` value but is represented by a direct column reference versus an equivalent `COALESCE()` expression.
The following table contains one non-NULL `LONGTEXT` value:
```sql
DROP TABLE IF EXISTS t0;
CREATE TABLE t0
(
c0 LONGTEXT
);
INSERT INTO t0 (c0)
VALUES ('
69!q]');
```
The following query returns:
```sql
SELECT
t0.c0,
COALESCE(t0.c0, t0.c0) AS co,
LAST_INSERT_ID(t0.c0) AS direct,
LAST_INSERT_ID(COALESCE(t0.c0, t0.c0)) AS wrapped
FROM t0;
```
Observed result:
```text
+--------+--------+--------+---------+
| c0 | co | direct | wrapped |
+--------+--------+--------+---------+
| 69!q] | 69!q] | 69 | 0 |
+--------+--------+--------+---------+
```
In particular:
```text
t0.c0 = '69!q]'
COALESCE(t0.c0, t0.c0) = '69!q]'
LAST_INSERT_ID(t0.c0) = 69
LAST_INSERT_ID(COALESCE(...)) = 0
```
Since `t0.c0` is non-NULL, `COALESCE(t0.c0, t0.c0)` evaluates to exactly the same value as `t0.c0`. Therefore, the two arguments supplied to `LAST_INSERT_ID(expr)` have the same value, but `LAST_INSERT_ID()` produces different results.
This difference is particularly unexpected because the transformation from:
```sql
t0.c0
```
to:
```sql
COALESCE(t0.c0, t0.c0)
```
does not change the value of the expression for this row.
How to repeat:
```sql
DROP TABLE IF EXISTS t0;
CREATE TABLE t0
(
c0 LONGTEXT
);
INSERT INTO t0 (c0)
VALUES ('
69!q]');
SELECT
t0.c0,
COALESCE(t0.c0, t0.c0) AS co,
LAST_INSERT_ID(t0.c0) AS direct,
LAST_INSERT_ID(COALESCE(t0.c0, t0.c0)) AS wrapped
FROM t0;
```
Expected behavior:
`LAST_INSERT_ID(t0.c0)` and `LAST_INSERT_ID(COALESCE(t0.c0, t0.c0))` should produce the same result because both arguments evaluate to the same non-NULL string value.
Actual behavior:
```text
direct = 69
wrapped = 0
```
Description: MySQL produces inconsistent results for `LAST_INSERT_ID(expr)` when `expr` evaluates to the same `LONGTEXT` value but is represented by a direct column reference versus an equivalent `COALESCE()` expression. The following table contains one non-NULL `LONGTEXT` value: ```sql DROP TABLE IF EXISTS t0; CREATE TABLE t0 ( c0 LONGTEXT ); INSERT INTO t0 (c0) VALUES (' 69!q]'); ``` The following query returns: ```sql SELECT t0.c0, COALESCE(t0.c0, t0.c0) AS co, LAST_INSERT_ID(t0.c0) AS direct, LAST_INSERT_ID(COALESCE(t0.c0, t0.c0)) AS wrapped FROM t0; ``` Observed result: ```text +--------+--------+--------+---------+ | c0 | co | direct | wrapped | +--------+--------+--------+---------+ | 69!q] | 69!q] | 69 | 0 | +--------+--------+--------+---------+ ``` In particular: ```text t0.c0 = '69!q]' COALESCE(t0.c0, t0.c0) = '69!q]' LAST_INSERT_ID(t0.c0) = 69 LAST_INSERT_ID(COALESCE(...)) = 0 ``` Since `t0.c0` is non-NULL, `COALESCE(t0.c0, t0.c0)` evaluates to exactly the same value as `t0.c0`. Therefore, the two arguments supplied to `LAST_INSERT_ID(expr)` have the same value, but `LAST_INSERT_ID()` produces different results. This difference is particularly unexpected because the transformation from: ```sql t0.c0 ``` to: ```sql COALESCE(t0.c0, t0.c0) ``` does not change the value of the expression for this row. How to repeat: ```sql DROP TABLE IF EXISTS t0; CREATE TABLE t0 ( c0 LONGTEXT ); INSERT INTO t0 (c0) VALUES (' 69!q]'); SELECT t0.c0, COALESCE(t0.c0, t0.c0) AS co, LAST_INSERT_ID(t0.c0) AS direct, LAST_INSERT_ID(COALESCE(t0.c0, t0.c0)) AS wrapped FROM t0; ``` Expected behavior: `LAST_INSERT_ID(t0.c0)` and `LAST_INSERT_ID(COALESCE(t0.c0, t0.c0))` should produce the same result because both arguments evaluate to the same non-NULL string value. Actual behavior: ```text direct = 69 wrapped = 0 ```