From a8e29750ccfad11d461cc938ec5788b64b0bbcbe Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Sun, 25 Oct 2026 20:53:57 +0100 Subject: [PATCH] PostgreSQL: Share operator-function formatting --- src/ast/ddl.rs | 54 ++++++++++++++++++++++--------------- tests/sqlparser_postgres.rs | 13 +++++++++ 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 2fd20dc836..c1780e2a43 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -5101,17 +5101,13 @@ impl fmt::Display for OperatorClassItem { op_types, function_name, argument_types, - } => { - write!(f, "FUNCTION {support_number}")?; - if let Some(types) = op_types { - write!(f, " ({})", display_comma_separated(types))?; - } - write!(f, " {function_name}")?; - if !argument_types.is_empty() { - write!(f, "({})", display_comma_separated(argument_types))?; - } - Ok(()) - } + } => fmt_operator_function( + f, + *support_number, + op_types.as_deref(), + function_name, + argument_types, + ), OperatorClassItem::Storage { storage_type } => { write!(f, "STORAGE {storage_type}") } @@ -5337,21 +5333,35 @@ impl fmt::Display for OperatorFamilyItem { op_types, function_name, argument_types, - } => { - write!(f, "FUNCTION {support_number}")?; - if let Some(types) = op_types { - write!(f, " ({})", display_comma_separated(types))?; - } - write!(f, " {function_name}")?; - if !argument_types.is_empty() { - write!(f, "({})", display_comma_separated(argument_types))?; - } - Ok(()) - } + } => fmt_operator_function( + f, + *support_number, + op_types.as_deref(), + function_name, + argument_types, + ), } } } +fn fmt_operator_function( + f: &mut fmt::Formatter, + support_number: u64, + op_types: Option<&[DataType]>, + function_name: &ObjectName, + argument_types: &[DataType], +) -> fmt::Result { + write!(f, "FUNCTION {support_number}")?; + if let Some(types) = op_types { + write!(f, " ({})", display_comma_separated(types))?; + } + write!(f, " {function_name}")?; + if !argument_types.is_empty() { + write!(f, "({})", display_comma_separated(argument_types))?; + } + Ok(()) +} + impl fmt::Display for OperatorFamilyDropItem { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index da83d4f71a..c99a0651aa 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -11026,3 +11026,16 @@ fn parse_create_foreign_data_wrapper_clause_order() { pg_and_generic().verified_stmt(sql); } } + +#[test] +fn operator_function_display_preserves_empty_operator_types() { + for (op_types, expected) in [(None, "FUNCTION 1 f"), (Some(vec![]), "FUNCTION 1 () f")] { + let item = OperatorFamilyItem::Function { + support_number: 1, + op_types, + function_name: ObjectName::from(vec![Ident::new("f")]), + argument_types: vec![], + }; + assert_eq!(item.to_string(), expected); + } +}