@@ -1054,6 +1054,7 @@ fn group_by_matches_view(outer_cols: &[String], view_cols: &[String]) -> bool {
10541054fn filter_group_by_cols_for_measure (
10551055 outer_cols : & [ String ] ,
10561056 view_cols : & [ String ] ,
1057+ dimension_exprs : & HashMap < String , String > ,
10571058) -> Vec < String > {
10581059 if view_cols. is_empty ( ) {
10591060 return outer_cols. to_vec ( ) ;
@@ -1064,9 +1065,34 @@ fn filter_group_by_cols_for_measure(
10641065 . map ( |col| normalize_group_by_col ( col) )
10651066 . collect ( ) ;
10661067
1068+ let mut alias_expr_norms: Vec < ( String , String ) > = Vec :: new ( ) ;
1069+ alias_expr_norms. reserve ( dimension_exprs. len ( ) ) ;
1070+ for ( alias, expr) in dimension_exprs. iter ( ) {
1071+ alias_expr_norms. push ( (
1072+ normalize_group_by_col ( alias) ,
1073+ normalize_group_by_col ( expr) ,
1074+ ) ) ;
1075+ }
1076+
10671077 outer_cols
10681078 . iter ( )
1069- . filter ( |col| view_set. contains ( & normalize_group_by_col ( col) ) )
1079+ . filter ( |col| {
1080+ let normalized_outer = normalize_group_by_col ( col) ;
1081+ if view_set. contains ( & normalized_outer) {
1082+ return true ;
1083+ }
1084+
1085+ if let Some ( expr) = dimension_exprs. get ( & normalized_outer) {
1086+ let normalized_expr = normalize_group_by_col ( expr) ;
1087+ if view_set. contains ( & normalized_expr) {
1088+ return true ;
1089+ }
1090+ }
1091+
1092+ alias_expr_norms. iter ( ) . any ( |( alias_norm, expr_norm) | {
1093+ expr_norm == & normalized_outer && view_set. contains ( alias_norm)
1094+ } )
1095+ } )
10701096 . cloned ( )
10711097 . collect ( )
10721098}
@@ -3436,8 +3462,11 @@ pub fn expand_aggregate_with_at(sql: &str) -> AggregateExpandResult {
34363462 for ( measure_name, modifiers, start, end) in patterns {
34373463 // Look up which view contains this measure (for JOIN support)
34383464 let resolved = resolve_measure_source ( & measure_name, & primary_table_name) ;
3439- let measure_group_by_cols =
3440- filter_group_by_cols_for_measure ( & group_by_cols, & resolved. view_group_by_cols ) ;
3465+ let measure_group_by_cols = filter_group_by_cols_for_measure (
3466+ & group_by_cols,
3467+ & resolved. view_group_by_cols ,
3468+ & resolved. dimension_exprs ,
3469+ ) ;
34413470
34423471 // Non-decomposable measures are recomputed from base rows (including AT modifiers)
34433472
@@ -3551,8 +3580,11 @@ pub fn expand_aggregate_with_at(sql: &str) -> AggregateExpandResult {
35513580
35523581 for ( measure_name, start, end) in plain_calls {
35533582 let resolved = resolve_measure_source ( & measure_name, & primary_table_name) ;
3554- let measure_group_by_cols =
3555- filter_group_by_cols_for_measure ( & group_by_cols, & resolved. view_group_by_cols ) ;
3583+ let measure_group_by_cols = filter_group_by_cols_for_measure (
3584+ & group_by_cols,
3585+ & resolved. view_group_by_cols ,
3586+ & resolved. dimension_exprs ,
3587+ ) ;
35563588
35573589
35583590 // For derived measures, use the expanded expression; otherwise use AGG(measure_name)
0 commit comments