Skip to content

Commit

Permalink
fix: support null insert
Browse files Browse the repository at this point in the history
Signed-off-by: cutecutecat <junyuchen@tensorchord.ai>
  • Loading branch information
cutecutecat committed Feb 27, 2024
1 parent e0dd790 commit f359560
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/index/am.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ pub unsafe extern "C" fn aminsert(
let oid = (*index_relation).rd_locator.relNumber;
let id = Handle::from_sys(oid);
let vector = from_datum(*values.add(0));
am_update::update_insert(id, vector, *heap_tid);
if let Some(v) = vector {
am_update::update_insert(id, v, *heap_tid);
}
true
}

Expand Down
10 changes: 9 additions & 1 deletion src/index/am_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ unsafe extern "C" fn callback(
_tuple_is_alive: bool,
state: *mut std::os::raw::c_void,
) {
let state = &mut *(state as *mut Builder);
if *_is_null.add(0) {
(*state.result).heap_tuples += 1.0;
return;
}
#[cfg(any(feature = "pg14", feature = "pg15"))]
let oid = (*index_relation).rd_node.relNode;
#[cfg(feature = "pg16")]
let oid = (*index_relation).rd_locator.relNumber;
let id = Handle::from_sys(oid);
let state = &mut *(state as *mut Builder);
let vector = from_datum(*values.add(0));
let vector = match vector {
Some(v) => v,
None => unreachable!(),
};
let pointer = Pointer::from_sys(*ctid);
match state.rpc.insert(id, vector, pointer) {
Ok(()) => (),
Expand Down
2 changes: 1 addition & 1 deletion src/index/am_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub unsafe fn start_scan(scan: pgrx::pg_sys::IndexScanDesc, orderbys: pgrx::pg_s
scanner,
Scanner::Initial {
node: scanner.node(),
vector: Some(vector),
vector,
},
);

Expand Down
11 changes: 7 additions & 4 deletions src/index/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ struct Header {
kind: u16,
}

pub unsafe fn from_datum(datum: pgrx::pg_sys::Datum) -> OwnedVector {
pub unsafe fn from_datum(datum: pgrx::pg_sys::Datum) -> Option<OwnedVector> {
let p = datum.cast_mut_ptr::<pgrx::pg_sys::varlena>();
if p.is_null() {
return None;
}
let q = pgrx::pg_sys::pg_detoast_datum(p);
let vector = match (*q.cast::<Header>()).kind {
0 => {
let v = &*q.cast::<Vecf32Header>();
OwnedVector::Vecf32(v.for_borrow().for_own())
Some(OwnedVector::Vecf32(v.for_borrow().for_own()))
}
1 => {
let v = &*q.cast::<Vecf16Header>();
OwnedVector::Vecf16(v.for_borrow().for_own())
Some(OwnedVector::Vecf16(v.for_borrow().for_own()))
}
2 => {
let v = &*q.cast::<SVecf32Header>();
OwnedVector::SVecF32(v.for_borrow().for_own())
Some(OwnedVector::SVecF32(v.for_borrow().for_own()))
}
_ => unreachable!(),
};
Expand Down
37 changes: 37 additions & 0 deletions tests/sqllogictest/null.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
statement ok
SET search_path TO pg_temp, vectors;

statement ok
CREATE TABLE t (val vector(3));

statement ok
INSERT INTO t (val) SELECT ARRAY[random(), random(), random()]::real[] FROM generate_series(1, 100);

statement ok
INSERT INTO t (val) SELECT ('[NaN, Infinity, -Infinity]') FROM generate_series(1, 100);

statement ok
INSERT INTO t (val) SELECT (NULL) FROM generate_series(1, 100);

query I
SELECT COUNT(1) FROM (SELECT 1 FROM t ORDER BY val <-> '[0.5,0.5,0.5]' limit 10) t2;
----
10

statement ok
CREATE INDEX hnsw_index ON t USING vectors (val vector_l2_ops)
WITH (options = "[indexing.hnsw]");

query I
SELECT COUNT(1) FROM (SELECT 1 FROM t ORDER BY val <-> '[0.5,0.5,0.5]' limit 10) t2;
----
10

statement ok
REINDEX INDEX hnsw_index;

query I
SELECT COUNT(1) FROM (SELECT 1 FROM t ORDER BY val <-> '[0.5,0.5,0.5]' limit 10) t2;
----
10

0 comments on commit f359560

Please sign in to comment.