1
0
mirror of https://github.com/tensorchord/pgvecto.rs.git synced 2025-04-18 21:44:00 +03:00

feat: make schema mutable in compile time (#493)

Signed-off-by: usamoi <usamoi@outlook.com>
This commit is contained in:
usamoi 2024-06-03 15:52:52 +08:00 committed by GitHub
parent 71035b5867
commit 0a5b0daf55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 40 additions and 13 deletions

1
.schema Normal file
View File

@ -0,0 +1 @@
vectors

View File

@ -144,7 +144,8 @@ impl IntoDatum for Vecf32AggregateAvgStype<'_> {
}
fn type_oid() -> Oid {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let t = pgrx::pg_catalog::PgType::search_typenamensp(
c"_vectors_vecf32_aggregate_avg_stype",

View File

@ -143,7 +143,8 @@ impl IntoDatum for BVecf32Output {
}
fn type_oid() -> Oid {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let t = pgrx::pg_catalog::PgType::search_typenamensp(c"bvector", namespace.oid()).unwrap();
let t = t.get().expect("pg_catalog is broken.");

View File

@ -159,7 +159,8 @@ impl IntoDatum for SVecf32Output {
}
fn type_oid() -> Oid {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let t = pgrx::pg_catalog::PgType::search_typenamensp(c"svector", namespace.oid()).unwrap();
let t = t.get().expect("pg_catalog is broken.");

View File

@ -136,7 +136,8 @@ impl IntoDatum for Vecf16Output {
}
fn type_oid() -> Oid {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let t = pgrx::pg_catalog::PgType::search_typenamensp(c"vecf16", namespace.oid()).unwrap();
let t = t.get().expect("pg_catalog is broken.");

View File

@ -136,7 +136,8 @@ impl IntoDatum for Vecf32Output {
}
fn type_oid() -> Oid {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let t = pgrx::pg_catalog::PgType::search_typenamensp(c"vector", namespace.oid()).unwrap();
let t = t.get().expect("pg_catalog is broken.");

View File

@ -198,7 +198,8 @@ impl IntoDatum for Veci8Output {
}
fn type_oid() -> Oid {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let t = pgrx::pg_catalog::PgType::search_typenamensp(c"veci8", namespace.oid()).unwrap();
let t = t.get().expect("pg_catalog is broken.");

View File

@ -30,7 +30,8 @@ impl Reloption {
}
pub fn convert_opclass_to_vd(opclass_oid: pgrx::pg_sys::Oid) -> Option<(VectorKind, DistanceKind)> {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let opclass = pgrx::pg_catalog::PgOpclass::search_claoid(opclass_oid).unwrap();
let opclass = opclass.get().expect("pg_catalog is broken.");
@ -47,7 +48,8 @@ pub fn convert_opclass_to_vd(opclass_oid: pgrx::pg_sys::Oid) -> Option<(VectorKi
pub fn convert_opfamily_to_vd(
opfamily_oid: pgrx::pg_sys::Oid,
) -> Option<(VectorKind, DistanceKind)> {
let namespace = pgrx::pg_catalog::PgNamespace::search_namespacename(c"vectors").unwrap();
let namespace =
pgrx::pg_catalog::PgNamespace::search_namespacename(crate::SCHEMA_C_STR).unwrap();
let namespace = namespace.get().expect("pgvecto.rs is not installed.");
let opfamily = pgrx::pg_catalog::PgOpfamily::search_opfamilyoid(opfamily_oid).unwrap();
let opfamily = opfamily.get().expect("pg_catalog is broken.");

View File

@ -120,7 +120,7 @@ fn check_vector_index(pg_class: pgrx::pg_catalog::PgClass<'_>) -> Option<()> {
}
let search = pgrx::pg_catalog::PgAm::search_amoid(relam)?;
let pg_am = search.get()?;
if pg_am.amname() != c"vectors" {
if pg_am.amname() != crate::SCHEMA_C_STR {
return None;
}
// probably a vector index, so enter a slow path to ensure it

View File

@ -17,10 +17,11 @@ fn _vectors_alter_vector_index(oid: pgrx::pg_sys::Oid, key: String, value: Strin
#[pgrx::pg_extern(volatile, strict, parallel_safe)]
fn _vectors_index_stat(
oid: pgrx::pg_sys::Oid,
) -> pgrx::composite_type!('static, "vectors.vector_index_stat") {
) -> pgrx::composite_type!('static, "vector_index_stat") {
use pgrx::heap_tuple::PgHeapTuple;
let handle = from_oid_to_handle(oid);
let mut res = PgHeapTuple::new_composite_type("vectors.vector_index_stat").unwrap();
let type_name = crate::SCHEMA.to_string() + ".vector_index_stat";
let mut res = PgHeapTuple::new_composite_type(&type_name).unwrap();
let mut rpc = check_client(client());
let stat = rpc.stat(handle);
match stat {

View File

@ -44,3 +44,20 @@ compile_error!("Target is not supported.");
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
const SCHEMA: &str = include_str!("../.schema");
const SCHEMA_C_BYTES: [u8; SCHEMA.len() + 1] = {
let mut bytes = [0u8; SCHEMA.len() + 1];
let mut i = 0_usize;
while i < SCHEMA.len() {
bytes[i] = SCHEMA.as_bytes()[i];
i += 1;
}
bytes
};
const SCHEMA_C_STR: &std::ffi::CStr = match std::ffi::CStr::from_bytes_with_nul(&SCHEMA_C_BYTES) {
Ok(x) => x,
Err(_) => panic!("there are null characters in schema"),
};

View File

@ -615,9 +615,9 @@ CREATE FUNCTION text2vec_openai_v3(input TEXT) RETURNS vector
STRICT PARALLEL SAFE LANGUAGE plpgsql AS
$$
DECLARE
variable vectors.vector;
variable vector;
BEGIN
variable := vectors.text2vec_openai(input, 'text-embedding-3-small');
variable := text2vec_openai(input, 'text-embedding-3-small');
RETURN variable;
END;
$$;