@@ -1580,6 +1580,19 @@ pub enum Statement {
15801580 params : CreateFunctionBody ,
15811581 } ,
15821582 /// ```sql
1583+ /// CREATE MACRO
1584+ /// ```
1585+ ///
1586+ /// Supported variants:
1587+ /// 1. [DuckDB](https://duckdb.org/docs/sql/statements/create_macro)
1588+ CreateMacro {
1589+ or_replace : bool ,
1590+ temporary : bool ,
1591+ name : ObjectName ,
1592+ args : Option < Vec < MacroArg > > ,
1593+ definition : MacroDefinition ,
1594+ } ,
1595+ /// ```sql
15831596 /// CREATE STAGE
15841597 /// ```
15851598 /// See <https://docs.snowflake.com/en/sql-reference/sql/create-stage>
@@ -2098,6 +2111,28 @@ impl fmt::Display for Statement {
20982111 write ! ( f, "{params}" ) ?;
20992112 Ok ( ( ) )
21002113 }
2114+ Statement :: CreateMacro {
2115+ or_replace,
2116+ temporary,
2117+ name,
2118+ args,
2119+ definition,
2120+ } => {
2121+ write ! (
2122+ f,
2123+ "CREATE {or_replace}{temp}MACRO {name}" ,
2124+ temp = if * temporary { "TEMPORARY " } else { "" } ,
2125+ or_replace = if * or_replace { "OR REPLACE " } else { "" } ,
2126+ ) ?;
2127+ if let Some ( args) = args {
2128+ write ! ( f, "({})" , display_comma_separated( args) ) ?;
2129+ }
2130+ match definition {
2131+ MacroDefinition :: Expr ( expr) => write ! ( f, " AS {expr}" ) ?,
2132+ MacroDefinition :: Table ( query) => write ! ( f, " AS TABLE {query}" ) ?,
2133+ }
2134+ Ok ( ( ) )
2135+ }
21012136 Statement :: CreateView {
21022137 name,
21032138 or_replace,
@@ -4304,6 +4339,56 @@ impl fmt::Display for CreateFunctionUsing {
43044339 }
43054340}
43064341
4342+ /// `NAME = <EXPR>` arguments for DuckDB macros
4343+ ///
4344+ /// See [Create Macro - DuckDB](https://duckdb.org/docs/sql/statements/create_macro)
4345+ /// for more details
4346+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
4347+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
4348+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
4349+ pub struct MacroArg {
4350+ pub name : Ident ,
4351+ pub default_expr : Option < Expr > ,
4352+ }
4353+
4354+ impl MacroArg {
4355+ /// Returns an argument with name.
4356+ pub fn new ( name : & str ) -> Self {
4357+ Self {
4358+ name : name. into ( ) ,
4359+ default_expr : None ,
4360+ }
4361+ }
4362+ }
4363+
4364+ impl fmt:: Display for MacroArg {
4365+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
4366+ write ! ( f, "{}" , self . name) ?;
4367+ if let Some ( default_expr) = & self . default_expr {
4368+ write ! ( f, " := {default_expr}" ) ?;
4369+ }
4370+ Ok ( ( ) )
4371+ }
4372+ }
4373+
4374+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
4375+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
4376+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
4377+ pub enum MacroDefinition {
4378+ Expr ( Expr ) ,
4379+ Table ( Query ) ,
4380+ }
4381+
4382+ impl fmt:: Display for MacroDefinition {
4383+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
4384+ match self {
4385+ MacroDefinition :: Expr ( expr) => write ! ( f, "{expr}" ) ?,
4386+ MacroDefinition :: Table ( query) => write ! ( f, "{query}" ) ?,
4387+ }
4388+ Ok ( ( ) )
4389+ }
4390+ }
4391+
43074392/// Schema possible naming variants ([1]).
43084393///
43094394/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#schema-definition
0 commit comments