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

Support in FromDatum for binary coercible types (including domain types) #1028

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
1 change: 1 addition & 0 deletions pgx-pg-sys/include/pg11.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Use of this source code is governed by the MIT license that can be found in the
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_type.h"
#include "parser/parse_coerce.h"
#include "parser/parser.h"
#include "parser/parsetree.h"
#include "postmaster/bgworker.h"
Expand Down
1 change: 1 addition & 0 deletions pgx-pg-sys/include/pg12.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Use of this source code is governed by the MIT license that can be found in the
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_type.h"
#include "parser/parse_coerce.h"
#include "parser/parser.h"
#include "parser/parsetree.h"
#include "postmaster/bgworker.h"
Expand Down
1 change: 1 addition & 0 deletions pgx-pg-sys/include/pg13.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Use of this source code is governed by the MIT license that can be found in the
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_type.h"
#include "parser/parse_coerce.h"
#include "parser/parser.h"
#include "parser/parsetree.h"
#include "postmaster/bgworker.h"
Expand Down
1 change: 1 addition & 0 deletions pgx-pg-sys/include/pg14.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Use of this source code is governed by the MIT license that can be found in the
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_type.h"
#include "parser/parse_coerce.h"
#include "parser/parser.h"
#include "parser/parsetree.h"
#include "postmaster/bgworker.h"
Expand Down
1 change: 1 addition & 0 deletions pgx-pg-sys/include/pg15.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Use of this source code is governed by the MIT license that can be found in the
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_type.h"
#include "parser/parse_coerce.h"
#include "parser/parser.h"
#include "parser/parsetree.h"
#include "postmaster/bgworker.h"
Expand Down
19 changes: 19 additions & 0 deletions pgx-tests/src/tests/spi_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,23 @@ mod tests {
assert_eq!(with_select, with_get_one);
Ok(())
}

#[pg_test]
fn spi_can_read_domain_types() -> spi::Result<Option<String>> {
Spi::run("CREATE DOMAIN my_text_type TEXT")?;
Spi::get_one::<String>("SELECT 'hello'::my_text_type")
}

#[pg_test]
fn spi_can_read_domain_types_based_on_domain_types() -> spi::Result<Option<String>> {
Spi::run("CREATE DOMAIN my_text_type TEXT")?;
Spi::run("CREATE DOMAIN my_other_text_type my_text_type")?;
Spi::get_one::<String>("SELECT 'hello'::my_other_text_type")
}

#[pg_test]
fn spi_can_read_binary_coercible_types() -> spi::Result<Option<pgx::Inet>> {
// cidr is binary coercible to inet
Spi::get_one::<pgx::Inet>("select '10.0.0.1/32'::cidr")
}
}
8 changes: 6 additions & 2 deletions pgx/src/datum/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub trait FromDatum {
where
Self: Sized + IntoDatum,
{
if !Self::is_compatible_with(type_oid) {
if !is_binary_coercible::<Self>(type_oid) {
Err(TryFromDatumError::IncompatibleTypes {
rust_type: std::any::type_name::<Self>(),
rust_oid: Self::type_oid(),
Expand All @@ -140,7 +140,7 @@ pub trait FromDatum {
where
Self: Sized + IntoDatum,
{
if !Self::is_compatible_with(type_oid) {
if !is_binary_coercible::<Self>(type_oid) {
Err(TryFromDatumError::IncompatibleTypes {
rust_type: std::any::type_name::<Self>(),
rust_oid: Self::type_oid(),
Expand All @@ -153,6 +153,10 @@ pub trait FromDatum {
}
}

fn is_binary_coercible<T: IntoDatum>(type_oid: pg_sys::Oid) -> bool {
T::is_compatible_with(type_oid) || unsafe { pg_sys::IsBinaryCoercible(type_oid, T::type_oid()) }
}

/// Retrieves a Postgres type name given its Oid
pub(crate) fn lookup_type_name(oid: pg_sys::Oid) -> String {
unsafe {
Expand Down