Skip to content

Fix addDays and other date add functions throwing on rows rejected by a short-circuit condition - #128895

Open
murphy-4o wants to merge 3 commits into
ClickHouse:masterfrom
murphy-4o:fix/add-interval-string-short-circuit
Open

murphy-4o wants to merge 3 commits into
ClickHouse:masterfrom
murphy-4o:fix/add-interval-string-short-circuit

Conversation

@murphy-4o

@murphy-4o murphy-4o commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

addDays and the other add*/subtract* functions told short-circuit evaluation that they never throw, so a guard in front of them did not protect them. A String date is parsed for every row, so this query failed on the rows the guard rejects:

SELECT sum(ds != 'x' AND addDays(ds, 1) > toDateTime('2000-01-01')) FROM t
-- Code: 41. CANNOT_PARSE_DATETIME

The same happened in if(cond, addDays(...), ...) and in PREWHERE. WHERE usually worked only because the plan splits the AND chain into separate filters.

isSuitableForShortCircuitArgumentsExecution now returns true for the inputs that can throw:

  • a String date, parsed row by row;
  • a UInt64 or floating-point delta that does not fit Int64;
  • a delta scaled to ticks with an overflow check: sub-second units and seconds on DateTime64 / Time64, and sub-second units with an Int64 delta on DateTime.

Other Date and DateTime arithmetic cannot throw and stays eager.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed addDays and the other add*/subtract* date functions throwing for rows that a preceding condition in AND, if or PREWHERE rejects, for example ds != 'x' AND addDays(ds, 1) > now() on a String column.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/128895]

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [33281f5]

Summary: ✅


AI Review

Summary

This PR makes throwing add*/subtract* date operations eligible for short-circuit evaluation and avoids evaluating unused dictionary entries or values behind NULL. Reviewed head: 33281f51972ab7ad440f44cd9f9db6576bc9d712. The added stateless test passes on that commit's CI binary, but the previously reported interval-wrapper gap remains reproducible. ⚠️ Request changes.

Findings

⚠️ Majors

  • src/Functions/FunctionDateOrDateTimeAddInterval.h:1043: Equivalent interval-arithmetic entrypoints still evaluate rejected rows. FunctionOpDate and FunctionBinaryArithmetic continue to report these operations as unsuitable for short-circuit evaluation. They invoke the inner date-add function only during execution, so the new predicate cannot protect calls through addDate, subDate, dateAdd, or + INTERVAL. On the current CI binary, the query below throws CANNOT_PARSE_DATETIME instead of returning 90; replacing addDate(ds, INTERVAL 1 DAY) with dateAdd(DAY, 1, ds), ds + INTERVAL 1 DAY, or subDate(ds, INTERVAL 1 DAY) also fails. Propagate short-circuit suitability through the delegates for addition and subtraction. This retains the existing finding; no duplicate inline comment was posted.
SELECT sum(if(ds != 'x',
              addDate(ds, INTERVAL 1 DAY) > toDateTime('2000-01-01'), 0))
FROM
(
    SELECT if(number % 10 = 0, 'x', '2020-01-01') AS ds
    FROM numbers(100)
);
Tests
  • ⚠️ Add a focused stateless regression for guarded interval syntax through the delegates, covering addition and subtraction. The existing test covers the direct functions and therefore passes while the wrapper issue remains.
Final Verdict

Status: ⚠️ Request changes. Propagate guarded execution through the interval-arithmetic delegates and add the focused regression above. No new inline findings or thread-state changes were necessary; the two resolved findings no longer hold in the current code.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 3, 2026
@murphy-4o
murphy-4o marked this pull request as ready for review November 3, 2026 07:54
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Nov 3, 2026
… a short-circuit condition

A String first argument is parsed per row, a UInt64 or floating-point delta
can overflow Int64, and the delta is scaled to ticks with an overflow check
for sub-second units and seconds on DateTime64/Time64 and for sub-second
units with an Int64 delta on DateTime. isSuitableForShortCircuitArgumentsExecution
returned false for all of them, so a preceding condition in and/if/PREWHERE
did not keep the rejected rows away from these functions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@murphy-4o
murphy-4o force-pushed the fix/add-interval-string-short-circuit branch from 59ca5b7 to 82aeb24 Compare November 3, 2026 08:52
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 33281f519 with master 7f11488e0 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

29 translation units recompiled, 87 s compile time in total, 29 of them have a recent master baseline.

Job report

murphy-4o and others added 2 commits November 4, 2026 12:14
…lity dictionary values and NULL rows

A rejected value stays in the dictionary of a filtered LowCardinality column, and the
nested value behind a NULL is arbitrary; executing on them threw. Decline execution on
default arguments, like conversions do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant