Skip to content

Commit

Permalink
fix(explore): Filters with custom SQL disappearing (apache#21114)
Browse files Browse the repository at this point in the history
* fix(explore): Filters with custom SQL disappearing

* Fix adhoc filter for query b disappearing

* Improve test coverage

(cherry picked from commit 55304b0)
  • Loading branch information
kgabryje authored and AnushaErrabelli committed Aug 18, 2022
1 parent 83dd851 commit b866b04
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export function isSimpleAdhocFilter(
return filter.expressionType === 'SIMPLE';
}

export function isFreeFormAdhocFilter(
filter: AdhocFilter,
): filter is FreeFormAdhocFilter {
return filter.expressionType === 'SQL';
}

export function isUnaryAdhocFilter(
filter: SimpleAdhocFilter,
): filter is UnaryAdhocFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isUnaryAdhocFilter,
isBinaryAdhocFilter,
isSetAdhocFilter,
isFreeFormAdhocFilter,
} from '@superset-ui/core';

describe('Filter type guards', () => {
Expand Down Expand Up @@ -95,4 +96,26 @@ describe('Filter type guards', () => {
).toEqual(false);
});
});
describe('isFreeFormAdhocFilter', () => {
it('should return true when it is the correct type', () => {
expect(
isFreeFormAdhocFilter({
expressionType: 'SQL',
clause: 'WHERE',
sqlExpression: 'gender = "boy"',
}),
).toEqual(true);
});
it('should return false otherwise', () => {
expect(
isFreeFormAdhocFilter({
expressionType: 'SIMPLE',
clause: 'WHERE',
subject: 'tea',
operator: '==',
comparator: 'matcha',
}),
).toEqual(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ const getExploreFormData = (overrides: JsonObject = {}) => ({
comparator: ['boys'],
filterOptionName: '123',
},
{
clause: 'WHERE' as const,
expressionType: 'SQL' as const,
operator: null,
subject: null,
comparator: null,
sqlExpression: "name = 'John'",
filterOptionName: '456',
},
{
clause: 'WHERE' as const,
expressionType: 'SQL' as const,
operator: null,
subject: null,
comparator: null,
sqlExpression: "city = 'Warsaw'",
filterOptionName: '567',
},
],
adhoc_filters_b: [
{
clause: 'WHERE' as const,
expressionType: 'SQL' as const,
operator: null,
subject: null,
comparator: null,
sqlExpression: "country = 'Poland'",
filterOptionName: '789',
},
],
applied_time_extras: {},
color_scheme: 'supersetColors',
Expand Down Expand Up @@ -111,6 +140,53 @@ const getExpectedResultFormData = (overrides: JsonObject = {}) => ({
comparator: ['boys'],
filterOptionName: '123',
},
{
clause: 'WHERE' as const,
expressionType: 'SQL' as const,
operator: null,
subject: null,
comparator: null,
sqlExpression: "name = 'John'",
filterOptionName: '456',
},
{
clause: 'WHERE' as const,
expressionType: 'SQL' as const,
operator: null,
subject: null,
comparator: null,
sqlExpression: "city = 'Warsaw'",
filterOptionName: '567',
},
{
clause: 'WHERE',
expressionType: 'SIMPLE',
operator: 'IN',
subject: 'name',
comparator: ['Aaron'],
isExtra: true,
filterOptionName: expect.any(String),
},
{
clause: 'WHERE',
expressionType: 'SIMPLE',
operator: '<=',
subject: 'num_boys',
comparator: 10000,
isExtra: true,
filterOptionName: expect.any(String),
},
],
adhoc_filters_b: [
{
clause: 'WHERE' as const,
expressionType: 'SQL' as const,
operator: null,
subject: null,
comparator: null,
sqlExpression: "country = 'Poland'",
filterOptionName: expect.any(String),
},
{
clause: 'WHERE',
expressionType: 'SIMPLE',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import {
QueryObjectFilterClause,
SimpleAdhocFilter,
QueryFormData,
AdhocFilter,
isFreeFormAdhocFilter,
isSimpleAdhocFilter,
} from '@superset-ui/core';
import { NO_TIME_RANGE } from '../constants';

Expand All @@ -51,28 +54,34 @@ const simpleFilterToAdhoc = (
return result;
};

const removeAdhocFilterDuplicates = (filters: SimpleAdhocFilter[]) => {
const removeAdhocFilterDuplicates = (filters: AdhocFilter[]) => {
const isDuplicate = (
adhocFilter: SimpleAdhocFilter,
existingFilters: SimpleAdhocFilter[],
adhocFilter: AdhocFilter,
existingFilters: AdhocFilter[],
) =>
existingFilters.some(
(existingFilter: SimpleAdhocFilter) =>
existingFilter.operator === adhocFilter.operator &&
existingFilter.subject === adhocFilter.subject &&
((!('comparator' in existingFilter) &&
!('comparator' in adhocFilter)) ||
('comparator' in existingFilter &&
'comparator' in adhocFilter &&
isEqual(existingFilter.comparator, adhocFilter.comparator))),
(existingFilter: AdhocFilter) =>
(isFreeFormAdhocFilter(existingFilter) &&
isFreeFormAdhocFilter(adhocFilter) &&
existingFilter.clause === adhocFilter.clause &&
existingFilter.sqlExpression === adhocFilter.sqlExpression) ||
(isSimpleAdhocFilter(existingFilter) &&
isSimpleAdhocFilter(adhocFilter) &&
existingFilter.operator === adhocFilter.operator &&
existingFilter.subject === adhocFilter.subject &&
((!('comparator' in existingFilter) &&
!('comparator' in adhocFilter)) ||
('comparator' in existingFilter &&
'comparator' in adhocFilter &&
isEqual(existingFilter.comparator, adhocFilter.comparator)))),
);

return filters.reduce((acc, filter) => {
if (!isDuplicate(filter, acc)) {
acc.push(filter);
}
return acc;
}, [] as SimpleAdhocFilter[]);
}, [] as AdhocFilter[]);
};

const mergeFilterBoxToFormData = (
Expand Down Expand Up @@ -176,16 +185,28 @@ export const getFormDataWithDashboardContext = (
exploreFormData,
dashboardContextFormData,
);
const adhocFilters = removeAdhocFilterDuplicates([
...ensureIsArray(exploreFormData.adhoc_filters),
...ensureIsArray(filterBoxData.adhoc_filters),
...ensureIsArray(nativeFiltersData.adhoc_filters),
]);
const adhocFilters = [
...Object.keys(exploreFormData),
...Object.keys(filterBoxData),
...Object.keys(nativeFiltersData),
]
.filter(key => key.match(/adhoc_filter.*/))
.reduce(
(acc, key) => ({
...acc,
[key]: removeAdhocFilterDuplicates([
...ensureIsArray(exploreFormData[key]),
...ensureIsArray(filterBoxData[key]),
...ensureIsArray(nativeFiltersData[key]),
]),
}),
{},
);
return {
...exploreFormData,
...dashboardContextFormData,
...filterBoxData,
...nativeFiltersData,
adhoc_filters: adhocFilters,
...adhocFilters,
};
};

0 comments on commit b866b04

Please sign in to comment.