From c942614970f78e6c47ed060168deb31144f8748e Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Sun, 20 Nov 2022 20:39:41 +0800 Subject: [PATCH 1/6] support basic pg CREATE FUNCTION Signed-off-by: Runji Wang --- src/ast/mod.rs | 66 +++++++++++++++++++++++++++++++---- src/keywords.rs | 3 ++ src/parser.rs | 69 +++++++++++++++++++++++++++++++------ tests/sqlparser_hive.rs | 22 ++++++------ tests/sqlparser_postgres.rs | 22 ++++++++++++ 5 files changed, 155 insertions(+), 27 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index f568c39603..eb6937630b 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1363,11 +1363,13 @@ pub enum Statement { /// CREATE FUNCTION /// /// Hive: https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction + /// Postgres: https://www.postgresql.org/docs/15/sql-createfunction.html CreateFunction { temporary: bool, name: ObjectName, - class_name: String, - using: Option, + args: Option>, + return_type: Option, + bodies: Vec, }, /// `ASSERT [AS ]` Assert { @@ -1826,17 +1828,22 @@ impl fmt::Display for Statement { Statement::CreateFunction { temporary, name, - class_name, - using, + args, + return_type, + bodies, } => { write!( f, - "CREATE {temp}FUNCTION {name} AS '{class_name}'", + "CREATE {temp}FUNCTION {name}", temp = if *temporary { "TEMPORARY " } else { "" }, )?; - if let Some(u) = using { - write!(f, " {}", u)?; + if let Some(args) = args { + write!(f, "{}", display_comma_separated(args))?; + } + if let Some(return_type) = return_type { + write!(f, " RETURNS {}", return_type)?; } + write!(f, " {}", display_separated(bodies, " "))?; Ok(()) } Statement::CreateView { @@ -3598,6 +3605,51 @@ impl fmt::Display for ContextModifier { } } +/// These attributes inform the query optimizer about the behavior of the function. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub enum FunctionBehavior { + Immutable, + Stable, + Volatile, +} + +impl fmt::Display for FunctionBehavior { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + FunctionBehavior::Immutable => write!(f, "IMMUTABLE"), + FunctionBehavior::Stable => write!(f, "STABLE"), + FunctionBehavior::Volatile => write!(f, "VOLATILE"), + } + } +} + +/// Postgres: https://www.postgresql.org/docs/15/sql-createfunction.html +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[non_exhaustive] +pub enum CreateFunctionBody { + /// AS 'definition' + As(String), + /// LANGUAGE lang_name + Language(Ident), + /// IMMUTABLE | STABLE | VOLATILE + Behavior(FunctionBehavior), + /// USING ... (Hive) + Using(CreateFunctionUsing), +} + +impl fmt::Display for CreateFunctionBody { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Self::As(definition) => write!(f, "AS '{definition}'"), + Self::Language(lang) => write!(f, "LANGUAGE {lang}"), + Self::Behavior(behavior) => write!(f, "{behavior}"), + Self::Using(using) => write!(f, "{using}"), + } + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CreateFunctionUsing { diff --git a/src/keywords.rs b/src/keywords.rs index 47dd21e217..43a0e5b4e2 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -279,6 +279,7 @@ define_keywords!( IF, IGNORE, ILIKE, + IMMUTABLE, IN, INCREMENT, INDEX, @@ -511,6 +512,7 @@ define_keywords!( SQLSTATE, SQLWARNING, SQRT, + STABLE, START, STATIC, STATISTICS, @@ -597,6 +599,7 @@ define_keywords!( VERSIONING, VIEW, VIRTUAL, + VOLATILE, WEEK, WHEN, WHENEVER, diff --git a/src/parser.rs b/src/parser.rs index f91223c2f2..24f88921c8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1957,7 +1957,7 @@ impl<'a> Parser<'a> { self.parse_create_schema() } else if self.parse_keyword(Keyword::DATABASE) { self.parse_create_database() - } else if dialect_of!(self is HiveDialect) && self.parse_keyword(Keyword::FUNCTION) { + } else if self.parse_keyword(Keyword::FUNCTION) { self.parse_create_function(temporary) } else if self.parse_keyword(Keyword::ROLE) { self.parse_create_role() @@ -2171,16 +2171,65 @@ impl<'a> Parser<'a> { pub fn parse_create_function(&mut self, temporary: bool) -> Result { let name = self.parse_object_name()?; - self.expect_keyword(Keyword::AS)?; - let class_name = self.parse_literal_string()?; - let using = self.parse_optional_create_function_using()?; + if dialect_of!(self is HiveDialect) { + self.expect_keyword(Keyword::AS)?; + let class_name = self.parse_literal_string()?; + let mut bodies = vec![CreateFunctionBody::As(class_name)]; + if let Some(using) = self.parse_optional_create_function_using()? { + bodies.push(CreateFunctionBody::Using(using)); + } - Ok(Statement::CreateFunction { - temporary, - name, - class_name, - using, - }) + Ok(Statement::CreateFunction { + temporary, + name, + args: None, + return_type: None, + bodies, + }) + } else if dialect_of!(self is PostgreSqlDialect) { + self.expect_token(&Token::LParen)?; + let args = self.parse_comma_separated(Parser::parse_data_type)?; + self.expect_token(&Token::RParen)?; + + let return_type = if self.parse_keyword(Keyword::RETURNS) { + Some(self.parse_data_type()?) + } else { + None + }; + + let mut bodies = vec![]; + while let Ok(body) = self.parse_create_function_body() { + bodies.push(body); + } + + Ok(Statement::CreateFunction { + temporary, + name, + args: Some(args), + return_type, + bodies, + }) + } else { + Err(ParserError::ParserError( + "Expected an object type after CREATE, found: FUNCTION".to_string(), + )) + } + } + + pub fn parse_create_function_body(&mut self) -> Result { + if self.parse_keyword(Keyword::AS) { + Ok(CreateFunctionBody::As(self.parse_literal_string()?)) + } else if self.parse_keyword(Keyword::LANGUAGE) { + Ok(CreateFunctionBody::Language(self.parse_identifier()?)) + } else if self.parse_keyword(Keyword::IMMUTABLE) { + Ok(CreateFunctionBody::Behavior(FunctionBehavior::Immutable)) + } else if self.parse_keyword(Keyword::STABLE) { + Ok(CreateFunctionBody::Behavior(FunctionBehavior::Stable)) + } else if self.parse_keyword(Keyword::VOLATILE) { + Ok(CreateFunctionBody::Behavior(FunctionBehavior::Volatile)) + } else { + self.expected("AS or USING", self.peek_token()) + } } pub fn parse_create_external_table( diff --git a/tests/sqlparser_hive.rs b/tests/sqlparser_hive.rs index 070f55089c..a0e499108f 100644 --- a/tests/sqlparser_hive.rs +++ b/tests/sqlparser_hive.rs @@ -16,8 +16,8 @@ //! is also tested (on the inputs it can handle). use sqlparser::ast::{ - CreateFunctionUsing, Expr, Function, Ident, ObjectName, SelectItem, Statement, TableFactor, - UnaryOperator, Value, + CreateFunctionBody, CreateFunctionUsing, Expr, Function, Ident, ObjectName, SelectItem, + Statement, TableFactor, UnaryOperator, Value, }; use sqlparser::dialect::{GenericDialect, HiveDialect}; use sqlparser::parser::ParserError; @@ -244,17 +244,19 @@ fn parse_create_function() { Statement::CreateFunction { temporary, name, - class_name, - using, + bodies, + .. } => { assert!(temporary); - assert_eq!("mydb.myfunc", name.to_string()); - assert_eq!("org.random.class.Name", class_name); + assert_eq!(name.to_string(), "mydb.myfunc"); assert_eq!( - using, - Some(CreateFunctionUsing::Jar( - "hdfs://somewhere.com:8020/very/far".to_string() - )) + bodies, + vec![ + CreateFunctionBody::As("org.random.class.Name".to_string()), + CreateFunctionBody::Using(CreateFunctionUsing::Jar( + "hdfs://somewhere.com:8020/very/far".to_string() + )) + ] ) } _ => unreachable!(), diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 5cc3339352..49bf910260 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2186,3 +2186,25 @@ fn parse_similar_to() { chk(false); chk(true); } + +#[test] +fn parse_create_function() { + let sql = "CREATE FUNCTION add(integer, integer) RETURNS integer + AS 'select $1 + $2;' + LANGUAGE SQL + IMMUTABLE;"; + assert_eq!( + pg().one_statement_parses_to(sql, ""), + Statement::CreateFunction { + temporary: false, + name: ObjectName(vec![Ident::new("add")]), + args: Some(vec![DataType::Integer(None), DataType::Integer(None)]), + return_type: Some(DataType::Integer(None)), + bodies: vec![ + CreateFunctionBody::As("select $1 + $2;".into()), + CreateFunctionBody::Language("SQL".into()), + CreateFunctionBody::Behavior(FunctionBehavior::Immutable), + ], + } + ); +} From d8367af89f1e865675f66327780690d1f3bf8a07 Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Sun, 20 Nov 2022 22:24:00 +0800 Subject: [PATCH 2/6] support function argument Signed-off-by: Runji Wang --- src/ast/mod.rs | 72 ++++++++++++++++++++++++++++++++++++- src/parser.rs | 41 +++++++++++++++++++-- tests/sqlparser_postgres.rs | 31 +++++++++++++++- 3 files changed, 140 insertions(+), 4 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index eb6937630b..ac3152b622 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1367,7 +1367,7 @@ pub enum Statement { CreateFunction { temporary: bool, name: ObjectName, - args: Option>, + args: Option>, return_type: Option, bodies: Vec, }, @@ -3605,6 +3605,73 @@ impl fmt::Display for ContextModifier { } } +/// Function argument in CREATE FUNCTION. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct CreateFunctionArg { + pub mode: Option, + pub name: Option, + pub data_type: DataType, + pub default_expr: Option, +} + +impl CreateFunctionArg { + /// Returns an unnamed argument. + pub fn unnamed(data_type: DataType) -> Self { + Self { + mode: None, + name: None, + data_type, + default_expr: None, + } + } + + /// Returns an argument with name. + pub fn with_name(name: &str, data_type: DataType) -> Self { + Self { + mode: None, + name: Some(name.into()), + data_type, + default_expr: None, + } + } +} + +impl fmt::Display for CreateFunctionArg { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(mode) = &self.mode { + write!(f, "{} ", mode)?; + } + if let Some(name) = &self.name { + write!(f, "{} ", name)?; + } + write!(f, "{}", self.data_type)?; + if let Some(default_expr) = &self.default_expr { + write!(f, " DEFAULT {}", default_expr)?; + } + Ok(()) + } +} + +/// The mode of an argument in CREATE FUNCTION. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub enum ArgMode { + In, + Out, + InOut, +} + +impl fmt::Display for ArgMode { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + ArgMode::In => write!(f, "IN"), + ArgMode::Out => write!(f, "OUT"), + ArgMode::InOut => write!(f, "INOUT"), + } + } +} + /// These attributes inform the query optimizer about the behavior of the function. #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] @@ -3635,6 +3702,8 @@ pub enum CreateFunctionBody { Language(Ident), /// IMMUTABLE | STABLE | VOLATILE Behavior(FunctionBehavior), + /// RETURN expression + Return(Expr), /// USING ... (Hive) Using(CreateFunctionUsing), } @@ -3645,6 +3714,7 @@ impl fmt::Display for CreateFunctionBody { Self::As(definition) => write!(f, "AS '{definition}'"), Self::Language(lang) => write!(f, "LANGUAGE {lang}"), Self::Behavior(behavior) => write!(f, "{behavior}"), + Self::Return(expr) => write!(f, "RETURN {expr}"), Self::Using(using) => write!(f, "{using}"), } } diff --git a/src/parser.rs b/src/parser.rs index 24f88921c8..a5c1462fd1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2188,7 +2188,7 @@ impl<'a> Parser<'a> { }) } else if dialect_of!(self is PostgreSqlDialect) { self.expect_token(&Token::LParen)?; - let args = self.parse_comma_separated(Parser::parse_data_type)?; + let args = self.parse_comma_separated(Parser::parse_create_function_arg)?; self.expect_token(&Token::RParen)?; let return_type = if self.parse_keyword(Keyword::RETURNS) { @@ -2216,7 +2216,41 @@ impl<'a> Parser<'a> { } } - pub fn parse_create_function_body(&mut self) -> Result { + fn parse_create_function_arg(&mut self) -> Result { + let mode = if self.parse_keyword(Keyword::IN) { + Some(ArgMode::In) + } else if self.parse_keyword(Keyword::OUT) { + Some(ArgMode::Out) + } else if self.parse_keyword(Keyword::INOUT) { + Some(ArgMode::InOut) + } else { + None + }; + + // parse: [ argname ] argtype + let mut name = None; + let mut data_type = self.parse_data_type()?; + if let DataType::Custom(n, _) = &data_type { + // the first token is actually a name + name = Some(n.0[0].clone()); + data_type = self.parse_data_type()?; + } + + let default_expr = if self.parse_keyword(Keyword::DEFAULT) || self.consume_token(&Token::Eq) + { + Some(self.parse_expr()?) + } else { + None + }; + Ok(CreateFunctionArg { + mode, + name, + data_type, + default_expr, + }) + } + + fn parse_create_function_body(&mut self) -> Result { if self.parse_keyword(Keyword::AS) { Ok(CreateFunctionBody::As(self.parse_literal_string()?)) } else if self.parse_keyword(Keyword::LANGUAGE) { @@ -2227,6 +2261,9 @@ impl<'a> Parser<'a> { Ok(CreateFunctionBody::Behavior(FunctionBehavior::Stable)) } else if self.parse_keyword(Keyword::VOLATILE) { Ok(CreateFunctionBody::Behavior(FunctionBehavior::Volatile)) + } else if self.parse_keyword(Keyword::RETURN) { + let expr = self.parse_expr()?; + Ok(CreateFunctionBody::Return(expr)) } else { self.expected("AS or USING", self.peek_token()) } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 49bf910260..726b35b143 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2198,7 +2198,10 @@ fn parse_create_function() { Statement::CreateFunction { temporary: false, name: ObjectName(vec![Ident::new("add")]), - args: Some(vec![DataType::Integer(None), DataType::Integer(None)]), + args: Some(vec![ + CreateFunctionArg::unnamed(DataType::Integer(None)), + CreateFunctionArg::unnamed(DataType::Integer(None)), + ]), return_type: Some(DataType::Integer(None)), bodies: vec![ CreateFunctionBody::As("select $1 + $2;".into()), @@ -2207,4 +2210,30 @@ fn parse_create_function() { ], } ); + + let sql = "CREATE FUNCTION add(a integer, b integer) RETURNS integer + LANGUAGE SQL + IMMUTABLE + RETURN a + b;"; + assert_eq!( + pg().one_statement_parses_to(sql, ""), + Statement::CreateFunction { + temporary: false, + name: ObjectName(vec![Ident::new("add")]), + args: Some(vec![ + CreateFunctionArg::with_name("a", DataType::Integer(None)), + CreateFunctionArg::with_name("b", DataType::Integer(None)), + ]), + return_type: Some(DataType::Integer(None)), + bodies: vec![ + CreateFunctionBody::Language("SQL".into()), + CreateFunctionBody::Behavior(FunctionBehavior::Immutable), + CreateFunctionBody::Return(Expr::BinaryOp { + left: Box::new(Expr::Identifier("a".into())), + op: BinaryOperator::Plus, + right: Box::new(Expr::Identifier("b".into())), + }), + ], + } + ); } From e2874d79ad7697802710e4a3920ef9e9bae1ec83 Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Mon, 21 Nov 2022 11:00:23 +0800 Subject: [PATCH 3/6] fix display and use verify in test Signed-off-by: Runji Wang --- src/ast/mod.rs | 4 ++-- tests/sqlparser_postgres.rs | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index ac3152b622..ad12b1dc18 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1838,7 +1838,7 @@ impl fmt::Display for Statement { temp = if *temporary { "TEMPORARY " } else { "" }, )?; if let Some(args) = args { - write!(f, "{}", display_comma_separated(args))?; + write!(f, "({})", display_comma_separated(args))?; } if let Some(return_type) = return_type { write!(f, " RETURNS {}", return_type)?; @@ -3647,7 +3647,7 @@ impl fmt::Display for CreateFunctionArg { } write!(f, "{}", self.data_type)?; if let Some(default_expr) = &self.default_expr { - write!(f, " DEFAULT {}", default_expr)?; + write!(f, " = {}", default_expr)?; } Ok(()) } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 726b35b143..9609403df6 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2189,12 +2189,9 @@ fn parse_similar_to() { #[test] fn parse_create_function() { - let sql = "CREATE FUNCTION add(integer, integer) RETURNS integer - AS 'select $1 + $2;' - LANGUAGE SQL - IMMUTABLE;"; + let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE"; assert_eq!( - pg().one_statement_parses_to(sql, ""), + pg().verified_stmt(sql), Statement::CreateFunction { temporary: false, name: ObjectName(vec![Ident::new("add")]), @@ -2211,18 +2208,20 @@ fn parse_create_function() { } ); - let sql = "CREATE FUNCTION add(a integer, b integer) RETURNS integer - LANGUAGE SQL - IMMUTABLE - RETURN a + b;"; + let sql = "CREATE FUNCTION add(a INTEGER, IN b INTEGER = 1) RETURNS INTEGER LANGUAGE SQL IMMUTABLE RETURN a + b"; assert_eq!( - pg().one_statement_parses_to(sql, ""), + pg().verified_stmt(sql), Statement::CreateFunction { temporary: false, name: ObjectName(vec![Ident::new("add")]), args: Some(vec![ CreateFunctionArg::with_name("a", DataType::Integer(None)), - CreateFunctionArg::with_name("b", DataType::Integer(None)), + CreateFunctionArg { + mode: Some(ArgMode::In), + name: Some("b".into()), + data_type: DataType::Integer(None), + default_expr: Some(Expr::Value(Value::Number("1".into(), false))), + } ]), return_type: Some(DataType::Integer(None)), bodies: vec![ From 239192dce0babb03a6d0e04ff4f5005b763926de Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Mon, 21 Nov 2022 11:10:41 +0800 Subject: [PATCH 4/6] support OR REPLACE Signed-off-by: Runji Wang --- src/ast/mod.rs | 5 ++++- src/parser.rs | 24 +++++++++++++++--------- tests/sqlparser_postgres.rs | 4 +++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index ad12b1dc18..cf7dc1cd42 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1365,6 +1365,7 @@ pub enum Statement { /// Hive: https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction /// Postgres: https://www.postgresql.org/docs/15/sql-createfunction.html CreateFunction { + or_replace: bool, temporary: bool, name: ObjectName, args: Option>, @@ -1826,6 +1827,7 @@ impl fmt::Display for Statement { Ok(()) } Statement::CreateFunction { + or_replace, temporary, name, args, @@ -1834,8 +1836,9 @@ impl fmt::Display for Statement { } => { write!( f, - "CREATE {temp}FUNCTION {name}", + "CREATE {or_replace}{temp}FUNCTION {name}", temp = if *temporary { "TEMPORARY " } else { "" }, + or_replace = if *or_replace { "OR REPLACE " } else { "" }, )?; if let Some(args) = args { write!(f, "({})", display_comma_separated(args))?; diff --git a/src/parser.rs b/src/parser.rs index a5c1462fd1..3a1c8dbde8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1942,9 +1942,11 @@ impl<'a> Parser<'a> { self.parse_create_view(or_replace) } else if self.parse_keyword(Keyword::EXTERNAL) { self.parse_create_external_table(or_replace) + } else if self.parse_keyword(Keyword::FUNCTION) { + self.parse_create_function(or_replace, temporary) } else if or_replace { self.expected( - "[EXTERNAL] TABLE or [MATERIALIZED] VIEW after CREATE OR REPLACE", + "[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION after CREATE OR REPLACE", self.peek_token(), ) } else if self.parse_keyword(Keyword::INDEX) { @@ -1957,8 +1959,6 @@ impl<'a> Parser<'a> { self.parse_create_schema() } else if self.parse_keyword(Keyword::DATABASE) { self.parse_create_database() - } else if self.parse_keyword(Keyword::FUNCTION) { - self.parse_create_function(temporary) } else if self.parse_keyword(Keyword::ROLE) { self.parse_create_role() } else if self.parse_keyword(Keyword::SEQUENCE) { @@ -2169,9 +2169,13 @@ impl<'a> Parser<'a> { } } - pub fn parse_create_function(&mut self, temporary: bool) -> Result { - let name = self.parse_object_name()?; + pub fn parse_create_function( + &mut self, + or_replace: bool, + temporary: bool, + ) -> Result { if dialect_of!(self is HiveDialect) { + let name = self.parse_object_name()?; self.expect_keyword(Keyword::AS)?; let class_name = self.parse_literal_string()?; let mut bodies = vec![CreateFunctionBody::As(class_name)]; @@ -2180,6 +2184,7 @@ impl<'a> Parser<'a> { } Ok(Statement::CreateFunction { + or_replace, temporary, name, args: None, @@ -2187,6 +2192,7 @@ impl<'a> Parser<'a> { bodies, }) } else if dialect_of!(self is PostgreSqlDialect) { + let name = self.parse_object_name()?; self.expect_token(&Token::LParen)?; let args = self.parse_comma_separated(Parser::parse_create_function_arg)?; self.expect_token(&Token::RParen)?; @@ -2203,6 +2209,7 @@ impl<'a> Parser<'a> { } Ok(Statement::CreateFunction { + or_replace, temporary, name, args: Some(args), @@ -2210,9 +2217,8 @@ impl<'a> Parser<'a> { bodies, }) } else { - Err(ParserError::ParserError( - "Expected an object type after CREATE, found: FUNCTION".to_string(), - )) + self.prev_token(); + self.expected("an object type after CREATE", self.peek_token()) } } @@ -2265,7 +2271,7 @@ impl<'a> Parser<'a> { let expr = self.parse_expr()?; Ok(CreateFunctionBody::Return(expr)) } else { - self.expected("AS or USING", self.peek_token()) + self.expected("AS or LANGUAGE or RETURN", self.peek_token()) } } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 9609403df6..3b79c8465e 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2193,6 +2193,7 @@ fn parse_create_function() { assert_eq!( pg().verified_stmt(sql), Statement::CreateFunction { + or_replace: false, temporary: false, name: ObjectName(vec![Ident::new("add")]), args: Some(vec![ @@ -2208,10 +2209,11 @@ fn parse_create_function() { } ); - let sql = "CREATE FUNCTION add(a INTEGER, IN b INTEGER = 1) RETURNS INTEGER LANGUAGE SQL IMMUTABLE RETURN a + b"; + let sql = "CREATE OR REPLACE FUNCTION add(a INTEGER, IN b INTEGER = 1) RETURNS INTEGER LANGUAGE SQL IMMUTABLE RETURN a + b"; assert_eq!( pg().verified_stmt(sql), Statement::CreateFunction { + or_replace: true, temporary: false, name: ObjectName(vec![Ident::new("add")]), args: Some(vec![ From 31ce8382e15e019fe12f56b4b01e17735debe485 Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Mon, 21 Nov 2022 11:52:51 +0800 Subject: [PATCH 5/6] fix compile error in bigdecimal Signed-off-by: Runji Wang --- tests/sqlparser_postgres.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 3b79c8465e..2f1668a675 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2222,7 +2222,7 @@ fn parse_create_function() { mode: Some(ArgMode::In), name: Some("b".into()), data_type: DataType::Integer(None), - default_expr: Some(Expr::Value(Value::Number("1".into(), false))), + default_expr: Some(Expr::Value(Value::Number("1".parse().unwrap(), false))), } ]), return_type: Some(DataType::Integer(None)), From a8af9282bc3a9e6fef527234f4dabbb37ac04f4f Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Thu, 1 Dec 2022 16:21:23 +0800 Subject: [PATCH 6/6] unify all `CreateFunctionBody` to a structure Signed-off-by: Runji Wang --- src/ast/mod.rs | 49 +++++++++++++++++----------- src/parser.rs | 64 ++++++++++++++++++++++--------------- tests/sqlparser_hive.rs | 15 +++++---- tests/sqlparser_postgres.rs | 24 +++++++------- 4 files changed, 90 insertions(+), 62 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 9bfb870eeb..94f44edf7a 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1412,7 +1412,8 @@ pub enum Statement { name: ObjectName, args: Option>, return_type: Option, - bodies: Vec, + /// Optional parameters. + params: CreateFunctionBody, }, /// `ASSERT [AS ]` Assert { @@ -1874,7 +1875,7 @@ impl fmt::Display for Statement { name, args, return_type, - bodies, + params, } => { write!( f, @@ -1888,7 +1889,7 @@ impl fmt::Display for Statement { if let Some(return_type) = return_type { write!(f, " RETURNS {}", return_type)?; } - write!(f, " {}", display_separated(bodies, " "))?; + write!(f, "{params}")?; Ok(()) } Statement::CreateView { @@ -3776,31 +3777,41 @@ impl fmt::Display for FunctionBehavior { } /// Postgres: https://www.postgresql.org/docs/15/sql-createfunction.html -#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[derive(Debug, Default, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[non_exhaustive] -pub enum CreateFunctionBody { - /// AS 'definition' - As(String), +pub struct CreateFunctionBody { /// LANGUAGE lang_name - Language(Ident), + pub language: Option, /// IMMUTABLE | STABLE | VOLATILE - Behavior(FunctionBehavior), + pub behavior: Option, + /// AS 'definition' + /// + /// Note that Hive's `AS class_name` is also parsed here. + pub as_: Option, /// RETURN expression - Return(Expr), - /// USING ... (Hive) - Using(CreateFunctionUsing), + pub return_: Option, + /// USING ... (Hive only) + pub using: Option, } impl fmt::Display for CreateFunctionBody { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Self::As(definition) => write!(f, "AS '{definition}'"), - Self::Language(lang) => write!(f, "LANGUAGE {lang}"), - Self::Behavior(behavior) => write!(f, "{behavior}"), - Self::Return(expr) => write!(f, "RETURN {expr}"), - Self::Using(using) => write!(f, "{using}"), + if let Some(language) = &self.language { + write!(f, " LANGUAGE {language}")?; } + if let Some(behavior) = &self.behavior { + write!(f, " {behavior}")?; + } + if let Some(definition) = &self.as_ { + write!(f, " AS '{definition}'")?; + } + if let Some(expr) = &self.return_ { + write!(f, " RETURN {expr}")?; + } + if let Some(using) = &self.using { + write!(f, " {using}")?; + } + Ok(()) } } diff --git a/src/parser.rs b/src/parser.rs index 83e25570d6..f37f3d1e66 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2262,10 +2262,11 @@ impl<'a> Parser<'a> { let name = self.parse_object_name()?; self.expect_keyword(Keyword::AS)?; let class_name = self.parse_literal_string()?; - let mut bodies = vec![CreateFunctionBody::As(class_name)]; - if let Some(using) = self.parse_optional_create_function_using()? { - bodies.push(CreateFunctionBody::Using(using)); - } + let params = CreateFunctionBody { + as_: Some(class_name), + using: self.parse_optional_create_function_using()?, + ..Default::default() + }; Ok(Statement::CreateFunction { or_replace, @@ -2273,7 +2274,7 @@ impl<'a> Parser<'a> { name, args: None, return_type: None, - bodies, + params, }) } else if dialect_of!(self is PostgreSqlDialect) { let name = self.parse_object_name()?; @@ -2287,10 +2288,7 @@ impl<'a> Parser<'a> { None }; - let mut bodies = vec![]; - while let Ok(body) = self.parse_create_function_body() { - bodies.push(body); - } + let params = self.parse_create_function_body()?; Ok(Statement::CreateFunction { or_replace, @@ -2298,7 +2296,7 @@ impl<'a> Parser<'a> { name, args: Some(args), return_type, - bodies, + params, }) } else { self.prev_token(); @@ -2341,21 +2339,37 @@ impl<'a> Parser<'a> { } fn parse_create_function_body(&mut self) -> Result { - if self.parse_keyword(Keyword::AS) { - Ok(CreateFunctionBody::As(self.parse_literal_string()?)) - } else if self.parse_keyword(Keyword::LANGUAGE) { - Ok(CreateFunctionBody::Language(self.parse_identifier()?)) - } else if self.parse_keyword(Keyword::IMMUTABLE) { - Ok(CreateFunctionBody::Behavior(FunctionBehavior::Immutable)) - } else if self.parse_keyword(Keyword::STABLE) { - Ok(CreateFunctionBody::Behavior(FunctionBehavior::Stable)) - } else if self.parse_keyword(Keyword::VOLATILE) { - Ok(CreateFunctionBody::Behavior(FunctionBehavior::Volatile)) - } else if self.parse_keyword(Keyword::RETURN) { - let expr = self.parse_expr()?; - Ok(CreateFunctionBody::Return(expr)) - } else { - self.expected("AS or LANGUAGE or RETURN", self.peek_token()) + let mut body = CreateFunctionBody::default(); + loop { + fn ensure_not_set(field: &Option, name: &str) -> Result<(), ParserError> { + if field.is_some() { + return Err(ParserError::ParserError(format!( + "{name} specified more than once", + ))); + } + Ok(()) + } + if self.parse_keyword(Keyword::AS) { + ensure_not_set(&body.as_, "AS")?; + body.as_ = Some(self.parse_literal_string()?); + } else if self.parse_keyword(Keyword::LANGUAGE) { + ensure_not_set(&body.language, "LANGUAGE")?; + body.language = Some(self.parse_identifier()?); + } else if self.parse_keyword(Keyword::IMMUTABLE) { + ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?; + body.behavior = Some(FunctionBehavior::Immutable); + } else if self.parse_keyword(Keyword::STABLE) { + ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?; + body.behavior = Some(FunctionBehavior::Stable); + } else if self.parse_keyword(Keyword::VOLATILE) { + ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?; + body.behavior = Some(FunctionBehavior::Volatile); + } else if self.parse_keyword(Keyword::RETURN) { + ensure_not_set(&body.return_, "RETURN")?; + body.return_ = Some(self.parse_expr()?); + } else { + return Ok(body); + } } } diff --git a/tests/sqlparser_hive.rs b/tests/sqlparser_hive.rs index a0e499108f..99a81eff21 100644 --- a/tests/sqlparser_hive.rs +++ b/tests/sqlparser_hive.rs @@ -244,19 +244,20 @@ fn parse_create_function() { Statement::CreateFunction { temporary, name, - bodies, + params, .. } => { assert!(temporary); assert_eq!(name.to_string(), "mydb.myfunc"); assert_eq!( - bodies, - vec![ - CreateFunctionBody::As("org.random.class.Name".to_string()), - CreateFunctionBody::Using(CreateFunctionUsing::Jar( + params, + CreateFunctionBody { + as_: Some("org.random.class.Name".to_string()), + using: Some(CreateFunctionUsing::Jar( "hdfs://somewhere.com:8020/very/far".to_string() - )) - ] + )), + ..Default::default() + } ) } _ => unreachable!(), diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 83d56f63b7..9805305303 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2235,7 +2235,7 @@ fn parse_similar_to() { #[test] fn parse_create_function() { - let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE"; + let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER LANGUAGE SQL IMMUTABLE AS 'select $1 + $2;'"; assert_eq!( pg().verified_stmt(sql), Statement::CreateFunction { @@ -2247,11 +2247,12 @@ fn parse_create_function() { CreateFunctionArg::unnamed(DataType::Integer(None)), ]), return_type: Some(DataType::Integer(None)), - bodies: vec![ - CreateFunctionBody::As("select $1 + $2;".into()), - CreateFunctionBody::Language("SQL".into()), - CreateFunctionBody::Behavior(FunctionBehavior::Immutable), - ], + params: CreateFunctionBody { + language: Some("SQL".into()), + behavior: Some(FunctionBehavior::Immutable), + as_: Some("select $1 + $2;".into()), + ..Default::default() + }, } ); @@ -2272,15 +2273,16 @@ fn parse_create_function() { } ]), return_type: Some(DataType::Integer(None)), - bodies: vec![ - CreateFunctionBody::Language("SQL".into()), - CreateFunctionBody::Behavior(FunctionBehavior::Immutable), - CreateFunctionBody::Return(Expr::BinaryOp { + params: CreateFunctionBody { + language: Some("SQL".into()), + behavior: Some(FunctionBehavior::Immutable), + return_: Some(Expr::BinaryOp { left: Box::new(Expr::Identifier("a".into())), op: BinaryOperator::Plus, right: Box::new(Expr::Identifier("b".into())), }), - ], + ..Default::default() + }, } ); }