Bug #117721 Inferred type of functional index is ambiguous and restricts column values
Submitted: 17 Mar 3:20 Modified: 17 Mar 7:59
Reporter: Emily Ong Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:8.0, 8.0.41 OS:Ubuntu
Assigned to: CPU Architecture:x86

[17 Mar 3:20] Emily Ong
Description:
The inferred type of a functional index on a column is ambiguous and arbitrarily restricts the allowable values that can be inserted into the column.

How to repeat:
CREATE TABLE t0 (c0 INT UNSIGNED);
CREATE INDEX i0 on t0 ((-c0));
INSERT INTO t0 (c0) VALUES (2147483649);

The insertion produces an error:
ERROR 3752 (22003) at line 3: Value is out of range for functional index 'i0' at row 1

This is unexpected because `2147483649` is well-within the range allowed by `c0`.

The behaviour in the example suggests that with the creation of `i0`, the column `c0` can now only take integer values in the range [0, 2147483648]. This restriction of the allowable values a column can take is ambiguous, especially because the creation of the functional index should not affect the range of values that a column can take.

According to the documentation (https://dev.mysql.com/doc/refman/8.4/en/create-index.html), it is mentioned that "functional indexes are implemented as hidden virtual generated columns".
Moreover, in the documentation (https://dev.mysql.com/doc/refman/8.4/en/create-table-generated-columns.html) for generated columns in MySQL, it is mentioned that "if the expression evaluates to a data type that differs from the declared column type, implicit coercion to the declared type occurs according to the usual MySQL type-conversion rules."

It is expected that the inferred type of `-c0` should be wide enough to accommodate values that `c0` can take --- specifically, it should be at least a BIGINT since `c0` ranges from [0, 4294967295], so `-c0` ranges from [-4294967295, 0].
[17 Mar 7:59] MySQL Verification Team
Hello Emily Ong,

Thank you for the report and test case.

regards,
Umesh