Skip to content

Commit 5cda5de

Browse files
committed
Add support for the TABLE returns datatype in create function for postgresql. Fixes #1687.
1 parent 269967a commit 5cda5de

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
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

+26-1
Original file line numberDiff line numberDiff line change
@@ -4535,7 +4535,14 @@ impl<'a> Parser<'a> {
45354535
self.expect_token(&Token::RParen)?;
45364536

45374537
let return_type = if self.parse_keyword(Keyword::RETURNS) {
4538-
Some(self.parse_data_type()?)
4538+
if dialect_of!(self is PostgreSqlDialect | GenericDialect)
4539+
&& self.parse_keyword(Keyword::TABLE)
4540+
{
4541+
let columns = self.parse_parenthesized_columns()?;
4542+
Some(DataType::Table(columns))
4543+
} else {
4544+
Some(self.parse_data_type()?)
4545+
}
45394546
} else {
45404547
None
45414548
};
@@ -8866,6 +8873,24 @@ impl<'a> Parser<'a> {
88668873
Ok((data, trailing_bracket))
88678874
}
88688875

8876+
fn parse_returns_table_column(&mut self) -> Result<ColumnDef, ParserError> {
8877+
let name = self.parse_identifier()?;
8878+
let data_type = self.parse_data_type()?;
8879+
Ok(ColumnDef {
8880+
name,
8881+
data_type,
8882+
collation: None,
8883+
options: Vec::new(), // No constraints expected here
8884+
})
8885+
}
8886+
8887+
fn parse_parenthesized_columns(&mut self) -> Result<Vec<ColumnDef>, ParserError> {
8888+
self.expect_token(&Token::LParen)?;
8889+
let columns = self.parse_comma_separated(Parser::parse_returns_table_column)?;
8890+
self.expect_token(&Token::RParen)?;
8891+
Ok(columns)
8892+
}
8893+
88698894
pub fn parse_string_values(&mut self) -> Result<Vec<String>, ParserError> {
88708895
self.expect_token(&Token::LParen)?;
88718896
let mut values = Vec::new();

tests/sqlparser_postgres.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3802,6 +3802,7 @@ fn parse_create_function_detailed() {
38023802
pg_and_generic().verified_stmt("CREATE OR REPLACE FUNCTION add(a INTEGER, IN b INTEGER = 1) RETURNS INTEGER LANGUAGE SQL STABLE PARALLEL UNSAFE RETURN a + b");
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; $$"#);
3805+
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; $$"#);
38053806
}
38063807
#[test]
38073808
fn parse_incorrect_create_function_parallel() {

0 commit comments

Comments
 (0)