Skip to content

Commit

Permalink
fix: Dashboard time grain in Table (apache#24746)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina authored Jul 20, 2023
1 parent ff42101 commit 41dffc2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
13 changes: 9 additions & 4 deletions superset-frontend/plugins/plugin-chart-table/src/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ const buildQuery: BuildQuery<TableChartFormData> = (
formData: TableChartFormData,
options,
) => {
const { percent_metrics: percentMetrics, order_desc: orderDesc = false } =
formData;
const {
percent_metrics: percentMetrics,
order_desc: orderDesc = false,
extra_form_data,
} = formData;
const queryMode = getQueryMode(formData);
const sortByMetric = ensureIsArray(formData.timeseries_limit_metric)[0];
const time_grain_sqla =
extra_form_data?.time_grain_sqla || formData.time_grain_sqla;
let formDataCopy = formData;
// never include time in raw records mode
if (queryMode === QueryMode.raw) {
Expand Down Expand Up @@ -102,12 +107,12 @@ const buildQuery: BuildQuery<TableChartFormData> = (
columns = columns.map(col => {
if (
isPhysicalColumn(col) &&
formData.time_grain_sqla &&
time_grain_sqla &&
hasGenericChartAxes &&
formData?.temporal_columns_lookup?.[col]
) {
return {
timeGrain: formData.time_grain_sqla,
timeGrain: time_grain_sqla,
columnType: 'BASE_AXIS',
sqlExpression: col,
label: col,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import { QueryMode } from '@superset-ui/core';
import { QueryMode, TimeGranularity } from '@superset-ui/core';
import * as supersetCoreModule from '@superset-ui/core';
import buildQuery from '../src/buildQuery';
import { TableChartFormData } from '../src/types';

Expand Down Expand Up @@ -81,5 +82,44 @@ describe('plugin-chart-table', () => {
expect(query.columns).toEqual(['rawcol']);
expect(query.post_processing).toEqual([]);
});
it('should prefer extra_form_data.time_grain_sqla over formData.time_grain_sqla', () => {
Object.defineProperty(supersetCoreModule, 'hasGenericChartAxes', {
value: true,
});
const query = buildQuery({
...basicFormData,
groupby: ['col1'],
query_mode: QueryMode.aggregate,
time_grain_sqla: TimeGranularity.MONTH,
extra_form_data: { time_grain_sqla: TimeGranularity.QUARTER },
temporal_columns_lookup: { col1: true },
}).queries[0];
expect(query.columns?.[0]).toEqual({
timeGrain: TimeGranularity.QUARTER,
columnType: 'BASE_AXIS',
sqlExpression: 'col1',
label: 'col1',
expressionType: 'SQL',
});
});
it('should fallback to formData.time_grain_sqla if extra_form_data.time_grain_sqla is not set', () => {
Object.defineProperty(supersetCoreModule, 'hasGenericChartAxes', {
value: true,
});
const query = buildQuery({
...basicFormData,
time_grain_sqla: TimeGranularity.MONTH,
groupby: ['col1'],
query_mode: QueryMode.aggregate,
temporal_columns_lookup: { col1: true },
}).queries[0];
expect(query.columns?.[0]).toEqual({
timeGrain: TimeGranularity.MONTH,
columnType: 'BASE_AXIS',
sqlExpression: 'col1',
label: 'col1',
expressionType: 'SQL',
});
});
});
});

0 comments on commit 41dffc2

Please sign in to comment.