From b0a2e13317b33b8e19373ef2ac8c33fb35b36ae1 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Wed, 4 Nov 2026 17:40:19 +0100 Subject: [PATCH 1/2] PostgreSQL: Support TRANSFORM in CREATE FUNCTION --- .../fuzz_seeds/create_function_attributes.sql | 1 + src/ast/ddl.rs | 13 +++++ src/parser/mod.rs | 11 ++++ tests/sqlparser_bigquery.rs | 1 + tests/sqlparser_mssql.rs | 2 + tests/sqlparser_postgres.rs | 53 +++++++++++++++++++ 6 files changed, 81 insertions(+) diff --git a/fuzz/fuzz_seeds/create_function_attributes.sql b/fuzz/fuzz_seeds/create_function_attributes.sql index 42ac2a8e9b..1667ce011b 100644 --- a/fuzz/fuzz_seeds/create_function_attributes.sql +++ b/fuzz/fuzz_seeds/create_function_attributes.sql @@ -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 diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index ad0c2338a0..7b583ca746 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -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, /// `COST execution_cost` /// /// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html) @@ -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")?; } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b5a37fcea9..b839c799dc 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -6216,6 +6216,7 @@ impl<'a> Parser<'a> { security: Option, leakproof: Option, window: Option<()>, + transform_types: Option>, cost: Option, rows: Option, support: Option, @@ -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()?); @@ -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, @@ -6389,6 +6397,7 @@ impl<'a> Parser<'a> { security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -6478,6 +6487,7 @@ impl<'a> Parser<'a> { security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -6574,6 +6584,7 @@ impl<'a> Parser<'a> { security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, diff --git a/tests/sqlparser_bigquery.rs b/tests/sqlparser_bigquery.rs index f26e591aac..30fcdba881 100644 --- a/tests/sqlparser_bigquery.rs +++ b/tests/sqlparser_bigquery.rs @@ -2355,6 +2355,7 @@ fn test_bigquery_create_function() { security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, diff --git a/tests/sqlparser_mssql.rs b/tests/sqlparser_mssql.rs index 9e73b589a8..3957f1484d 100644 --- a/tests/sqlparser_mssql.rs +++ b/tests/sqlparser_mssql.rs @@ -278,6 +278,7 @@ fn parse_create_function() { security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -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, diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index ed5de7c9ac..e90f093213 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -5547,6 +5547,7 @@ $$"#; security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -5595,6 +5596,7 @@ $$"#; security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -5647,6 +5649,7 @@ $$"#; security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -5699,6 +5702,7 @@ $$"#; security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -5744,6 +5748,7 @@ $$"#; security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -5795,6 +5800,7 @@ fn parse_create_function() { security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -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, @@ -7844,6 +7851,7 @@ fn parse_trigger_related_functions() { security: None, leakproof: None, window: false, + transform_types: vec![], cost: None, rows: None, support: None, @@ -14622,3 +14630,48 @@ fn parse_alter_column_set_storage() { ); } } + +#[test] +fn parse_create_function_transform() { + let Statement::CreateFunction(create) = 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'", + ) 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}" + ); + } +} From 9cf69b5dd158afbd10bb307d76de7e07ec37977a Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Wed, 4 Nov 2026 19:36:28 +0100 Subject: [PATCH 2/2] Test TRANSFORM display write error propagation --- tests/sqlparser_postgres.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index e90f093213..4f23ca1860 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -14633,9 +14633,11 @@ fn parse_alter_column_set_storage() { #[test] fn parse_create_function_transform() { - let Statement::CreateFunction(create) = pg_and_generic().verified_stmt( + 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'", - ) else { + ); + assert_display_propagates_write_errors(&stmt, &[" TRANSFORM ", "FOR TYPE ", ", "]); + let Statement::CreateFunction(create) = stmt else { unreachable!() }; assert_eq!(