Skip to content

Fix bug in levenshteinDistanceWeighted function - #101767

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-101725
Apr 7, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-101725

Conversation

@Felixoid

@Felixoid Felixoid commented Apr 4, 2026 •

Copy link
Copy Markdown
Member

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):

Fixes bug in arrayLevenshteinDistanceWeighted and arraySilimarity functions. Closes #101725

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.598 (included in 26.4 and later)
  • Backported to: 26.3.17.58, 25.8.29.24

@clickhouse-gh

clickhouse-gh Bot commented Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [17443e2]

Summary: ✅


AI Review

Summary

This PR fixes the dynamic-programming state transition in levenshteinDistanceWeighted for consecutive matching elements and adds focused stateless regression tests for both arrayLevenshteinDistanceWeighted and arraySimilarity. The change is minimal, directly addresses issue #101725, and I found no correctness, safety, or performance concerns in the patch.

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 Apr 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes an incorrect dynamic-programming update in the weighted Levenshtein implementation that produced wrong distances (and therefore wrong arraySimilarity) for inputs with consecutive matching elements, and adds a regression test to prevent recurrence.

Changes:

  • Fix levenshteinDistanceWeighted() to correctly advance the prev (diagonal) value on matches and to use the diagonal for substitution cost.
  • Add stateless SQL regression queries covering consecutive-match scenarios for both arrayLevenshteinDistanceWeighted and arraySimilarity.
  • Update the corresponding .reference expected output.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Common/levenshteinDistance.h Corrects the DP inner loop to preserve/update the diagonal (prev) properly and compute substitution from the diagonal.
tests/queries/0_stateless/03369_function_arrayLevenshtein.sql Adds regression queries for consecutive matching elements (issue #101725).
tests/queries/0_stateless/03369_function_arrayLevenshtein.reference Updates expected results for the new regression queries.

@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@clickhouse-gh

clickhouse-gh Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.60% +0.00%

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

Full report · Diff report

arraySimilarity(['1', '2'], ['1'], [toFloat64(1), 2], [toFloat64(1)]);
SELECT '';

-- Regression test for consecutive matching elements (https://github.com/ClickHouse/ClickHouse/issues/101725)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We prefer adding a new test for every new found case, instead of editing existing tests.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM anyway.

@alexey-milovidov alexey-milovidov self-assigned this Apr 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 7, 2026
Merged via the queue into master with commit 83fbc99 Apr 7, 2026
161 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-101725 branch April 7, 2026 15:01
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 7, 2026
@Felixoid Felixoid added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jul 20, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 20, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Jul 20, 2026
Cherry pick #101767 to 25.8: Fix bug in levenshteinDistanceWeighted function
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 20, 2026
Felixoid added a commit that referenced this pull request Jul 20, 2026
Backport #101767 to 26.3: Fix bug in levenshteinDistanceWeighted function
Felixoid added a commit that referenced this pull request Jul 20, 2026
Backport #101767 to 25.8: Fix bug in levenshteinDistanceWeighted function
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
OTHER: arrayLevenshteinDistanceWeighted / arraySimilarity -> wrong.
Effect in the production build a394e92: its upstream test(s) 03369_function_arrayLevenshtein fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv).
Upstream patch unchanged, with its test(s): 03369_function_arrayLevenshtein.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

levenshteinDistanceWeighted fails to update prev on match, producing wrong distance for consecutive matching elements

6 participants