Bug #119444 Union with bound variable silently trims result
Submitted: 22 Nov 2025 11:32 Modified: 5 Feb 20:32
Reporter: Michal Vorisek Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:any OS:Any
Assigned to: CPU Architecture:Any
Tags: bind value, corrupt, trimmed, UNION

[22 Nov 2025 11:32] Michal Vorisek
Description:
The following query:
```
select v
from (
  select ? v
  union all
  select ? v
) t
```

silently trims the `v` column when the column is longer than 65536 bytes.

This is highly dangerous as UNION and bind value is basic SQL functionality frequently used.

In our usecase, such query was built by ORM and MySQL silently corrupted our data.

How to repeat:
full repro: https://3v4l.org/2IHsS

Suggested fix:
Please reproduce on your side and fix. There should be no artificial limit of the string length.
[22 Nov 2025 11:34] Michal Vorisek
The repro script currently outputs:
```
expected:
Array
(
    [0] => Array
        (
            [v] => xxx...65540*...xxx
        )

    [1] => Array
        (
            [v] => yxxx...65540*...xxx
        )

)

actual using mysqli driver:
Array
(
    [0] => Array
        (
            [v] => xxx...65535*...xxx
        )

    [1] => Array
        (
            [v] => yxxx...65534*...xxx
        )

)
bool(false)

actual using PDO driver:
Array
(
    [0] => Array
        (
            [v] => xxx...65535*...xxx
        )

    [1] => Array
        (
            [v] => yxxx...65534*...xxx
        )

)
bool(false)
```
[5 Feb 20:32] Roy Lyseng
Thank you for the bug report.
However, I don't think this is a bug.
The default type for a dynamic parameter when no type information is present
is VARCHAR(<64 kbytes>);
Thus, it is impossible to return more than 64 kb in such expressions.

It is possible to use a CAST to set a different type in such cases.
Ideally, it should be possible to do CAST(? AS LONGTEXT), but LONGTEXT is
not a valid CAST type.

It is however possible to do CAST(? AS CHAR(1000000)) which assigns
a LONGTEXT type when size is greater than what is supported for VARCHAR.