ClickHouse icon indicating copy to clipboard operation
ClickHouse copied to clipboard

26.10: `addDate(formatDateTime(...))` over a Nullable DateTime64 with NULL rows fails with CANNOT_PARSE_DATETIME (regression from #117359)

Open naushniki opened this issue 3 weeks ago • 2 comments

Caused by: https://github.com/ClickHouse/ClickHouse/pull/117359 (automated bisect over master builds narrowed to its merge; the two adjacent candidate commits have no CI build and the same build inputs as the last good commit)

Description

addDate(formatDateTime(x, '%Y-%m-%d 00:00:00'), INTERVAL) over a Nullable(DateTime64) column that contains NULL rows now fails with CANNOT_PARSE_DATETIME (Code 41). formatDateTime now leaves an empty string under NULL rows, where it used to leave '1970-01-01 00:00:00', and addDate still parses that value.

Regression between 26.8.1.667 (OLD) and 26.10.2.142 (NEW).

How to reproduce

Measure only the SELECT on each version.

SELECT min(addDate(formatDateTime(c1, '%Y-%m-%d 00:00:00'), toIntervalSecond(0))) AS lo, max(addDate(formatDateTime(c1, '%Y-%m-%d 00:00:00'), toIntervalSecond(0))) AS hi FROM (SELECT parseDateTime64BestEffortOrNull(arrayJoin(['2026-01-01 10:00:00', '']), 3, 'UTC') AS c1)
SETTINGS compatibility = '24.12';

Expected behavior

NULL rows give NULL and are ignored by min/max. The query should return lo = hi = 2026-01-01 00:00:00.000, as 26.8 does.

Error: CANNOT_PARSE_DATETIME (Code 41).

Version info

  • Resolved by: #130646
  • Merged into: 26.11.1.2046-master (included in 26.11 and later)
  • Backported to: 26.10.5.16

naushniki avatar Nov 09 '26 19:11 naushniki

@naushniki I read the code path, and I think the cause is small.

formatDateTime declines canBeExecutedOnDefaultArguments, so since #117359 it is no longer run on the rows behind a NULL. Those rows are now filled with the default of the result type, an empty string. Before, they held formatDateTime(default), which was '1970-01-01 00:00:00'. The next function in the chain still accepts the contract, so it runs over the whole nested column, placeholder included, and tries to parse ''.

So #117359 does what it promises. What broke is an accidental guarantee: the placeholder under a NULL used to be a parseable string by luck.

addDate only wraps plus, which calls the addSeconds/addDays/... family, and the String is actually parsed in FunctionDateOrDateTimeAddInterval. So addSeconds(formatDateTime(...), 0) should fail the same way. Suggested fix, one line in each place:

FunctionDateOrDateTimeAddInterval (all addX / subtractX) FunctionOpDate (addDate / subDate)

Both decline canBeExecutedOnDefaultArguments, and I would not change how IFunction fills the filtered rows. One trade-off: the hook has no access to the argument types, so a Nullable(DateTime) input that contains NULLs also takes the filtered path. Inputs without NULLs keep the fast path.

I’d add a regression test with the query from this issue.

Marouane-elfadli avatar Nov 09 '26 21:11 Marouane-elfadli

@naushniki I’d add a regression test with the query from this issue. Could I try to fix this one? Could you assign me?

Marouane-elfadli avatar Nov 09 '26 23:11 Marouane-elfadli