Bug #121222 GREATEST/LEAST mixed-type aggregation produces illegal packed dates (16383-15-31 and 7 more)
Submitted: 3 Sep 7:43 Modified: 3 Sep 12:31
Reporter: Chunling Qin Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:9.7.2 OS:Any
Assigned to: CPU Architecture:Any

[3 Sep 7:43] Chunling Qin
Description:
mysql> SELECT GREATEST(2024, DATE'2024-01-01');        -- 16383-15-31  ← illegal date!
+----------------------------------+
| GREATEST(2024, DATE'2024-01-01') |
+----------------------------------+
| 16383-15-31                      |
+----------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> SELECT MONTH(GREATEST(2024, DATE'2024-01-01')); -- NULL (month 15 out of range)
+-----------------------------------------+
| MONTH(GREATEST(2024, DATE'2024-01-01')) |
+-----------------------------------------+
|                                    NULL |
+-----------------------------------------+
1 row in set, 2 warnings (0.04 sec)

Month 00 / month 15 / year 16383 are bit-mixing products of the YEAR numeric value and the DATE packed value; MONTH() returns NULL for all of them. All GREATEST variants return the same garbage 16383-15-31 regardless of arguments.

Column/constant split: GREATEST(YEAR_col, DATE'2024-01-01') returns 2024-01-01 ✓ (correct) — the constant form is wrong, the column form is right.

Impact: a legal GREATEST(YEAR expression, date) query outputs an illegal date — all downstream MONTH()/DAY()/date arithmetic/formatting silently misbehaves, with no warning.

Anchor: adjacent to fix 38469912f01 (LEAST/GREATEST cmp_context fix for ZEROFILL constant substitution — does not cover YEAR×DATE type aggregation).

How to repeat:
SELECT GREATEST(2024, DATE'2024-01-01');        -- 16383-15-31  ← illegal date!
SELECT MONTH(GREATEST(2024, DATE'2024-01-01')); -- NULL (month 15 out of range)
[3 Sep 12:31] Roy Lyseng
Thank you for the bug report.
Verified as described.