-
Notifications
You must be signed in to change notification settings - Fork 2
Expose count_tokens() and add refresh_token_counts() helper #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
syedkazim110
wants to merge
4
commits into
pgEdge:main
Choose a base branch
from
syedkazim110:feature/count-tokens-expose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d68eaa9
Expose count_tokens() and add refresh_token_counts() helper
syedkazim110 eeea965
Use count_tokens() on all chunk write paths for consistent token_count
syedkazim110 eb41ada
Skip no-op rewrites in refresh_token_counts() and tighten test assert…
syedkazim110 74f9073
Add PG_FUNCTION_INFO_V1 wrapper for count_tokens SQL callable
syedkazim110 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| -- count_tokens.sql | ||
| -- Regression tests for count_tokens() and refresh_token_counts(). | ||
| --------------------------------------------------------------------------- | ||
| -- Test 1: count_tokens() is registered | ||
| --------------------------------------------------------------------------- | ||
| SELECT proname | ||
| FROM pg_proc | ||
| WHERE proname = 'count_tokens' | ||
| AND pronamespace = ( | ||
| SELECT oid FROM pg_namespace WHERE nspname = 'pgedge_vectorizer' | ||
| ); | ||
| proname | ||
| -------------- | ||
| count_tokens | ||
| (1 row) | ||
|
|
||
| --------------------------------------------------------------------------- | ||
| -- Test 2: count_tokens() approximation values | ||
| --------------------------------------------------------------------------- | ||
| -- 'hello world' = 11 chars, (11+3)/4 = 3 | ||
| SELECT pgedge_vectorizer.count_tokens('hello world') AS tokens; | ||
| tokens | ||
| -------- | ||
| 3 | ||
| (1 row) | ||
|
|
||
| -- empty string returns 0 | ||
| SELECT pgedge_vectorizer.count_tokens('') AS tokens; | ||
| tokens | ||
| -------- | ||
| 0 | ||
| (1 row) | ||
|
|
||
| -- 'test' = 4 chars, (4+3)/4 = 1 | ||
| SELECT pgedge_vectorizer.count_tokens('test') AS tokens; | ||
| tokens | ||
| -------- | ||
| 1 | ||
| (1 row) | ||
|
|
||
| -- NULL returns NULL (STRICT function) | ||
| SELECT pgedge_vectorizer.count_tokens(NULL) IS NULL AS is_null; | ||
| is_null | ||
| --------- | ||
| t | ||
| (1 row) | ||
|
|
||
| -- '你好世界' = 4 UTF-8 characters, not 12 bytes; (4+3)/4 = 1 | ||
| SELECT pgedge_vectorizer.count_tokens('你好世界') AS tokens; | ||
| tokens | ||
| -------- | ||
| 1 | ||
| (1 row) | ||
|
|
||
| --------------------------------------------------------------------------- | ||
| -- Test 3: refresh_token_counts() is registered | ||
| --------------------------------------------------------------------------- | ||
| SELECT proname | ||
| FROM pg_proc | ||
| WHERE proname = 'refresh_token_counts' | ||
| AND pronamespace = ( | ||
| SELECT oid FROM pg_namespace WHERE nspname = 'pgedge_vectorizer' | ||
| ); | ||
| proname | ||
| ---------------------- | ||
| refresh_token_counts | ||
| (1 row) | ||
|
|
||
| --------------------------------------------------------------------------- | ||
| -- Test 4: refresh_token_counts() updates rows and returns the count | ||
| --------------------------------------------------------------------------- | ||
| CREATE TABLE count_tokens_refresh_test ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| content TEXT | ||
| ); | ||
| INSERT INTO count_tokens_refresh_test (content) | ||
| VALUES ('Refresh test document one.'), | ||
| ('Refresh test document two.'); | ||
| SELECT pgedge_vectorizer.enable_vectorization( | ||
| 'count_tokens_refresh_test'::regclass, | ||
| 'content', | ||
| 'token_based', | ||
| 100, | ||
| 10, | ||
| 1536 | ||
| ); | ||
| NOTICE: Using primary key column: id (bigint) | ||
| NOTICE: column "sparse_embedding" of relation "count_tokens_refresh_test_content_chunks" already exists, skipping | ||
| NOTICE: Vectorization enabled: count_tokens_refresh_test -> count_tokens_refresh_test_content_chunks | ||
| NOTICE: Strategy: token_based, chunk_size: 100, overlap: 10 | ||
| NOTICE: Processing existing rows... | ||
| NOTICE: Processed 2 existing rows | ||
| enable_vectorization | ||
| ---------------------- | ||
|
|
||
| (1 row) | ||
|
|
||
| -- Force token_count to 0 to simulate stale values | ||
| UPDATE count_tokens_refresh_test_content_chunks SET token_count = 0; | ||
| SELECT COUNT(*) AS zeroed | ||
| FROM count_tokens_refresh_test_content_chunks | ||
| WHERE token_count = 0; | ||
| zeroed | ||
| -------- | ||
| 2 | ||
| (1 row) | ||
|
|
||
| -- refresh_token_counts() should update all rows and return the exact count | ||
| SELECT pgedge_vectorizer.refresh_token_counts( | ||
| 'count_tokens_refresh_test_content_chunks'::regclass | ||
| ) = ( | ||
| SELECT COUNT(*) FROM count_tokens_refresh_test_content_chunks | ||
| ) AS refresh_returned_count; | ||
| NOTICE: refresh_token_counts: updated 2 rows in count_tokens_refresh_test_content_chunks | ||
| refresh_returned_count | ||
| ------------------------ | ||
| t | ||
| (1 row) | ||
|
|
||
| -- All token_counts should now be positive | ||
| SELECT COALESCE(BOOL_AND(token_count > 0), false) AS all_refreshed | ||
| FROM count_tokens_refresh_test_content_chunks; | ||
| all_refreshed | ||
| --------------- | ||
| t | ||
| (1 row) | ||
|
|
||
| -- Cleanup | ||
| SELECT pgedge_vectorizer.disable_vectorization( | ||
| 'count_tokens_refresh_test'::regclass, 'content', true | ||
| ); | ||
| NOTICE: Vectorization disabled and chunk table dropped: count_tokens_refresh_test_content_chunks | ||
| disable_vectorization | ||
| ----------------------- | ||
|
|
||
| (1 row) | ||
|
|
||
| DROP TABLE count_tokens_refresh_test; | ||
| --------------------------------------------------------------------------- | ||
| -- Test 5: refresh_token_counts() works with a schema-qualified table | ||
| --------------------------------------------------------------------------- | ||
| CREATE SCHEMA count_tokens_ns_test; | ||
| CREATE TABLE count_tokens_ns_test.ns_chunks ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| source_id BIGINT NOT NULL DEFAULT 1, | ||
| content TEXT NOT NULL, | ||
| token_count INT | ||
| ); | ||
| INSERT INTO count_tokens_ns_test.ns_chunks (content) | ||
| VALUES ('Schema-qualified refresh test.'), | ||
| ('Another row for schema test.'); | ||
| UPDATE count_tokens_ns_test.ns_chunks SET token_count = 0; | ||
| SELECT COUNT(*) AS zeroed | ||
| FROM count_tokens_ns_test.ns_chunks | ||
| WHERE token_count = 0; | ||
| zeroed | ||
| -------- | ||
| 2 | ||
| (1 row) | ||
|
|
||
| SELECT pgedge_vectorizer.refresh_token_counts( | ||
| 'count_tokens_ns_test.ns_chunks'::regclass | ||
| ) = ( | ||
| SELECT COUNT(*) FROM count_tokens_ns_test.ns_chunks | ||
| ) AS refresh_returned_count; | ||
| NOTICE: refresh_token_counts: updated 2 rows in count_tokens_ns_test.ns_chunks | ||
| refresh_returned_count | ||
| ------------------------ | ||
| t | ||
| (1 row) | ||
|
|
||
| SELECT COALESCE(BOOL_AND(token_count > 0), false) AS all_refreshed | ||
| FROM count_tokens_ns_test.ns_chunks; | ||
| all_refreshed | ||
| --------------- | ||
| t | ||
| (1 row) | ||
|
|
||
| -- Cleanup | ||
| DROP TABLE count_tokens_ns_test.ns_chunks; | ||
| DROP SCHEMA count_tokens_ns_test; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.