@@ -66,10 +66,16 @@ const ID: RuleID = RuleID::HierarchicalGroupingSetsToUnion;
6666pub struct RuleHierarchicalGroupingSetsToUnion {
6767 id : RuleID ,
6868 matchers : Vec < Matcher > ,
69+ cte_channel_size : usize ,
6970}
7071
7172impl RuleHierarchicalGroupingSetsToUnion {
72- pub fn new ( _ctx : Arc < OptimizerContext > ) -> Self {
73+ pub fn new ( ctx : Arc < OptimizerContext > ) -> Self {
74+ let cte_channel_size = ctx
75+ . get_table_ctx ( )
76+ . get_settings ( )
77+ . get_grouping_sets_channel_size ( )
78+ . unwrap ( ) ;
7379 Self {
7480 id : ID ,
7581 matchers : vec ! [ Matcher :: MatchOp {
@@ -79,21 +85,36 @@ impl RuleHierarchicalGroupingSetsToUnion {
7985 children: vec![ Matcher :: Leaf ] ,
8086 } ] ,
8187 } ] ,
88+ cte_channel_size : cte_channel_size as usize ,
8289 }
8390 }
8491
8592 /// Analyzes grouping sets to build a true hierarchical dependency DAG
86- fn build_hierarchy_dag ( & self , grouping_sets : & [ Vec < IndexType > ] ) -> HierarchyDAG {
93+ fn build_hierarchy_dag (
94+ & self ,
95+ grouping_sets : & [ Vec < IndexType > ] ,
96+ agg : & Aggregate ,
97+ ) -> HierarchyDAG {
8798 let mut levels: Vec < GroupingLevel > = grouping_sets
8899 . iter ( )
89100 . enumerate ( )
90- . map ( |( idx, set) | GroupingLevel {
91- set_index : idx,
92- columns : set. clone ( ) ,
93- direct_children : Vec :: new ( ) ,
94- possible_parents : Vec :: new ( ) ,
95- chosen_parent : None ,
96- level : set. len ( ) ,
101+ . map ( |( idx, set) | {
102+ // Sort columns according to their order in group_items for consistent schema ordering
103+ let mut sorted_columns = set. clone ( ) ;
104+ sorted_columns. sort_by_key ( |& col_idx| {
105+ agg. group_items
106+ . iter ( )
107+ . position ( |item| item. index == col_idx)
108+ . unwrap_or ( usize:: MAX ) // Put unknown columns at the end
109+ } ) ;
110+ GroupingLevel {
111+ set_index : idx,
112+ columns : sorted_columns,
113+ direct_children : Vec :: new ( ) ,
114+ possible_parents : Vec :: new ( ) ,
115+ chosen_parent : None ,
116+ level : set. len ( ) ,
117+ }
97118 } )
98119 . collect ( ) ;
99120
@@ -406,7 +427,7 @@ impl RuleHierarchicalGroupingSetsToUnion {
406427 cte_name : cte_name. to_string ( ) ,
407428 cte_output_columns : None ,
408429 ref_count : 1 ,
409- channel_size : None ,
430+ channel_size : Some ( self . cte_channel_size ) ,
410431 }
411432 . into ( ) ,
412433 ) ,
@@ -457,7 +478,7 @@ impl RuleHierarchicalGroupingSetsToUnion {
457478 cte_name : cte_name. to_string ( ) ,
458479 cte_output_columns : None ,
459480 ref_count : 1 ,
460- channel_size : None ,
481+ channel_size : Some ( self . cte_channel_size ) ,
461482 }
462483 . into ( ) ,
463484 ) ,
@@ -497,7 +518,7 @@ impl RuleHierarchicalGroupingSetsToUnion {
497518 cte_name : cte_name. to_string ( ) ,
498519 cte_output_columns : None ,
499520 ref_count : 1 ,
500- channel_size : None ,
521+ channel_size : Some ( self . cte_channel_size ) ,
501522 }
502523 . into ( ) ,
503524 ) ,
@@ -632,10 +653,11 @@ impl RuleHierarchicalGroupingSetsToUnion {
632653 // Create parent CTE consumer
633654 let parent_output_columns: Vec < IndexType > = {
634655 let mut output_cols = Vec :: new ( ) ;
635- // Then : aggregate function output columns
656+ // First : aggregate function output columns
636657 for agg_item in & agg. aggregate_functions {
637658 output_cols. push ( agg_item. index ) ;
638659 }
660+ // Then: parent level columns (already sorted from build_hierarchy_dag)
639661 for & col_idx in & parent_level. columns {
640662 output_cols. push ( col_idx) ;
641663 }
@@ -666,7 +688,7 @@ impl RuleHierarchicalGroupingSetsToUnion {
666688 cte_name : cte_name. to_string ( ) ,
667689 cte_output_columns : None ,
668690 ref_count : 1 ,
669- channel_size : None ,
691+ channel_size : Some ( self . cte_channel_size ) ,
670692 }
671693 . into ( ) ,
672694 ) ,
@@ -850,7 +872,7 @@ impl Rule for RuleHierarchicalGroupingSetsToUnion {
850872 }
851873
852874 // Build hierarchy DAG
853- let hierarchy = self . build_hierarchy_dag ( & grouping_sets. sets ) ;
875+ let hierarchy = self . build_hierarchy_dag ( & grouping_sets. sets , & agg ) ;
854876 // Check if we have meaningful hierarchical structure
855877 let hierarchical_levels = hierarchy
856878 . levels
0 commit comments