Description:
When a temporary table is used (triggered by SQL_BUFFER_RESULT or ORDER BY), the conversion from TIME type to JSON type produces incorrect results. Specifically: the TIME value -838:59:59.000000 is converted to JSON as "-630:59:59.000000" when processed through a temporary table, whereas without the temporary table it shows as "-06:59:59.000000" (although this value itself may also be problematic, the result with temporary table is clearly more incorrect).
How to repeat:
CREATE TABLE `bug` (
`id` int NOT NULL DEFAULT '0',
`c30` time(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO bug VALUES (1, '-838:59:59.000000');
-- Conversion with temporary table (incorrect result):
SELECT SQL_BUFFER_RESULT CAST(bug.c30 AS JSON) FROM bug;
-- Conversion without temporary table (expected result):
SELECT CAST(bug.c30 AS JSON) FROM bug;