Skip to content

Fix positionCaseInsensitiveUTF8 giving different results for constant and column arguments - #122113

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
valerypetrov:fix-position-case-insensitive-utf8-constant
Sep 27, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
valerypetrov:fix-position-case-insensitive-utf8-constant

Conversation

@valerypetrov

@valerypetrov valerypetrov commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

positionCaseInsensitiveUTF8 gave different results for the same strings depending on which arguments were constant. It happens for characters whose lowercase has a different length in bytes, for example the Kelvin sign K (U+212A, 3 bytes) and k, or Ⱥ (2 bytes) and ⱥ (3 bytes).

All four combinations now use the same searcher and the same rule as a haystack column with a constant needle: a match whose needle bytes run past the end of the string is not a match.

haystack, needle build const, const column, const column, column const, column
Kelvin sign, k master 1 0 0 0
this PR 0 0 0 0
k, Kelvin sign master 1 0 1 1
this PR 0 0 0 0
Ⱥ, ⱥ master 1 0 1 1
this PR 0 0 0 0

The results are from local builds of master and of this PR.

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

Fix positionCaseInsensitiveUTF8 returning different results depending on whether its arguments are constant, for characters whose lowercase has a different length in bytes, such as the Kelvin sign.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.852-master (included in 26.10 and later)

For a constant haystack the function lowercased both strings and used std::string::find,
while a column is searched with UTF8CaseInsensitiveStringSearcher. The two disagree when
a character's lowercase has a different UTF-8 length, e.g. the Kelvin sign U+212A and 'k':
the constant returned 1, the column 0. A constant now uses the same searcher.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@valerypetrov
valerypetrov marked this pull request as ready for review September 24, 2026 22:31
@clickhouse-gh clickhouse-gh Bot added comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization labels Sep 24, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Sep 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e98703b]

Summary: ✅


AI Review

Summary

This PR routes the constant-evaluation path of positionCaseInsensitiveUTF8 through the same UTF-8 searcher used by the column paths and adds focused regressions for the previously inconsistent constant/column combinations. I did not find a remaining contract violation in the current patch.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit e98703b.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 26, 2026
For a column, positionCaseInsensitiveUTF8 treats a match whose needle
bytes run past the end of the row as not found. The constant path did
not, so 'Ⱥ' (2 bytes) against the needle 'ⱥ' (3 bytes) gave 1 for a
constant and 0 for a column. The constant path now uses the same rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Functions/PositionImpl.h Resolved
valerypetrov and others added 2 commits September 26, 2026 11:39
…e column

positionCaseInsensitiveUTF8 with a constant needle treats a match whose
needle bytes run past the end of the row as not found. With a needle
column it did not, so 'Ⱥ' (2 bytes) against 'ⱥ' (3 bytes) gave 0 for a
constant needle and 1 for a needle column. Both needle-column paths now
use the same rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A haystack 'k' against a Kelvin sign needle must give 0 for every
constant/column combination, like the reverse case in 05258.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@valerypetrov valerypetrov changed the title Fix positionCaseInsensitiveUTF8 on a constant string Fix positionCaseInsensitiveUTF8 giving different results for constant and column arguments Sep 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e98703bd0 with master 5b7567834 (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

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

Job report

@valerypetrov

Copy link
Copy Markdown
Contributor Author

Should be ready for review.

@alexey-milovidov alexey-milovidov self-assigned this Sep 27, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 27, 2026
Merged via the queue into ClickHouse:master with commit deb0319 Sep 27, 2026
172 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 27, 2026
@@ -0,0 +1,8 @@
-- A constant haystack must give the same result as a column.

SELECT 'Kelvin sign', positionCaseInsensitiveUTF8(unhex('E284AA'), 'k'), positionCaseInsensitiveUTF8(materialize(unhex('E284AA')), 'k');

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.

I think the test can be rewritten without hex.

-- A needle column must give the same result as a constant needle.
-- The lowercase of 'Ⱥ' (2 bytes) is 'ⱥ' (3 bytes), so the needle does not fit in the haystack.

SELECT 'Ⱥ and ⱥ', positionCaseInsensitiveUTF8(materialize('Ⱥ'), 'ⱥ'), positionCaseInsensitiveUTF8(materialize('Ⱥ'), materialize('ⱥ')), positionCaseInsensitiveUTF8('Ⱥ', materialize('ⱥ'));

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.

Could you please merge this test into the other one?

@@ -0,0 +1,9 @@
-- A needle column must give the same result as a constant needle.
-- The lowercase of 'Ⱥ' (2 bytes) is 'ⱥ' (3 bytes), so the needle does not fit in the haystack.

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.

L. 2 is confusing, let's remove it.

res = data.find(needle, start_byte);
if (res == std::string::npos)

/// Search like a column does: a match whose needle bytes run past the end of the string is not a match.

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.

"like a column does" --> please reference a function here, I believe you mean vectorVector but not sure.

Also, it looks like this comment should go before l. 306

if (res == std::string::npos)

/// Search like a column does: a match whose needle bytes run past the end of the string is not a match.
auto searcher = Impl::createSearcherInSmallHaystack(needle.data(), needle.size());

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.

The changes in l. 301-309 look actually unrelated.

/// `res_null` serves as an output parameter for implementing an XYZOrNull variant.
chassert(!res_null);

Impl::toLowerIfNeed(data);

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 no longer lower the data and needle. How do we still ensure that matching is case-insensitive?

@rschu1ze

Copy link
Copy Markdown
Member

@valerypetrov I needed to revert, sorry. But you're welcome to re-submit the PR if it addresses

Thanks!

clickhouse-gh Bot pushed a commit that referenced this pull request Sep 29, 2026
Edited by the nightly changelog CI job following .claude/skills/edit-changelog/SKILL.md.

Changelog-revert: 122634 122113 Revert "Fix `positionCaseInsensitiveUTF8` giving different results for constant and column arguments"
Changelog-deleted-entry: 122113 [Bug Fix (user-visible misbehavior in an official stable release)] [section] * Fix `positionCaseInsensitiveUTF8` returning different results depending on whether its arguments are constant, for characters whose lowercase has a different length in bytes, such as the Kelvin sign. [#122113](#122113) ([Valery Petrov](https://github.com/valerypetrov)).
shankar-iyer pushed a commit to shankar-iyer/ClickHouse that referenced this pull request Oct 21, 2026
…, tail length and row layout

Re-submission of ClickHouse#122113, which was reverted in
ClickHouse#122634, addressing the review comments and the follow-up issues
ClickHouse#122594 and ClickHouse#122595.

`positionCaseInsensitiveUTF8`, `countSubstringsCaseInsensitiveUTF8` and `ILIKE` gave different results depending on which
arguments were constant, on how many bytes followed a match, and on the neighbouring rows of a block.

- A case-insensitive UTF-8 match can cover a different number of haystack bytes than the needle has (`ẞ` and `ß`, `Ⱥ` and `ⱥ`).
  The row boundary checks in `PositionImpl`, `CountSubstringsImpl` and `MatchImpl` compared `needle.size()` with the end of
  the row, so a match could continue into the next row, or be rejected although it fits. `countSubstrings*` also advanced
  by `needle.size()` after a match. Add `matchEnd` to the searchers, which returns the end of the haystack part that a match
  covers, and use it at these places. The exact length searchers keep a plain pointer comparison.
- The SIMD pre-checks accepted only the lower and upper case encodings of the needle, while the scalar comparison accepted
  every code point with the same `Poco::Unicode::toLower`, so a match could depend on the number of bytes after it. Make both
  use one rule, `utf8CaseFold`: `Poco::Unicode::toLower`, except that KELVIN SIGN, ANGSTROM SIGN, OHM SIGN and GREEK CAPITAL
  LETTER THETA SYMBOL match only themselves. Matching them to `k`, `å`, `ω` and `θ` is not possible without losing the SIMD
  and `Volnitsky` fast paths for every needle that contains one of these letters, which was measured to be up to 5 times
  slower.
- Fix the first-byte and SIMD filters for the Greek `ι` and `μ`, whose `Poco::Unicode::toUpper` is a different code point,
  so that `Ι` and `Μ` were never found in a long haystack.
- Route the constant haystack and constant needle case through the same searcher, and remove `toLowerIfNeed`.
- Skip the decoding for two ASCII bytes in the scalar comparison.

`ILIKE` patterns that go through `re2` keep the folding of `re2`.

Add the test `05331_position_case_insensitive_utf8_constness`. In `02346_text_index_ilike_utf8_case_folding`, a KELVIN SIGN
no longer matches `ILIKE '%zzzk%'`.

Closes: ClickHouse#122594
Closes: ClickHouse#122595
Related: ClickHouse#122113
Related: ClickHouse#122634

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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... external Author is not a member of the ClickHouse organization 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