Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub enum EnumMember {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum DataType {
/// Set-returning function type in [PostgreSQL], e.g. CREATE FUNCTION RETURNS SETOF UUID.
///
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html
SetOf(Box<DataType>),
/// Table type in [PostgreSQL], e.g. CREATE FUNCTION RETURNS TABLE(...).
///
/// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
Expand Down Expand Up @@ -501,6 +505,7 @@ pub enum DataType {
impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DataType::SetOf(data_type) => write!(f, "SETOF {data_type}"),
DataType::Character(size) => format_character_string_type(f, "CHARACTER", size),
DataType::Char(size) => format_character_string_type(f, "CHAR", size),
DataType::CharacterVarying(size) => {
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ define_keywords!(
SESSION_USER,
SET,
SETERROR,
SETOF,
SETS,
SETTINGS,
SHARE,
Expand Down
8 changes: 7 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5581,7 +5581,13 @@ impl<'a> Parser<'a> {
self.expect_token(&Token::RParen)?;

let return_type = if self.parse_keyword(Keyword::RETURNS) {
Some(self.parse_data_type()?)
let setof = self.parse_keyword(Keyword::SETOF);
let return_type = self.parse_data_type()?;
Some(if setof {
DataType::SetOf(Box::new(return_type))
} else {
return_type
})
} else {
None
};
Expand Down
25 changes: 25 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4642,6 +4642,31 @@ fn parse_create_function_detailed() {
);
}

#[test]
fn parse_create_function_returns_setof() {
let cases = [
"CREATE FUNCTION any_ids() RETURNS SETOF UUID LANGUAGE sql STABLE AS 'SELECT ''00000000-0000-0000-0000-000000000000''::uuid'",
"CREATE FUNCTION ids_for_user(p_user_id UUID) RETURNS SETOF UUID LANGUAGE sql STABLE AS 'SELECT p_user_id'",
"CREATE FUNCTION ids_from_array() RETURNS SETOF UUID LANGUAGE sql STABLE AS 'SELECT unnest(ARRAY[''00000000-0000-0000-0000-000000000000''::uuid])'",
];

for sql in cases {
match pg_and_generic().verified_stmt(sql) {
Statement::CreateFunction(CreateFunction {
return_type,
language,
behavior,
..
}) => {
assert_eq!(return_type, Some(DataType::SetOf(Box::new(DataType::Uuid))));
assert_eq!(language, Some(Ident::new("sql")));
assert_eq!(behavior, Some(FunctionBehavior::Stable));
}
_ => panic!("Expected CreateFunction"),
}
}
}

#[test]
fn parse_create_function_with_security() {
let sql =
Expand Down
Loading