Skip to content

Commit

Permalink
feat: add renameOperator (apache#19776)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie authored and philipher29 committed Jun 9, 2022
1 parent 17a5a7f commit d1ddc32
Show file tree
Hide file tree
Showing 12 changed files with 512 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@
* specific language governing permissions and limitationsxw
* under the License.
*/
import { ensureIsArray, PostProcessingFlatten } from '@superset-ui/core';
import { PostProcessingFlatten } from '@superset-ui/core';
import { PostProcessingFactory } from './types';

export const flattenOperator: PostProcessingFactory<PostProcessingFlatten> = (
formData,
queryObject,
) => {
const drop_levels: number[] = [];
if (ensureIsArray(queryObject.metrics).length === 1) {
drop_levels.push(0);
}
return {
operation: 'flatten',
options: {
drop_levels,
},
};
};
) => ({
operation: 'flatten',
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { timeComparePivotOperator } from './timeComparePivotOperator';
export { sortOperator } from './sortOperator';
export { pivotOperator } from './pivotOperator';
export { resampleOperator } from './resampleOperator';
export { renameOperator } from './renameOperator';
export { contributionOperator } from './contributionOperator';
export { prophetOperator } from './prophetOperator';
export { boxplotOperator } from './boxplotOperator';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable camelcase */
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitationsxw
* under the License.
*/
import {
PostProcessingRename,
ensureIsArray,
getMetricLabel,
ComparisionType,
} from '@superset-ui/core';
import { PostProcessingFactory } from './types';
import { getMetricOffsetsMap, isValidTimeCompare } from './utils';

export const renameOperator: PostProcessingFactory<PostProcessingRename> = (
formData,
queryObject,
) => {
const metrics = ensureIsArray(queryObject.metrics);
const columns = ensureIsArray(queryObject.columns);
const { x_axis: xAxis } = formData;
// remove or rename top level of column name(metric name) in the MultiIndex when
// 1) only 1 metric
// 2) exist dimentsion
// 3) exist xAxis
// 4) exist time comparison, and comparison type is "actual values"
if (
metrics.length === 1 &&
columns.length > 0 &&
(xAxis || queryObject.is_timeseries) &&
!(
// todo: we should provide an approach to handle derived metrics
(
isValidTimeCompare(formData, queryObject) &&
[
ComparisionType.Difference,
ComparisionType.Ratio,
ComparisionType.Percentage,
].includes(formData.comparison_type)
)
)
) {
const renamePairs: [string, string | null][] = [];

if (
// "actual values" will add derived metric.
// we will rename the "metric" from the metricWithOffset label
// for example: "count__1 year ago" => "1 year ago"
isValidTimeCompare(formData, queryObject) &&
formData.comparison_type === ComparisionType.Values
) {
const metricOffsetMap = getMetricOffsetsMap(formData, queryObject);
const timeOffsets = ensureIsArray(formData.time_compare);
[...metricOffsetMap.keys()].forEach(metricWithOffset => {
const offsetLabel = timeOffsets.find(offset =>
metricWithOffset.includes(offset),
);
renamePairs.push([metricWithOffset, offsetLabel]);
});
}

renamePairs.push([getMetricLabel(metrics[0]), null]);

return {
operation: 'rename',
options: {
columns: Object.fromEntries(renamePairs),
level: 0,
inplace: true,
},
};
}

return undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,9 @@ const queryObject: QueryObject = {
},
],
};
const singleMetricQueryObject: QueryObject = {
metrics: ['count(*)'],
time_range: '2015 : 2016',
granularity: 'month',
post_processing: [
{
operation: 'pivot',
options: {
index: ['__timestamp'],
columns: ['nation'],
aggregates: {
'count(*)': {
operator: 'sum',
},
},
},
},
],
};

test('should do flattenOperator', () => {
expect(flattenOperator(formData, queryObject)).toEqual({
operation: 'flatten',
options: {
drop_levels: [],
},
});
});

test('should add drop level', () => {
expect(flattenOperator(formData, singleMetricQueryObject)).toEqual({
operation: 'flatten',
options: {
drop_levels: [0],
},
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ComparisionType, QueryObject, SqlaFormData } from '@superset-ui/core';
import { renameOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
x_axis: 'dttm',
metrics: ['count(*)'],
groupby: ['gender'],
time_range: '2015 : 2016',
granularity: 'month',
datasource: 'foo',
viz_type: 'table',
};
const queryObject: QueryObject = {
is_timeseries: true,
metrics: ['count(*)'],
columns: ['gender', 'dttm'],
time_range: '2015 : 2016',
granularity: 'month',
post_processing: [],
};

test('should skip renameOperator if exists multiple metrics', () => {
expect(
renameOperator(formData, {
...queryObject,
...{
metrics: ['count(*)', 'sum(sales)'],
},
}),
).toEqual(undefined);
});

test('should skip renameOperator if does not exist series', () => {
expect(
renameOperator(formData, {
...queryObject,
...{
columns: [],
},
}),
).toEqual(undefined);
});

test('should skip renameOperator if does not exist x_axis and is_timeseries', () => {
expect(
renameOperator(
{
...formData,
...{ x_axis: null },
},
{ ...queryObject, ...{ is_timeseries: false } },
),
).toEqual(undefined);
});

test('should skip renameOperator if exists derived metrics', () => {
[
ComparisionType.Difference,
ComparisionType.Ratio,
ComparisionType.Percentage,
].forEach(type => {
expect(
renameOperator(
{
...formData,
...{
comparison_type: type,
time_compare: ['1 year ago'],
},
},
{
...queryObject,
...{
metrics: ['count(*)'],
},
},
),
).toEqual(undefined);
});
});

test('should add renameOperator', () => {
expect(renameOperator(formData, queryObject)).toEqual({
operation: 'rename',
options: { columns: { 'count(*)': null }, inplace: true, level: 0 },
});
});

test('should add renameOperator if does not exist x_axis', () => {
expect(
renameOperator(
{
...formData,
...{ x_axis: null },
},
queryObject,
),
).toEqual({
operation: 'rename',
options: { columns: { 'count(*)': null }, inplace: true, level: 0 },
});
});

test('should add renameOperator if exist "actual value" time comparison', () => {
expect(
renameOperator(
{
...formData,
...{
comparison_type: ComparisionType.Values,
time_compare: ['1 year ago', '1 year later'],
},
},
queryObject,
),
).toEqual({
operation: 'rename',
options: {
columns: {
'count(*)': null,
'count(*)__1 year ago': '1 year ago',
'count(*)__1 year later': '1 year later',
},
inplace: true,
level: 0,
},
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ export type PostProcessingResample =
| _PostProcessingResample
| DefaultPostProcessing;

interface _PostProcessingRename {
operation: 'rename';
options: {
columns: Record<string, string | null>;
inplace?: boolean;
level?: number | string;
};
}
export type PostProcessingRename =
| _PostProcessingRename
| DefaultPostProcessing;

interface _PostProcessingFlatten {
operation: 'flatten';
options?: {
Expand Down Expand Up @@ -228,6 +240,7 @@ export type PostProcessingRule =
| PostProcessingCompare
| PostProcessingSort
| PostProcessingResample
| PostProcessingRename
| PostProcessingFlatten;

export function isPostProcessingAggregation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isValidTimeCompare,
pivotOperator,
resampleOperator,
renameOperator,
contributionOperator,
prophetOperator,
timeComparePivotOperator,
Expand Down Expand Up @@ -91,7 +92,12 @@ export default function buildQuery(formData: QueryFormData) {
rollingWindowOperator(formData, baseQueryObject),
timeCompareOperator(formData, baseQueryObject),
resampleOperator(formData, baseQueryObject),
renameOperator(formData, {
...baseQueryObject,
...{ is_timeseries },
}),
flattenOperator(formData, baseQueryObject),
// todo: move contribution and prophet before flatten
contributionOperator(formData, baseQueryObject),
prophetOperator(formData, baseQueryObject),
],
Expand Down
Loading

0 comments on commit d1ddc32

Please sign in to comment.