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

Composite Type Support #592

Merged
merged 58 commits into from
Jul 19, 2022
Merged

Composite Type Support #592

merged 58 commits into from
Jul 19, 2022

Conversation

Hoverbear
Copy link
Contributor

@Hoverbear Hoverbear commented Jun 17, 2022

Addresses #499, #203, #326. Helps along #451.

Adds "Composite Type" support to PGX.

What's changed

  • A new pgx::composite_type!("name") macro now exists. It can be used in most PGX related macros where one would use an argument. It expands to create a PgHeapTuple, but the PGX related macros are able to derive additional metadata from this macro for SQL generation.
  • PgHeapTuple now implements FromDatum.
  • PgHeapTuple acquired a new_composite_type constructor which allows users to create a heap tuple shaped for a given composite type string. Eg gHeapTuple::new_composite_type("dog").
  • PgHeapTuple now has a into_trigger_datum function which will result in the correct Datum for returning from a #[pg_trigger] function. Since this was done as part of macro expanded code, no user changes should be required.
  • The IntoDatum implementation of PgHeapTuple changed to better work with composite types. Specialized trigger related functions exist and should work transparently for users.
  • PgTupleDesc now has a for_composite_type function which allows users to construct one for a given composite type by name.

Breaking Changes

  • You can no longer do Option<default!(i32, 1)> in your #[pg_externs]. Change it to default!(Option<i32>, 1).
  • The ordering of pgx related function argument macros is now fixed. Build errors should correctly guide users into the correct ordering with location context.
  • Various types inside pgx::utils::sql_entity_graph were updated to include fields for composite types. MaybeNamedVariadicTypeList was removed, a new UsedType was added, and various internals around macro-expansion-time type resolution were tinkered with.

    As a reminder, while these structures are public, they are not recommended for users unless they are ok with them breaking nearly every 0.x version.

  • If you are not using pgx::pg_module_magic!(); (or pgx::pg_sql_graph_magic!();) in your extension, they now expand to expose a pub extern "C" fn __pgx_sql_mappings() -> ::pgx::utils::sql_entity_graph::RustToSqlMapping instead of their prior. This was done to both reduce the number of exported symbols extensions you, as well as support composite types.

Hoverbear and others added 21 commits June 3, 2022 13:13
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
@Hoverbear Hoverbear self-assigned this Jun 17, 2022
@Hoverbear Hoverbear changed the title Udts Composite Type Support Jun 17, 2022
@Hoverbear Hoverbear requested a review from eeeebbbbrrrr June 28, 2022 18:40
@Hoverbear
Copy link
Contributor Author

@eeeebbbbrrrr I'd appreciate you giving this a lookie-loo if you can. :)

This was referenced Jun 28, 2022

Demonstrates how to create a `IntegerAvgState` aggregate.

This example also demonstrates the use of `PgVarlena<T>` and how to use `#[pgvarlena_inoutfuncs]` with `#[derive(PostgresType)]`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops...

Copy link
Contributor Author

@Hoverbear Hoverbear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably do an aggregate test...

@@ -517,7 +517,8 @@ pub fn anonymonize_lifetimes(value: &mut syn::Type) {
match arg {
// rename lifetimes to the anonymous lifetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment no longer matches the code below. Why are we now using 'static?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks!

We need anything using a lifetime to use the same lifetime so that the TypeIds match (&'a Foo and &'b Foo have different TypeIds)

@@ -331,23 +331,6 @@ impl<T: FromDatum> FromDatum for Vec<T> {
}
}

impl<T: FromDatum> FromDatum for Vec<Option<T>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this go away? This is for supporting postgres ARRAY[]s that might contain NULL elements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is now a conflicting implementation which covers the same type and works the same:

error[E0119]: conflicting implementations of trait `datum::from::FromDatum` for type `std::vec::Vec<std::option::Option<_>>`
   --> pgx/src/datum/array.rs:334:1
    |
317 | impl<T: FromDatum> FromDatum for Vec<T> {
    | --------------------------------------- first implementation here
...
334 | impl<T: FromDatum> FromDatum for Vec<Option<T>> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::vec::Vec<std::option::Option<_>>`

For more information about this error, try `rustc --explain E0119`.
error: could not compile `pgx` due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `pgx` due to previous error

Copy link
Contributor Author

@Hoverbear Hoverbear Jul 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I re-add this and remove that conflicting implementation I get a lot of type failures, eg

   Compiling pgx v0.5.0-beta.0 (/home/ana/git/tcdi/pgx/pgx)
error[E0277]: the trait bound `std::option::Option<heap_tuple::PgHeapTuple<'static, pgbox::AllocatedByRust>>: datum::PostgresType` is not satisfied
   --> pgx/src/lib.rs:263:26
    |
263 |             TypeId::of::<Array<Option<PgHeapTuple<'static, AllocatedByRust>>>>(),
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `datum::PostgresType` is not implemented for `std::option::Option<heap_tuple::PgHeapTuple<'static, pgbox::AllocatedByRust>>`
    |
note: required because of the requirements on the impl of `from::FromDatum` for `std::option::Option<heap_tuple::PgHeapTuple<'static, pgbox::AllocatedByRust>>`
   --> pgx/src/datum/varlena.rs:344:14
    |
344 | impl<'de, T> FromDatum for T
    |              ^^^^^^^^^     ^
note: required by a bound in `datum::array::Array`
   --> pgx/src/datum/array.rs:16:25
    |
16  | pub struct Array<'a, T: FromDatum> {
    |                         ^^^^^^^^^ required by this bound in `datum::array::Array`

pgx/src/datum/from.rs Outdated Show resolved Hide resolved
@Hoverbear
Copy link
Contributor Author

@zombodb I added many changes around aggregate support, did you want to take another review pass or shall I merge once I'm comfortable with it? I need to do a self review still.

pgx/src/tupdesc.rs Outdated Show resolved Hide resolved
pgx/src/tupdesc.rs Outdated Show resolved Hide resolved
Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants