Transformations: groupBy transformation now preserves data order (#43584)

* fix: groupBy transformers keep the order of numeric keys

* fix: groupBy transformers keep the order of numeric keys
This commit is contained in:
MeetzhDing 2022-01-05 17:49:15 +08:00 committed by GitHub
parent 1d20c82c7b
commit 9c67ce77d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 13 deletions

View File

@ -260,4 +260,50 @@ describe('GroupBy transformer', () => {
expect(result[1].fields).toEqual(expectedB);
});
});
it('should group values and keep the order of the fields', async () => {
const testSeries = toDataFrame({
name: 'A',
fields: [
{ name: 'message', type: FieldType.string, values: ['500', '404', '404', 'one', 'one', 'two', '200'] },
{ name: 'values', type: FieldType.number, values: [1, 2, 2, 3, 3, 3, 4] },
],
});
const cfg: DataTransformerConfig<GroupByTransformerOptions> = {
id: DataTransformerID.groupBy,
options: {
fields: {
message: {
operation: GroupByOperationID.groupBy,
aggregations: [],
},
values: {
operation: GroupByOperationID.aggregate,
aggregations: [ReducerID.sum],
},
},
},
};
await expect(transformDataFrame([cfg], [testSeries])).toEmitValuesWith((received) => {
const result = received[0];
const expected: Field[] = [
{
name: 'message',
type: FieldType.string,
values: new ArrayVector(['500', '404', 'one', 'two', '200']),
config: {},
},
{
name: 'values (sum)',
type: FieldType.number,
values: new ArrayVector([1, 4, 6, 3, 4]),
config: {},
},
];
expect(result[0].fields).toEqual(expected);
});
});
});

View File

@ -63,13 +63,13 @@ export const groupByTransformer: DataTransformerInfo<GroupByTransformerOptions>
// Group the values by fields and groups so we can get all values for a
// group for a given field.
const valuesByGroupKey: Record<string, Record<string, MutableField>> = {};
const valuesByGroupKey = new Map<string, Record<string, MutableField>>();
for (let rowIndex = 0; rowIndex < frame.length; rowIndex++) {
const groupKey = String(groupByFields.map((field) => field.values.get(rowIndex)));
const valuesByField = valuesByGroupKey[groupKey] ?? {};
const valuesByField = valuesByGroupKey.get(groupKey) ?? {};
if (!valuesByGroupKey[groupKey]) {
valuesByGroupKey[groupKey] = valuesByField;
if (!valuesByGroupKey.has(groupKey)) {
valuesByGroupKey.set(groupKey, valuesByField);
}
for (let field of frame.fields) {
@ -89,16 +89,14 @@ export const groupByTransformer: DataTransformerInfo<GroupByTransformerOptions>
}
const fields: Field[] = [];
const groupKeys = Object.keys(valuesByGroupKey);
for (const field of groupByFields) {
const values = new ArrayVector();
const fieldName = getFieldDisplayName(field);
for (let key of groupKeys) {
const valuesByField = valuesByGroupKey[key];
values.add(valuesByField[fieldName].values.get(0));
}
valuesByGroupKey.forEach((value) => {
values.add(value[fieldName].values.get(0));
});
fields.push({
name: field.name,
@ -120,8 +118,8 @@ export const groupByTransformer: DataTransformerInfo<GroupByTransformerOptions>
const aggregations = options.fields[fieldName].aggregations;
const valuesByAggregation: Record<string, any[]> = {};
for (const groupKey of groupKeys) {
const fieldWithValuesForGroup = valuesByGroupKey[groupKey][fieldName];
valuesByGroupKey.forEach((value) => {
const fieldWithValuesForGroup = value[fieldName];
const results = reduceField({
field: fieldWithValuesForGroup,
reducers: aggregations,
@ -133,7 +131,7 @@ export const groupByTransformer: DataTransformerInfo<GroupByTransformerOptions>
}
valuesByAggregation[aggregation].push(results[aggregation]);
}
}
});
for (const aggregation of aggregations) {
const aggregationField: Field = {
@ -150,7 +148,7 @@ export const groupByTransformer: DataTransformerInfo<GroupByTransformerOptions>
processed.push({
fields,
length: groupKeys.length,
length: valuesByGroupKey.size,
});
}