Skip to content

feat(Postgres): support nested domain types #3641

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion sqlx-postgres/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ impl PgType {
/// If `soft_eq` is true and `self` or `other` is `DeclareWithOid` but not both, return `true`
/// before checking names.
fn eq_impl(&self, other: &Self, soft_eq: bool) -> bool {
if let (Some(a), Some(b)) = (self.try_oid(), other.try_oid()) {
if let (Some(a), Some(b)) = (self.try_base_oid(), other.try_base_oid()) {
// If there are OIDs available, use OIDs to perform a direct match
return a == b;
}
Expand All @@ -1035,6 +1035,18 @@ impl PgType {
// Otherwise, perform a match on the name
name_eq(self.name(), other.name())
}

// Tries to return the OID of the type, returns the OID of the base_type for domain types
#[inline(always)]
fn try_base_oid(&self) -> Option<Oid> {
match self {
PgType::Custom(custom) => match &custom.kind {
PgTypeKind::Domain(domain) => domain.try_oid(),
_ => Some(custom.oid),
},
ty => ty.try_oid(),
}
}
}

impl TypeInfo for PgTypeInfo {
Expand Down
42 changes: 21 additions & 21 deletions sqlx-postgres/src/types/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,7 @@ impl<'r> PgRecordDecoder<'r> {
match self.fmt {
PgValueFormat::Binary => {
let element_type_oid = Oid(self.buf.get_u32());
let element_type_opt = match self.typ.0.kind() {
PgTypeKind::Simple if self.typ.0 == PgType::Record => {
PgTypeInfo::try_from_oid(element_type_oid)
}

PgTypeKind::Composite(fields) => {
let ty = fields[self.ind].1.clone();
if ty.0.oid() != element_type_oid {
return Err("unexpected mismatch of composite type information".into());
}

Some(ty)
}

_ => {
return Err(
"unexpected non-composite type being decoded as a composite type"
.into(),
);
}
};
let element_type_opt = self.find_type_info(&self.typ, element_type_oid)?;

if let Some(ty) = &element_type_opt {
if !ty.is_null() && !T::compatible(ty) {
Expand Down Expand Up @@ -202,4 +182,24 @@ impl<'r> PgRecordDecoder<'r> {
}
}
}

fn find_type_info(
&self,
typ: &PgTypeInfo,
oid: Oid,
) -> Result<Option<PgTypeInfo>, BoxDynError> {
match typ.kind() {
PgTypeKind::Simple if typ.0 == PgType::Record => Ok(PgTypeInfo::try_from_oid(oid)),
PgTypeKind::Composite(fields) => {
let ty = fields[self.ind].1.clone();
if ty.0.oid() != oid {
return Err("unexpected mismatch of composite type information".into());
}

Ok(Some(ty))
}
PgTypeKind::Domain(domain) => self.find_type_info(domain, oid),
_ => Err("unexpected custom type being decoded as a composite type".into()),
}
}
}
13 changes: 13 additions & 0 deletions tests/postgres/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ CREATE SCHEMA IF NOT EXISTS foo;
CREATE TYPE foo."Foo" as ENUM ('Bar', 'Baz');

CREATE TABLE mytable(f HSTORE);

CREATE DOMAIN positive_int AS integer CHECK (VALUE >= 0);
CREATE DOMAIN percentage AS positive_int CHECK (VALUE <= 100);

CREATE TYPE person as (
id int,
age positive_int,
percent percentage
);

CREATE TYPE leaf_composite AS (prim integer);
CREATE DOMAIN domain AS leaf_composite;
CREATE TYPE root_composite AS (domain domain);
39 changes: 39 additions & 0 deletions tests/postgres/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,45 @@ test_type!(ltree_vec<Vec<sqlx::postgres::types::PgLTree>>(Postgres,
]
));

#[derive(sqlx::Type, Debug, PartialEq)]
#[sqlx(type_name = "positive_int")]
struct PositiveInt(i32);

#[derive(sqlx::Type, Debug, PartialEq)]
#[sqlx(type_name = "percentage")]
struct Percentage(PositiveInt);

#[derive(sqlx::Type, Debug, PartialEq)]
struct Person {
id: i32,
age: PositiveInt,
percent: Percentage,
}

test_type!(nested_domain_types_1<Person>(Postgres,
"ROW(1, 21::positive_int, 50::percentage)::person" == Person { id: 1, age: PositiveInt(21), percent: Percentage(PositiveInt(50)) })
);

#[derive(sqlx::Type, Debug, PartialEq)]
#[sqlx(type_name = "leaf_composite")]
struct LeafComposite {
prim: i32,
}

#[derive(sqlx::Type, Debug, PartialEq)]
#[sqlx(type_name = "domain")]
struct Domain(LeafComposite);

#[derive(sqlx::Type, Debug, PartialEq)]
#[sqlx(type_name = "root_composite")]
struct RootComposite {
domain: Domain,
}

test_type!(nested_domain_types_2<RootComposite>(Postgres,
"ROW(ROW(1))::root_composite" == RootComposite { domain: Domain(LeafComposite { prim: 1})})
);

#[sqlx_macros::test]
async fn test_text_adapter() -> anyhow::Result<()> {
#[derive(sqlx::FromRow, Debug, PartialEq, Eq)]
Expand Down
Loading