Re: Support LIKE with nondeterministic collations

From: Nico Williams <nico(at)cryptonector(dot)com>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>
Cc: Daniel Verite <daniel(at)manitou-mail(dot)org>, Robert Haas <robertmhaas(at)gmail(dot)com>, Pgsql-Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Support LIKE with nondeterministic collations
Date: 2026-05-18 11:43:32
Message-ID: agr7ZLXhdLsi5XHk@ubby
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, May 03, 2024 at 08:58:30PM +0200, Peter Eisentraut wrote:
> On 03.05.24 17:47, Daniel Verite wrote:
> > Peter Eisentraut wrote:
> >
> > > However, off the top of my head, this definition has three flaws: (1)
> > > It would make the single-character wildcard effectively an
> > > any-number-of-characters wildcard, but only in some circumstances, which
> > > could be confusing, (2) it would be difficult to compute, because you'd
> > > have to check equality against all possible single-character strings,
> > > and (3) it is not what the SQL standard says.
> >
> > For #1 we're currently using the definition of a "character" as
> > being any single point of code,
>
> That is the definition that is used throughout SQL and PostgreSQL. We can't
> change that without redefining everything. To pick just one example, the
> various trim function also behave in seemingly inconsistent ways when you
> apply then to strings in different normalization forms. The better fix there
> is to enforce the normalization form somehow.

That's unfortunate, because in Unicode a character (see below) need not
be represented as a single code point, or even be possible to represent
using a single code point -- multiple codepoints can be required even in
order to represent a character. This means that substring operations
need to exist that respect character boundaries rather than code point
boundaries.

Changing functions like `overlay()`, `position()` and `substring()` to
count characters rather than codepoints would be a potentially breaking
change. Or maybe a 'fixing change', depending on how one looks at it!

But changing LIKE to respect character boundaries, especially in cases
where LIKE would not have worked to begin with before this change, seems
safe to me -- certainly it seems desirable. The example Daniel posted
definitely shows how interpreting 'character' to mean 'code point'
yields broken behaviors as seen by _humans_[0].

[0] And also by LLMs, though since those are [mainly / for now] trained
on human-generated corpuses, these are human-like too!

> > Intuitively I think that our interpretation of "character" here should
> > be whatever sequence of code points are between character
> > boundaries [1], and that the equality of such characters would be the
> > equality of their sequences of code points, with the string equality
> > check of the collation, whatever the length of these sequences.
>
> > [1]:
> > https://unicode-org.github.io/icu/userguide/boundaryanalysis/#character-boundary
>
> Even that page says, what we are calling character here is really called a
> grapheme cluster.

Not quite.

https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries

| A single Unicode code point is often, but not always the same as a
| basic unit of a writing system for a language, or what a typical user
| might think of as a “character”. There are many cases where such a basic
| unit is made up of multiple Unicode code points. To avoid ambiguity with
| the term character as defined for encoding purposes, it can be useful to
| speak of a user-perceived character. For example, “G” + grave-accent is
| a user-perceived character: users think of it as a single character, yet
| is actually represented by two Unicode code points.

Here the spec refers to "user-perceived character", and leaves
'character' rather ill-defined, but then:

https://www.unicode.org/glossary/

The Unicode glossary says:

| Code Point. (1) Any value in the Unicode codespace; that is, the range
| of integers from 0 to 10FFFF16. (See definition D10 in Section 3.4,
| Characters and Encoding.) Not all code points are assigned to encoded
| characters. See code point type. (2) A value, or position, for a
| character, in any coded character set.

| Character. (1) The smallest component of written language that has
| semantic value; refers to the abstract meaning and/or shape, rather than
| a specific shape (see also glyph), though in code tables some form of
| visual representation is essential for the reader’s understanding. (2)
| Synonym for abstract character. (3) The basic unit of encoding for the
| Unicode character encoding. (4) The English name for the ideographic
| written elements of Chinese origin. [See ideograph (2).]

Note that a combining mark code point is _not_ a character by this
definition (1), though (2):

| Abstract Character. A unit of information used for the organization,
| control, or representation of textual data. (See definition D7 in
| Section 3.4, Characters and Encoding.)

So control characters are also characters. Makes sense: newline and
carriage return are meaningful to users of Latin scripts.

For characters other than 'control' ones this does make it clear that a
character is the coded representation of what we see as a 'glyph'.

| Glyph. (1) An abstract form that represents one or more glyph images.
| (2) A synonym for glyph image. In displaying Unicode character data, one
| or more glyphs may be selected to depict a particular character. These
| glyphs are selected by a rendering engine during composition and layout
| processing. (See also character.)
|
| Grapheme. (1) A minimally distinctive unit of writing in the context of
| a particular writing system. For example, ‹b› and ‹d› are distinct
| graphemes in English writing systems because there exist distinct words
| like big and dig. Conversely, a lowercase italiform letter a and a
| lowercase Roman letter a are not distinct graphemes because no word is
| distinguished on the basis of these two different forms. (2) What a user
| thinks of as a character.
|
| Grapheme Cluster. The text between grapheme cluster boundaries as
| specified by Unicode Standard Annex #29, "Unicode Text Segmentation."
| (See definition D60 in Section 3.6, Combination.) A grapheme cluster
| represents a horizontally segmentable unit of text, consisting of some
| grapheme base (which may consist of a Korean syllable) together with any
| number of nonspacing marks applied to it.

> In a different world, pattern matching, character trimming, etc. would work
> by grapheme, but it does not.

In this very particular case it should though, and it wouldn't be a
breaking change because:

- LIKE with non-deterministic collations is not yet supported, so you
wouldn't be changing any existing behaviors in a breaking manner

- there is no PG function for partitioning a string into substrings
using LIKE patterns (regular expressions, yes) (I think!)

One could argue that counting graphemes rather than code points can only
_fix_ bugs rather than break code, and I want to make that argument, but
it's a harder argument to make, so I won't make it today.

Nico
--

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2026-05-18 11:49:14 Re: [PATCH] Fix psql tab completion for REPACK boolean options
Previous Message Michael Paquier 2026-05-18 11:41:45 Failure in test_slru for host gokiburi (REL_16_STABLE only)