Skip to content

Comments

Add SETOF support for PostgreSQL function return types#2217

Open
fmguerreiro wants to merge 3 commits intoapache:mainfrom
fmguerreiro:setof-support
Open

Add SETOF support for PostgreSQL function return types#2217
fmguerreiro wants to merge 3 commits intoapache:mainfrom
fmguerreiro:setof-support

Conversation

@fmguerreiro
Copy link
Contributor

@fmguerreiro fmguerreiro commented Feb 13, 2026

PostgreSQL functions can return sets via RETURNS SETOF <type>. This is currently not supported by the parser, which fails when encountering SETOF after RETURNS.

Changes

  • Add SETOF keyword
  • Add DataType::SetOf(Box<DataType>) variant
  • Handle SETOF in parse_data_type_helper() so any DataType::SetOf round-trips through Display → parse
  • Tests for SETOF with TEXT, INTEGER, RECORD, and schema-qualified custom types

Example

CREATE FUNCTION get_users() RETURNS SETOF TEXT LANGUAGE sql AS 'SELECT name FROM users';
CREATE FUNCTION get_all() RETURNS SETOF my_schema."MyType" LANGUAGE sql AS 'SELECT * FROM t';

Reference

/// e.g. `CREATE FUNCTION ... RETURNS SETOF text`.
///
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html
SetOf(Box<DataType>),
Copy link
Contributor

Choose a reason for hiding this comment

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

would it make sense to represent this as a qualifier around the return type of a function instead? e.g.

enum FunctionReturnType {
    DataType(DataType),
    Setof(DataType)
}
CreateFunction { ... return_type: FunctionReturnType ... }

Thinking that would probably be better because it would be a bit confusing having a SETOF DataType that isn't a valid data type in any dialect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, makes sense! 62385ea

fmguerreiro added a commit to fmguerreiro/datafusion-sqlparser-rs that referenced this pull request Feb 20, 2026
SETOF is a PostgreSQL-specific modifier for function return types, not a
standalone data type. Introduce FunctionReturnType enum with DataType and
SetOf variants, replacing DataType::SetOf.

Addresses review feedback on apache#2217.
fmguerreiro added a commit to fmguerreiro/datafusion-sqlparser-rs that referenced this pull request Feb 20, 2026
SETOF is a PostgreSQL-specific modifier for function return types, not a
standalone data type. Introduce FunctionReturnType enum with DataType and
SetOf variants, replacing DataType::SetOf.

Addresses review feedback on apache#2217.
SETOF is a PostgreSQL-specific modifier for function return types, not a
standalone data type. Introduce FunctionReturnType enum with DataType and
SetOf variants, replacing DataType::SetOf.

Addresses review feedback on apache#2217.
pub enum FunctionReturnType {
/// `RETURNS <type>`
DataType(DataType),
/// `RETURNS SETOF <type>`
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we instead move the link to the postgres docs here? since the enum itself applies to all dialects, whereas only this variant is pg specific

}
}

impl Spanned for PartitionBoundValue {
Copy link
Contributor

Choose a reason for hiding this comment

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

is there any changes in this diff (if not can we revert it)?

Comment on lines +5887 to +5888
///
/// Handles `RETURNS SETOF <type>` and plain `RETURNS <type>`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
///
/// Handles `RETURNS SETOF <type>` and plain `RETURNS <type>`.

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