JoinDataFrames: Keep field name if possible (#69289)

This commit is contained in:
Ryan McKinley 2023-05-31 07:46:39 -07:00 committed by GitHub
parent ced71ba26b
commit 3647392b40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import { toDataFrame } from '../../dataframe/processDataFrame';
import { FieldType } from '../../types/dataFrame';
import { getFieldDisplayName } from '../../field';
import { DataFrame, FieldType } from '../../types/dataFrame';
import { mockTransformationsRegistry } from '../../utils/tests/mockTransformationsRegistry';
import { calculateFieldTransformer } from './calculateField';
@ -266,6 +267,64 @@ describe('align frames', () => {
`);
});
it('maintains naming convention after join', () => {
const series1 = toDataFrame({
name: 'Muta',
fields: [
{ name: 'Time', type: FieldType.time, values: [1000, 2000] },
{ name: 'Value', type: FieldType.number, values: [1, 100] },
],
});
expect(getFieldDisplayNames([series1])).toMatchInlineSnapshot(`
[
"Time",
"Muta",
]
`);
expect(getFieldNames([series1])).toMatchInlineSnapshot(`
[
"Time",
"Value",
]
`);
const series2 = toDataFrame({
name: 'Muta',
fields: [
{ name: 'Time', type: FieldType.time, values: [1000] },
{ name: 'Value', type: FieldType.number, values: [150] },
],
});
expect(getFieldDisplayNames([series2])).toMatchInlineSnapshot(`
[
"Time",
"Muta",
]
`);
expect(getFieldNames([series2])).toMatchInlineSnapshot(`
[
"Time",
"Value",
]
`);
const out = joinDataFrames({ frames: [series1, series2] })!;
expect(getFieldDisplayNames([out])).toMatchInlineSnapshot(`
[
"Time",
"Muta 1",
"Muta 2",
]
`);
expect(getFieldNames([out])).toMatchInlineSnapshot(`
[
"Time",
"Muta",
"Muta",
]
`);
});
it('supports duplicate times', () => {
//----------
// NOTE!!!
@ -357,3 +416,11 @@ describe('align frames', () => {
});
});
});
function getFieldDisplayNames(data: DataFrame[]): string[] {
return data.flatMap((frame) => frame.fields.map((f) => getFieldDisplayName(f, frame, data)));
}
function getFieldNames(data: DataFrame[]): string[] {
return data.flatMap((frame) => frame.fields.map((f) => f.name));
}

View File

@ -1,7 +1,7 @@
import intersect from 'fast_array_intersect';
import { getTimeField, sortDataFrame } from '../../dataframe';
import { DataFrame, Field, FieldMatcher, FieldType } from '../../types';
import { DataFrame, Field, FieldMatcher, FieldType, TIME_SERIES_VALUE_FIELD_NAME } from '../../types';
import { fieldMatchers } from '../matchers';
import { FieldMatcherID } from '../matchers/ids';
@ -180,12 +180,18 @@ export function joinDataFrames(options: JoinOptions): DataFrame | undefined {
nullModesFrame.push(spanNulls === true ? NULL_REMOVE : spanNulls === -1 ? NULL_RETAIN : NULL_EXPAND);
let labels = field.labels ?? {};
let name = field.name;
if (frame.name) {
labels = { ...labels, name: frame.name };
if (field.name === TIME_SERIES_VALUE_FIELD_NAME) {
name = frame.name;
} else {
labels = { ...labels, name: frame.name };
}
}
fields.push({
...field,
name,
labels, // add the name label from frame
});
}