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

Don't look up the type oid for types that never use it #168

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pgx/src/datum/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::ops::{Deref, DerefMut};
#[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> {
if is_null {
Expand Down
18 changes: 18 additions & 0 deletions pgx/src/datum/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::ffi::CStr;
/// If implementing this, also implement `IntoDatum` for the reverse
/// conversion.
pub trait FromDatum {
const NEEDS_TYPID: bool = true;
/// ## Safety
///
/// This method is inherently unsafe as the `datum` argument can represent an arbitrary
Expand Down Expand Up @@ -56,6 +57,7 @@ pub trait FromDatum {

/// for pg_sys::Datum
impl FromDatum for pg_sys::Datum {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(
datum: pg_sys::Datum,
Expand All @@ -72,6 +74,7 @@ impl FromDatum for pg_sys::Datum {

/// for bool
impl FromDatum for bool {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<bool> {
if is_null {
Expand All @@ -84,6 +87,7 @@ impl FromDatum for bool {

/// for `"char"`
impl FromDatum for i8 {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<i8> {
if is_null {
Expand All @@ -96,6 +100,7 @@ impl FromDatum for i8 {

/// for smallint
impl FromDatum for i16 {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<i16> {
if is_null {
Expand All @@ -108,6 +113,7 @@ impl FromDatum for i16 {

/// for integer
impl FromDatum for i32 {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<i32> {
if is_null {
Expand All @@ -120,6 +126,7 @@ impl FromDatum for i32 {

/// for oid
impl FromDatum for u32 {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<u32> {
if is_null {
Expand All @@ -132,6 +139,7 @@ impl FromDatum for u32 {

/// for bigint
impl FromDatum for i64 {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<i64> {
if is_null {
Expand All @@ -144,6 +152,7 @@ impl FromDatum for i64 {

/// for real
impl FromDatum for f32 {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<f32> {
if is_null {
Expand All @@ -156,6 +165,7 @@ impl FromDatum for f32 {

/// for double precision
impl FromDatum for f64 {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<f64> {
if is_null {
Expand All @@ -168,6 +178,7 @@ impl FromDatum for f64 {

/// for text, varchar
impl<'a> FromDatum for &'a str {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<&'a str> {
if is_null {
Expand Down Expand Up @@ -212,6 +223,7 @@ impl<'a> FromDatum for &'a str {
///
/// This returns a **copy**, allocated and managed by Rust, of the underlying `varlena` Datum
impl FromDatum for String {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(
datum: pg_sys::Datum,
Expand All @@ -227,6 +239,7 @@ impl FromDatum for String {
}

impl FromDatum for char {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, typoid: pg_sys::Oid) -> Option<char> {
let refstr: Option<&str> = FromDatum::from_datum(datum, is_null, typoid);
Expand All @@ -239,6 +252,7 @@ impl FromDatum for char {

/// for cstring
impl<'a> FromDatum for &'a std::ffi::CStr {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<&'a CStr> {
if is_null {
Expand All @@ -255,6 +269,7 @@ impl<'a> FromDatum for &'a std::ffi::CStr {

/// for bytea
impl<'a> FromDatum for &'a [u8] {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: usize, is_null: bool, _typoid: u32) -> Option<&'a [u8]> {
if is_null {
Expand Down Expand Up @@ -296,6 +311,7 @@ impl<'a> FromDatum for &'a [u8] {
}

impl FromDatum for Vec<u8> {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: usize, is_null: bool, typoid: u32) -> Option<Vec<u8>> {
if is_null {
Expand All @@ -318,6 +334,7 @@ impl FromDatum for Vec<u8> {

/// for NULL -- always converts to a `None`, even if the is_null argument is false
impl FromDatum for () {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(_datum: pg_sys::Datum, _is_null: bool, _: pg_sys::Oid) -> Option<()> {
None
Expand All @@ -326,6 +343,7 @@ impl FromDatum for () {

/// for user types
impl<T> FromDatum for PgBox<T> {
const NEEDS_TYPID: bool = false;
#[inline]
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<PgBox<T>> {
if is_null {
Expand Down
1 change: 1 addition & 0 deletions pgx/src/datum/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{direct_function_call_as_datum, pg_sys, FromDatum, IntoDatum};

impl FromDatum for pg_sys::BOX {
const NEEDS_TYPID: bool = false;
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _: pg_sys::Oid) -> Option<Self>
where
Self: Sized,
Expand Down
2 changes: 2 additions & 0 deletions pgx/src/datum/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ where
A: FromDatum + IntoDatum,
B: FromDatum + IntoDatum,
{
const NEEDS_TYPID: bool = A::NEEDS_TYPID || B::NEEDS_TYPID;
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, typoid: pg_sys::Oid) -> Option<Self>
where
Self: Sized,
Expand Down Expand Up @@ -73,6 +74,7 @@ where
B: FromDatum + IntoDatum,
C: FromDatum + IntoDatum,
{
const NEEDS_TYPID: bool = A::NEEDS_TYPID || B::NEEDS_TYPID || C::NEEDS_TYPID;
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, typoid: pg_sys::Oid) -> Option<Self>
where
Self: Sized,
Expand Down
1 change: 1 addition & 0 deletions pgx/src/datum/varlena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl<T> FromDatum for PgVarlena<T>
where
T: Copy + Sized,
{
const NEEDS_TYPID: bool = false;
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _typoid: u32) -> Option<Self> {
if is_null {
None
Expand Down
18 changes: 16 additions & 2 deletions pgx/src/fcinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ mod pg_10_11 {
pub fn pg_getarg<T: FromDatum>(fcinfo: pg_sys::FunctionCallInfo, num: usize) -> Option<T> {
let datum = unsafe { fcinfo.as_ref() }.unwrap().arg[num];
let isnull = pg_arg_is_null(fcinfo, num);
unsafe { T::from_datum(datum, isnull, crate::get_getarg_type(fcinfo, num)) }
unsafe {
let typid =
if T::NEEDS_TYPID {
crate::get_getarg_type(fcinfo, num)
} else {
pg_sys::InvalidOid
};
T::from_datum(datum, isnull, typid)
}
}

#[inline]
Expand Down Expand Up @@ -119,10 +127,16 @@ mod pg_12_13 {
pub fn pg_getarg<T: FromDatum>(fcinfo: pg_sys::FunctionCallInfo, num: usize) -> Option<T> {
let datum = get_nullable_datum(fcinfo, num);
unsafe {
let typid =
if T::NEEDS_TYPID {
crate::get_getarg_type(fcinfo, num)
} else {
pg_sys::InvalidOid
};
T::from_datum(
datum.value,
datum.isnull,
crate::get_getarg_type(fcinfo, num),
typid,
)
}
}
Expand Down
1 change: 1 addition & 0 deletions pgx/src/rel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl Clone for PgRelation {
}

impl FromDatum for PgRelation {
const NEEDS_TYPID: bool = false;
unsafe fn from_datum(datum: pg_sys::Datum, is_null: bool, _typoid: u32) -> Option<PgRelation> {
if is_null {
None
Expand Down