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
Unable to create SRF function with SETOF #200
Comments
plrust doesn’t yet support custom types like this. It only supports the built-in Postgres types |
The I guess I don't see any way to create an SRF then. Am I missing something? The only other way to define a return type for CREATE TABLE foo (id BIGINT);
CREATE FUNCTION spi_srf()
RETURNS SETOF foo
LANGUAGE plrust
AS
$$
...
$$ |
You can return setof text or bigint for example. Not any kind of UDT. |
Okay. Changing to CREATE FUNCTION spi_srf()
RETURNS SETOF BIGINT
LANGUAGE plrust
AS
$$
let query = "SELECT id::BIGINT FROM generate_series(1, 3) id;";
Spi::connect(|client| {
let mut results = Vec::new();
let mut tup_table = client.select(query, None, None)?;
while let Some(row) = tup_table.next() {
let id = row["id"].value::<i64>();
results.push((id,));
}
Ok(TableIterator::new(results.into_iter()))
})
$$
; Full error
|
All plrust functions return Your function here, at the end of the Spi::connect(), returns However, and this is fun, plrust doesn't support TableIterator yet, only SetOfIterator, so in fact, your function should be rewritten as: CREATE FUNCTION spi_srf()
RETURNS SETOF BIGINT
LANGUAGE plrust
AS
$$
let query = "SELECT id::BIGINT FROM generate_series(1, 3) id;";
Spi::connect(|client| {
let mut results = Vec::new();
let mut tup_table = client.select(query, None, None)?;
while let Some(row) = tup_table.next() {
let id = row["id"].value::<i64>()?;
results.push(id);
}
Ok(Some(SetOfIterator::new(results)))
})
$$; Also note the |
however, it does look like there's a bug in plrust preventing this from working. I'll fix... |
This adds a `<'a>` lifetime to all user functions so that the few cases where arguments are borrowed (`&'a str`) and return types might borrow (`SetOfIterator<'a, ...>)` can do so. Also blindly adds the `#[allow(unused_lifetime)]` annotation to all instances of user functions so that compilation warnings won't happen in the cases where all args/return don't borrow.
I've put up a PR to fix the plrust-side of the problem. #202 We'll get this merged ASAP. |
This adds a `<'a>` lifetime to all user functions so that the few cases where arguments are borrowed (`&'a str`) and return types might borrow (`SetOfIterator<'a, ...>)` can do so. Also blindly adds the `#[allow(unused_lifetime)]` annotation to all instances of user functions so that compilation warnings won't happen in the cases where all args/return don't borrow.
Success, thank you! Closing. select spi_srf();
┌─────────┐
│ spi_srf │
╞═════════╡
│ 1 │
│ 2 │
│ 3 │
└─────────┘ |
I have not been able to create a set returning function using
SETOF
. I get the same basic error using both a custom type for the return as well as the genericrecord
type.Use custom type
This fails with the
\errverbose
output:Confirmed the custom type is the
oid
mentioned.Record type
Attempting to return
SETOF record
also fails with the same basic error.Same error location on the
RECORDOID
.The text was updated successfully, but these errors were encountered: