|
12 | 12 | import org.apache.calcite.rel.core.AggregateCall; |
13 | 13 | import org.apache.calcite.rel.type.RelDataType; |
14 | 14 | import org.apache.calcite.rel.type.RelDataTypeFactory; |
| 15 | +import org.apache.calcite.rel.type.RelDataTypeField; |
15 | 16 | import org.apache.calcite.sql.fun.SqlStdOperatorTable; |
16 | 17 | import org.apache.calcite.sql.type.SqlTypeName; |
17 | 18 | import org.apache.calcite.util.ImmutableBitSet; |
@@ -103,7 +104,7 @@ public AggregationMetadata build(RelDataType inputRowType, RelDataTypeFactory ty |
103 | 104 | List<Integer> allGroupIndices = new ArrayList<>(); |
104 | 105 | List<String> allGroupFieldNames = new ArrayList<>(); |
105 | 106 | for (GroupingInfo g : groupings) { |
106 | | - allGroupIndices.addAll(g.resolveIndices(inputRowType)); |
| 107 | + allGroupIndices.addAll(resolveFieldIndices(g, inputRowType)); |
107 | 108 | allGroupFieldNames.addAll(g.getFieldNames()); |
108 | 109 | } |
109 | 110 |
|
@@ -152,4 +153,25 @@ public AggregationMetadata build(RelDataType inputRowType, RelDataTypeFactory ty |
152 | 153 |
|
153 | 154 | return new AggregationMetadata(ImmutableBitSet.of(allGroupIndices), allGroupFieldNames, allCalls, allFieldNames, bucketOrders); |
154 | 155 | } |
| 156 | + |
| 157 | + /** |
| 158 | + * Resolves field-based grouping names to column indices in the input schema. |
| 159 | + * |
| 160 | + * @param grouping the grouping info containing field names |
| 161 | + * @param inputRowType the schema before aggregation |
| 162 | + * @return column indices for each field name |
| 163 | + * @throws ConversionException if a field name is not found in the schema |
| 164 | + */ |
| 165 | + private static List<Integer> resolveFieldIndices(GroupingInfo grouping, RelDataType inputRowType) throws ConversionException { |
| 166 | + List<String> fieldNames = grouping.getFieldNames(); |
| 167 | + List<Integer> indices = new ArrayList<>(fieldNames.size()); |
| 168 | + for (String name : fieldNames) { |
| 169 | + RelDataTypeField field = inputRowType.getField(name, false, false); |
| 170 | + if (field == null) { |
| 171 | + throw new ConversionException("Group-by field '" + name + "' not found in schema"); |
| 172 | + } |
| 173 | + indices.add(field.getIndex()); |
| 174 | + } |
| 175 | + return indices; |
| 176 | + } |
155 | 177 | } |
0 commit comments