@@ -7,36 +7,64 @@ pub trait LogicalSource: Sized {
77 fn with_plan_node ( & self , plan_node : PlanNode ) -> Result < Self , CubeError > ;
88}
99
10- /* test example */
10+ /// Generates an enum and trait implementations for LogicalSource types.
11+ ///
12+ /// This macro creates:
13+ /// - An enum with variants wrapping `Rc<T>` for each specified type
14+ /// - LogicalSource trait implementation that delegates to inner types
15+ /// - PrettyPrint trait implementation for debugging SQL plans
16+ /// - From<Rc<T>> implementations for convenient construction
17+ ///
18+ /// # Usage
19+ /// ```
20+ /// logical_source_enum!(MySource, [Table, Join, Subquery]);
21+ /// ```
22+ ///
23+ /// The enum variant name always matches the inner type name, and all variants
24+ /// are wrapped in `Rc` for efficient cloning in the query planner.
25+ macro_rules! logical_source_enum {
26+ ( $enum_name: ident, [ $( $variant: ident) ,+ $( , ) ?] ) => {
27+ #[ derive( Clone ) ]
28+ pub enum $enum_name {
29+ $(
30+ $variant( Rc <$variant>) ,
31+ ) +
32+ }
1133
12- pub enum TestSouce {
13- Cube ( Rc < Cube > ) ,
14- MeasureSubquery ( Rc < MeasureSubquery > ) ,
15- }
34+ impl LogicalSource for $enum_name {
35+ fn as_plan_node( & self ) -> PlanNode {
36+ match self {
37+ $(
38+ Self :: $variant( item) => item. as_plan_node( ) ,
39+ ) +
40+ }
41+ }
1642
17- impl LogicalSource for TestSouce {
18- fn as_plan_node ( & self ) -> PlanNode {
19- match self {
20- Self :: Cube ( item) => item. as_plan_node ( ) ,
21- Self :: MeasureSubquery ( item) => item. as_plan_node ( ) ,
43+ fn with_plan_node( & self , plan_node: PlanNode ) -> Result <Self , CubeError > {
44+ Ok ( match self {
45+ $(
46+ Self :: $variant( _) => Self :: $variant( plan_node. into_logical_node( ) ?) ,
47+ ) +
48+ } )
49+ }
2250 }
23- }
24- fn with_plan_node ( & self , plan_node : PlanNode ) -> Result < Self , CubeError > {
25- Ok ( match self {
26- Self :: Cube ( _) => Self :: Cube ( plan_node. into_logical_node ( ) ?) ,
27- Self :: MeasureSubquery ( _) => Self :: MeasureSubquery ( plan_node. into_logical_node ( ) ?) ,
28- } )
29- }
30- }
3151
32- impl From < Rc < Cube > > for TestSouce {
33- fn from ( value : Rc < Cube > ) -> Self {
34- Self :: Cube ( value)
35- }
36- }
52+ impl PrettyPrint for $enum_name {
53+ fn pretty_print( & self , result: & mut PrettyPrintResult , state: & PrettyPrintState ) {
54+ match self {
55+ $(
56+ Self :: $variant( item) => item. pretty_print( result, state) ,
57+ ) +
58+ }
59+ }
60+ }
3761
38- impl From < Rc < MeasureSubquery > > for TestSouce {
39- fn from ( value : Rc < MeasureSubquery > ) -> Self {
40- Self :: MeasureSubquery ( value)
41- }
62+ $(
63+ impl From <Rc <$variant>> for $enum_name {
64+ fn from( value: Rc <$variant>) -> Self {
65+ Self :: $variant( value)
66+ }
67+ }
68+ ) +
69+ } ;
4270}
0 commit comments