mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-24 07:54:06 +00:00
Add RETURNS TABLE() support for CREATE FUNCTION in Postgresql (#1687)
Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
This commit is contained in:
parent
906f395341
commit
257da5a82c
3 changed files with 28 additions and 0 deletions
|
@ -45,6 +45,10 @@ pub enum EnumMember {
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||||
pub enum DataType {
|
pub enum DataType {
|
||||||
|
/// Table type in [postgresql]. e.g. CREATE FUNCTION RETURNS TABLE(...)
|
||||||
|
///
|
||||||
|
/// [postgresql]: https://www.postgresql.org/docs/15/sql-createfunction.html
|
||||||
|
Table(Vec<ColumnDef>),
|
||||||
/// Fixed-length character type e.g. CHARACTER(10)
|
/// Fixed-length character type e.g. CHARACTER(10)
|
||||||
Character(Option<CharacterLength>),
|
Character(Option<CharacterLength>),
|
||||||
/// Fixed-length char type e.g. CHAR(10)
|
/// Fixed-length char type e.g. CHAR(10)
|
||||||
|
@ -630,6 +634,7 @@ impl fmt::Display for DataType {
|
||||||
DataType::Unspecified => Ok(()),
|
DataType::Unspecified => Ok(()),
|
||||||
DataType::Trigger => write!(f, "TRIGGER"),
|
DataType::Trigger => write!(f, "TRIGGER"),
|
||||||
DataType::AnyType => write!(f, "ANY TYPE"),
|
DataType::AnyType => write!(f, "ANY TYPE"),
|
||||||
|
DataType::Table(fields) => write!(f, "TABLE({})", display_comma_separated(fields)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8867,6 +8867,10 @@ impl<'a> Parser<'a> {
|
||||||
let _ = self.parse_keyword(Keyword::TYPE);
|
let _ = self.parse_keyword(Keyword::TYPE);
|
||||||
Ok(DataType::AnyType)
|
Ok(DataType::AnyType)
|
||||||
}
|
}
|
||||||
|
Keyword::TABLE => {
|
||||||
|
let columns = self.parse_returns_table_columns()?;
|
||||||
|
Ok(DataType::Table(columns))
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
self.prev_token();
|
self.prev_token();
|
||||||
let type_name = self.parse_object_name(false)?;
|
let type_name = self.parse_object_name(false)?;
|
||||||
|
@ -8894,6 +8898,24 @@ impl<'a> Parser<'a> {
|
||||||
Ok((data, trailing_bracket))
|
Ok((data, trailing_bracket))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_returns_table_column(&mut self) -> Result<ColumnDef, ParserError> {
|
||||||
|
let name = self.parse_identifier()?;
|
||||||
|
let data_type = self.parse_data_type()?;
|
||||||
|
Ok(ColumnDef {
|
||||||
|
name,
|
||||||
|
data_type,
|
||||||
|
collation: None,
|
||||||
|
options: Vec::new(), // No constraints expected here
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_returns_table_columns(&mut self) -> Result<Vec<ColumnDef>, ParserError> {
|
||||||
|
self.expect_token(&Token::LParen)?;
|
||||||
|
let columns = self.parse_comma_separated(Parser::parse_returns_table_column)?;
|
||||||
|
self.expect_token(&Token::RParen)?;
|
||||||
|
Ok(columns)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_string_values(&mut self) -> Result<Vec<String>, ParserError> {
|
pub fn parse_string_values(&mut self) -> Result<Vec<String>, ParserError> {
|
||||||
self.expect_token(&Token::LParen)?;
|
self.expect_token(&Token::LParen)?;
|
||||||
let mut values = Vec::new();
|
let mut values = Vec::new();
|
||||||
|
|
|
@ -3803,6 +3803,7 @@ fn parse_create_function_detailed() {
|
||||||
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");
|
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");
|
||||||
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION increment(i INTEGER) RETURNS INTEGER LANGUAGE plpgsql AS $$ BEGIN RETURN i + 1; END; $$"#);
|
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION increment(i INTEGER) RETURNS INTEGER LANGUAGE plpgsql AS $$ BEGIN RETURN i + 1; END; $$"#);
|
||||||
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION no_arg() RETURNS VOID LANGUAGE plpgsql AS $$ BEGIN DELETE FROM my_table; END; $$"#);
|
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION no_arg() RETURNS VOID LANGUAGE plpgsql AS $$ BEGIN DELETE FROM my_table; END; $$"#);
|
||||||
|
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; $$"#);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_incorrect_create_function_parallel() {
|
fn parse_incorrect_create_function_parallel() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue