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

FromDatum overhaul #521

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pgx-examples/bgworker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub extern "C" fn _PG_init() {
#[pg_guard]
#[no_mangle]
pub extern "C" fn background_worker_main(arg: pg_sys::Datum) {
let arg = unsafe { i32::from_datum(arg, false, pg_sys::INT4OID) };
let arg = unsafe { i32::from_datum(arg, false) };

// these are the signals we want to receive. If we don't attach the SIGTERM handler, then
// we'll never be able to exit via an external notification
Expand Down
2 changes: 1 addition & 1 deletion pgx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ fn impl_postgres_enum(ast: DeriveInput) -> proc_macro2::TokenStream {
stream.extend(quote! {
impl pgx::FromDatum for #enum_ident {
#[inline]
unsafe fn from_datum(datum: pgx::pg_sys::Datum, is_null: bool, typeoid: pgx::pg_sys::Oid) -> Option<#enum_ident> {
unsafe fn from_datum(datum: pgx::pg_sys::Datum, is_null: bool) -> Option<#enum_ident> {
if is_null {
None
} else {
Expand Down
15 changes: 3 additions & 12 deletions pgx/src/datum/anyarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,26 @@ use crate::{pg_sys, FromDatum, IntoDatum};
#[derive(Debug, Clone, Copy)]
pub struct AnyArray {
datum: pg_sys::Datum,
typoid: pg_sys::Oid,
}

impl AnyArray {
pub fn datum(&self) -> pg_sys::Datum {
self.datum
}

pub fn oid(&self) -> pg_sys::Oid {
self.typoid
}

#[inline]
pub fn into<T: FromDatum>(&self) -> Option<T> {
unsafe { T::from_datum(self.datum(), false, self.oid()) }
unsafe { T::from_datum(self.datum(), false) }
}
}

impl FromDatum for AnyArray {
#[inline]
unsafe fn from_datum(
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<AnyArray> {
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool) -> Option<AnyArray> {
if is_null {
None
} else {
Some(AnyArray { datum, typoid })
Some(AnyArray { datum })
}
}
}
Expand Down
15 changes: 3 additions & 12 deletions pgx/src/datum/anyelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,26 @@ use crate::{pg_sys, FromDatum, IntoDatum};
#[derive(Debug, Clone, Copy)]
pub struct AnyElement {
datum: pg_sys::Datum,
typoid: pg_sys::Oid,
}

impl AnyElement {
pub fn datum(&self) -> pg_sys::Datum {
self.datum
}

pub fn oid(&self) -> pg_sys::Oid {
self.typoid
}

#[inline]
pub fn into<T: FromDatum>(&self) -> Option<T> {
unsafe { T::from_datum(self.datum(), false, self.oid()) }
unsafe { T::from_datum(self.datum(), false) }
}
}

impl FromDatum for AnyElement {
#[inline]
unsafe fn from_datum(
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<AnyElement> {
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool) -> Option<AnyElement> {
if is_null {
None
} else {
Some(AnyElement { datum, typoid })
Some(AnyElement { datum })
}
}
}
Expand Down
51 changes: 18 additions & 33 deletions pgx/src/datum/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub struct Array<'a, T: FromDatum> {
array_type: *mut pg_sys::ArrayType,
elements: *mut pg_sys::Datum,
nulls: *mut bool,
typoid: pg_sys::Oid,
nelems: usize,
elem_slice: &'a [pg_sys::Datum],
null_slice: &'a [bool],
Expand Down Expand Up @@ -63,7 +62,6 @@ impl<'a, T: FromDatum> Array<'a, T> {
array_type: std::ptr::null_mut(),
elements,
nulls,
typoid: pg_sys::InvalidOid,
nelems,
elem_slice: std::slice::from_raw_parts(elements, nelems),
null_slice: std::slice::from_raw_parts(nulls, nelems),
Expand All @@ -76,15 +74,13 @@ impl<'a, T: FromDatum> Array<'a, T> {
array_type: *mut pg_sys::ArrayType,
elements: *mut pg_sys::Datum,
nulls: *mut bool,
typoid: pg_sys::Oid,
nelems: usize,
) -> Self {
Array::<T> {
ptr,
array_type,
elements,
nulls,
typoid,
nelems,
elem_slice: std::slice::from_raw_parts(elements, nelems),
null_slice: std::slice::from_raw_parts(nulls, nelems),
Expand Down Expand Up @@ -153,7 +149,7 @@ impl<'a, T: FromDatum> Array<'a, T> {
if i >= self.nelems {
None
} else {
Some(unsafe { T::from_datum(self.elem_slice[i], self.null_slice[i], self.typoid) })
Some(unsafe { T::from_datum(self.elem_slice[i], self.null_slice[i]) })
}
}
}
Expand Down Expand Up @@ -277,11 +273,9 @@ impl<'a, T: FromDatum> Drop for Array<'a, T> {

impl<'a, T: FromDatum> FromDatum for Array<'a, T> {
#[inline]
unsafe fn from_datum(datum: usize, is_null: bool, typoid: u32) -> Option<Array<'a, T>> {
unsafe fn from_datum(datum: usize, is_null: bool) -> Option<Array<'a, T>> {
if is_null {
None
} else if datum == 0 {
panic!("array was flagged not null but datum is zero");
} else {
let ptr = datum as *mut pg_sys::varlena;
let array =
Expand Down Expand Up @@ -316,31 +310,18 @@ impl<'a, T: FromDatum> FromDatum for Array<'a, T> {
&mut nelems,
);

Some(Array::from_pg(
ptr,
array,
elements,
nulls,
typoid,
nelems as usize,
))
Some(Array::from_pg(ptr, array, elements, nulls, nelems as usize))
}
}
}

impl<T: FromDatum> FromDatum for Vec<T> {
#[inline]
unsafe fn from_datum(
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<Vec<T>> {
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool) -> Option<Vec<T>> {
if is_null {
None
} else if datum == 0 {
panic!("array was flagged not null but datum is zero");
} else {
let array = Array::<T>::from_datum(datum, is_null, typoid).unwrap();
let array = Array::<T>::from_datum(datum, is_null).unwrap();
let mut v = Vec::with_capacity(array.len());

for element in array.iter() {
Expand All @@ -353,17 +334,11 @@ impl<T: FromDatum> FromDatum for Vec<T> {

impl<T: FromDatum> FromDatum for Vec<Option<T>> {
#[inline]
unsafe fn from_datum(
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<Vec<Option<T>>> {
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool) -> Option<Vec<Option<T>>> {
if is_null {
None
} else if datum == 0 {
panic!("array was flagged not null but datum is zero");
} else {
let array = Array::<T>::from_datum(datum, is_null, typoid).unwrap();
let array = Array::<T>::from_datum(datum, is_null).unwrap();
let mut v = Vec::with_capacity(array.len());

for element in array.iter() {
Expand Down Expand Up @@ -414,11 +389,16 @@ where
fn type_oid() -> u32 {
unsafe { pg_sys::get_array_type(T::type_oid()) }
}

#[inline]
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other || other == unsafe { pg_sys::get_array_type(T::type_oid()) }
}
}

impl<'a, T> IntoDatum for &'a [T]
where
T: IntoDatum + Copy,
T: IntoDatum + Copy + 'a,
{
fn into_datum(self) -> Option<pg_sys::Datum> {
let mut state = unsafe {
Expand Down Expand Up @@ -456,4 +436,9 @@ where
fn type_oid() -> u32 {
unsafe { pg_sys::get_array_type(T::type_oid()) }
}

#[inline]
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other || other == unsafe { pg_sys::get_array_type(T::type_oid()) }
}
}
3 changes: 1 addition & 2 deletions pgx/src/datum/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use time::format_description::FormatItem;
#[derive(Debug)]
pub struct Date(time::Date);
impl FromDatum for Date {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _typoid: u32) -> Option<Date> {
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool) -> Option<Date> {
if is_null {
None
} else {
Expand Down
Loading