Bug #121241 LEAST/GREATEST documentation: overlapping numeric and mixed-string comparison rules
Submitted: 7 Sep 3:28
Reporter: Zhang James Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Documentation Severity:S3 (Non-critical)
Version:8.4.11、8.4.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: documentation, GREATEST, LEAST, type conversion

[7 Sep 3:28] Zhang James
Description:
The MySQL 8.4 Reference Manual, Section 14.4.2, describes comparison rules for LEAST(). GREATEST() refers to the same rules.

Documentation:
https://dev.mysql.com/doc/refman/8.4/en/comparison-operators.html#function_least

Two rules have overlapping conditions:

1. The presence of a DOUBLE argument selects double-precision comparison; otherwise, the presence of a DECIMAL argument selects decimal comparison.
2. A mixture of numeric and string arguments selects string comparison.

An argument list containing DECIMAL and VARCHAR, or DOUBLE and VARCHAR, satisfies both conditions as currently written. The numeric rule does not explicitly restrict itself to all-numeric argument lists, and the precedence between these overlapping rules is not stated.

This is a documentation clarification request, not a claim that the current implementation is incorrect.

Related reports already establish the intended mixed-type behavior:

- Bug #94267 records the documentation change to string comparison for mixed numeric/string arguments in MySQL 8.0:
  https://bugs.mysql.com/bug.php?id=94267

- Bug #115125 concerns DOUBLE/TEXT arguments and is classified as Not a Bug. Its discussion cites the mixed-type comparison rules and recommends explicit casts when numeric comparison is required:
  https://bugs.mysql.com/bug.php?id=115125

Those reports explain the intended behavior, but the current 8.4 wording still leaves the numeric and mixed-string conditions overlapping. Explicitly stating their precedence would prevent readers from deriving conflicting expected results.

How to repeat:
1. Read the LEAST() comparison rules in Section 14.4.2 of the MySQL 8.4 Reference Manual.

2. Consider the following queries, reproduced on MySQL 8.4.11
(Linux, x86_64), with the same results previously observed
on MySQL 8.4.0. SHOW WARNINGS immediately after each query
returned no rows on 8.4.11:

SELECT LEAST(CAST(999 AS DECIMAL(20,4)), '1000');
-- Observed value: '1000'

SELECT GREATEST(CAST(999 AS DECIMAL(20,4)), '1000');
-- Observed value: '999.0000'

The quotes above indicate string values, not literal quote characters in the returned data.

3. Apply the two documented conditions:

- Each query contains a DECIMAL argument, so the numeric-comparison condition appears applicable.
- Each query also mixes a number and a string, so the string-comparison condition is applicable.

Numeric comparison would select 999.0000 for LEAST() and 1000 for GREATEST().

String comparison selects '1000' for LEAST() and '999.0000' for GREATEST(), matching the observed behavior.

The example uses ordinary positive values and ASCII digits. It does not depend on floating-point precision, overflow, or non-ASCII collation behavior.

The ambiguity is which documented condition takes precedence, not whether the observed string comparison is intentional.

Suggested fix:
Please clarify the precedence of the mixed numeric/string rule over the DOUBLE/DECIMAL rules.

For example, if this accurately reflects the intended behavior:

- State explicitly that mixed numeric/string arguments are compared as strings, including mixtures containing DOUBLE or DECIMAL.
- Restrict the numeric comparison rules to argument lists consisting entirely of numeric values.
- Add a DECIMAL/string example such as the one above, showing that comparison is lexical rather than numeric.

Please ensure that the clarification applies to both LEAST() and GREATEST().

No implementation change is requested.