pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.65k stars 248 forks source link

Weird error when trying to create function/aggregate with PgHeapTuple arguments #1585

Open Feyko opened 8 months ago

Feyko commented 8 months ago

Tested with pgrx 0.11.3 & 0.12.0-alpha.0

Error:

Error: 
   0: Could not write SQL to /home/feyko/.pgrx/13.14/pgrx-install/share/postgresql/extension/timestuff--0.0.0.sql
   1: Macro expansion time suggested a composite_type!() in return

Repros:

#[pg_extern]
fn first_test(record: PgHeapTuple<AllocatedByRust>){}
#[derive(Clone, Default, PostgresType, Serialize, Deserialize)]
pub struct AggregateIncrementalRecords {
    attr_count: i16,
}

#[pg_aggregate]
impl Aggregate for AggregateIncrementalRecords {
    const INITIAL_CONDITION: Option<&'static str> = Some(r#"{ "attr_count": 0 }"#);
    type Args = PgHeapTuple<'static, AllocatedByRust>;

    fn state(
        mut current: Self::State,
        arg: Self::Args,
        _fcinfo: pg_sys::FunctionCallInfo
    ) -> Self::State {
        return current
    }
}
workingjubilee commented 8 months ago

Spoke too soon.

Hm, this remains a problem even on develop. Joy.

workingjubilee commented 6 months ago

This code currently compiles on develop:

use pgrx::prelude::*;

#[pg_extern]
fn aggregate_test(_record: PgHeapTuple<AllocatedByRust>){}

#[derive(Clone, Default, PostgresType)]
#[bikeshed_postgres_type_manually_impl_from_into_datum]
pub struct AggregateIncrementalRecords {
    attr_count: i16,
}

impl IntoDatum for AggregateIncrementalRecords {
    fn into_datum(self) -> Option<pg_sys::Datum> {
        todo!()
    }

    fn type_oid() -> pg_sys::Oid {
        todo!()
    }
}

impl FromDatum for AggregateIncrementalRecords {
    unsafe fn from_polymorphic_datum(_: pg_sys::Datum, _: bool, _: pg_sys::Oid) -> Option<Self> {
        todo!()
    }
}

#[pg_aggregate]
impl Aggregate for AggregateIncrementalRecords {
    const INITIAL_CONDITION: Option<&'static str> = Some(r#"{ "attr_count": 0 }"#);
    type Args = PgHeapTuple<'static, AllocatedByRust>;

    fn state(
        current: Self::State,
        _arg: Self::Args,
        _fcinfo: pg_sys::FunctionCallInfo
    ) -> Self::State {
        return current
    }
}

fn main() {}
workingjubilee commented 6 months ago

As does this:

use serde::{Serialize, Deserialize};
use pgrx::prelude::*;

#[pg_extern]
fn aggregate_test(_record: PgHeapTuple<AllocatedByRust>){}

#[derive(Clone, Default, PostgresType, Serialize, Deserialize)]
pub struct AggregateIncrementalRecords {
    attr_count: i16,
}

#[pg_aggregate]
impl Aggregate for AggregateIncrementalRecords {
    const INITIAL_CONDITION: Option<&'static str> = Some(r#"{ "attr_count": 0 }"#);
    type Args = PgHeapTuple<'static, AllocatedByRust>;

    fn state(
        current: Self::State,
        _arg: Self::Args,
        _fcinfo: pg_sys::FunctionCallInfo
    ) -> Self::State {
        return current
    }
}

fn main() {}
workingjubilee commented 6 months ago

Oh, I'm a fool, I keep forgetting schema gen is a separate pass from build.

workingjubilee commented 6 months ago

Okay, Okay, I don't believe this is supposed to be supported, really, but it's not producing a useful error.