Repository navigation
Description
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
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
ardenwick commentedon Aug 30, 2024
I think that's because '%_' are both special characters for
LIKE.FrankChen021 commentedon Sep 2, 2024
@cangyin Thanks for the reminder. That makes sense.
However, current implementation of
nextInStringLikeis 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
nextInStringLikeis:/\_not\_an\_example/, only two tokensnot,exampleare extracted.The problem is:
ArthurZou commentedon Nov 20, 2024
Same question: #72065
rschu1ze commentedon Sep 7, 2026
@ahmadov Could you take a look at this if you have some time?