@@ -1697,6 +1697,19 @@ pub enum Statement {
1697
1697
params : CreateFunctionBody ,
1698
1698
} ,
1699
1699
/// ```sql
1700
+ /// CREATE MACRO
1701
+ /// ```
1702
+ ///
1703
+ /// Supported variants:
1704
+ /// 1. [DuckDB](https://duckdb.org/docs/sql/statements/create_macro)
1705
+ CreateMacro {
1706
+ or_replace : bool ,
1707
+ temporary : bool ,
1708
+ name : ObjectName ,
1709
+ args : Option < Vec < MacroArg > > ,
1710
+ definition : MacroDefinition ,
1711
+ } ,
1712
+ /// ```sql
1700
1713
/// CREATE STAGE
1701
1714
/// ```
1702
1715
/// See <https://docs.snowflake.com/en/sql-reference/sql/create-stage>
@@ -2288,6 +2301,28 @@ impl fmt::Display for Statement {
2288
2301
write ! ( f, "{params}" ) ?;
2289
2302
Ok ( ( ) )
2290
2303
}
2304
+ Statement :: CreateMacro {
2305
+ or_replace,
2306
+ temporary,
2307
+ name,
2308
+ args,
2309
+ definition,
2310
+ } => {
2311
+ write ! (
2312
+ f,
2313
+ "CREATE {or_replace}{temp}MACRO {name}" ,
2314
+ temp = if * temporary { "TEMPORARY " } else { "" } ,
2315
+ or_replace = if * or_replace { "OR REPLACE " } else { "" } ,
2316
+ ) ?;
2317
+ if let Some ( args) = args {
2318
+ write ! ( f, "({})" , display_comma_separated( args) ) ?;
2319
+ }
2320
+ match definition {
2321
+ MacroDefinition :: Expr ( expr) => write ! ( f, " AS {expr}" ) ?,
2322
+ MacroDefinition :: Table ( query) => write ! ( f, " AS TABLE {query}" ) ?,
2323
+ }
2324
+ Ok ( ( ) )
2325
+ }
2291
2326
Statement :: CreateView {
2292
2327
name,
2293
2328
or_replace,
@@ -4585,6 +4620,56 @@ impl fmt::Display for CreateFunctionUsing {
4585
4620
}
4586
4621
}
4587
4622
4623
+ /// `NAME = <EXPR>` arguments for DuckDB macros
4624
+ ///
4625
+ /// See [Create Macro - DuckDB](https://duckdb.org/docs/sql/statements/create_macro)
4626
+ /// for more details
4627
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
4628
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
4629
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
4630
+ pub struct MacroArg {
4631
+ pub name : Ident ,
4632
+ pub default_expr : Option < Expr > ,
4633
+ }
4634
+
4635
+ impl MacroArg {
4636
+ /// Returns an argument with name.
4637
+ pub fn new ( name : & str ) -> Self {
4638
+ Self {
4639
+ name : name. into ( ) ,
4640
+ default_expr : None ,
4641
+ }
4642
+ }
4643
+ }
4644
+
4645
+ impl fmt:: Display for MacroArg {
4646
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
4647
+ write ! ( f, "{}" , self . name) ?;
4648
+ if let Some ( default_expr) = & self . default_expr {
4649
+ write ! ( f, " := {default_expr}" ) ?;
4650
+ }
4651
+ Ok ( ( ) )
4652
+ }
4653
+ }
4654
+
4655
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
4656
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
4657
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
4658
+ pub enum MacroDefinition {
4659
+ Expr ( Expr ) ,
4660
+ Table ( Query ) ,
4661
+ }
4662
+
4663
+ impl fmt:: Display for MacroDefinition {
4664
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
4665
+ match self {
4666
+ MacroDefinition :: Expr ( expr) => write ! ( f, "{expr}" ) ?,
4667
+ MacroDefinition :: Table ( query) => write ! ( f, "{query}" ) ?,
4668
+ }
4669
+ Ok ( ( ) )
4670
+ }
4671
+ }
4672
+
4588
4673
/// Schema possible naming variants ([1]).
4589
4674
///
4590
4675
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#schema-definition
0 commit comments