@@ -126,10 +126,11 @@ const isSubQueryResult = (
126126} ;
127127
128128/**
129- * Flattens a record into one or more rows.
130- * Sub-query results are expanded: each sub-record produces a separate output row.
131- * Parent fields are shown only on the first child row; subsequent rows leave them blank,
132- * matching the Salesforce CLI display style.
129+ * Flattens a record into one or more rows, matching the Salesforce CLI display style.
130+ * Each sub-query's first element shares row 0 with the parent fields and other
131+ * sub-queries' first elements. Remaining elements ("overflow") are stacked in
132+ * subsequent rows in sub-query order, with all other columns blank on those rows.
133+ * Total rows = 1 + sum(len - 1) for each sub-query — never a cross-product.
133134 * When a sub-query has no records the parent row is still emitted with empty sub-columns.
134135 */
135136const flattenRecord = ( record : Record < string , unknown > ) : Record < string , unknown > [ ] => {
@@ -172,32 +173,25 @@ const flattenRecord = (record: Record<string, unknown>): Record<string, unknown>
172173 return [ baseRow ] ;
173174 }
174175
175- // Cross-product of all sub-query expansions merged with the base row.
176- // Multiple sub-queries in a single SELECT are rare but handled correctly.
177- let rows : Record < string , unknown > [ ] = [ baseRow ] ;
176+ // Total rows = 1 + sum of (each sub-query's overflow = length - 1)
177+ const totalRows = subQueryExpansions . reduce ( ( sum , exp ) => sum + exp . length - 1 , 1 ) ;
178+
179+ // Allocate the grid. Row 0 starts with the parent fields.
180+ const grid : Record < string , unknown > [ ] = Array . from ( { length : totalRows } , ( ) => ( { } ) ) ;
181+ Object . assign ( grid [ 0 ] , baseRow ) ;
182+
183+ // Place each sub-query: element[0] → row 0, overflow elements → consecutive rows
184+ // starting immediately after all previous sub-queries' overflow rows.
185+ let overflowStart = 1 ;
178186 for ( const expansion of subQueryExpansions ) {
179- const next : Record < string , unknown > [ ] = [ ] ;
180- for ( const existing of rows ) {
181- for ( const expanded of expansion ) {
182- next . push ( { ...existing , ...expanded } ) ;
183- }
187+ Object . assign ( grid [ 0 ] , expansion [ 0 ] ) ;
188+ for ( let j = 1 ; j < expansion . length ; j ++ ) {
189+ Object . assign ( grid [ overflowStart + j - 1 ] , expansion [ j ] ) ;
184190 }
185- rows = next ;
191+ overflowStart += expansion . length - 1 ;
186192 }
187193
188- // Blank out parent fields on all rows after the first, matching the Salesforce CLI
189- // display style where repeated parent values are omitted for readability.
190- const baseKeys = Object . keys ( baseRow ) ;
191- return rows . map ( ( row , i ) => {
192- if ( i === 0 || baseKeys . length === 0 ) {
193- return row ;
194- }
195- const blanked = { ...row } ;
196- for ( const key of baseKeys ) {
197- blanked [ key ] = '' ;
198- }
199- return blanked ;
200- } ) ;
194+ return grid ;
201195} ;
202196
203197/**
0 commit comments