@@ -163,14 +163,13 @@ private static QueryTable aggregation(
163
163
final PermuteKernel [] permuteKernels = ac .makePermuteKernels ();
164
164
165
165
if (dataIndex != null && initialKeys == null && !input .isRefreshing ()) {
166
- return staticIndexedAggregation (dataIndex , keyNames , ac );
166
+ return staticIndexedAggregation (input , dataIndex , keyNames , ac );
167
167
}
168
168
169
169
final Table symbolTable ;
170
170
final boolean useSymbolTable ;
171
171
if (!input .isRefreshing () && control .considerSymbolTables (input , dataIndex != null , keySources )) {
172
172
Assert .eq (keySources .length , "keySources.length" , 1 );
173
-
174
173
symbolTable = ((SymbolTableSource <?>) keySources [0 ]).getStaticSymbolTable (input .getRowSet (),
175
174
control .useSymbolTableLookupCaching ());
176
175
useSymbolTable = control .useSymbolTables (input .size (), symbolTable .size ());
@@ -1619,6 +1618,7 @@ private static void modifySlots(RowSetBuilderRandom modifiedBuilder, IntChunk<Ch
1619
1618
1620
1619
@ NotNull
1621
1620
private static QueryTable staticIndexedAggregation (
1621
+ final QueryTable input ,
1622
1622
final BasicDataIndex dataIndex ,
1623
1623
final String [] keyNames ,
1624
1624
final AggregationContext ac ) {
@@ -1628,10 +1628,22 @@ private static QueryTable staticIndexedAggregation(
1628
1628
1629
1629
final Map <String , ColumnSource <?>> resultColumnSourceMap = new LinkedHashMap <>();
1630
1630
1631
- // Add the index key columns directly to the result table.
1632
- final Table indexKeyTable = indexTable .flatten ().select (keyNames );
1633
- for (final String keyName : keyNames ) {
1634
- resultColumnSourceMap .put (keyName , indexKeyTable .getColumnSource (keyName ));
1631
+ // Add the index table's key columns directly to the result table (after flattening and selecting).
1632
+ final Table indexKeyTable = indexTable .flatten ().select (dataIndex .keyColumnNames ().toArray (String []::new ));
1633
+ // Index key names corresponding to the input key names in the order of the input key names
1634
+ final String [] dataIndexKeyNames = new String [keyNames .length ];
1635
+ final Map <String , ColumnSource <?>> inputNamesToSources = input .getColumnSourceMap ();
1636
+ final Map <ColumnSource <?>, String > indexedSourcesToNames = dataIndex .keyColumnNamesByIndexedColumn ();
1637
+ for (int ki = 0 ; ki < keyNames .length ; ki ++) {
1638
+ final String keyName = keyNames [ki ];
1639
+ // Note that we must be careful to find the index key column sources by indexed (input) column source, not
1640
+ // by input key name, as the input key names may not match the indexed column names.
1641
+ // We build dataIndexKeyNames as part of this mapping, so we can use them for the row lookup remapping.
1642
+ final ColumnSource <?> keyColumnSource = inputNamesToSources .get (keyName );
1643
+ final String dataIndexKeyName = indexedSourcesToNames .get (keyColumnSource );
1644
+ dataIndexKeyNames [ki ] = dataIndexKeyName ;
1645
+ final ColumnSource <?> indexKeyColumnSource = indexKeyTable .getColumnSource (dataIndexKeyName );
1646
+ resultColumnSourceMap .put (keyName , indexKeyColumnSource );
1635
1647
}
1636
1648
ac .getResultColumns (resultColumnSourceMap );
1637
1649
@@ -1648,7 +1660,7 @@ private static QueryTable staticIndexedAggregation(
1648
1660
// Create a lookup function from the index table
1649
1661
ac .supplyRowLookup (() -> {
1650
1662
final ToLongFunction <Object > lookupKeyToRowKey =
1651
- DataIndexUtils .buildRowKeyMappingFunction (indexKeyTable , keyNames );
1663
+ DataIndexUtils .buildRowKeyMappingFunction (indexKeyTable , dataIndexKeyNames );
1652
1664
return key -> (int ) lookupKeyToRowKey .applyAsLong (key );
1653
1665
});
1654
1666
0 commit comments