111 lines
4.3 KiB
SQL
111 lines
4.3 KiB
SQL
-- 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);
|