Skip to content

Add sparseGrams function implementation - #78176

Merged
GrigoryPervakov merged 6 commits into
masterfrom
sparse_grams
Apr 2, 2025
Merged

GrigoryPervakov merged 6 commits into
masterfrom
sparse_grams

Conversation

@GrigoryPervakov

@GrigoryPervakov GrigoryPervakov commented Mar 24, 2025 •

Copy link
Copy Markdown
Member

Continuation of #66341
This closes #65694

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Functions sparseGrams and sparseGramsHashes with UTF8 versions added. Author: scanhex12.

Documentation entry for user-facing changes

  • [*] Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b4f2f33]

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Mar 24, 2025
@GrigoryPervakov
GrigoryPervakov force-pushed the sparse_grams branch 2 times, most recently from 3def5d0 to f5a4bc0 Compare March 24, 2025 19:45
@jkartseva jkartseva self-assigned this Mar 24, 2025
@pamarcos

pamarcos commented Mar 26, 2025 •

Copy link
Copy Markdown
Member

@jkartseva jkartseva 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, test failures are unrelated.
Please take a look at the comments.

Comment thread src/Functions/sparseGrams.cpp Resolved
Comment thread src/Functions/sparseGrams.cpp Resolved
Comment thread src/Functions/sparseGrams.cpp Resolved
Comment thread src/Functions/sparseGrams.cpp Resolved
Comment thread src/Functions/sparseGrams.cpp Resolved
Comment thread tests/performance/sparse_grams.xml Resolved
private:
Pos pos;
Pos end;
std::vector<UInt32> ngram_hashes;

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.

Thinking about the problem, it can be optimized (there is no need to do it here). For example, in corner cases that produce a lot of repetitive n-grams, a hashmap can help.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it doesn't make sense to store hashes in a hashmap. It means you have to calculate hash to get hash from the map :)
crc32 is a fast algo, maybe we don't need to store it at all, just left and right to avoid double calculation

@GrigoryPervakov

Copy link
Copy Markdown
Member Author

Test failures are unrelated

@GrigoryPervakov
GrigoryPervakov added this pull request to the merge queue Apr 2, 2025
Merged via the queue into master with commit 2877cec Apr 2, 2025
@GrigoryPervakov
GrigoryPervakov deleted the sparse_grams branch April 2, 2025 14:56
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 2, 2025
}

/** Functions that finds all substrings win minimal length n
* such their border (n-1)-grams' hashes are more than hashes of every (n-1)-grams' in substring.

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.

are more -> are larger

{
if constexpr (is_utf8)
{
Poco::UTF8Encoding encoder{};

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.

This is extremely slow. We have good functions in UTF8Helpers.h

Pos pos;
Pos end;
std::vector<UInt32> ngram_hashes;
std::vector<size_t> utf8_offsets;

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.

This is slow.

private:
Pos pos;
Pos end;
std::vector<UInt32> ngram_hashes;

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.

This is slow.

utf8_offsets.push_back(byte_offset);
auto len = encoder.sequenceLength(reinterpret_cast<const unsigned char *>(pos + byte_offset), end - pos - byte_offset);
if (len < 1)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Incorrect utf8 symbol");

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 should better use implementation-defined behavior, similarly to ngramDistanceUTF8.

chassert(right > left);
while (left < ngram_hashes.size())
{
while (right < ngram_hashes.size() && right <= left + max_ngram_length - min_ngram_length + 1)

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 size is calculated in a loop, and the compiler typically cannot optimize it.


/** Functions that finds all substrings win minimal length n
* such their border (n-1)-grams' hashes are more than hashes of every (n-1)-grams' in substring.
* As a hash function use zlib crc32, which is crc32-ieee with 0xffffffff as initial value

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.

Should use hardware-optimized CRC32-C.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi @GrigoryPervakov @jkartseva — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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.

A new function sparseGrams

6 participants