From c758c5a3e23274ee74df6c101ca9349f07ee7bcb Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Wed, 19 Apr 2023 14:59:24 -0700 Subject: [PATCH 1/3] Fix Iterator overrides for ArrayIntoIterator count, nth, and size_hint did not account for element exhaustion. --- pgrx/src/datum/array.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pgrx/src/datum/array.rs b/pgrx/src/datum/array.rs index 645c5c201..d7b95e309 100644 --- a/pgrx/src/datum/array.rs +++ b/pgrx/src/datum/array.rs @@ -424,18 +424,27 @@ impl<'a, T: FromDatum> Iterator for ArrayIntoIterator<'a, T> { } fn size_hint(&self) -> (usize, Option) { - (0, Some(self.array.nelems)) + // If asking for size, it's not clear if they want "actual size" + // or "size minus nulls"? Let's lower bound on 0 if nulls exist. + let left = self.array.nelems - self.curr; + if let NullKind::Strict(_) = self.array.null_slice { + (left, Some(left)) + } else { + (0, Some(left)) + } } fn count(self) -> usize where Self: Sized, { - self.array.nelems + // TODO: This code is dangerously under-exercised in the test suite. + self.array.nelems - self.curr } fn nth(&mut self, n: usize) -> Option { - self.array.get(n) + // TODO: This code is dangerously under-exercised in the test suite. + self.array.get(self.curr + n) } } From 6ffed4fb1b77133c4d017af1c021d0d4e5da17c9 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Wed, 19 Apr 2023 15:04:03 -0700 Subject: [PATCH 2/3] relax ArrayIntoIterator::count bounds --- pgrx/src/datum/array.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pgrx/src/datum/array.rs b/pgrx/src/datum/array.rs index d7b95e309..0dbda97ad 100644 --- a/pgrx/src/datum/array.rs +++ b/pgrx/src/datum/array.rs @@ -434,10 +434,7 @@ impl<'a, T: FromDatum> Iterator for ArrayIntoIterator<'a, T> { } } - fn count(self) -> usize - where - Self: Sized, - { + fn count(self) -> usize { // TODO: This code is dangerously under-exercised in the test suite. self.array.nelems - self.curr } From caae6aa6813a2f2671738e9601dddf4d952d9480 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Wed, 19 Apr 2023 17:24:53 -0700 Subject: [PATCH 3/3] Just drop the nth optimization It will be made incorrect by zero-copy arrays. --- pgrx/src/datum/array.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pgrx/src/datum/array.rs b/pgrx/src/datum/array.rs index 0dbda97ad..9a27dc7cb 100644 --- a/pgrx/src/datum/array.rs +++ b/pgrx/src/datum/array.rs @@ -438,11 +438,6 @@ impl<'a, T: FromDatum> Iterator for ArrayIntoIterator<'a, T> { // TODO: This code is dangerously under-exercised in the test suite. self.array.nelems - self.curr } - - fn nth(&mut self, n: usize) -> Option { - // TODO: This code is dangerously under-exercised in the test suite. - self.array.get(self.curr + n) - } } impl<'a, T: FromDatum> FromDatum for VariadicArray<'a, T> {