Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
feat(core): add support for adhoc columns
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Sep 1, 2021
1 parent 1bbd84f commit 749898c
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* specific language governing permissions and limitationsxw
* under the License.
*/
import { ensureIsArray, getMetricLabel, PostProcessingPivot } from '@superset-ui/core';
import {
ensureIsArray,
getColumnLabel,
getMetricLabel,
PostProcessingPivot,
} from '@superset-ui/core';
import { PostProcessingFactory } from './types';
import { TIME_COLUMN, isValidTimeCompare } from './utils';
import { timeComparePivotOperator } from './timeComparePivotOperator';
Expand All @@ -35,7 +40,7 @@ export const pivotOperator: PostProcessingFactory<PostProcessingPivot | undefine
operation: 'pivot',
options: {
index: [TIME_COLUMN],
columns: queryObject.columns || [],
columns: ensureIsArray(queryObject.columns).map(getColumnLabel),
// Create 'dummy' mean aggregates to assign cell values in pivot table
// use the 'mean' aggregates to avoid drop NaN. PR: https://github.com/apache-superset/superset-ui/pull/1231
aggregates: Object.fromEntries(metricLabels.map(metric => [metric, { operator: 'mean' }])),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
* specific language governing permissions and limitationsxw
* under the License.
*/
import { ComparisionType, PostProcessingPivot, NumpyFunction } from '@superset-ui/core';
import {
ComparisionType,
PostProcessingPivot,
NumpyFunction,
ensureIsArray,
getColumnLabel,
} from '@superset-ui/core';
import { getMetricOffsetsMap, isValidTimeCompare, TIME_COMPARISON_SEPARATOR } from './utils';
import { PostProcessingFactory } from './types';

Expand Down Expand Up @@ -47,7 +53,7 @@ export const timeComparePivotOperator: PostProcessingFactory<PostProcessingPivot
operation: 'pivot',
options: {
index: ['__timestamp'],
columns: queryObject.columns || [],
columns: ensureIsArray(queryObject.columns).map(getColumnLabel),
aggregates: comparisonType === ComparisionType.Values ? valuesAgg : changeAgg,
drop_missing_columns: false,
},
Expand Down
6 changes: 5 additions & 1 deletion packages/superset-ui-core/src/query/extractQueryFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { t } from '../translation';
import { removeDuplicates } from '../utils';
import { DTTM_ALIAS } from './constants';
import getColumnLabel from './getColumnLabel';
import getMetricLabel from './getMetricLabel';
import {
QueryFields,
Expand Down Expand Up @@ -105,7 +106,10 @@ export default function extractQueryFields(
}

return {
columns: removeDuplicates(columns.filter(x => typeof x === 'string' && x)),
columns: removeDuplicates(
columns.filter(col => col !== ''),
getColumnLabel,
),
metrics: queryMode === QueryMode.raw ? undefined : removeDuplicates(metrics, getMetricLabel),
orderby:
orderby.length > 0
Expand Down
11 changes: 11 additions & 0 deletions packages/superset-ui-core/src/query/getColumnLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isPhysicalColumn, QueryFormColumn } from './types';

export default function getColumnLabel(column: QueryFormColumn): string {
if (isPhysicalColumn(column)) {
return column;
}
if (column.label) {
return column.label;
}
return column.sqlExpression;
}
8 changes: 4 additions & 4 deletions packages/superset-ui-core/src/query/getMetricLabel.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { QueryFormMetric } from './types/QueryFormData';
import { isAdhocMetricSimple, isSavedMetric, QueryFormMetric } from './types';

export default function getMetricLabel(metric: QueryFormMetric) {
if (typeof metric === 'string') {
export default function getMetricLabel(metric: QueryFormMetric): string {
if (isSavedMetric(metric)) {
return metric;
}
if (metric.label) {
return metric.label;
}
if (metric.expressionType === 'SIMPLE') {
if (isAdhocMetricSimple(metric)) {
return `${metric.aggregate}(${metric.column.columnName || metric.column.column_name})`;
}
return metric.sqlExpression;
Expand Down
1 change: 1 addition & 0 deletions packages/superset-ui-core/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { default as buildQueryContext } from './buildQueryContext';
export { default as buildQueryObject } from './buildQueryObject';
export { default as convertFilter } from './convertFilter';
export { default as extractTimegrain } from './extractTimegrain';
export { default as getColumnLabel } from './getColumnLabel';
export { default as getMetricLabel } from './getMetricLabel';
export { default as DatasourceKey } from './DatasourceKey';
export { default as normalizeOrderBy } from './normalizeOrderBy';
Expand Down
16 changes: 16 additions & 0 deletions packages/superset-ui-core/src/query/types/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@

import { GenericDataType } from './QueryResponse';

export interface AdhocColumn {
hasCustomLabel?: boolean;
label?: string;
optionName?: string;
sqlExpression: string;
}

/**
* A column that is physically defined in datasource.
*/
export type PhysicalColumn = string;

/**
* Column information defined in datasource.
*/
Expand All @@ -39,3 +51,7 @@ export interface Column {
}

export default {};

export function isPhysicalColumn(column: AdhocColumn | PhysicalColumn): column is PhysicalColumn {
return typeof column === 'string';
}
5 changes: 5 additions & 0 deletions packages/superset-ui-core/src/query/types/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Column } from './Column';
export type Aggregate = 'AVG' | 'COUNT' | 'COUNT_DISTINCT' | 'MAX' | 'MIN' | 'SUM';

export interface AdhocMetricBase {
hasCustomLabel?: boolean;
label?: string;
optionName?: string;
}
Expand Down Expand Up @@ -66,3 +67,7 @@ export interface Metric {
}

export default {};

export function isAdhocMetricSimple(metric: AdhocMetric): metric is AdhocMetricSimple {
return metric.expressionType === 'SIMPLE';
}
11 changes: 8 additions & 3 deletions packages/superset-ui-core/src/query/types/QueryFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { QueryObject, QueryObjectExtras, QueryObjectFilterClause } from './Query
import { TimeRange, TimeRangeEndpoints } from './Time';
import { TimeGranularity } from '../../time-format';
import { JsonObject } from '../../connection';
import { AdhocColumn, PhysicalColumn } from './Column';

/**
* Metric definition/reference in query object.
Expand All @@ -37,9 +38,9 @@ export type QueryFormMetric = SavedMetric | AdhocMetric;

/**
* Column selects in query object (used as dimensions in both groupby or raw
* query mode). Only support referring existing columns.
* query mode). Can be either reference to physical column or expresion.
*/
export type QueryFormColumn = string;
export type QueryFormColumn = PhysicalColumn | AdhocColumn;

/**
* Order query results by columns.
Expand Down Expand Up @@ -167,7 +168,7 @@ export interface BaseFormData extends TimeRange, FormDataResidual {
/** row offset for server side pagination */
row_offset?: string | number | null;
/** The metric used to order timeseries for limiting */
timeseries_limit_metric?: QueryFormColumn;
timeseries_limit_metric?: QueryFormMetric;
/** Force refresh */
force?: boolean;
result_format?: string;
Expand Down Expand Up @@ -209,4 +210,8 @@ export function isDruidFormData(formData: QueryFormData): formData is DruidFormD
return 'granularity' in formData;
}

export function isSavedMetric(metric: QueryFormMetric): metric is SavedMetric {
return typeof metric === 'string';
}

export default {};
42 changes: 42 additions & 0 deletions packages/superset-ui-core/test/query/getColumnLabel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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 { getColumnLabel } from '@superset-ui/core/src/query';

describe('getColumnLabel', () => {
it('should handle physical column', () => {
expect(getColumnLabel('gender')).toEqual('gender');
});

it('should handle adhoc columns with label', () => {
expect(
getColumnLabel({
sqlExpression: "case when 1 then 'a' else 'b' end",
label: 'my col',
}),
).toEqual('my col');
});

it('should handle adhoc columns without label', () => {
expect(
getColumnLabel({
sqlExpression: "case when 1 then 'a' else 'b' end",
}),
).toEqual("case when 1 then 'a' else 'b' end");
});
});
4 changes: 2 additions & 2 deletions plugins/plugin-chart-echarts/src/BoxPlot/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { buildQueryContext, getMetricLabel } from '@superset-ui/core';
import { buildQueryContext, getColumnLabel, getMetricLabel } from '@superset-ui/core';
import { BoxPlotQueryFormData, BoxPlotQueryObjectWhiskerType } from './types';

const PERCENTILE_REGEX = /(\d+)\/(\d+) percentiles/;
Expand Down Expand Up @@ -49,7 +49,7 @@ export default function buildQuery(formData: BoxPlotQueryFormData) {
options: {
whisker_type: whiskerType,
percentiles,
groupby: columns.filter(x => !distributionColumns.includes(x)),
groupby: columns.map(getColumnLabel).filter(x => !distributionColumns.includes(x)),
metrics: metrics.map(getMetricLabel),
},
},
Expand Down
20 changes: 11 additions & 9 deletions plugins/plugin-chart-echarts/src/BoxPlot/transformProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import {
CategoricalColorNamespace,
DataRecordValue,
getColumnLabel,
getMetricLabel,
getNumberFormatter,
getTimeFormatter,
Expand All @@ -43,22 +44,23 @@ export default function transformProps(
const coltypeMapping = getColtypesMapping(queriesData[0]);
const {
colorScheme,
groupby = [],
metrics: formdataMetrics = [],
groupby: formDataGroupby = [],
metrics: formDataMetrics = [],
numberFormat,
dateFormat,
xTicksLayout,
emitFilter,
} = formData as BoxPlotQueryFormData;
const colorFn = CategoricalColorNamespace.getScale(colorScheme as string);
const numberFormatter = getNumberFormatter(numberFormat);
const metricLabels = formdataMetrics.map(getMetricLabel);
const metricLabels = formDataMetrics.map(getMetricLabel);
const groupbyLabels = formDataGroupby.map(getColumnLabel);

const transformedData = data
.map((datum: any) => {
const groupbyLabel = extractGroupbyLabel({
datum,
groupby,
groupby: groupbyLabels,
coltypeMapping,
timeFormatter: getTimeFormatter(dateFormat),
});
Expand Down Expand Up @@ -91,7 +93,7 @@ export default function transformProps(
metricLabels.map(metric => {
const groupbyLabel = extractGroupbyLabel({
datum,
groupby,
groupby: groupbyLabels,
coltypeMapping,
timeFormatter: getTimeFormatter(dateFormat),
});
Expand All @@ -106,7 +108,7 @@ export default function transformProps(
tooltip: {
formatter: (param: { data: [string, number] }) => {
const [outlierName, stats] = param.data;
const headline = groupby
const headline = groupbyLabels.length
? `<p><strong>${sanitizeHtml(outlierName)}</strong></p>`
: '';
return `${headline}${numberFormatter(stats)}`;
Expand All @@ -124,13 +126,13 @@ export default function transformProps(
const labelMap = data.reduce((acc: Record<string, DataRecordValue[]>, datum) => {
const label = extractGroupbyLabel({
datum,
groupby,
groupby: groupbyLabels,
coltypeMapping,
timeFormatter: getTimeFormatter(dateFormat),
});
return {
...acc,
[label]: groupby.map(col => datum[col]),
[label]: groupbyLabels.map(col => datum[col]),
};
}, {});

Expand Down Expand Up @@ -224,7 +226,7 @@ export default function transformProps(
setDataMask,
emitFilter,
labelMap,
groupby,
groupby: groupbyLabels,
selectedValues,
};
}
14 changes: 9 additions & 5 deletions plugins/plugin-chart-echarts/src/Funnel/transformProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getNumberFormatter,
NumberFormats,
NumberFormatter,
getColumnLabel,
} from '@superset-ui/core';
import { CallbackDataParams } from 'echarts/types/src/util/types';
import { EChartsOption, FunnelSeriesOption } from 'echarts';
Expand Down Expand Up @@ -107,16 +108,19 @@ export default function transformProps(
...formData,
};
const metricLabel = getMetricLabel(metric);
const keys = data.map(datum => extractGroupbyLabel({ datum, groupby, coltypeMapping: {} }));
const groupbyLabels = groupby.map(getColumnLabel);
const keys = data.map(datum =>
extractGroupbyLabel({ datum, groupby: groupbyLabels, coltypeMapping: {} }),
);
const labelMap = data.reduce((acc: Record<string, DataRecordValue[]>, datum) => {
const label = extractGroupbyLabel({
datum,
groupby,
groupby: groupbyLabels,
coltypeMapping: {},
});
return {
...acc,
[label]: groupby.map(col => datum[col]),
[label]: groupbyLabels.map(col => datum[col]),
};
}, {});

Expand All @@ -126,7 +130,7 @@ export default function transformProps(
const numberFormatter = getNumberFormatter(numberFormat);

const transformedData: FunnelSeriesOption[] = data.map(datum => {
const name = extractGroupbyLabel({ datum, groupby, coltypeMapping: {} });
const name = extractGroupbyLabel({ datum, groupby: groupbyLabels, coltypeMapping: {} });
const isFiltered = filterState.selectedValues && !filterState.selectedValues.includes(name);
return {
value: datum[metricLabel],
Expand Down Expand Up @@ -214,7 +218,7 @@ export default function transformProps(
setDataMask,
emitFilter,
labelMap,
groupby,
groupby: groupbyLabels,
selectedValues,
};
}
2 changes: 1 addition & 1 deletion plugins/plugin-chart-echarts/src/Funnel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
export type EchartsFunnelFormData = QueryFormData &
EchartsLegendFormData & {
colorScheme?: string;
groupby: string[];
groupby: QueryFormData[];
labelLine: boolean;
labelType: EchartsFunnelLabelTypeType;
metric?: string;
Expand Down
Loading

0 comments on commit 749898c

Please sign in to comment.