Describe the bug
The formatted variants of to_date, to_timestamp*, and to_unixtime do not handle some NULL string and format arguments consistently.
There are three related cases:
- A NULL scalar string input produces an internal error.
- When all scalar format arguments are NULL, an internal error is produced.
- When an array format argument is NULL for the current row, it is read as an empty format string instead of being skipped.
to_date and to_timestamp* use the affected shared implementation in datetime/common.rs. String inputs to to_unixtime delegate to to_timestamp_seconds, so they are affected by the same issue.
To Reproduce
A NULL scalar string input produces an internal error:
SELECT to_date(NULL::VARCHAR, '%Y-%m-%d');
Internal error: a should not be None.
The same issue affects formatted to_timestamp* and to_unixtime calls:
SELECT to_timestamp(NULL::VARCHAR, '%Y-%m-%d');
SELECT to_unixtime(NULL::VARCHAR, '%Y-%m-%d');
When all scalar formats are NULL:
SELECT to_date('2020-09-08', NULL::VARCHAR, NULL::VARCHAR);
Internal error: ret should not be None.
A NULL format in an array argument is read as an empty format string instead of being skipped for the current row. The following multi-row input ensures the format arguments are evaluated as arrays:
SELECT id, to_date(value, format1, format2)
FROM (
VALUES
(1, '2020-09-08', NULL::VARCHAR, NULL::VARCHAR),
(2, '2020-09-08', '%Y-%m-%d', NULL::VARCHAR)
) AS t(id, value, format1, format2)
ORDER BY id;
The first row produces:
Execution error: Error parsing timestamp from '2020-09-08' using format '': trailing input
Expected behavior
- A NULL scalar string input should return NULL.
- NULL format arguments should be skipped.
- If all candidate formats are NULL, the result should be NULL.
- If at least one non-NULL format is provided but all non-NULL formats fail to parse the input, the existing parsing error behavior should be preserved.
This is consistent with the existing NULL propagation behavior of the unformatted date/time conversion paths, formatted array inputs with NULL source values, and other date/time formatting functions such as to_char.
Additional context
No response
Describe the bug
The formatted variants of
to_date,to_timestamp*, andto_unixtimedo not handle some NULL string and format arguments consistently.There are three related cases:
to_dateandto_timestamp*use the affected shared implementation indatetime/common.rs. String inputs toto_unixtimedelegate toto_timestamp_seconds, so they are affected by the same issue.To Reproduce
A NULL scalar string input produces an internal error:
The same issue affects formatted
to_timestamp*andto_unixtimecalls:When all scalar formats are NULL:
A NULL format in an array argument is read as an empty format string instead of being skipped for the current row. The following multi-row input ensures the format arguments are evaluated as arrays:
The first row produces:
Expected behavior
This is consistent with the existing NULL propagation behavior of the unformatted date/time conversion paths, formatted array inputs with NULL source values, and other date/time formatting functions such as
to_char.Additional context
No response