diff --git a/public/app/features/transformers/utils.test.ts b/public/app/features/transformers/utils.test.ts index 536ffbea701..095ff0c5fa1 100644 --- a/public/app/features/transformers/utils.test.ts +++ b/public/app/features/transformers/utils.test.ts @@ -90,4 +90,25 @@ describe('useAllFieldNamesFromDataFrames', () => { expect(names).toEqual(['T', 'N', 'S', 'T (A)', 'N (A)', 'S (A)', 'T (B)', 'N (B)', 'S (B)']); }); + + it('omit base names when field.name is unique', () => { + let frames = [ + toDataFrame({ + refId: 'A', + fields: [ + { name: 'T', config: { displayName: 't' }, type: FieldType.time, values: [1, 2, 3] }, + { name: 'N', config: { displayName: 'n' }, type: FieldType.number, values: [100, 200, 300] }, + { name: 'S', config: { displayName: 's' }, type: FieldType.string, values: ['1', '2', '3'] }, + ], + }), + toDataFrame({ + refId: 'B', + fields: [{ name: 'T', config: { displayName: 't2' }, type: FieldType.time, values: [1, 2, 3] }], + }), + ]; + + const names = getAllFieldNamesFromDataFrames(frames, true); + + expect(names).toEqual(['T', 't', 'n', 's', 't2']); + }); }); diff --git a/public/app/features/transformers/utils.ts b/public/app/features/transformers/utils.ts index 9109c5c0411..540de803932 100644 --- a/public/app/features/transformers/utils.ts +++ b/public/app/features/transformers/utils.ts @@ -16,7 +16,23 @@ export const getAllFieldNamesFromDataFrames = (frames: DataFrame[], withBaseFiel let names = frames.flatMap((frame) => frame.fields.map((field) => getFieldDisplayName(field, frame, frames))); if (withBaseFieldNames) { - let baseNames = frames.flatMap((frame) => frame.fields.map((field) => field.name)); + // only add base names of fields that have same field.name + let baseNameCounts = new Map(); + + frames.forEach((frame) => + frame.fields.forEach((field) => { + let count = baseNameCounts.get(field.name) ?? 0; + baseNameCounts.set(field.name, count + 1); + }) + ); + + let baseNames: string[] = []; + + baseNameCounts.forEach((count, name) => { + if (count > 1) { + baseNames.push(name); + } + }); // prepend base names + uniquify names = [...new Set(baseNames.concat(names))];