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: support multiple time columns with time grain in Pivot Table v2 #21537

Merged
merged 9 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -25,7 +25,9 @@
"publishConfig": {
"access": "public"
},
"dependencies": {},
"dependencies": {
"lodash": "^4.17.11"
zhaoyongjie marked this conversation as resolved.
Show resolved Hide resolved
},
"peerDependencies": {
"@superset-ui/chart-controls": "*",
"@superset-ui/core": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
import omit from 'lodash/omit';

import {
AdhocColumn,
buildQueryContext,
ensureIsArray,
FeatureFlag,
isFeatureEnabled,
isPhysicalColumn,
QueryFormColumn,
QueryFormOrderBy,
} from '@superset-ui/core';
Expand All @@ -27,10 +33,29 @@ import { PivotTableQueryFormData } from '../types';
export default function buildQuery(formData: PivotTableQueryFormData) {
const { groupbyColumns = [], groupbyRows = [] } = formData;
// TODO: add deduping of AdhocColumns
const groupbySet = new Set([
...ensureIsArray<QueryFormColumn>(groupbyColumns),
...ensureIsArray<QueryFormColumn>(groupbyRows),
]);
const groupbySet = Array.from(
zhaoyongjie marked this conversation as resolved.
Show resolved Hide resolved
new Set([
...ensureIsArray<QueryFormColumn>(groupbyColumns),
...ensureIsArray<QueryFormColumn>(groupbyRows),
]),
).map(col => {
if (
isPhysicalColumn(col) &&
formData.time_grain_sqla &&
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES) &&
formData?.datetimeColumnLookup[col]
zhaoyongjie marked this conversation as resolved.
Show resolved Hide resolved
) {
return {
timeGrain: formData.time_grain_sqla,
columnType: 'BASE_AXIS',
sqlExpression: col,
label: col,
expressionType: 'SQL',
} as AdhocColumn;
}
return col;
});

return buildQueryContext(formData, baseQueryObject => {
const { series_limit_metric, metrics, order_desc } = baseQueryObject;
let orderBy: QueryFormOrderBy[] | undefined;
Expand All @@ -41,9 +66,11 @@ export default function buildQuery(formData: PivotTableQueryFormData) {
}
return [
{
...baseQueryObject,
...(isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES)
? omit(baseQueryObject, ['extras.time_grain_sqla'])
: baseQueryObject),
orderby: orderBy,
columns: [...groupbySet],
columns: groupbySet,
},
];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import React from 'react';
import {
ensureIsArray,
FeatureFlag,
isAdhocColumn,
isFeatureEnabled,
isPhysicalColumn,
QueryFormMetric,
smartDateFormatter,
t,
Expand All @@ -33,12 +37,14 @@ import {
emitFilterControl,
Dataset,
getStandardizedControls,
ControlState,
ControlPanelState,
} from '@superset-ui/chart-controls';
import { MetricsLayoutEnum } from '../types';

const config: ControlPanelConfig = {
controlPanelSections: [
{ ...sections.legacyTimeseriesTime, expanded: false },
{ ...sections.genericTime, expanded: false },
{
label: t('Query'),
expanded: true,
Expand All @@ -63,6 +69,57 @@ const config: ControlPanelConfig = {
},
},
],
[
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES)
? {
name: 'time_grain_sqla',
config: {
...sharedControls.time_grain_sqla,
visibility: ({ controls }) => {
const DTTMLookup = Object.fromEntries(
zhaoyongjie marked this conversation as resolved.
Show resolved Hide resolved
ensureIsArray(controls?.groupbyColumns?.options).map(
option => [option.column_name, option.is_dttm],
),
);
const selections = [
...ensureIsArray(controls?.groupbyColumns.value),
...ensureIsArray(controls?.groupbyRows.value),
];
return selections
.map(selection => {
if (isAdhocColumn(selection)) {
return true;
}
if (isPhysicalColumn(selection)) {
return !!DTTMLookup[selection];
}
return false;
})
.some(Boolean);
},
},
}
: null,
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES)
? {
name: 'datetimeColumnLookup',
config: {
type: 'HiddenControl',
default: (control: ControlState, state: ControlPanelState) =>
Object.fromEntries(
ensureIsArray<Record<string, any>>(
state?.datasource?.columns,
)
.filter(option => option.is_dttm)
.map(option => [
option.column_name ?? option.name,
option.is_dttm,
]),
),
},
}
: null,
],
[
{
name: 'metrics',
Expand Down