Skip to content

Commit

Permalink
feat: support multiple columns with time grain in Table Chart
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie committed Sep 22, 2022
1 parent e671d80 commit 59013bb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
25 changes: 24 additions & 1 deletion superset-frontend/plugins/plugin-chart-table/src/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
* under the License.
*/
import {
AdhocColumn,
buildQueryContext,
ensureIsArray,
FeatureFlag,
getMetricLabel,
isFeatureEnabled,
isPhysicalColumn,
QueryMode,
QueryObject,
removeDuplicates,
Expand Down Expand Up @@ -63,7 +67,7 @@ const buildQuery: BuildQuery<TableChartFormData> = (
}

return buildQueryContext(formDataCopy, baseQueryObject => {
let { metrics, orderby = [] } = baseQueryObject;
let { metrics, orderby = [], columns = [] } = baseQueryObject;
let postProcessing: PostProcessingRule[] = [];

if (queryMode === QueryMode.aggregate) {
Expand Down Expand Up @@ -95,6 +99,24 @@ const buildQuery: BuildQuery<TableChartFormData> = (
},
];
}

columns = columns.map(col => {
if (
isPhysicalColumn(col) &&
formData.time_grain_sqla &&
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES) &&
formData?.datetime_columns_lookup?.[col]
) {
return {
timeGrain: formData.time_grain_sqla,
columnType: 'BASE_AXIS',
sqlExpression: col,
label: col,
expressionType: 'SQL',
} as AdhocColumn;
}
return col;
});
}

const moreProps: Partial<QueryObject> = {};
Expand All @@ -108,6 +130,7 @@ const buildQuery: BuildQuery<TableChartFormData> = (

let queryObject = {
...baseQueryObject,
columns,
orderby,
metrics,
post_processing: postProcessing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import {
ensureIsArray,
FeatureFlag,
GenericDataType,
isAdhocColumn,
isFeatureEnabled,
isPhysicalColumn,
QueryFormColumn,
QueryMode,
smartDateFormatter,
Expand Down Expand Up @@ -145,7 +147,7 @@ const percentMetricsControl: typeof sharedControls.metrics = {

const config: ControlPanelConfig = {
controlPanelSections: [
sections.legacyTimeseriesTime,
sections.genericTime,
{
label: t('Query'),
expanded: true,
Expand Down Expand Up @@ -186,6 +188,39 @@ const config: ControlPanelConfig = {
},
},
],
[
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES) && isAggMode
? {
name: 'time_grain_sqla',
config: {
...sharedControls.time_grain_sqla,
visibility: ({ controls }) => {
const dttmLookup = Object.fromEntries(
ensureIsArray(controls?.groupby?.options).map(option => [
option.column_name,
option.is_dttm,
]),
);

return ensureIsArray(controls?.groupby.value)
.map(selection => {
if (isAdhocColumn(selection)) {
return true;
}
if (isPhysicalColumn(selection)) {
return !!dttmLookup[selection];
}
return false;
})
.some(Boolean);
},
},
}
: null,
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES) && isAggMode
? 'datetime_columns_lookup'
: null,
],
[
{
name: 'metrics',
Expand Down

0 comments on commit 59013bb

Please sign in to comment.