Skip to content

Commit c44a8f0

Browse files
committed
fix: remove funcs
Signed-off-by: cutecutecat <junyuchen@tensorchord.ai>
1 parent 94312ef commit c44a8f0

File tree

3 files changed

+57
-61
lines changed

3 files changed

+57
-61
lines changed

src/datatype/text_svecf32.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use num_traits::Zero;
2+
use pgrx::error;
3+
14
use super::memory_svecf32::SVecf32Output;
25
use crate::datatype::memory_svecf32::SVecf32Input;
36
use crate::error::*;
@@ -9,22 +12,66 @@ use std::fmt::Write;
912

1013
#[pgrx::pg_extern(immutable, strict, parallel_safe)]
1114
fn _vectors_svecf32_in(input: &CStr, _oid: Oid, _typmod: i32) -> SVecf32Output {
12-
use crate::utils::parse::{parse_pgvector_svector, svector_filter_nonzero, svector_sorted};
15+
use crate::utils::parse::parse_pgvector_svector;
1316
let v = parse_pgvector_svector(input.to_bytes(), |s| s.parse::<F32>().ok());
1417
match v {
1518
Err(e) => {
1619
bad_literal(&e.to_string());
1720
}
18-
Ok((indexes, values, dims)) => {
19-
let (mut sorted_indexes, mut sorted_values) = svector_sorted(&indexes, &values);
21+
Ok((mut indexes, mut values, dims)) => {
2022
check_value_dims_1048575(dims);
21-
check_index_in_bound(&sorted_indexes, dims);
22-
svector_filter_nonzero(&mut sorted_indexes, &mut sorted_values);
23-
SVecf32Output::new(SVecf32Borrowed::new(
24-
dims as u32,
25-
&sorted_indexes,
26-
&sorted_values,
27-
))
23+
// is_sorted
24+
if !indexes.windows(2).all(|i| i[0] <= i[1]) {
25+
assert_eq!(indexes.len(), values.len());
26+
let n = indexes.len();
27+
let mut permutation = (0..n).collect::<Vec<_>>();
28+
permutation.sort_unstable_by_key(|&i| &indexes[i]);
29+
for i in 0..n {
30+
if i == permutation[i] || usize::MAX == permutation[i] {
31+
continue;
32+
}
33+
let index = indexes[i];
34+
let value = values[i];
35+
let mut j = i;
36+
while i != permutation[j] {
37+
let next = permutation[j];
38+
indexes[j] = indexes[permutation[j]];
39+
values[j] = values[permutation[j]];
40+
permutation[j] = usize::MAX;
41+
j = next;
42+
}
43+
indexes[j] = index;
44+
values[j] = value;
45+
permutation[j] = usize::MAX;
46+
}
47+
}
48+
let mut last: Option<u32> = None;
49+
for index in indexes.clone() {
50+
if last == Some(index) {
51+
error!(
52+
"Indexes need to be unique, but there are more than one same index {index}"
53+
)
54+
}
55+
if last >= Some(dims as u32) {
56+
error!("Index out of bounds: the dim is {dims} but the index is {index}");
57+
}
58+
last = Some(index);
59+
{
60+
let mut i = 0;
61+
let mut j = 0;
62+
while j < values.len() {
63+
if !values[j].is_zero() {
64+
indexes[i] = indexes[j];
65+
values[i] = values[j];
66+
i += 1;
67+
}
68+
j += 1;
69+
}
70+
indexes.truncate(i);
71+
values.truncate(i);
72+
}
73+
}
74+
SVecf32Output::new(SVecf32Borrowed::new(dims as u32, &indexes, &values))
2875
}
2976
}
3077
}

src/error.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@ ADVICE: Check if dimensions of the vector are among 1 and 1_048_575."
6969
NonZeroU32::new(dims as u32).unwrap()
7070
}
7171

72-
pub fn check_index_in_bound(indexes: &[u32], dims: usize) -> NonZeroU32 {
73-
let mut last: Option<u32> = None;
74-
for index in indexes {
75-
if last == Some(*index) {
76-
error!("Indexes need to be unique, but there are more than one same index {index}")
77-
}
78-
if *index >= dims as u32 {
79-
error!("Index out of bounds: the dim is {dims} but the index is {index}");
80-
}
81-
last = Some(*index);
82-
}
83-
NonZeroU32::new(dims as u32).unwrap()
84-
}
85-
8672
pub fn bad_literal(hint: &str) -> ! {
8773
error!(
8874
"\

src/utils/parse.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,43 +98,6 @@ enum ParseState {
9898
Dims,
9999
}
100100

101-
#[inline(always)]
102-
pub fn svector_sorted<T: Zero + Clone + PartialEq>(
103-
indexes: &[u32],
104-
values: &[T],
105-
) -> (Vec<u32>, Vec<T>) {
106-
let mut indices = (0..indexes.len()).collect::<Vec<_>>();
107-
indices.sort_by_key(|&i| &indexes[i]);
108-
109-
let mut sorted_indexes: Vec<u32> = Vec::with_capacity(indexes.len());
110-
let mut sorted_values: Vec<T> = Vec::with_capacity(indexes.len());
111-
for i in indices {
112-
sorted_indexes.push(*indexes.get(i).unwrap());
113-
sorted_values.push(values.get(i).unwrap().clone());
114-
}
115-
(sorted_indexes, sorted_values)
116-
}
117-
118-
#[inline(always)]
119-
pub fn svector_filter_nonzero<T: Zero + Clone + PartialEq>(
120-
indexes: &mut Vec<u32>,
121-
values: &mut Vec<T>,
122-
) {
123-
// Index must be sorted!
124-
let mut i = 0;
125-
let mut j = 0;
126-
while j < values.len() {
127-
if !values[j].is_zero() {
128-
indexes[i] = indexes[j];
129-
values[i] = values[j].clone();
130-
i += 1;
131-
}
132-
j += 1;
133-
}
134-
indexes.truncate(i);
135-
values.truncate(i);
136-
}
137-
138101
#[inline(always)]
139102
pub fn parse_pgvector_svector<T: Zero + Clone, F>(
140103
input: &[u8],

0 commit comments

Comments
 (0)