Skip to content

Commit 257da5a

Browse files
remysaissyiffyio
andauthored
Add RETURNS TABLE() support for CREATE FUNCTION in Postgresql (#1687)
Co-authored-by: Ifeanyi Ubah <[email protected]>
1 parent 906f395 commit 257da5a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/ast/data_type.rs

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub enum EnumMember {
4545
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4646
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4747
pub enum DataType {
48+
/// Table type in [postgresql]. e.g. CREATE FUNCTION RETURNS TABLE(...)
49+
///
50+
/// [postgresql]: https://www.postgresql.org/docs/15/sql-createfunction.html
51+
Table(Vec<ColumnDef>),
4852
/// Fixed-length character type e.g. CHARACTER(10)
4953
Character(Option<CharacterLength>),
5054
/// Fixed-length char type e.g. CHAR(10)
@@ -630,6 +634,7 @@ impl fmt::Display for DataType {
630634
DataType::Unspecified => Ok(()),
631635
DataType::Trigger => write!(f, "TRIGGER"),
632636
DataType::AnyType => write!(f, "ANY TYPE"),
637+
DataType::Table(fields) => write!(f, "TABLE({})", display_comma_separated(fields)),
633638
}
634639
}
635640
}

src/parser/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -8867,6 +8867,10 @@ impl<'a> Parser<'a> {
88678867
let _ = self.parse_keyword(Keyword::TYPE);
88688868
Ok(DataType::AnyType)
88698869
}
8870+
Keyword::TABLE => {
8871+
let columns = self.parse_returns_table_columns()?;
8872+
Ok(DataType::Table(columns))
8873+
}
88708874
_ => {
88718875
self.prev_token();
88728876
let type_name = self.parse_object_name(false)?;
@@ -8894,6 +8898,24 @@ impl<'a> Parser<'a> {
88948898
Ok((data, trailing_bracket))
88958899
}
88968900

8901+
fn parse_returns_table_column(&mut self) -> Result<ColumnDef, ParserError> {
8902+
let name = self.parse_identifier()?;
8903+
let data_type = self.parse_data_type()?;
8904+
Ok(ColumnDef {
8905+
name,
8906+
data_type,
8907+
collation: None,
8908+
options: Vec::new(), // No constraints expected here
8909+
})
8910+
}
8911+
8912+
fn parse_returns_table_columns(&mut self) -> Result<Vec<ColumnDef>, ParserError> {
8913+
self.expect_token(&Token::LParen)?;
8914+
let columns = self.parse_comma_separated(Parser::parse_returns_table_column)?;
8915+
self.expect_token(&Token::RParen)?;
8916+
Ok(columns)
8917+
}
8918+
88978919
pub fn parse_string_values(&mut self) -> Result<Vec<String>, ParserError> {
88988920
self.expect_token(&Token::LParen)?;
88998921
let mut values = Vec::new();

tests/sqlparser_postgres.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3803,6 +3803,7 @@ fn parse_create_function_detailed() {
38033803
pg_and_generic().verified_stmt("CREATE OR REPLACE FUNCTION add(a INTEGER, IN b INTEGER = 1) RETURNS INTEGER LANGUAGE SQL STABLE CALLED ON NULL INPUT PARALLEL UNSAFE RETURN a + b");
38043804
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION increment(i INTEGER) RETURNS INTEGER LANGUAGE plpgsql AS $$ BEGIN RETURN i + 1; END; $$"#);
38053805
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION no_arg() RETURNS VOID LANGUAGE plpgsql AS $$ BEGIN DELETE FROM my_table; END; $$"#);
3806+
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION return_table(i INTEGER) RETURNS TABLE(id UUID, is_active BOOLEAN) LANGUAGE plpgsql AS $$ BEGIN RETURN QUERY SELECT NULL::UUID, NULL::BOOLEAN; END; $$"#);
38063807
}
38073808
#[test]
38083809
fn parse_incorrect_create_function_parallel() {

0 commit comments

Comments
 (0)