Skip to content

Commit

Permalink
Add UnboxDatum for Range<T> (#1827)
Browse files Browse the repository at this point in the history
I was getting compiler complaints from this input type `my_func(arr:
Array<Range<Date>>)` and calling `.get_by_name::<Range<Date>>`.
Implement the required trait.
  • Loading branch information
mhov authored Aug 24, 2024
1 parent 7107b51 commit ed18e63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pgrx-tests/src/tests/range_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ fn accept_range_date(range: Range<Date>) -> Range<Date> {
range
}

#[pg_extern]
fn accept_range_date_array(arr: Array<Range<Date>>) -> Vec<Range<Date>> {
arr.iter_deny_null().collect()
}

#[pg_extern]
fn accept_range_ts(range: Range<Timestamp>) -> Range<Timestamp> {
range
Expand Down Expand Up @@ -143,6 +148,13 @@ mod tests {
assert_eq!(matched, Ok(Some(true)));
}

#[pg_test]
fn test_accept_range_date_array() {
let matched =
Spi::get_one::<bool>("SELECT accept_range_date_array(ARRAY[daterange'[2000-01-01,2022-01-01)']::daterange[]) = ARRAY[daterange'[2000-01-01,2022-01-01)']::daterange[]");
assert_eq!(matched, Ok(Some(true)));
}

#[pg_test]
fn test_accept_range_ts() {
let matched =
Expand Down
10 changes: 10 additions & 0 deletions pgrx/src/datum/unbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ unsafe impl<T: FromDatum + UnboxDatum> UnboxDatum for VariadicArray<'_, T> {
}
}

unsafe impl<T: FromDatum + UnboxDatum + RangeSubType> UnboxDatum for Range<T> {
type As<'src> = Range<T> where Self: 'src;
unsafe fn unbox<'src>(d: Datum<'src>) -> Self::As<'src>
where
Self: 'src,
{
Range::<T>::from_datum(d.0, false).unwrap()
}
}

unsafe impl<const P: u32, const S: u32> UnboxDatum for Numeric<P, S> {
type As<'src> = Numeric<P, S>;
#[inline]
Expand Down

0 comments on commit ed18e63

Please sign in to comment.