Skip to content

Add test: arraySimilarity weights_sum == 0 division-by-zero guard untested (mixed-sign weights) - #107353

Merged
Felixoid merged 3 commits into
ClickHouse:masterfrom
clickgapai:qa-bot/coverage-pr106934
Jun 18, 2026
Merged

Felixoid merged 3 commits into
ClickHouse:masterfrom
clickgapai:qa-bot/coverage-pr106934

Conversation

@clickgapai

@clickgapai clickgapai commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

This is a test-only PR — no source code changes. Please review: test quality, whether the claimed coverage gaps are real, and whether test output makes sense.

Adds test coverage for 1 untested code path(s), found during automated review of PR #106934.
That PR (1) The PR rewrites levenshteinDistanceWeighted (src/Common/levenshteinDistance.h) and the arraySimilarity weight summation (src/Functions/array/arrayLevenshtein.cpp) to accumulate weighted distances/sums in a wide accumulator LevenshteinWeightAccumulator<Weight> (a wide::integer for integra

1. arraySimilarity weights_sum == 0 division-by-zero guard untested (mixed-sign weights)
src/Functions/array/arrayLevenshtein.cpp:542
Risk: In similarity at arrayLevenshtein.cpp:519-545, row processing first checks distance == 0 (line 526); only when distance is non-zero does it reach if (weights_sum == 0) (line 540). For non-negative weights weights_sum==0 implies distance==0, so this branch is reachable only via signed mixed-sign weights. Risk: if this guard is broken/removed, arraySimilarity divides a non-zero distance by zero and returns inf/nan instead of 1.0 — silent wrong results.
What's unique vs PR tests: The PR's 04327 test covers large positive weights and 256-bit weights; its single arraySimilarity case uses positive weights (weights_sum>0) and never reaches the weights_sum == 0 guard. This test uses mixed-sign weights that sum to zero with a non-zero distance — the only input class that reaches that guard.
Try it on ClickHouse Fiddle

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 applicable — test-only change.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.975

@clickgapai

Copy link
Copy Markdown
Contributor Author

Pre-PR Validation Gate (per ClickGap pipeline):

a) Deterministic repro? ✅
Test exercises the previously-uncovered branch and passes (correct behaviour).

b) Root cause explained? ✅
src/Functions/array/arrayLevenshtein.cpp:540-543: if (weights_sum == 0) { res_values[row] = 1.0; continue; } — correct but executed by no test (LLVM-confirmed uncovered).

c) Fix matches root cause? ✅
N/A for coverage gap — this PR adds a regression test, not a fix. Asserts current correct behaviour at the cited line.

d) Test intent preserved? / New tests added? ✅
New regression test added: 04337_array_similarity_zero_weights_sum.sql.

e) Both directions demonstrated? ✅
Test passes (MATCH) on master proving current correctness; would fail if the cited branch regressed. Single-direction is the correct shape for a coverage gap test.

f) Fix is general, not a narrow patch? ✅
Test asserts the invariant at the cited line — covers the branch, not a specific input shape.

@groeneai

Copy link
Copy Markdown
Collaborator

Reviewed this test-only PR. LGTM.

The coverage gap is real. In similarity() (src/Functions/array/arrayLevenshtein.cpp) the distance == 0 shortcut (line 526) runs before the weights_sum == 0 guard (line 540). For non-negative weights, weights_sum == 0 implies all weights are 0, which implies distance == 0, so the line-540 branch is reachable only with mixed-sign weights that sum to zero while distance is non-zero. PR #106934's 04327 test used only positive weights, so it never hit this guard. Without it, line 545 would divide a non-zero distance by zero and return inf/nan.

The test is correct. I built the merged HEAD of #106934 and ran the queries: output matches the .reference exactly, exit 0.

  • The three arrayLevenshteinDistanceWeighted cases each have weights_sum == 0 (verified: -100+1+99=0, -50-50+1+99=0, -8+1+7=0) with non-zero distance (-100, -100, -8), so they genuinely reach the guard.
  • arraySimilarity returns 1 for all three (guard fires) and isFinite is 1 (no division by zero).

Test number 04337 is free on master (no collision).

I am a fork contributor and cannot apply the can be tested label myself; a maintainer will need to add it.

@zlareb1 zlareb1 added the can be tested Allows running workflows for external contributors label Jun 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [df902f8]

Summary: ✅


AI Review

Summary

This test-only PR adds stateless coverage for the arraySimilarity path where signed mixed weights make the total weight sum zero while the weighted distance is non-zero. The added cases match the current arrayLevenshteinDistanceWeighted and arraySimilarity behavior and cover the intended zero-denominator guard without introducing a test-quality concern, so I did not post any inline findings.

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 Jun 12, 2026
@clickgapai

Copy link
Copy Markdown
Contributor Author

CI failure in Stress test (amd_msan) appears to be unrelated breakage and is not related to this change. Let me know if you'd like me to merge master and retrigger CI.

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, investigate the hung check using the information and logs from CI in this PR and all other PRs where it happened.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov Investigated.

This PR (Stress test (amd_msan), commit 0dd6987) - from hung_check.log:
The hung check found one lingering query in system.processes:

  • INSERT INTO t__fuzz_0 SELECT * FROM generateRandom() LIMIT 3 (AST-fuzzer mutation, ast_fuzzer_runs=5), elapsed 497.8s, is_cancelled=1, is_all_data_sent=0
  • log_comment = 01710_projection_in_index.sql (an unrelated stateless test, not this PR's 04337_array_similarity...)

Stuck-thread stacks:

  • main query thread: TCPHandler::runImpl -> executeASTFuzzerQueries -> CompletedPipelineExecutor::execute -> PipelineExecutor::executeImpl -> ThreadPoolImpl::wait() (ThreadPool.cpp:540), parked in pthread_cond_wait
  • a pipeline worker: MergeTreeSink::consume -> MergeTreeDataWriter::writeTempPart -> MergedBlockOutputStream::writeWithPermutation -> MergeTreeDataPartWriterWide::writeColumn

So the query was cancelled but the pipeline executor never unwound: the main thread sits in the local ThreadPool wait() while a worker is still inside the MergeTree wide-part writer. This msan build runs under ThreadFuzzer ("Application will run slowly and unstable") with memory_tracker_fault_probability=0.001; that combination is what lets a cancelled INSERT pipeline hang here.

All PRs where it happened (30d):
"Hung check failed, possible deadlock found" is chronic and endemic across stress jobs, not specific to this PR: 395 hits / 112 distinct PRs. 265 of those are the master-only experimental, serverfuzz, * jobs. Regular stress configs: 18 master + 127 PR hits over 111 distinct PRs. On Stress test (amd_msan) alone: 6 hits in 30d, including a master run 20 min before this one (commit e66e6c1) - it reproduces on master with no PR involved.

Conclusion: umbrella chronic hung check (several independent stuck-after-tests root causes), not a regression from this 2-file test-only PR. We track it; diagnostic PR #105905 (log live local thread pools at GlobalThreadPool shutdown) is open to pin down the leaked-local-ThreadPool variant. Merging master and retriggering will clear this run.

Session: cron:clickhouse-worker-slot-1:20260615-191000

@ClickHouse ClickHouse deleted a comment from CLAassistant Jun 17, 2026
@CLAassistant

CLAassistant commented Jun 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
2 out of 3 committers have signed the CLA.

✅ alexey-milovidov
✅ Felixoid
❌ ClickGap AI Bot


ClickGap AI Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.20% -0.20%
Functions 92.30% 92.30% +0.00%
Branches 77.60% 77.50% -0.10%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 366 line(s), 17 function(s) across 124 file(s) · Details

Top files
  • src/Databases/DatabaseReplicated.cpp: 22 line(s), 1 function(s)
  • src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp: 19 line(s)
  • src/Storages/StorageReplicatedMergeTree.cpp: 15 line(s)
  • src/Functions/FunctionsCodingIP.cpp: 10 line(s)
  • src/AggregateFunctions/AggregateFunctionQuantileExact.cpp: 8 line(s)

Full report

@Felixoid
Felixoid added this pull request to the merge queue Jun 18, 2026
Merged via the queue into ClickHouse:master with commit 2282311 Jun 18, 2026
165 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 Jun 18, 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-not-for-changelog This PR should not be mentioned in the changelog 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.

7 participants