Skip to content

Name HAVING when an aggregate is rejected in WHERE, and stop calling every unparseable date too short - #116817

Merged
alexey-milovidov merged 6 commits into
masterfrom
improve-aggregate-and-date-error-messages
Aug 30, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
improve-aggregate-and-date-error-messages

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 28, 2026 •

Copy link
Copy Markdown
Member

Related: #116806
Related: #116809
Related: #116812
Related: #116815

Two findings from a second pass over the survey of the error messages ClickHouse produces for mistakes in SQL queries. The first pass damaged benchmark queries at random, which is good at finding messages that point at the wrong place but says nothing about the mistakes people make on purpose. This pass ran a hand-written corpus of those instead — syntax borrowed from T-SQL, Oracle, MySQL and Postgres, plus the classic semantic errors. Most of it is answered well already (NVL, TOP, ROWNUM, MERGE, FOR UPDATE, NUMBER, positional-argument bounds, arrayJoin on a non-array, comparing a string to a number). Two answers were not.

An aggregate rejected in WHERE did not mention HAVING. SELECT count() AS c FROM t WHERE c > 1 is the textbook SQL mistake, and the message was actively confusing, because the alias is expanded before the check runs:

Code: 184. DB::Exception: Aggregate function count() AS c is found in WHERE in query (ILLEGAL_AGGREGATION)

The user never wrote an aggregate in WHERE — they wrote c — and nothing in the message says where an aggregate is allowed. Now:

Code: 184. DB::Exception: Aggregate function count() AS c is found in WHERE in query. Aggregate functions
are not allowed in WHERE and PREWHERE, because they are computed after filtering; use HAVING to filter by
the result of an aggregation (ILLEGAL_AGGREGATION)

The hint goes to the WHERE and PREWHERE checks only. The other callers of assertNoAggregateFunctionNodes — JOIN TREE, GROUP BY, inside another aggregate function, a column DEFAULT expression — have no such alternative to suggest and keep the message they had.

Every unparseable date was called too short. toDate('yesterday') reported:

Code: 38. DB::Exception: Cannot parse date: value is too short (CANNOT_PARSE_DATE)

The value is nine characters; the parse failed because the first character is not a digit. readDateTextFallback uses a single error lambda for every way the parse can fail — a non-digit where a digit belongs, a delimiter outside the allowed set, or the value genuinely ending early — so the message cannot claim a particular one:

Code: 38. DB::Exception: Cannot parse date: expected a date in the YYYY-MM-DD or YYYYMMDD format

Values of ten characters or more take the fast path in readDateTextImpl, which already shows the offending text (Cannot parse date here: abcd-01-02) and is left alone. The new wording names the canonical formats rather than a required delimiter, because delimiters other than - are accepted (toDate('2020!01!02') works) — the test covers that.

No test asserts either of the two old strings. The 48 stateless tests that mention ILLEGAL_AGGREGATION, CANNOT_PARSE_DATE or Cannot parse date were run; the 7 failures are unrelated environment ones (no ZooKeeper, no second loopback listener for remote('127.0.0.2', …), no writeable access storage), and every test that covers the change passes, including 02916_date_text_parsing, 01933_invalid_date, 01424_parse_date_time_bad_date and 04203_analyzer_having_non_aggregate_compat.

Changelog category (leave one):

  • Improvement

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

An aggregate function used in WHERE or PREWHERE — most often through an alias, as in SELECT count() AS c FROM t WHERE c > 1 — is now told to use HAVING instead. A value that cannot be parsed as a Date is no longer reported as being too short when that is not the reason; the message now names the expected format.


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

…every unparseable date too short

Two findings from a second pass over the error-message survey. The first
pass damaged benchmark queries at random, which is good at finding
messages that point at the wrong place but says nothing about the
mistakes people make on purpose, so this pass ran a hand-written corpus
of those instead: syntax borrowed from other dialects and the classic
semantic errors.

`SELECT count() AS c FROM t WHERE c > 1` is the textbook mistake, and the
message was actively confusing, because the alias is expanded before the
check runs:

    Aggregate function count() AS c is found in WHERE in query

The user never wrote an aggregate in WHERE, and nothing said where it
would be allowed. Name that:

    Aggregate function count() AS c is found in WHERE in query.
    Aggregate functions are not allowed in WHERE and PREWHERE, because
    they are computed after filtering; use HAVING to filter by the result
    of an aggregation

The hint is attached to the WHERE and PREWHERE checks only. The other
callers of `assertNoAggregateFunctionNodes` - JOIN TREE, GROUP BY, another
aggregate function, a column DEFAULT expression - have no such
alternative to suggest and keep the message they had.

`toDate('yesterday')` reported

    Cannot parse date: value is too short

which is false: the value is nine characters and the parse failed on the
first character not being a digit. `readDateTextFallback` uses one `error`
lambda for every way the parse can fail - a non-digit where a digit
belongs, a delimiter that is not in the allowed set, or the value ending
early - so the message must not claim a particular one:

    Cannot parse date: expected a date in the YYYY-MM-DD or YYYYMMDD format

Values of ten characters or more take the fast path, which already shows
the offending text (`Cannot parse date here: abcd-01-02`) and is left
alone. No test asserts either of the two old strings; the tests that
assert the error codes `ILLEGAL_AGGREGATION` and `CANNOT_PARSE_DATE` were
run and pass.
@clickhouse-gh

clickhouse-gh Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [eed6f5d]

Summary: ✅


AI Review

Summary

This PR improves two SQL diagnostics: it adds a HAVING hint when aggregate functions are rejected in WHERE or PREWHERE, and it replaces the misleading fallback Date parse error that previously claimed every invalid value was "too short" with a format-oriented message. The old-analyzer path is now wired to the same HAVING hint, the added stateless test covers both analyzer modes plus the date cases described in the PR, and I did not find an unresolved correctness, safety, or evidence gap in the current code.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit eed6f5d.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 92.10% 92.00% -0.10%
Branches 80.80% 80.80% +0.00%

Changed lines: Changed C/C++ lines covered: 46/47 (97.87%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 28, 2026
Comment thread src/Analyzer/ValidationUtils.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing eed6f5dfd with master 9593cfada (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

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

Job report

The messages it asserts are produced by the analyzer; with `enable_analyzer = 0`
the same errors come from a different code path and are worded differently, so
the test failed in the `old analyzer` CI configuration.
Review pointed out that the hint only covered the query-tree analyzer,
while `enable_analyzer = 0` keeps its own copy of the check in
`GetAggregatesVisitor` and so kept the old message. Share the text between
the two and pass it from `TreeRewriter` as well, so the advice does not
depend on which analyzer runs. The test covers that path explicitly with
`SETTINGS enable_analyzer = 0`.

The test also drops the table it creates, as `_cleanup_database` in
`clickhouse-test` asks new tests to do.
alexey-milovidov added a commit that referenced this pull request Aug 28, 2026
The test showed the `Date` path as a contrast to the integer one, and did
it by pinning the whole message - which couples it to
#116817, where the reason
part of `Cannot parse date` changes. Normalise the reason away: what this
test is about is that the `Date` path has a message of its own, not what
that message says.
Master added `05049_totime_legacy_create_as_nested`, so the test moves to 05053.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Aug 30, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 30, 2026
Merged via the queue into master with commit a92c1b8 Aug 30, 2026
175 checks passed
@alexey-milovidov
alexey-milovidov deleted the improve-aggregate-and-date-error-messages branch August 30, 2026 12:22
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants