Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Arrays with leading nulls #1180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pgrx-tests/src/tests/array_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,52 @@ mod tests {
}
Ok(())
}

#[pg_test]
fn test_text_array_as_vec_string() -> Result<(), Box<dyn std::error::Error>> {
let a = Spi::get_one::<Array<String>>(
"SELECT ARRAY[NULL, NULL, NULL, NULL, 'the fifth element']::text[]",
)?
.expect("spi result was NULL")
.into_iter()
.collect::<Vec<_>>();
assert_eq!(a, vec![None, None, None, None, Some(String::from("the fifth element"))]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this passes on all distros in every PG configuration, does that mean it is..

... multi-pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when it does, YES! hahaha

Ok(())
}

#[pg_test]
fn test_text_array_iter() -> Result<(), Box<dyn std::error::Error>> {
let a = Spi::get_one::<Array<String>>(
"SELECT ARRAY[NULL, NULL, NULL, NULL, 'the fifth element']::text[]",
)?
.expect("spi result was NULL");

let mut iter = a.iter();

assert_eq!(iter.next(), Some(None));
assert_eq!(iter.next(), Some(None));
assert_eq!(iter.next(), Some(None));
assert_eq!(iter.next(), Some(None));
assert_eq!(iter.next(), Some(Some(String::from("the fifth element"))));
assert_eq!(iter.next(), None);

Ok(())
}

#[pg_test]
fn test_text_array_via_getter() -> Result<(), Box<dyn std::error::Error>> {
let a = Spi::get_one::<Array<String>>(
"SELECT ARRAY[NULL, NULL, NULL, NULL, 'the fifth element']::text[]",
)?
.expect("spi result was NULL");

assert_eq!(a.get(0), Some(None));
assert_eq!(a.get(1), Some(None));
assert_eq!(a.get(2), Some(None));
assert_eq!(a.get(3), Some(None));
assert_eq!(a.get(4), Some(Some(String::from("the fifth element"))));
assert_eq!(a.get(5), None);

Ok(())
}
}
16 changes: 12 additions & 4 deletions pgrx/src/datum/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,13 @@ impl<'a, T: FromDatum> Iterator for ArrayIterator<'a, T> {
fn next(&mut self) -> Option<Self::Item> {
let Self { array, curr, ptr } = self;
let Some(is_null) = array.null_slice.get(*curr) else { return None };
let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
*curr += 1;
if let Some(false) = array.null_slice.get(*curr) {

let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
if !is_null {
// SAFETY: This has to not move for nulls, as they occupy 0 data bytes,
// and it has to move only after unpacking a non-null varlena element,
// as the iterator starts by pointing to the first non-null element!
*ptr = unsafe { array.one_hop_this_time(*ptr) };
}
Some(element)
Expand Down Expand Up @@ -679,9 +683,13 @@ impl<'a, T: FromDatum> Iterator for ArrayIntoIterator<'a, T> {
fn next(&mut self) -> Option<Self::Item> {
let Self { array, curr, ptr } = self;
let Some(is_null) = array.null_slice.get(*curr) else { return None };
let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
*curr += 1;
if let Some(false) = array.null_slice.get(*curr) {

let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
if !is_null {
// SAFETY: This has to not move for nulls, as they occupy 0 data bytes,
// and it has to move only after unpacking a non-null varlena element,
// as the iterator starts by pointing to the first non-null element!
*ptr = unsafe { array.one_hop_this_time(*ptr) };
}
Some(element)
Expand Down