Skip to content

Commit

Permalink
Fixes for aggregates (#408)
Browse files Browse the repository at this point in the history
* Fix source type mapping in aggregates

Source type mapping (as used with pg_sys::TimestampTz) requires an exact
match between the source type map, and the interpreted source type for a
given type. The stringify!() macro adds unwanted whitespace in qualified
types (e.g. "pg_sys::TimestampTz" becomes "pg_sys :: TimestampTz") which
breaks type mapping.

* Fix generated type for moving state in aggregates
  • Loading branch information
JamesGuthrie authored Jan 28, 2022
1 parent f3c1c25 commit 2452450
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ impl AggregateType {

pub(crate) fn entity_tokens(&self) -> Expr {
let ty = &self.ty;
let ty_string = ty.to_token_stream().to_string().replace(" ", "");
parse_quote! {
pgx::datum::sql_entity_graph::aggregate::AggregateType {
ty_source: stringify!(#ty),
ty_source: #ty_string,
ty_id: core::any::TypeId::of::<#ty>(),
full_path: core::any::type_name::<#ty>(),
name: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ impl MaybeNamedVariadicType {

fn entity_tokens(&self) -> Expr {
let ty = self.variadic_ty.as_ref().unwrap_or(&self.ty);
let ty_string = ty.to_token_stream().to_string().replace(" ", "");
let variadic = self.variadic_ty.is_some();
let name = self.name.iter();
parse_quote! {
pgx::datum::sql_entity_graph::aggregate::MaybeVariadicAggregateType {
agg_ty: pgx::datum::sql_entity_graph::aggregate::AggregateType {
ty_source: stringify!(#ty),
ty_source: #ty_string,
ty_id: core::any::TypeId::of::<#ty>(),
full_path: core::any::type_name::<#ty>(),
name: None#( .unwrap_or(Some(#name)) )*,
Expand Down
4 changes: 3 additions & 1 deletion pgx-utils/src/sql_entity_graph/pg_aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ impl PgAggregate {
let type_args_iter = &self.type_args.entity_tokens();
let type_order_by_iter = self.type_order_by.iter().map(|x| x.entity_tokens());
let type_moving_state_iter = self.type_moving_state.iter();
let type_moving_state_string = self.type_moving_state.as_ref().map(|t| { t.to_token_stream().to_string().replace(" ", "") });
let type_stype = self.type_stype.entity_tokens();
let const_parallel_iter = self.const_parallel.iter();
let const_finalize_modify_iter = self.const_finalize_modify.iter();
Expand Down Expand Up @@ -557,9 +558,10 @@ impl PgAggregate {
msfunc: None#( .unwrap_or(Some(stringify!(#fn_moving_state_iter))) )*,
minvfunc: None#( .unwrap_or(Some(stringify!(#fn_moving_state_inverse_iter))) )*,
mstype: None#( .unwrap_or(Some(pgx::datum::sql_entity_graph::aggregate::AggregateType {
ty_source: stringify!(#type_moving_state_iter),
ty_source: #type_moving_state_string,
ty_id: core::any::TypeId::of::<#type_moving_state_iter>(),
full_path: core::any::type_name::<#type_moving_state_iter>(),
name: None
})) )*,
mfinalfunc: None#( .unwrap_or(Some(stringify!(#fn_moving_finalize_iter))) )*,
mfinalfunc_modify: None#( .unwrap_or(#const_moving_finalize_modify_iter) )*,
Expand Down

0 comments on commit 2452450

Please sign in to comment.