Skip to content

Commit

Permalink
fix: by comments
Browse files Browse the repository at this point in the history
Signed-off-by: cutecutecat <junyuchen@tensorchord.ai>
  • Loading branch information
cutecutecat committed Jun 18, 2024
1 parent 0fbe5fc commit e4be2c3
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 220 deletions.
23 changes: 15 additions & 8 deletions src/datatype/text_svecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ use base::scalar::*;
use base::vector::*;
use pgrx::pg_sys::Oid;
use std::ffi::{CStr, CString};
use std::fmt::Write;

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_svecf32_in(input: &CStr, _oid: Oid, _typmod: i32) -> SVecf32Output {
use crate::utils::parse::parse_pgvector_svector;
use crate::utils::parse::{parse_pgvector_svector, ParsedSvector};
let v = parse_pgvector_svector(input.to_bytes(), |s| s.parse::<F32>().ok());
match v {
Err(e) => {
bad_literal(&e.to_string());
}
Ok((indexes, values, dims)) => {
Ok(ParsedSvector {
all_indexes,
indexes,
values,
dims,
}) => {
check_value_dims_1048575(dims);
check_index_in_bound(all_indexes, dims);
SVecf32Output::new(SVecf32Borrowed::new(dims as u32, &indexes, &values))
}
}
Expand All @@ -27,16 +34,16 @@ fn _vectors_svecf32_out(vector: SVecf32Input<'_>) -> CString {
let mut buffer = String::new();
buffer.push('{');
let svec = vector.for_borrow();
let mut need_splitter = true;
let mut need_splitter = false;
for (&index, &value) in svec.indexes().iter().zip(svec.values().iter()) {
match need_splitter {
true => {
buffer.push_str(format!("{}:{}", index, value).as_str());
need_splitter = false;
false => {
write!(buffer, "{}:{}", index, value).unwrap();
need_splitter = true;
}
false => buffer.push_str(format!(", {}:{}", index, value).as_str()),
true => write!(buffer, ", {}:{}", index, value).unwrap(),
}
}
buffer.push_str(format!("}}/{}", dims).as_str());
write!(buffer, "}}/{}", dims).unwrap();
CString::new(buffer).unwrap()
}
14 changes: 14 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ ADVICE: Check if dimensions of the vector are among 1 and 1_048_575."
NonZeroU32::new(dims as u32).unwrap()
}

pub fn check_index_in_bound(all_indexes: Vec<u32>, dims: usize) -> NonZeroU32 {
let mut last: u32 = 0;
for (i, index) in all_indexes.into_iter().enumerate() {
if i > 0 && last == index {
error!("Indexes need to be unique, but there are more than one same index {index}")
}
if index as usize >= dims {
error!("Index out of bounds: the dim is {dims} but the index is {index}");
}
last = index;
}
NonZeroU32::new(dims as u32).unwrap()
}

pub fn bad_literal(hint: &str) -> ! {
error!(
"\
Expand Down
Loading

0 comments on commit e4be2c3

Please sign in to comment.