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

fix(explore): Filters with custom SQL disappearing #21114

Merged
merged 3 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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) =>
Copy link
Member

@michael-s-molina michael-s-molina Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split this code into equality functions for each filter type to improve readability? You can do it in a follow-up PR.

(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.*/))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handles the case of fields like adhoc_filters_b in mixed chart. Logic similar to superset/utils/core.py, line 1121

.reduce(
(acc, key) => ({
...acc,
[key]: removeAdhocFilterDuplicates([
...ensureIsArray(exploreFormData[key]),
...ensureIsArray(filterBoxData[key]),
...ensureIsArray(nativeFiltersData[key]),
]),
}),
{},
);
return {
...exploreFormData,
...dashboardContextFormData,
...filterBoxData,
...nativeFiltersData,
adhoc_filters: adhocFilters,
...adhocFilters,
};
};