Skip to content

Make only the zero-argument year() non-deterministic - #120009

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/year-arity-determinism-119947
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/year-arity-determinism-119947

Conversation

@groeneai

@groeneai groeneai commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #119947
Related: #109097

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Not required: the behaviour being fixed was introduced in 26.9, and 26.9 is not released, so no released version ever reported year(<date>) as non-deterministic.

Description

#109097 turned year from a case-insensitive alias of toYear into its own variadic function. Its resolver reports isDeterministic() == false for every arity, while build() forwards any non-empty argument list to toYear, whose base is deterministic. Gates that inspect the unresolved AST by name therefore see one arity-blind answer and reject the argument-bearing form:

CREATE TABLE t (d Date) ENGINE = MergeTree ORDER BY d;
INSERT INTO t VALUES ('2020-05-05'), ('2020-06-06');
SELECT count() FROM t WHERE toYear(d) = 2020 SETTINGS use_query_cache = 1;  -- 2
SELECT count() FROM t WHERE YEAR(d)   = 2020 SETTINGS use_query_cache = 1;  -- Code: 704

ALTER TABLE ... UPDATE ... WHERE year(d) = ... likewise throws BAD_ARGUMENTS, both at submission on a Replicated table and on the apply_mutations_on_fly read path. YEAR has spelled toYear since 2022 (421d28542bc76eb), so queries written against any released version hit this after upgrading to 26.9.

Root cause: IFunctionOverloadResolver could only be asked whether the function is deterministic, never whether its N-argument form is, so one answer had to cover both forms and the conservative one is what year() needs.

This adds isDeterministicForArity(size_t), defaulting to isDeterministic(), and passes the argument count at the eleven gates that ask a resolver rather than a built IFunctionBase. Exactly one resolver overrides it, so the only behaviour that changes is year(<date>), which goes back to behaving like toYear(<date>). The gates keep asking the resolver on purpose: joinGet and dictGetDescendants report false from their resolvers while inheriting true on their bases, so asking the base instead would have started folding those as deterministic constants.

system.functions still reports deterministic = 0 for year: one row per name cannot express arity, so the arity-blind answer stays conservative.

The new method is distinctly named rather than a second isDeterministic overload, which was declined on #109097 (r3569877383).


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

ClickHouse#109097 reworked `year` from a case-insensitive alias of `toYear` into its own
variadic function. Its resolver reports `isDeterministic() == false` for every
arity, but `build()` forwards every non-empty argument list to `toYear`, whose
base is deterministic. Determinism gates that inspect the unresolved AST by
function name therefore get one arity-blind answer and reject the
argument-bearing form: `SELECT count() FROM t WHERE YEAR(d) = 2020 SETTINGS
use_query_cache = 1` throws `QUERY_CACHE_USED_WITH_NONDETERMINISTIC_FUNCTIONS`,
and a mutation predicate on `year(<date>)` throws `BAD_ARGUMENTS` both at
submission on a Replicated table and on the `apply_mutations_on_fly` read path.
`YEAR` has been a spelling of `toYear` since 421d285 (2022-08-27), so
queries written against any released version hit this after upgrading.

`IFunctionOverloadResolver` could only be asked "is this function
deterministic?", never "is the form with N arguments deterministic?", so the
single answer had to cover both forms and the conservative one is what the
niladic form needs. This adds `isDeterministicForArity(size_t)`, defaulting to
`isDeterministic()`, overridden only by `year`, and passes the argument count at
the eleven gates that ask a resolver rather than a built `IFunctionBase`: the
query-result-cache and mutation determinism gates (which threw), plus partition
pruning for mutations, `additional_table_filters` push-down, normal and
aggregate projection selection, the query-oracle probe, and three analyzer
sites (constant folding, the function-base cache, early short-circuit).

Because exactly one resolver overrides the new method, the only behaviour that
changes is `year(<date>)`, which goes back to behaving like `toYear(<date>)`.
The gates keep asking the resolver rather than the built base on purpose:
`joinGet` and `dictGetDescendants` report `false` from their resolvers while
inheriting `true` on their bases, so asking the base would have started folding
those as deterministic constants, well outside the scope of this change.

`system.functions` keeps reporting `deterministic = 0` for `year`: it has one
row per name and cannot express arity, so the arity-blind answer stays the
conservative one.

The new entry point is a distinctly named method rather than a second
`isDeterministic` overload, which was declined on ClickHouse#109097 (r3569877383): no
name hiding, no `-Woverloaded-virtual` exposure, and a self-documenting call
site.

Closes: ClickHouse#119947
Related: ClickHouse#109097

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings) plus my own cold
read of the diff. Both gate findings ask for extra test coverage; both are declined with
measurement, and the reasons are here so they are not re-litigated silently.

# Sev Finding Verdict Evidence / action
1 ⚠️ The query-cache cell proves YEAR(d) no longer throws, not that the result is cached; assert a QueryCacheHits delta or a system.query_cache entry DISAGREE The diff changes only the eligibility predicate (QueryResultCache.cpp:103); the insert/lookup path at :235-259 is untouched and name-agnostic, so the posited silent decline would hide behind the cell's own toYear(d) control and is pinned by the 31 green 02494_query_cache_* tests, several of which do assert hits and misses. At the default query_cache_nondeterministic_function_handling = 'throw', acceptance IS the cacheability observable: the same statement is Code: 704 before the fix and 2 after. The cell is bound to the changed line, not to "does not throw": reverting only QueryResultCache.cpp reddens this axis while the mutation axis stays green, and reverting only the mutation helper does the exact complement. A hit assertion costs a tag: the one parallel-safe in-tree pattern (02494_query_cache_ignore_output_settings.sql) is no-parallel-replicas, and all 25 other cache-content tests, including 02494_query_cache_nondeterministic_functions.sql for this very gate, are no-parallel. Either tag would drop this whole file, both new axes and its eight existing arms, from configurations where it runs today
2 ⚠️ The mutation cell uses plain MergeTree, so the ReplicatedMergeTree submission path is not exercised; add a Replicated test with a zero-argument control DISAGREE MutationsInterpreter::validateNonDeterministicMutationsForStorage holds no determinism logic of its own and MutationsInterpreter.cpp is not in this diff: it calls findFirstNonDeterministicFunction, whose single classification statement is MutationsNonDeterministicHelpers.cpp:66, the same line AlterConversions.cpp:224 reaches and the committed cell pins. Reverting that one line reddens the committed cell, so a regression reachable from this change cannot pass it. The Replicated behaviour is measured on both binaries anyway: ALTER ... UPDATE ... WHERE YEAR(d) = 2020 on ReplicatedMergeTree is Code: 36 before and accepted after, with a toYear control accepted on both. The zero-argument control exists twice: SELECT year() SETTINGS use_query_cache = 1 must still throw 704 in this file, and 03047_on_fly_mutations_non_deterministic.sql still refuses rand() and a subquery. A Replicated fixture would force -- Tags: zookeeper, which clickhouse-test skips under --no-zookeeper, the ParallelReplicas configuration's own flag
3 💡 PlannerJoinTree.cpp:265 says isDeterministicForArity() == false for a method that takes the argument count AGREE, not blocking Accurate but imprecise wording in a comment this diff already touches. Noted rather than spent on a two-token round trip; happy to change it if you would rather read it exact

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20260914-151400

@clickhouse-gh clickhouse-gh Bot closed this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 14, 2026
@groeneai
groeneai requested review from rschu1ze and vdimir September 14, 2026 15:57
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4fa33a0]

Summary: ⏳

job_name test_name status info comment
Stress test (arm_debug) FAIL
Logical error: Invalid number of columns in chunk pushed to OutputPort. Expected A, found B (STID: 2270-38be) FAIL cidb

AI Review

Summary

This PR makes overload-level determinism checks arity-aware so year(<date>) is treated like toYear(<date>) again, while year() stays non-deterministic. I did not find an unresolved correctness, safety, or evidence gap in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 14, 2026
@rschu1ze rschu1ze added the hold Skip for auto-updating. label Sep 14, 2026
@rschu1ze rschu1ze self-assigned this Sep 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4fa33a0b3 with master de1224b14 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 28fd75867; 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

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

Job report

@rschu1ze

Copy link
Copy Markdown
Member

#119947 (comment)

@rschu1ze rschu1ze closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai hold Skip for auto-updating. pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

year(<date>) is reported non-deterministic, so existing YEAR(d) queries now fail with the query cache (704)

2 participants