@@ -140,27 +140,28 @@ export class MemoryAnalyticsService implements IAnalyticsService {
140140
141141 pipeline . push ( { $group : groupStage } ) ;
142142
143- // Stage 4: $project to reshape results
143+ // Stage 4: $project to reshape results (use short names, we'll fix them later)
144144 const projectStage : Record < string , any > = { _id : 0 } ;
145145 if ( query . dimensions && query . dimensions . length > 0 ) {
146146 for ( const dim of query . dimensions ) {
147147 const dimName = this . getShortName ( dim ) ;
148- projectStage [ dim ] = `$_id.${ dimName } ` ;
148+ projectStage [ dimName ] = `$_id.${ dimName } ` ;
149149 }
150150 }
151151 if ( query . measures && query . measures . length > 0 ) {
152152 for ( const measure of query . measures ) {
153153 const measureName = this . getShortName ( measure ) ;
154- projectStage [ measure ] = `$${ measureName } ` ;
154+ projectStage [ measureName ] = `$${ measureName } ` ;
155155 }
156156 }
157157 pipeline . push ( { $project : projectStage } ) ;
158158
159- // Stage 5: $sort
159+ // Stage 5: $sort (use short names)
160160 if ( query . order && Object . keys ( query . order ) . length > 0 ) {
161161 const sortStage : Record < string , any > = { } ;
162162 for ( const [ field , direction ] of Object . entries ( query . order ) ) {
163- sortStage [ field ] = direction === 'asc' ? 1 : - 1 ;
163+ const shortName = this . getShortName ( field ) ;
164+ sortStage [ shortName ] = direction === 'asc' ? 1 : - 1 ;
164165 }
165166 pipeline . push ( { $sort : sortStage } ) ;
166167 }
@@ -175,7 +176,34 @@ export class MemoryAnalyticsService implements IAnalyticsService {
175176
176177 // Execute the aggregation pipeline
177178 const tableName = this . extractTableName ( cube . sql ) ;
178- const rows = await this . driver . aggregate ( tableName , pipeline ) ;
179+ const rawRows = await this . driver . aggregate ( tableName , pipeline ) ;
180+
181+ // Rename fields from short names to full cube.field names
182+ const rows = rawRows . map ( row => {
183+ const renamedRow : Record < string , unknown > = { } ;
184+
185+ // Rename dimensions
186+ if ( query . dimensions ) {
187+ for ( const dim of query . dimensions ) {
188+ const shortName = this . getShortName ( dim ) ;
189+ if ( shortName in row ) {
190+ renamedRow [ dim ] = row [ shortName ] ;
191+ }
192+ }
193+ }
194+
195+ // Rename measures
196+ if ( query . measures ) {
197+ for ( const measure of query . measures ) {
198+ const shortName = this . getShortName ( measure ) ;
199+ if ( shortName in row ) {
200+ renamedRow [ measure ] = row [ shortName ] ;
201+ }
202+ }
203+ }
204+
205+ return renamedRow ;
206+ } ) ;
179207
180208 // Build field metadata
181209 const fields : Array < { name : string ; type : string } > = [ ] ;
0 commit comments