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

feat: add renameOperator #19776

Merged
merged 8 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,131 @@
/**
* 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 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,9 @@ export default function buildQuery(formData: QueryFormData) {
rollingWindowOperator(formData, baseQueryObject),
timeCompareOperator(formData, baseQueryObject),
resampleOperator(formData, baseQueryObject),
renameOperator(formData, baseQueryObject),
flattenOperator(formData, baseQueryObject),
// todo: move contribution and prophet before flatten
contributionOperator(formData, baseQueryObject),
prophetOperator(formData, baseQueryObject),
],
Expand Down
27 changes: 8 additions & 19 deletions superset/charts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# pylint: disable=too-many-lines
from __future__ import annotations

import inspect
from typing import Any, Dict, Optional, TYPE_CHECKING

from flask_babel import gettext as _
Expand All @@ -27,7 +28,7 @@
from superset import app
from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType
from superset.db_engine_specs.base import builtin_time_grains
from superset.utils import schema as utils
from superset.utils import pandas_postprocessing, schema as utils
from superset.utils.core import (
AnnotationType,
FilterOperator,
Expand Down Expand Up @@ -770,24 +771,12 @@ class ChartDataPostProcessingOperationSchema(Schema):
description="Post processing operation type",
required=True,
validate=validate.OneOf(
choices=(
"aggregate",
"boxplot",
"contribution",
"cum",
"geodetic_parse",
"geohash_decode",
"geohash_encode",
"pivot",
"prophet",
"rolling",
"select",
"sort",
"diff",
"compare",
"resample",
"flatten",
)
choices=[
name
for name, value in inspect.getmembers(
pandas_postprocessing, inspect.isfunction
)
]
),
example="aggregate",
)
Expand Down
Loading