Bug #121328 `LAST_INSERT_ID(expr)` returns different results when the same non-NULL `LONGTEXT` value is passed directly or through a

Submitted: 21 Sep 2:41 Modified: 21 Sep 12:26
Reporter: Wang Ojiken Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.46, 8.4.11, 9.7.2 OS:Any
Assigned to: CPU Architecture:Any

[21 Sep 2:41] Wang Ojiken
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
```
[21 Sep 12:26] Chaithra Marsur Gopala Reddy
Hi Wang Ojiken,

Thank you for the test case. Verified as described.

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.