Skip to content

to_date, to_timestamp, and to_unixtime fail on NULL string and format arguments #23640

Description

@lyne7-sc

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:

  1. A NULL scalar string input produces an internal error.
  2. When all scalar format arguments are NULL, an internal error is produced.
  3. 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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Fields

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions