Bug #121328 `LAST_INSERT_ID(expr)` returns different results when the same non-NULL `LONGTEXT` value is passed directly or through a
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
```
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.050 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
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 ```