Skip to content

Commit

Permalink
chore: stable schema generation (#254)
Browse files Browse the repository at this point in the history
Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi committed Jan 11, 2024
1 parent 4bdf192 commit 97ebbf7
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 219 deletions.
8 changes: 6 additions & 2 deletions src/datatype/casts_f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::datatype::vecf32::{Vecf32, Vecf32Input, Vecf32Output};
use service::prelude::*;

#[pgrx::pg_extern(immutable, parallel_safe, strict)]
fn vecf32_cast_array_to_vector(
fn _vectors_cast_array_to_vecf32(
array: pgrx::Array<f32>,
typmod: i32,
_explicit: bool,
Expand All @@ -21,6 +21,10 @@ fn vecf32_cast_array_to_vector(
}

#[pgrx::pg_extern(immutable, parallel_safe, strict)]
fn vecf32_cast_vector_to_array(vector: Vecf32Input<'_>, _typmod: i32, _explicit: bool) -> Vec<f32> {
fn _vectors_cast_vecf32_to_array(
vector: Vecf32Input<'_>,
_typmod: i32,
_explicit: bool,
) -> Vec<f32> {
vector.data().iter().map(|x| x.to_f32()).collect()
}
83 changes: 22 additions & 61 deletions src/datatype/operators_f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use crate::prelude::*;
use service::prelude::*;
use std::ops::Deref;

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(+)]
#[pgrx::commutator(+)]
fn vecf16_operator_add(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> Vecf16Output {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_add(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> Vecf16Output {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -22,9 +20,8 @@ fn vecf16_operator_add(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> Vecf16Outp
Vecf16::new_in_postgres(&v)
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(-)]
fn vecf16_operator_minus(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> Vecf16Output {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_minus(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> Vecf16Output {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -40,13 +37,8 @@ fn vecf16_operator_minus(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> Vecf16Ou
Vecf16::new_in_postgres(&v)
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(<)]
#[pgrx::negator(>=)]
#[pgrx::commutator(>)]
#[pgrx::restrict(scalarltsel)]
#[pgrx::join(scalarltjoinsel)]
fn vecf16_operator_lt(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_lt(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -57,13 +49,8 @@ fn vecf16_operator_lt(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
lhs.deref() < rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(<=)]
#[pgrx::negator(>)]
#[pgrx::commutator(>=)]
#[pgrx::restrict(scalarltsel)]
#[pgrx::join(scalarltjoinsel)]
fn vecf16_operator_lte(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_lte(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -74,13 +61,8 @@ fn vecf16_operator_lte(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
lhs.deref() <= rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(>)]
#[pgrx::negator(<=)]
#[pgrx::commutator(<)]
#[pgrx::restrict(scalargtsel)]
#[pgrx::join(scalargtjoinsel)]
fn vecf16_operator_gt(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_gt(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -91,13 +73,8 @@ fn vecf16_operator_gt(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
lhs.deref() > rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(>=)]
#[pgrx::negator(<)]
#[pgrx::commutator(<=)]
#[pgrx::restrict(scalargtsel)]
#[pgrx::join(scalargtjoinsel)]
fn vecf16_operator_gte(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_gte(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -108,13 +85,8 @@ fn vecf16_operator_gte(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
lhs.deref() >= rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(=)]
#[pgrx::negator(<>)]
#[pgrx::commutator(=)]
#[pgrx::restrict(eqsel)]
#[pgrx::join(eqjoinsel)]
fn vecf16_operator_eq(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_eq(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -125,13 +97,8 @@ fn vecf16_operator_eq(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
lhs.deref() == rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(<>)]
#[pgrx::negator(=)]
#[pgrx::commutator(<>)]
#[pgrx::restrict(eqsel)]
#[pgrx::join(eqjoinsel)]
fn vecf16_operator_neq(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_neq(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -142,10 +109,8 @@ fn vecf16_operator_neq(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
lhs.deref() != rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(<=>)]
#[pgrx::commutator(<=>)]
fn vecf16_operator_cosine(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_cosine(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -156,10 +121,8 @@ fn vecf16_operator_cosine(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
F16Cos::distance(&lhs, &rhs).to_f32()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(<#>)]
#[pgrx::commutator(<#>)]
fn vecf16_operator_dot(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_dot(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -170,10 +133,8 @@ fn vecf16_operator_dot(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
F16Dot::distance(&lhs, &rhs).to_f32()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf16"])]
#[pgrx::opname(<->)]
#[pgrx::commutator(<->)]
fn vecf16_operator_l2(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf16_operator_l2(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> f32 {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand Down
83 changes: 22 additions & 61 deletions src/datatype/operators_f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use crate::prelude::*;
use service::prelude::*;
use std::ops::Deref;

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(+)]
#[pgrx::commutator(+)]
fn vecf32_operator_add(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> Vecf32Output {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_add(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> Vecf32Output {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -22,9 +20,8 @@ fn vecf32_operator_add(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> Vecf32Outp
Vecf32::new_in_postgres(&v)
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(-)]
fn vecf32_operator_minus(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> Vecf32Output {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_minus(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> Vecf32Output {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -40,13 +37,8 @@ fn vecf32_operator_minus(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> Vecf32Ou
Vecf32::new_in_postgres(&v)
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(<)]
#[pgrx::negator(>=)]
#[pgrx::commutator(>)]
#[pgrx::restrict(scalarltsel)]
#[pgrx::join(scalarltjoinsel)]
fn vecf32_operator_lt(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_lt(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -57,13 +49,8 @@ fn vecf32_operator_lt(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
lhs.deref() < rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(<=)]
#[pgrx::negator(>)]
#[pgrx::commutator(>=)]
#[pgrx::restrict(scalarltsel)]
#[pgrx::join(scalarltjoinsel)]
fn vecf32_operator_lte(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_lte(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -74,13 +61,8 @@ fn vecf32_operator_lte(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
lhs.deref() <= rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(>)]
#[pgrx::negator(<=)]
#[pgrx::commutator(<)]
#[pgrx::restrict(scalargtsel)]
#[pgrx::join(scalargtjoinsel)]
fn vecf32_operator_gt(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_gt(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -91,13 +73,8 @@ fn vecf32_operator_gt(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
lhs.deref() > rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(>=)]
#[pgrx::negator(<)]
#[pgrx::commutator(<=)]
#[pgrx::restrict(scalargtsel)]
#[pgrx::join(scalargtjoinsel)]
fn vecf32_operator_gte(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_gte(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -108,13 +85,8 @@ fn vecf32_operator_gte(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
lhs.deref() >= rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(=)]
#[pgrx::negator(<>)]
#[pgrx::commutator(=)]
#[pgrx::restrict(eqsel)]
#[pgrx::join(eqjoinsel)]
fn vecf32_operator_eq(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_eq(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -125,13 +97,8 @@ fn vecf32_operator_eq(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
lhs.deref() == rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(<>)]
#[pgrx::negator(=)]
#[pgrx::commutator(<>)]
#[pgrx::restrict(eqsel)]
#[pgrx::join(eqjoinsel)]
fn vecf32_operator_neq(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_neq(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -142,10 +109,8 @@ fn vecf32_operator_neq(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
lhs.deref() != rhs.deref()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(<=>)]
#[pgrx::commutator(<=>)]
fn vecf32_operator_cosine(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_cosine(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -156,10 +121,8 @@ fn vecf32_operator_cosine(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
F32Cos::distance(&lhs, &rhs).to_f32()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(<#>)]
#[pgrx::commutator(<#>)]
fn vecf32_operator_dot(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_dot(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand All @@ -170,10 +133,8 @@ fn vecf32_operator_dot(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
F32Dot::distance(&lhs, &rhs).to_f32()
}

#[pgrx::pg_operator(immutable, parallel_safe, requires = ["vecf32"])]
#[pgrx::opname(<->)]
#[pgrx::commutator(<->)]
fn vecf32_operator_l2(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
#[pgrx::pg_extern(immutable, parallel_safe)]
fn _vectors_vecf32_operator_l2(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> f32 {
if lhs.len() != rhs.len() {
SessionError::Unmatched {
left_dimensions: lhs.len() as _,
Expand Down
4 changes: 2 additions & 2 deletions src/datatype/typmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Typmod {
}

#[pgrx::pg_extern(immutable, parallel_safe, strict)]
fn typmod_in(list: Array<&CStr>) -> i32 {
fn _vectors_typmod_in(list: Array<&CStr>) -> i32 {
if list.is_empty() {
-1
} else if list.len() == 1 {
Expand All @@ -68,7 +68,7 @@ fn typmod_in(list: Array<&CStr>) -> i32 {
}

#[pgrx::pg_extern(immutable, parallel_safe, strict)]
fn typmod_out(typmod: i32) -> CString {
fn _vectors_typmod_out(typmod: i32) -> CString {
let typmod = Typmod::parse_from_i32(typmod).unwrap();
match typmod.into_option_string() {
Some(s) => CString::new(format!("({})", s)).unwrap(),
Expand Down
Loading

0 comments on commit 97ebbf7

Please sign in to comment.