Skip to content

Commit 364e099

Browse files
committed
Add support for the TABLE returns datatype in create function for postgresql. Fixes apache#1687.
# Conflicts: # tests/sqlparser_postgres.rs
1 parent 447142c commit 364e099

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/ast/data_type.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4563,7 +4563,14 @@ impl<'a> Parser<'a> {
45634563
self.expect_token(&Token::RParen)?;
45644564

45654565
let return_type = if self.parse_keyword(Keyword::RETURNS) {
4566-
Some(self.parse_data_type()?)
4566+
if dialect_of!(self is PostgreSqlDialect | GenericDialect)
4567+
&& self.parse_keyword(Keyword::TABLE)
4568+
{
4569+
let columns = self.parse_parenthesized_columns()?;
4570+
Some(DataType::Table(columns))
4571+
} else {
4572+
Some(self.parse_data_type()?)
4573+
}
45674574
} else {
45684575
None
45694576
};
@@ -8894,6 +8901,24 @@ impl<'a> Parser<'a> {
88948901
Ok((data, trailing_bracket))
88958902
}
88968903

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

tests/sqlparser_postgres.rs

Lines changed: 3 additions & 2 deletions
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() {
@@ -4372,7 +4373,7 @@ fn parse_join_constraint_unnest_alias() {
43724373
with_ordinality: false,
43734374
},
43744375
global: false,
4375-
join_operator: JoinOperator::Join(JoinConstraint::On(Expr::BinaryOp {
4376+
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
43764377
left: Box::new(Expr::Identifier("c1".into())),
43774378
op: BinaryOperator::Eq,
43784379
right: Box::new(Expr::Identifier("c2".into())),
@@ -5147,7 +5148,7 @@ fn parse_trigger_related_functions() {
51475148
temporary: false,
51485149
if_not_exists: false,
51495150
name: ObjectName::from(vec![Ident::new("emp_stamp")]),
5150-
args: Some(vec![]),
5151+
args: None,
51515152
return_type: Some(DataType::Trigger),
51525153
function_body: Some(
51535154
CreateFunctionBody::AsBeforeOptions(

0 commit comments

Comments
 (0)