Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/create_function_attributes.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE FUNCTION public.f(x integer) RETURNS SETOF integer
LANGUAGE c
TRANSFORM FOR TYPE hstore, FOR TYPE ltree
WINDOW
IMMUTABLE STRICT NOT LEAKPROOF
COST 2.5 ROWS 10 SUPPORT public.f_support
Expand Down
13 changes: 13 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4044,6 +4044,10 @@ pub struct CreateFunction {
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
pub window: bool,
/// Types listed in `TRANSFORM { FOR TYPE type_name } [, ...]`
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
pub transform_types: Vec<DataType>,
/// `COST execution_cost`
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
Expand Down Expand Up @@ -4117,6 +4121,15 @@ impl fmt::Display for CreateFunction {
if let Some(language) = &self.language {
write!(f, " LANGUAGE {language}")?;
}
if !self.transform_types.is_empty() {
write!(f, " TRANSFORM ")?;
for (i, transform_type) in self.transform_types.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "FOR TYPE {transform_type}")?;
}
}
if self.window {
write!(f, " WINDOW")?;
}
Expand Down
11 changes: 11 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6216,6 +6216,7 @@ impl<'a> Parser<'a> {
security: Option<FunctionSecurity>,
leakproof: Option<bool>,
window: Option<()>,
transform_types: Option<Vec<DataType>>,
cost: Option<Expr>,
rows: Option<Expr>,
support: Option<ObjectName>,
Expand Down Expand Up @@ -6296,6 +6297,12 @@ impl<'a> Parser<'a> {
} else if self.parse_keyword(Keyword::WINDOW) {
ensure_not_set(&body.window, "WINDOW")?;
body.window = Some(());
} else if self.parse_keyword(Keyword::TRANSFORM) {
ensure_not_set(&body.transform_types, "TRANSFORM")?;
body.transform_types = Some(self.parse_comma_separated(|parser| {
parser.expect_keywords(&[Keyword::FOR, Keyword::TYPE])?;
parser.parse_data_type()
})?);
} else if self.parse_keyword(Keyword::COST) {
ensure_not_set(&body.cost, "COST")?;
body.cost = Some(self.parse_number()?);
Expand Down Expand Up @@ -6345,6 +6352,7 @@ impl<'a> Parser<'a> {
security: body.security,
leakproof: body.leakproof,
window: body.window.is_some(),
transform_types: body.transform_types.unwrap_or_default(),
cost: body.cost,
rows: body.rows,
support: body.support,
Expand Down Expand Up @@ -6389,6 +6397,7 @@ impl<'a> Parser<'a> {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -6478,6 +6487,7 @@ impl<'a> Parser<'a> {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -6574,6 +6584,7 @@ impl<'a> Parser<'a> {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,7 @@ fn test_bigquery_create_function() {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ fn parse_create_function() {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -458,6 +459,7 @@ fn parse_create_function_parameter_default_values() {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down
55 changes: 55 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5547,6 +5547,7 @@ $$"#;
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -5595,6 +5596,7 @@ $$"#;
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -5647,6 +5649,7 @@ $$"#;
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -5699,6 +5702,7 @@ $$"#;
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -5744,6 +5748,7 @@ $$"#;
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -5795,6 +5800,7 @@ fn parse_create_function() {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -5942,6 +5948,7 @@ fn parse_create_function_c_with_module_pathname() {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -7844,6 +7851,7 @@ fn parse_trigger_related_functions() {
security: None,
leakproof: None,
window: false,
transform_types: vec![],
cost: None,
rows: None,
support: None,
Expand Down Expand Up @@ -14622,3 +14630,50 @@ fn parse_alter_column_set_storage() {
);
}
}

#[test]
fn parse_create_function_transform() {
let stmt = pg_and_generic().verified_stmt(
"CREATE FUNCTION f(x INT) RETURNS INT LANGUAGE plpython3u TRANSFORM FOR TYPE hstore, FOR TYPE public.ltree IMMUTABLE AS 'return x'",
);
assert_display_propagates_write_errors(&stmt, &[" TRANSFORM ", "FOR TYPE ", ", "]);
let Statement::CreateFunction(create) = stmt else {
unreachable!()
};
assert_eq!(
create.transform_types,
vec![
DataType::Custom(ObjectName::from(vec![Ident::new("hstore")]), vec![]),
DataType::Custom(
ObjectName::from(vec![Ident::new("public"), Ident::new("ltree")]),
vec![]
),
]
);

pg_and_generic().one_statement_parses_to(
"CREATE FUNCTION f(x INT) RETURNS INT IMMUTABLE TRANSFORM FOR TYPE int LANGUAGE plpython3u AS 'return x'",
"CREATE FUNCTION f(x INT) RETURNS INT LANGUAGE plpython3u TRANSFORM FOR TYPE INT IMMUTABLE AS 'return x'",
);

for (sql, expected) in [
(
"CREATE FUNCTION f() RETURNS INT TRANSFORM FOR TYPE a TRANSFORM FOR TYPE b AS 'x'",
"TRANSFORM specified more than once",
),
(
"CREATE FUNCTION f() RETURNS INT TRANSFORM hstore AS 'x'",
"Expected: FOR, found: hstore",
),
(
"CREATE FUNCTION f() RETURNS INT TRANSFORM FOR hstore AS 'x'",
"Expected: TYPE, found: hstore",
),
] {
assert_eq!(
pg().parse_sql_statements(sql).unwrap_err().to_string(),
format!("sql parser error: {expected}"),
"{sql}"
);
}
}
Loading