Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JoinDataFrames: Keep field name if possible #69289

Merged
merged 3 commits into from May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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';
Expand Down Expand Up @@ -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!!!
Expand Down Expand Up @@ -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));
}
@@ -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';

Expand Down Expand Up @@ -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
});
}
Expand Down