Repository navigation
Refactor Naive Bayes Classifier as a dictionary layout NAIVE_BAYES - #108773
Conversation
|
Updating the branch to fix the |
Under parallel replicas the secondary queries carry a fully-qualified table
name but keep their own current database (`default`), so the unqualified
dictionary names passed to `naiveBayesClassifier` could not be resolved on the
remote replicas and the query failed with `BAD_ARGUMENTS` "Dictionary not
found" (the same limitation as `dictGet`). Qualify the dictionary references
with `${CLICKHOUSE_DATABASE}` so they resolve regardless of the secondary
query's current database, which keeps the ParallelReplicas CI coverage active
instead of disabling it.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108773&sha=6873626a93cf75cca0dfb59f8bc370dc70806b72&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29
PR: ClickHouse#108773
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Merged the latest Root cause: under parallel replicas the secondary queries carry a fully-qualified table name but keep their own current database ( Verified locally against a 3-replica On the AI-review "Needs follow-up" doc items ( |
george-larionov
left a comment
There was a problem hiding this comment.
The changes look really nice to me overall, great improvement over the old implementation! I have added a few mostly minor comments.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 1251/1278 (97.89%) · Uncovered code |
99a759b
Under parallel replicas the secondary queries carry a fully-qualified table
name but keep their own current database (`default`), so the unqualified
dictionary names passed to `naiveBayesClassifier` could not be resolved on the
remote replicas and the query failed with `BAD_ARGUMENTS` "Dictionary not
found" (the same limitation as `dictGet`). Qualify the dictionary references
with `${CLICKHOUSE_DATABASE}` so they resolve regardless of the secondary
query's current database, which keeps the ParallelReplicas CI coverage active
instead of disabling it.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108773&sha=6873626a93cf75cca0dfb59f8bc370dc70806b72&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29
PR: ClickHouse#108773
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Refactor Naive Bayes Classifier as a dictionary layout `NAIVE_BAYES`
Previously,
naiveBayesClassifierwas very hard to use: user had to setup configurations files and produce model in a specific binary format. This PR solves the problem by allowing data source to be a normal ClickHouse table! For example, now using the classifier is as simple as:Have some training data in this format:
Create the model:
Notice that
0meanspositivesentiment and1meansnegativesentiment.Now, we can just do:
Since it's a dictionary, we can achieve the same using
dictGet:Additionally, we have two new functions:
Get prediction + probability:
Get all class probability:
Additionally, this PR optimizes internal representation of the model data which means load time, peak memory usage and classification rate would be significantly faster!
For
codepoint_3model (9.7 MB in size):Try out the
codepoint_2model that's inside the repo for testing:Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Naive Bayes models (used by
naiveBayesClassifier) are now configured as a dictionary with theNAIVE_BAYESlayout, built at load time from a table of pre-aggregated per-class n-gram counts, instead of the previous server-side configuration (an XML config file referencing serialized.binmodel files), which is no longer supported — existing models must be recreated as dictionaries. Three new functions are added alongsidenaiveBayesClassifier:naiveBayesClassifierWithProbreturns the predicted class together with its probability,naiveBayesClassifierWithAllProbsreturns every class with its probability, andnaiveBayesNgramstokenizes text into n-grams the same way the dictionary does, for building the training data from raw labelled text. Additionally, several performance optimizations have been applied: on a 9.7 MiB code-point trigram language model, it uses ~49× less memory (from 1.84 GiB to 38 MiB), loads ~11× faster, and classifies ~11× faster.