Skip to content

Fix SIGABRT crash when aggregate_functions_null_for_empty is used with Null combinator functions - #101147

Merged
pufit merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/buzzhouse-sigabrt-aggregate-null-for-empty
Mar 30, 2026
Merged

pufit merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/buzzhouse-sigabrt-aggregate-null-for-empty

Conversation

@groeneai

@groeneai groeneai commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator

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 to CHANGELOG.md):

Fix server crash (SIGABRT) when using aggregate functions with the internal-only Null combinator (e.g. sumNull, avgNull) and aggregate_functions_null_for_empty = 1 setting enabled.

What this PR fixes

In rewriteAggregateFunctionNameIfNeeded() (QueryAnalyzer.cpp:950), the code calls tryGetProperties() and immediately dereferences the returned std::optional without checking if it has a value:

auto properties = AggregateFunctionFactory::instance().tryGetProperties(result_aggregate_function_name, action);
if (!properties->returns_default_when_only_null)  // ← crash when properties is nullopt

tryGetProperties() returns nullopt for functions using the internal-only Null combinator. The Null combinator passes isAggregateFunctionName() (which doesn't check isForInternalUsageOnly()) but fails tryGetProperties() (which does check). When aggregate_functions_null_for_empty = 1, this path is entered and the dereference of an empty optional calls std::abort() → SIGABRT (signal 6).

The correct pattern is already used 12 lines below (line 962) for the same tryGetProperties() call:

if (function_properies && !function_properies->returns_default_when_only_null)

Reproduction

SELECT sumNull(number) FROM numbers(10) SETTINGS aggregate_functions_null_for_empty = 1;
-- Server crashes with signal 6 (SIGABRT)

After fix: returns error Code 63 UNKNOWN_AGGREGATE_FUNCTION ("Aggregate function combinator 'Null' is only for internal usage").

CI impact

This crash was hitting BuzzHouse CI on master and 12+ unrelated PRs across multiple build types (amd_tsan, arm_asan_ubsan, amd_debug, amd_msan) since 2026-03-22:

  • STID: 5390-4fca, 5390-57f3
  • 14 failures in 8 days, accelerating

Test

Added regression test 04068_aggregate_null_for_empty_with_null_combinator that verifies the server returns a proper error instead of crashing. Passes 50/50 with full randomization.

Version info

  • Merged into: 26.4.1.420

When `aggregate_functions_null_for_empty` is enabled, the function
`rewriteAggregateFunctionNameIfNeeded` calls `tryGetProperties()` and
dereferences the result without checking if the optional has a value.
`tryGetProperties()` returns nullopt for aggregate functions using the
internal-only `Null` combinator (e.g. `sumNull`, `avgNull`), causing
`std::optional::operator->` to abort.

The fix adds a null check before dereferencing, matching the pattern
already used 12 lines below at line 962 for the same function.

This crash was hitting BuzzHouse CI on master and 12+ unrelated PRs
(STID: 5390-4fca, 5390-57f3) since 2026-03-22.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (Session: cron:clickhouse-ci-task-worker:20260330-011500)

a) Deterministic repro? ✅ Yes.

SELECT sumNull(number) FROM numbers(10) SETTINGS aggregate_functions_null_for_empty = 1;

Crashes 100% of the time on the unfixed binary. Signal 6 (SIGABRT) at QueryAnalyzer.cpp:950.

b) Root cause explained? ✅ Yes.
isAggregateFunctionName() accepts functions with the Null combinator (it doesn't check isForInternalUsageOnly()), so the Analyzer classifies sumNull as a valid aggregate. rewriteAggregateFunctionNameIfNeeded() then calls tryGetProperties(), which returns nullopt because the Null combinator IS marked as internal-only. Line 950 dereferences this nullopt with properties->returns_default_when_only_null, triggering std::abort().

c) Fix matches root cause? ✅ Yes.
Added a null check: if (!properties || !properties->returns_default_when_only_null) — matching the identical pattern already used at line 962. When properties are unknown, we conservatively add "OrNull" (which then fails downstream with a proper user-facing error UNKNOWN_AGGREGATE_FUNCTION, not a crash).

d) Test intent preserved / New tests added? ✅ Yes.
Added 04068_aggregate_null_for_empty_with_null_combinator regression test verifying the server returns UNKNOWN_AGGREGATE_FUNCTION instead of crashing. No existing tests modified.

e) Demonstrated in both directions? ✅ Yes.

  • Without fix: SELECT sumNull(number) FROM numbers(10) SETTINGS aggregate_functions_null_for_empty = 1 → SIGABRT crash, server dies.
  • With fix: Same query → Code: 63 UNKNOWN_AGGREGATE_FUNCTION error, server stays alive. 50/50 passes with full randomization.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @novikd — could you review this? One-line fix for a server crash (SIGABRT) in rewriteAggregateFunctionNameIfNeeded when aggregate_functions_null_for_empty=1 is used with internal-only Null combinator functions. The original aggregate_functions_null_for_empty code was introduced by @vdimir in c50e186. 14 BuzzHouse CI hits in 8 days (STID: 5390-4fca).

@alexey-milovidov alexey-milovidov self-assigned this Mar 30, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Mar 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a516da3]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_tsan, 2/6) failure
test_storage_kafka/test_batch_fast.py::test_kafka_commit_on_block_write[generate_old_create_table_query] FAIL cidb IGNORED

AI Review

Summary

This PR fixes a real correctness issue in QueryAnalyzer::rewriteAggregateFunctionNameIfNeeded: dereferencing an empty std::optional from tryGetProperties when aggregate_functions_null_for_empty = 1 and internal-only Null combinator names are used. The fix adds the missing nullopt guard and includes a focused stateless regression test covering multiple *Null aggregate names plus normal behavior. High-level verdict: the change is surgical, correct, and adequately tested for the reported exception path.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.70% 76.70% +0.00%

Changed lines: 100.00% (6/6) · Uncovered code

Full report · Diff report

@pufit
pufit added this pull request to the merge queue Mar 30, 2026
Merged via the queue into ClickHouse:master with commit 1b38da3 Mar 30, 2026
148 of 149 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 30, 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 pr-bugfix Pull request with bugfix, not backported by default 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.

4 participants