Skip to content

LIKE operator does not correctly extract tokens from patterns for fulltext/tokenbf_v1 index analysis  #68985

Open

Description

@FrankChen021

Describe the situation
When the search pattern in the LIKE expression contains underscore characters, tokens are not full extracted from the pattern to apply token bloom filter or full text index. So granules are not fully dropped by the secondary index, in another word, the search performance is lower in such case.

How to reproduce

  • Which ClickHouse server version to use
    All that support such secondary index

  • Steps to reproduce

-- create a table with bloom filter and full text index
create table token (
    id String, 
    msg_token String,
    msg_fulltext String, 
    index idx_token msg_token type tokenbf_v1(32768, 3, 0),
    index idx_fulltext msg_fulltext type full_text) 
engine = MergeTree order by id

insert into token (id, msg_token, msg_fulltext)
values('1', 
'this is a test',
'this is a test'
)

-- search pattern contains underscore, granule SHOULD be dropped, BUT NOT

explain indexes = 1
select * from token where msg_fulltext like '%/_not_/%'

    ┌─explain──────────────────────────────────────┐
 1. │ Expression ((Project names + Projection))    │
 2. │   Expression                                 │
 3. │     ReadFromMergeTree (default.token)        │
 4. │     Indexes:                                 │
 5. │       PrimaryKey                             │
 6. │         Condition: true                      │
 7. │         Parts: 1/1                           │
 8. │         Granules: 1/1                        │
 9. │       Skip                                   │
10. │         Name: idx_fulltext                   │
11. │         Description: full_text GRANULARITY 1 │
12. │         Parts: 1/1                           │
13. │         Granules: 1/1                        │
    └──────────────────────────────────────────────┘

-- search pattern contains underscore, granule SHOULD be dropped, BUT NOT

explain indexes = 1
select * from token where msg_token like '%/_not_/%'

    ┌─explain───────────────────────────────────────┐
 1. │ Expression ((Project names + Projection))     │
 2. │   Expression                                  │
 3. │     ReadFromMergeTree (default.token)         │
 4. │     Indexes:                                  │
 5. │       PrimaryKey                              │
 6. │         Condition: true                       │
 7. │         Parts: 1/1                            │
 8. │         Granules: 1/1                         │
 9. │       Skip                                    │
10. │         Name: idx_token                       │
11. │         Description: tokenbf_v1 GRANULARITY 1 │
12. │         Parts: 1/1                            │
13. │         Granules: 1/1                         │
    └───────────────────────────────────────────────┘

-- if the underscore is removed from the pattern, the granule is dropped

explain indexes = 1
select * from token where msg_token like '%/not/%'
    ┌─explain───────────────────────────────────────┐
 1. │ Expression ((Project names + Projection))     │
 2. │   Expression                                  │
 3. │     ReadFromMergeTree (default.token)         │
 4. │     Indexes:                                  │
 5. │       PrimaryKey                              │
 6. │         Condition: true                       │
 7. │         Parts: 1/1                            │
 8. │         Granules: 1/1                         │
 9. │       Skip                                    │
10. │         Name: idx_token                       │
11. │         Description: tokenbf_v1 GRANULARITY 1 │
12. │         Parts: 0/1                            │
13. │         Granules: 0/1                         │
    └───────────────────────────────────────────────┘

Expected performance
This does not cause incorrect result but leading to performance problem if the search is performed on a very large scale data set because lots of granules are not dropped.

Additional context

We checked the implementation, the following function is used to extract tokens from LIKE pattern during index analysis

image

Above problem is caused by the check in the red frame. And it was there since the first commit #29738.
Don't know why such check is needed.

If it's removed, the function can output the token not in above example.

And the tokens function has no such problem.

select tokens('%/_not_/%')

   ┌─tokens('%/_not_/%')─┐
1. │ ['not']             │
   └─────────────────────┘

Activity

  1. ardenwick commented on Aug 30, 2024

    @ardenwick
    Contributor

    I think that's because '%_' are both special characters for LIKE.

    SELECT '_not_' LIKE '_not_';
    SELECT ',not,' LIKE '_not_';
    
    1
    1
  2. FrankChen021 commented on Sep 2, 2024

    @FrankChen021
    ContributorAuthor

    @cangyin Thanks for the reminder. That makes sense.

    However, current implementation of nextInStringLike is still not correct if we pass a escaped string to it.

    For example:
    The phase we want to search is /_not_an_example/, the expected tokens should be: not, an, example,
    When the escaped string passed to the nextInStringLike is: /\_not\_an\_example/, only two tokens not, example are extracted.

    The problem is:

            else if (isASCII(data[*pos]) && !isAlphaNumericASCII(data[*pos]))
            {
                if (!bad_token && !token.empty())
                    return true; //<--------------------when it returns from here, the `pos` is not increased to skip current non-token character including the escaped underscore or percentage
    
                token.clear();
                bad_token = false;
                escaped = false;
                ++*pos; 
            }
  3. ArthurZou commented on Nov 20, 2024

    @ArthurZou

    Same question: #72065

  4. added
    comp-text-index
    on Sep 4, 2026
  5. rschu1ze commented on Sep 7, 2026

    @rschu1ze
    Member

    @ahmadov Could you take a look at this if you have some time?

  6. added
    external
    on Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp-text-indexexternalperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions