feat(matching): rank with semantic embeddings and explain matches by evidence
This commit is contained in:
1 parent
facc1789b1
commit
24b64d698a
215 files changed
+16951
-327
No files matched your search
@@ -0,0 +1,265 @@
|
||||
-- +goose Up
|
||||
-- Candidate projects: a new list kind of profile claims, a publishable card
|
||||
-- field `projects`, and the stored server check of each linked GitHub
|
||||
-- repository. A project is a claim of trust level 0 like every other; the
|
||||
-- repository check is separate evidence and never changes the claim.
|
||||
|
||||
ALTER TABLE candidate_profile_claims
|
||||
DROP CONSTRAINT candidate_profile_claims_kind_ordinal,
|
||||
ADD CONSTRAINT candidate_profile_claims_kind_ordinal CHECK (
|
||||
(
|
||||
kind IN (
|
||||
'full_name',
|
||||
'contact_email',
|
||||
'contact_phone',
|
||||
'contact_telegram',
|
||||
'terms_location',
|
||||
'terms_relocation',
|
||||
'terms_salary_minimum'
|
||||
)
|
||||
AND ordinal = 0
|
||||
)
|
||||
OR (kind = 'stack' AND ordinal BETWEEN 0 AND 29)
|
||||
OR (kind = 'role' AND ordinal BETWEEN 0 AND 8)
|
||||
OR (kind = 'experience' AND ordinal BETWEEN 0 AND 19)
|
||||
OR (kind = 'soft_skill' AND ordinal BETWEEN 0 AND 9)
|
||||
OR (kind = 'project' AND ordinal BETWEEN 0 AND 9)
|
||||
),
|
||||
-- The bounds repeat ProfileProjectItem of the public contract. The address
|
||||
-- is the canonical https://github.com/<owner>/<name> that the domain stores.
|
||||
ADD CONSTRAINT candidate_profile_claims_project_value CHECK (
|
||||
kind <> 'project' OR (
|
||||
jsonb_typeof(value) = 'object'
|
||||
AND value ?& ARRAY['title', 'description', 'stack', 'repository_url']
|
||||
AND value - 'title' - 'description' - 'stack' - 'repository_url' = '{}'::jsonb
|
||||
AND jsonb_typeof(value -> 'title') = 'string'
|
||||
AND char_length(value ->> 'title') BETWEEN 1 AND 100
|
||||
AND value ->> 'title' !~ '^\s|\s$'
|
||||
AND value ->> 'title' !~ '[[:cntrl:]]'
|
||||
AND (
|
||||
jsonb_typeof(value -> 'description') = 'null'
|
||||
OR (
|
||||
jsonb_typeof(value -> 'description') = 'string'
|
||||
AND char_length(value ->> 'description') BETWEEN 1 AND 500
|
||||
AND value ->> 'description' !~ '^\s|\s$'
|
||||
AND translate(value ->> 'description', E'\n', '') !~ '[[:cntrl:]]'
|
||||
)
|
||||
)
|
||||
AND jsonb_typeof(value -> 'stack') = 'array'
|
||||
AND jsonb_array_length(value -> 'stack') <= 10
|
||||
AND (
|
||||
jsonb_typeof(value -> 'repository_url') = 'null'
|
||||
OR (
|
||||
jsonb_typeof(value -> 'repository_url') = 'string'
|
||||
AND value ->> 'repository_url'
|
||||
~ '^https://github\.com/[A-Za-z0-9][A-Za-z0-9-]{0,38}/[A-Za-z0-9._-]{1,100}$'
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
-- The publishable card fields gain `projects`; saved sets stay valid.
|
||||
-- +goose StatementBegin
|
||||
CREATE OR REPLACE FUNCTION candidate_card_fields_are_canonical(fields text []) RETURNS boolean
|
||||
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
||||
SELECT
|
||||
(cardinality(fields) = 0 OR array_ndims(fields) = 1)
|
||||
AND cardinality(fields) BETWEEN 0 AND 10
|
||||
AND array_position(fields, NULL) IS NULL
|
||||
AND fields <@ ARRAY[
|
||||
'categories', 'experience', 'location', 'projects', 'relocation',
|
||||
'salary_compatibility', 'self_assessment', 'soft_skills',
|
||||
'stack', 'work_formats'
|
||||
]::text []
|
||||
AND fields = ARRAY(
|
||||
SELECT DISTINCT field COLLATE "C"
|
||||
FROM unnest(fields) AS field
|
||||
ORDER BY field COLLATE "C"
|
||||
);
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- A card view can now return the published projects.
|
||||
-- +goose StatementBegin
|
||||
CREATE OR REPLACE FUNCTION candidate_card_access_fields_are_valid(kind text, field_codes text []) RETURNS boolean
|
||||
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
||||
SELECT
|
||||
cardinality(field_codes) BETWEEN 0 AND 7
|
||||
AND array_position(field_codes, NULL) IS NULL
|
||||
AND field_codes = ARRAY(
|
||||
SELECT DISTINCT code COLLATE "C"
|
||||
FROM unnest(field_codes) AS code
|
||||
ORDER BY code COLLATE "C"
|
||||
)
|
||||
AND (
|
||||
(
|
||||
kind = 'card_viewed'
|
||||
AND field_codes <@ ARRAY[
|
||||
'categories', 'experience', 'projects', 'salary_compatibility',
|
||||
'self_assessment', 'stack', 'work_formats'
|
||||
]::text []
|
||||
)
|
||||
OR (
|
||||
kind = 'contact_read'
|
||||
AND cardinality(field_codes) BETWEEN 1 AND 4
|
||||
AND field_codes <@ ARRAY['email', 'full_name', 'phone', 'telegram']::text []
|
||||
)
|
||||
);
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- The latest server check of each repository that a candidate's projects
|
||||
-- link. The key is "<owner>/<name>" in lower case. Fetched metadata is kept
|
||||
-- only while the repository is verified public and is used for ranking only;
|
||||
-- the API never returns it. No token, header, or response body other than the
|
||||
-- bounded fields below is stored.
|
||||
CREATE TABLE candidate_project_repositories (
|
||||
candidate_id uuid NOT NULL,
|
||||
repository_key text NOT NULL,
|
||||
state text NOT NULL,
|
||||
github_id bigint,
|
||||
description text,
|
||||
topics text [] NOT NULL DEFAULT '{}',
|
||||
languages text [] NOT NULL DEFAULT '{}',
|
||||
readme_excerpt text,
|
||||
repository_etag text,
|
||||
readme_etag text,
|
||||
failure_class text,
|
||||
checked_at timestamptz,
|
||||
requested_at timestamptz NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
CONSTRAINT candidate_project_repositories_pkey PRIMARY KEY (candidate_id, repository_key),
|
||||
CONSTRAINT candidate_project_repositories_candidate_fkey FOREIGN KEY (candidate_id)
|
||||
REFERENCES users (id) ON DELETE CASCADE,
|
||||
CONSTRAINT candidate_project_repositories_key_format CHECK (
|
||||
repository_key ~ '^[a-z0-9][a-z0-9-]{0,38}/[a-z0-9._-]{1,100}$'
|
||||
),
|
||||
CONSTRAINT candidate_project_repositories_state_known CHECK (
|
||||
state IN ('pending', 'verified', 'not_found', 'unavailable')
|
||||
),
|
||||
CONSTRAINT candidate_project_repositories_failure_known CHECK (
|
||||
failure_class IS NULL OR failure_class IN (
|
||||
'rate_limited', 'server_error', 'network', 'invalid_response'
|
||||
)
|
||||
),
|
||||
CONSTRAINT candidate_project_repositories_failure_state CHECK (
|
||||
(state = 'unavailable') = (failure_class IS NOT NULL)
|
||||
),
|
||||
CONSTRAINT candidate_project_repositories_checked CHECK (
|
||||
state = 'pending' OR checked_at IS NOT NULL
|
||||
),
|
||||
-- Metadata exists only for a verified public repository.
|
||||
CONSTRAINT candidate_project_repositories_evidence_verified CHECK (
|
||||
state = 'verified' OR (
|
||||
github_id IS NULL
|
||||
AND description IS NULL
|
||||
AND readme_excerpt IS NULL
|
||||
AND cardinality(topics) = 0
|
||||
AND cardinality(languages) = 0
|
||||
)
|
||||
),
|
||||
CONSTRAINT candidate_project_repositories_github_id CHECK (
|
||||
github_id IS NULL OR github_id > 0
|
||||
),
|
||||
CONSTRAINT candidate_project_repositories_bounds CHECK (
|
||||
(description IS NULL OR char_length(description) <= 1000)
|
||||
AND (readme_excerpt IS NULL OR char_length(readme_excerpt) <= 4000)
|
||||
AND cardinality(topics) <= 20
|
||||
AND cardinality(languages) <= 10
|
||||
AND (repository_etag IS NULL OR char_length(repository_etag) <= 200)
|
||||
AND (readme_etag IS NULL OR char_length(readme_etag) <= 200)
|
||||
),
|
||||
CONSTRAINT candidate_project_repositories_times CHECK (
|
||||
updated_at >= created_at
|
||||
)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
-- Refuse to strand stored projects or published project fields outside the
|
||||
-- older contract; remove them through the product first.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM candidate_profile_claims WHERE kind = 'project')
|
||||
OR EXISTS (SELECT 1 FROM candidate_publications WHERE 'projects' = ANY (fields))
|
||||
OR EXISTS (SELECT 1 FROM candidate_privacy_events WHERE 'projects' = ANY (fields))
|
||||
OR EXISTS (SELECT 1 FROM candidate_card_access_events WHERE 'projects' = ANY (field_codes))
|
||||
THEN
|
||||
RAISE EXCEPTION 'candidate projects are still stored or published';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
DROP TABLE candidate_project_repositories;
|
||||
|
||||
-- +goose StatementBegin
|
||||
CREATE OR REPLACE FUNCTION candidate_card_access_fields_are_valid(kind text, field_codes text []) RETURNS boolean
|
||||
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
||||
SELECT
|
||||
cardinality(field_codes) BETWEEN 0 AND 6
|
||||
AND array_position(field_codes, NULL) IS NULL
|
||||
AND field_codes = ARRAY(
|
||||
SELECT DISTINCT code COLLATE "C"
|
||||
FROM unnest(field_codes) AS code
|
||||
ORDER BY code COLLATE "C"
|
||||
)
|
||||
AND (
|
||||
(
|
||||
kind = 'card_viewed'
|
||||
AND field_codes <@ ARRAY[
|
||||
'categories', 'experience', 'salary_compatibility',
|
||||
'self_assessment', 'stack', 'work_formats'
|
||||
]::text []
|
||||
)
|
||||
OR (
|
||||
kind = 'contact_read'
|
||||
AND cardinality(field_codes) BETWEEN 1 AND 4
|
||||
AND field_codes <@ ARRAY['email', 'full_name', 'phone', 'telegram']::text []
|
||||
)
|
||||
);
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose StatementBegin
|
||||
CREATE OR REPLACE FUNCTION candidate_card_fields_are_canonical(fields text []) RETURNS boolean
|
||||
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
||||
SELECT
|
||||
(cardinality(fields) = 0 OR array_ndims(fields) = 1)
|
||||
AND cardinality(fields) BETWEEN 0 AND 9
|
||||
AND array_position(fields, NULL) IS NULL
|
||||
AND fields <@ ARRAY[
|
||||
'categories', 'experience', 'location', 'relocation',
|
||||
'salary_compatibility', 'self_assessment', 'soft_skills',
|
||||
'stack', 'work_formats'
|
||||
]::text []
|
||||
AND fields = ARRAY(
|
||||
SELECT DISTINCT field COLLATE "C"
|
||||
FROM unnest(fields) AS field
|
||||
ORDER BY field COLLATE "C"
|
||||
);
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
ALTER TABLE candidate_profile_claims
|
||||
DROP CONSTRAINT candidate_profile_claims_project_value,
|
||||
DROP CONSTRAINT candidate_profile_claims_kind_ordinal,
|
||||
ADD CONSTRAINT candidate_profile_claims_kind_ordinal CHECK (
|
||||
(
|
||||
kind IN (
|
||||
'full_name',
|
||||
'contact_email',
|
||||
'contact_phone',
|
||||
'contact_telegram',
|
||||
'terms_location',
|
||||
'terms_relocation',
|
||||
'terms_salary_minimum'
|
||||
)
|
||||
AND ordinal = 0
|
||||
)
|
||||
OR (kind = 'stack' AND ordinal BETWEEN 0 AND 29)
|
||||
OR (kind = 'role' AND ordinal BETWEEN 0 AND 8)
|
||||
OR (kind = 'experience' AND ordinal BETWEEN 0 AND 19)
|
||||
OR (kind = 'soft_skill' AND ordinal BETWEEN 0 AND 9)
|
||||
);
|
||||
@@ -0,0 +1,64 @@
|
||||
-- +goose Up
|
||||
-- Stored embedding vectors of candidate cards and need snapshots. A row is
|
||||
-- one text of a subject (the whole view or one item) encoded by one model,
|
||||
-- revision, and prompt recipe; content_sha256 is the digest of the rendered
|
||||
-- text, so a stale row is never mistaken for the current one. The vector is
|
||||
-- little-endian float32: exact, platform independent, 4 KiB for 1024
|
||||
-- dimensions. Derived data follows its subject: an account or a need that
|
||||
-- is deleted takes its vectors with it.
|
||||
CREATE TABLE semantic_vectors (
|
||||
subject_kind text NOT NULL,
|
||||
subject_id uuid NOT NULL,
|
||||
candidate_id uuid,
|
||||
need_id uuid,
|
||||
model_id text NOT NULL,
|
||||
model_revision text NOT NULL,
|
||||
prompt_version text NOT NULL,
|
||||
item_key text NOT NULL,
|
||||
content_sha256 bytea NOT NULL,
|
||||
label_sha256 bytea,
|
||||
dimension integer NOT NULL,
|
||||
vector bytea NOT NULL,
|
||||
computed_at timestamptz NOT NULL,
|
||||
CONSTRAINT semantic_vectors_pkey PRIMARY KEY (
|
||||
subject_kind, subject_id, model_id, model_revision, prompt_version, item_key, content_sha256
|
||||
),
|
||||
CONSTRAINT semantic_vectors_candidate_fkey FOREIGN KEY (candidate_id)
|
||||
REFERENCES users (id) ON DELETE CASCADE,
|
||||
CONSTRAINT semantic_vectors_need_fkey FOREIGN KEY (need_id)
|
||||
REFERENCES organization_needs (id) ON DELETE CASCADE,
|
||||
CONSTRAINT semantic_vectors_subject CHECK (
|
||||
(subject_kind = 'candidate' AND candidate_id = subject_id AND need_id IS NULL)
|
||||
OR (subject_kind = 'need' AND need_id = subject_id AND candidate_id IS NULL)
|
||||
),
|
||||
CONSTRAINT semantic_vectors_model_format CHECK (
|
||||
model_id ~ '^[A-Za-z0-9._/-]{1,200}$'
|
||||
AND model_revision ~ '^[A-Za-z0-9._/-]{1,200}$'
|
||||
AND prompt_version ~ '^[A-Za-z0-9._/-]{1,200}$'
|
||||
),
|
||||
CONSTRAINT semantic_vectors_item_key_format CHECK (
|
||||
item_key ~ '^[a-z_]{1,32}(:[0-9]{1,3})?$'
|
||||
),
|
||||
CONSTRAINT semantic_vectors_digests CHECK (
|
||||
octet_length(content_sha256) = 32
|
||||
AND (label_sha256 IS NULL OR octet_length(label_sha256) = 32)
|
||||
),
|
||||
CONSTRAINT semantic_vectors_dimension CHECK (
|
||||
dimension BETWEEN 1 AND 4096 AND octet_length(vector) = 4 * dimension
|
||||
)
|
||||
);
|
||||
|
||||
-- The search reads the whole-view vector of many candidates for one model.
|
||||
CREATE INDEX semantic_vectors_candidate_whole_idx
|
||||
ON semantic_vectors (model_id, model_revision, prompt_version, candidate_id)
|
||||
WHERE subject_kind = 'candidate' AND item_key = 'whole';
|
||||
|
||||
-- Account and need deletion find the rows of their subject through these.
|
||||
CREATE INDEX semantic_vectors_candidate_idx ON semantic_vectors (candidate_id)
|
||||
WHERE candidate_id IS NOT NULL;
|
||||
CREATE INDEX semantic_vectors_need_idx ON semantic_vectors (need_id)
|
||||
WHERE need_id IS NOT NULL;
|
||||
|
||||
-- +goose Down
|
||||
-- The vectors are derived data: the backfill command computes them again.
|
||||
DROP TABLE semantic_vectors;
|
||||
@@ -125,7 +125,7 @@ func TestOnlyTheSettingsQueriesReadThePublicationTableDirectly(t *testing.T) {
|
||||
for _, required := range []string{
|
||||
"from candidate_effective_publications as effective",
|
||||
"where effective.user_id = sqlc.arg(candidate_id)",
|
||||
"claim.kind in ('stack', 'role', 'experience')",
|
||||
"claim.kind in ('stack', 'role', 'experience', 'project')",
|
||||
"coalesce(questionnaire.work_formats, '{}'::text[])",
|
||||
} {
|
||||
if !strings.Contains(previewQuery, required) {
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
-- usecase has already authorized the active organization membership. This
|
||||
-- single statement snapshot filters both revoked/unpublished C2 states and
|
||||
-- candidates hidden from this organization before any profile value is read.
|
||||
-- The projection reads only stack, declared role, and experience dates. It
|
||||
-- deliberately excludes names, contacts, salary, location, soft skills,
|
||||
-- employer names, summaries, and imported excerpts. The only assessment data
|
||||
-- The projection reads only stack, declared role, experience dates, and
|
||||
-- projects with the keys of their verified repositories. It deliberately
|
||||
-- excludes names, contacts, salary, location, soft skills, employer names,
|
||||
-- summaries, and imported excerpts; the repository address of a project stays
|
||||
-- in the private input and never reaches the card. The only assessment data
|
||||
-- is the current category (specialization and grade) of the separate current
|
||||
-- category table; it carries no attempt, score, review, policy, or history.
|
||||
-- name: ListVisibleCandidateViewSources :many
|
||||
@@ -64,11 +66,21 @@ SELECT
|
||||
WHERE category.candidate_id = effective.user_id
|
||||
),
|
||||
'[]'::text
|
||||
) AS confirmed_categories
|
||||
) AS confirmed_categories,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT array_agg(repository.repository_key ORDER BY repository.repository_key)
|
||||
FROM candidate_project_repositories AS repository
|
||||
WHERE
|
||||
repository.candidate_id = effective.user_id
|
||||
AND repository.state = 'verified'
|
||||
),
|
||||
'{}'::text []
|
||||
)::text [] AS verified_repositories
|
||||
FROM candidate_effective_publications AS effective
|
||||
LEFT JOIN candidate_profile_claims AS claim
|
||||
ON claim.user_id = effective.user_id
|
||||
AND claim.kind IN ('stack', 'role', 'experience')
|
||||
AND claim.kind IN ('stack', 'role', 'experience', 'project')
|
||||
LEFT JOIN candidate_questionnaires AS questionnaire
|
||||
ON questionnaire.user_id = effective.user_id
|
||||
WHERE NOT EXISTS (
|
||||
@@ -202,11 +214,21 @@ SELECT
|
||||
WHERE category.candidate_id = effective.user_id
|
||||
),
|
||||
'[]'::text
|
||||
) AS confirmed_categories
|
||||
) AS confirmed_categories,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT array_agg(repository.repository_key ORDER BY repository.repository_key)
|
||||
FROM candidate_project_repositories AS repository
|
||||
WHERE
|
||||
repository.candidate_id = effective.user_id
|
||||
AND repository.state = 'verified'
|
||||
),
|
||||
'{}'::text []
|
||||
)::text [] AS verified_repositories
|
||||
FROM candidate_effective_publications AS effective
|
||||
LEFT JOIN candidate_profile_claims AS claim
|
||||
ON claim.user_id = effective.user_id
|
||||
AND claim.kind IN ('stack', 'role', 'experience')
|
||||
AND claim.kind IN ('stack', 'role', 'experience', 'project')
|
||||
LEFT JOIN candidate_questionnaires AS questionnaire
|
||||
ON questionnaire.user_id = effective.user_id
|
||||
WHERE effective.user_id = sqlc.arg(candidate_id)
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
-- Server checks of the GitHub repositories that candidate projects link.
|
||||
-- Every statement is scoped by the candidate.
|
||||
|
||||
-- name: ListCandidateProjectRepositories :many
|
||||
SELECT
|
||||
candidate_id, repository_key, state, github_id, description, topics,
|
||||
languages, readme_excerpt, repository_etag, readme_etag, failure_class,
|
||||
checked_at, requested_at, created_at, updated_at
|
||||
FROM candidate_project_repositories
|
||||
WHERE candidate_id = sqlc.arg(candidate_id)
|
||||
ORDER BY repository_key;
|
||||
|
||||
-- name: GetCandidateProjectRepositoryForUpdate :one
|
||||
SELECT
|
||||
candidate_id, repository_key, state, github_id, description, topics,
|
||||
languages, readme_excerpt, repository_etag, readme_etag, failure_class,
|
||||
checked_at, requested_at, created_at, updated_at
|
||||
FROM candidate_project_repositories
|
||||
WHERE candidate_id = sqlc.arg(candidate_id) AND repository_key = sqlc.arg(repository_key)
|
||||
FOR UPDATE;
|
||||
|
||||
-- A new repository starts pending; a known one only records the request.
|
||||
-- name: RequestCandidateProjectRepositoryCheck :exec
|
||||
INSERT INTO candidate_project_repositories (
|
||||
candidate_id, repository_key, state, requested_at, created_at, updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(candidate_id), sqlc.arg(repository_key), 'pending', sqlc.arg(requested_at),
|
||||
sqlc.arg(requested_at), sqlc.arg(requested_at)
|
||||
)
|
||||
ON CONFLICT (candidate_id, repository_key) DO UPDATE
|
||||
SET
|
||||
requested_at = excluded.requested_at,
|
||||
updated_at = GREATEST(candidate_project_repositories.updated_at, excluded.updated_at);
|
||||
|
||||
-- name: SaveCandidateProjectRepositoryCheck :exec
|
||||
UPDATE candidate_project_repositories
|
||||
SET
|
||||
state = sqlc.arg(state),
|
||||
github_id = sqlc.narg(github_id),
|
||||
description = sqlc.narg(description),
|
||||
topics = sqlc.arg(topics),
|
||||
languages = sqlc.arg(languages),
|
||||
readme_excerpt = sqlc.narg(readme_excerpt),
|
||||
repository_etag = sqlc.narg(repository_etag),
|
||||
readme_etag = sqlc.narg(readme_etag),
|
||||
failure_class = sqlc.narg(failure_class),
|
||||
checked_at = sqlc.arg(checked_at),
|
||||
updated_at = GREATEST(updated_at, sqlc.arg(checked_at))
|
||||
WHERE candidate_id = sqlc.arg(candidate_id) AND repository_key = sqlc.arg(repository_key);
|
||||
|
||||
-- Removes the checks of repositories that no project links any more.
|
||||
-- name: DeleteUnlinkedCandidateProjectRepositories :exec
|
||||
DELETE FROM candidate_project_repositories
|
||||
WHERE
|
||||
candidate_id = sqlc.arg(candidate_id)
|
||||
AND NOT (repository_key = ANY(sqlc.arg(linked_keys)::text []));
|
||||
|
||||
-- The latest check request of a candidate, for the per-candidate limit.
|
||||
-- name: GetLatestRepositoryCheckRequest :one
|
||||
SELECT MAX(requested_at)::timestamptz AS requested_at
|
||||
FROM candidate_project_repositories
|
||||
WHERE candidate_id = sqlc.arg(candidate_id);
|
||||
@@ -0,0 +1,110 @@
|
||||
-- Stored embedding vectors. A candidate keeps one set of rows for a model:
|
||||
-- the job deletes every other row of the candidate and model before it
|
||||
-- inserts the new set, in one transaction. A need keeps the rows of its
|
||||
-- latest computed requirements snapshot the same way.
|
||||
|
||||
-- name: DeleteSemanticVectorsOfModel :exec
|
||||
DELETE FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = sqlc.arg(subject_kind)
|
||||
AND subject_id = sqlc.arg(subject_id)
|
||||
AND model_id = sqlc.arg(model_id)
|
||||
AND model_revision = sqlc.arg(model_revision)
|
||||
AND prompt_version = sqlc.arg(prompt_version);
|
||||
|
||||
-- Removes every vector of a subject, of any model: a closed card or a need
|
||||
-- without requirements keeps no derived data.
|
||||
-- name: DeleteSemanticVectorsOfSubject :exec
|
||||
DELETE FROM semantic_vectors
|
||||
WHERE subject_kind = sqlc.arg(subject_kind) AND subject_id = sqlc.arg(subject_id);
|
||||
|
||||
-- name: InsertSemanticVector :exec
|
||||
INSERT INTO semantic_vectors (
|
||||
subject_kind, subject_id, candidate_id, need_id, model_id, model_revision,
|
||||
prompt_version, item_key, content_sha256, label_sha256, dimension, vector,
|
||||
computed_at
|
||||
) VALUES (
|
||||
sqlc.arg(subject_kind), sqlc.arg(subject_id), sqlc.narg(candidate_id),
|
||||
sqlc.narg(need_id), sqlc.arg(model_id), sqlc.arg(model_revision),
|
||||
sqlc.arg(prompt_version), sqlc.arg(item_key), sqlc.arg(content_sha256),
|
||||
sqlc.narg(label_sha256), sqlc.arg(dimension), sqlc.arg(vector),
|
||||
sqlc.arg(computed_at)
|
||||
);
|
||||
|
||||
-- The digests of the stored rows of a subject and model, for the job that
|
||||
-- decides whether anything changed.
|
||||
-- name: ListSemanticVectorDigests :many
|
||||
SELECT item_key, content_sha256
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = sqlc.arg(subject_kind)
|
||||
AND subject_id = sqlc.arg(subject_id)
|
||||
AND model_id = sqlc.arg(model_id)
|
||||
AND model_revision = sqlc.arg(model_revision)
|
||||
AND prompt_version = sqlc.arg(prompt_version)
|
||||
ORDER BY item_key, content_sha256;
|
||||
|
||||
-- The whole-view vectors of the candidates of one search.
|
||||
-- name: ListCandidateWholeVectors :many
|
||||
SELECT candidate_id, dimension, vector
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = 'candidate'
|
||||
AND item_key = 'whole'
|
||||
AND model_id = sqlc.arg(model_id)
|
||||
AND model_revision = sqlc.arg(model_revision)
|
||||
AND prompt_version = sqlc.arg(prompt_version)
|
||||
AND candidate_id = ANY(sqlc.arg(candidate_ids)::uuid [])
|
||||
ORDER BY candidate_id;
|
||||
|
||||
-- The item vectors of the candidates that a search returns, at most a few
|
||||
-- dozen candidates.
|
||||
-- name: ListCandidateItemVectors :many
|
||||
SELECT candidate_id, item_key, label_sha256, dimension, vector
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = 'candidate'
|
||||
AND item_key <> 'whole'
|
||||
AND model_id = sqlc.arg(model_id)
|
||||
AND model_revision = sqlc.arg(model_revision)
|
||||
AND prompt_version = sqlc.arg(prompt_version)
|
||||
AND candidate_id = ANY(sqlc.arg(candidate_ids)::uuid [])
|
||||
ORDER BY candidate_id, item_key;
|
||||
|
||||
-- The vectors of a need whose texts match the given digests of the
|
||||
-- requirements snapshot that the search renders.
|
||||
-- name: ListNeedVectors :many
|
||||
SELECT item_key, content_sha256, dimension, vector
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = 'need'
|
||||
AND need_id = sqlc.arg(need_id)
|
||||
AND model_id = sqlc.arg(model_id)
|
||||
AND model_revision = sqlc.arg(model_revision)
|
||||
AND prompt_version = sqlc.arg(prompt_version)
|
||||
AND content_sha256 = ANY(sqlc.arg(content_digests)::bytea [])
|
||||
ORDER BY item_key;
|
||||
|
||||
-- Candidates with a saved profile for the backfill, in identifier order
|
||||
-- after a cursor. The job of a candidate who is not published removes the
|
||||
-- stored vectors, so a backfill also cleans up after a closed card.
|
||||
-- name: ListProfileCandidatesAfter :many
|
||||
SELECT profiles.user_id
|
||||
FROM candidate_profiles AS profiles
|
||||
WHERE profiles.user_id > sqlc.arg(after_id)
|
||||
ORDER BY profiles.user_id
|
||||
LIMIT sqlc.arg(page_size);
|
||||
|
||||
-- Needs and their current requirements snapshot for the backfill.
|
||||
-- name: ListNeedsAfter :many
|
||||
SELECT id, organization_id, requirements_version
|
||||
FROM organization_needs
|
||||
WHERE id > sqlc.arg(after_id)
|
||||
ORDER BY id
|
||||
LIMIT sqlc.arg(page_size);
|
||||
|
||||
-- Serializes the vector jobs of one subject: a job takes this lock before it
|
||||
-- reads the subject, so the job that runs last also read last and its vectors
|
||||
-- describe the latest committed state. User requests never take it.
|
||||
-- name: LockSemanticSubject :exec
|
||||
SELECT pg_advisory_xact_lock(sqlc.arg(lock_key)::bigint);
|
||||
Reference in new issue
Block a user