You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2023/04/19 00:29:13 UTC

[superset] 01/01: fix: pivot v2 charts created before GENERIC_CHART_AXES is enabled

This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch fix_generic_axis
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 06b066f741a8bd87d2ce8845120cde85c168fc7e
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Tue Apr 18 17:12:20 2023 -0700

    fix: pivot v2 charts created before GENERIC_CHART_AXES is enabled
---
 .../src/query/buildQueryContext.ts                 |  33 ++++++
 .../test/query/buildQueryContext.test.ts           | 118 +++++++++++++++++++++
 2 files changed, 151 insertions(+)

diff --git a/superset-frontend/packages/superset-ui-core/src/query/buildQueryContext.ts b/superset-frontend/packages/superset-ui-core/src/query/buildQueryContext.ts
index c1dc5bd89d..d74d5db807 100644
--- a/superset-frontend/packages/superset-ui-core/src/query/buildQueryContext.ts
+++ b/superset-frontend/packages/superset-ui-core/src/query/buildQueryContext.ts
@@ -17,6 +17,7 @@
  * under the License.
  */
 
+import { hasGenericChartAxes } from '@superset-ui/core';
 import buildQueryObject from './buildQueryObject';
 import DatasourceKey from './DatasourceKey';
 import { QueryFieldAliases, QueryFormData } from './types/QueryFormData';
@@ -59,6 +60,38 @@ export default function buildQueryContext(
   if (isXAxisSet(formData)) {
     queries = queries.map(query => normalizeTimeColumn(formData, query));
   }
+
+  /* Some charts saved before GENERIC_CHART_AXES is enabled will have the wrong
+   * time granularity set. This fixes the generated payload to conform with the new
+   * schema used when GENERIC_CHART_AXES is enabled.
+   */
+  if (hasGenericChartAxes && formData.granularity_sqla) {
+    const filterSubject = formData.granularity_sqla;
+    const filterComparator = formData?.time_range || 'No Filter';
+
+    queries.forEach(query => {
+      if (query.columns) {
+        const index = query.columns.indexOf(filterSubject);
+        if (index > -1) {
+          // eslint-disable-next-line no-param-reassign
+          query.columns[index] = {
+            columnType: 'BASE_AXIS',
+            expressionType: 'SQL',
+            label: filterSubject,
+            sqlExpression: filterSubject,
+            timeGrain: formData.time_grain_sqla,
+          };
+        }
+      }
+      // eslint-disable-next-line no-param-reassign
+      query.filters = query.filters || [];
+      query.filters.push({
+        col: filterSubject,
+        op: 'TEMPORAL_RANGE',
+        val: filterComparator,
+      });
+    });
+  }
   // --- query mutator end ---
   return {
     datasource: new DatasourceKey(formData.datasource).toObject(),
diff --git a/superset-frontend/packages/superset-ui-core/test/query/buildQueryContext.test.ts b/superset-frontend/packages/superset-ui-core/test/query/buildQueryContext.test.ts
index 9d47361e8f..defccc16ee 100644
--- a/superset-frontend/packages/superset-ui-core/test/query/buildQueryContext.test.ts
+++ b/superset-frontend/packages/superset-ui-core/test/query/buildQueryContext.test.ts
@@ -164,4 +164,122 @@ describe('buildQueryContext', () => {
     expect(spyNormalizeTimeColumn).not.toBeCalled();
     spyNormalizeTimeColumn.mockRestore();
   });
+  it('should fix pivot v2 charts saved before GENERIC_CHART_AXES was enabled', () => {
+    const queryContext = buildQueryContext({
+      datasource: '22__table',
+      viz_type: 'pivot_table_v2',
+      slice_id: 138,
+      url_params: {
+        native_filters_key:
+          'oGUUQKLlluSODgWflOlhSKi2dMljuOTbyGVxVlZ8s5xp_nTJpd3rdWram_xNDotb',
+      },
+      groupbyColumns: ['order_date'],
+      groupbyRows: [],
+      time_grain_sqla: 'P1M',
+      temporal_columns_lookup: {},
+      metrics: ['count'],
+      metricsLayout: 'COLUMNS',
+      adhoc_filters: [],
+      row_limit: 10000,
+      order_desc: true,
+      aggregateFunction: 'Sum',
+      valueFormat: 'SMART_NUMBER',
+      date_format: 'smart_date',
+      rowOrder: 'key_a_to_z',
+      colOrder: 'key_a_to_z',
+      dashboards: [13],
+      extra_form_data: {},
+      granularity_sqla: 'order_date',
+      time_range: 'No filter',
+      label_colors: {},
+      shared_label_colors: {},
+      extra_filters: [],
+      dashboardId: 13,
+      force: undefined,
+      result_format: 'json',
+      result_type: 'full',
+    });
+    expect(queryContext).toEqual({
+      datasource: {
+        id: 22,
+        type: 'table',
+      },
+      force: false,
+      queries: [
+        {
+          time_range: 'No filter',
+          granularity: 'order_date',
+          filters: [
+            {
+              col: 'order_date',
+              op: 'TEMPORAL_RANGE',
+              val: 'No filter',
+            },
+          ],
+          extras: {
+            having: '',
+            where: '',
+          },
+          applied_time_extras: {},
+          columns: [
+            {
+              columnType: 'BASE_AXIS',
+              expressionType: 'SQL',
+              label: 'order_date',
+              sqlExpression: 'order_date',
+              timeGrain: 'P1M',
+            },
+          ],
+          metrics: ['count'],
+          orderby: [['count', false]],
+          annotation_layers: [],
+          row_limit: 10000,
+          series_limit: 0,
+          order_desc: true,
+          url_params: {
+            native_filters_key:
+              'oGUUQKLlluSODgWflOlhSKi2dMljuOTbyGVxVlZ8s5xp_nTJpd3rdWram_xNDotb',
+          },
+          custom_params: {},
+          custom_form_data: {},
+        },
+      ],
+      form_data: {
+        datasource: '22__table',
+        viz_type: 'pivot_table_v2',
+        slice_id: 138,
+        url_params: {
+          native_filters_key:
+            'oGUUQKLlluSODgWflOlhSKi2dMljuOTbyGVxVlZ8s5xp_nTJpd3rdWram_xNDotb',
+        },
+        groupbyColumns: ['order_date'],
+        groupbyRows: [],
+        time_grain_sqla: 'P1M',
+        temporal_columns_lookup: {},
+        metrics: ['count'],
+        metricsLayout: 'COLUMNS',
+        adhoc_filters: [],
+        row_limit: 10000,
+        order_desc: true,
+        aggregateFunction: 'Sum',
+        valueFormat: 'SMART_NUMBER',
+        date_format: 'smart_date',
+        rowOrder: 'key_a_to_z',
+        colOrder: 'key_a_to_z',
+        dashboards: [13],
+        extra_form_data: {},
+        granularity_sqla: 'order_date',
+        time_range: 'No filter',
+        label_colors: {},
+        shared_label_colors: {},
+        extra_filters: [],
+        dashboardId: 13,
+        force: null,
+        result_format: 'json',
+        result_type: 'full',
+      },
+      result_format: 'json',
+      result_type: 'full',
+    });
+  });
 });