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(Filters): Apply native & cross filters on common columns #30438

Merged
merged 21 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bc85d17
fix(Filters): Apply native & cross filters on common columns
geido Sep 30, 2024
0f661c6
chore(Dashboard): Implement affected logic to highlight
geido Oct 3, 2024
8091a09
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 3, 2024
08ea4be
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 4, 2024
d2314e3
fix(Scope): Only include charts in scope
geido Oct 4, 2024
7fbb688
fix(CrossFilters): Check for deleted filter
geido Oct 4, 2024
2f180ae
fix(Scopes): Check for scopes array
geido Oct 7, 2024
2b6dd47
chore(Tests): Align tests with best practices
geido Oct 7, 2024
8fffbec
fix(Filters): Apply filters on underlying dataset columns
geido Oct 8, 2024
2106416
fix(Columns): Include all column names
geido Oct 8, 2024
bdef07f
fix(Dataset): Combine all columns
geido Oct 8, 2024
433ae8e
fix(Dataset): Remove unnecessary ref to column names
geido Oct 8, 2024
b5a69dd
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 8, 2024
03ebe9c
fix(CrossFilters): Check for actual cross filter target
geido Oct 8, 2024
b2e0b38
fix(CrossFilter): Relaunch query for removed cross-filter
geido Oct 8, 2024
f293ae1
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 10, 2024
e45da52
fix(Filters): Let backend handle jinja and adhoc cols
geido Oct 10, 2024
8de9cd6
chore(Todo): Remove todo comment
geido Oct 10, 2024
a18cddb
chore(Tests): Enhance tests
geido Oct 11, 2024
0d750b5
fix(Expression): Defult to array for non-array expression
geido Oct 14, 2024
1e471fc
chore(Type): Improve AppliedNativeFilterType
geido Oct 15, 2024
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
15 changes: 11 additions & 4 deletions superset-frontend/src/dashboard/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { areObjectsEqual } from '../../reduxUtils';
import getLocationHash from '../util/getLocationHash';
import isDashboardEmpty from '../util/isDashboardEmpty';
import { getAffectedOwnDataCharts } from '../util/charts/getOwnDataCharts';
import { getRelatedCharts } from '../util/getRelatedCharts';

const propTypes = {
actions: PropTypes.shape({
Expand Down Expand Up @@ -211,7 +212,7 @@ class Dashboard extends PureComponent {

applyFilters() {
const { appliedFilters } = this;
const { activeFilters, ownDataCharts } = this.props;
const { activeFilters, ownDataCharts, datasources, slices } = this.props;

// refresh charts if a filter was removed, added, or changed
const currFilterKeys = Object.keys(activeFilters);
Expand All @@ -228,10 +229,14 @@ class Dashboard extends PureComponent {
appliedFilterKeys.includes(filterKey)
) {
// filterKey is removed?
affectedChartIds.push(...appliedFilters[filterKey].scope);
affectedChartIds.push(
...getRelatedCharts(appliedFilters, slices, datasources)[filterKey],
);
} else if (!appliedFilterKeys.includes(filterKey)) {
// filterKey is newly added?
affectedChartIds.push(...activeFilters[filterKey].scope);
affectedChartIds.push(
...getRelatedCharts(activeFilters, slices, datasources)[filterKey],
);
} else {
// if filterKey changes value,
// update charts in its scope
Expand All @@ -244,7 +249,9 @@ class Dashboard extends PureComponent {
},
)
) {
affectedChartIds.push(...activeFilters[filterKey].scope);
affectedChartIds.push(
...getRelatedCharts(activeFilters, slices, datasources)[filterKey],
);
}

// if filterKey changes scope,
Expand Down
9 changes: 9 additions & 0 deletions superset-frontend/src/dashboard/components/Dashboard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ describe('Dashboard', () => {
[NATIVE_FILTER_ID]: {
scope: [230],
values: extraFormData,
filterType: null,
targets: [
{
datasetId: 13,
column: {
name: 'ethnic_minority',
},
},
],
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ export const getAllActiveFilters = ({
chartConfiguration?.[filterId]?.crossFilters?.chartsInScope ??
allSliceIds ??
[];
const filterType = nativeFilters?.[filterId]?.filterType ?? null;
geido marked this conversation as resolved.
Show resolved Hide resolved
const targets = nativeFilters?.[filterId]?.targets ?? scope;
// Iterate over all roots to find all affected charts
activeFilters[filterId] = {
scope,
filterType,
targets,
values: extraFormData,
};
});
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/dashboard/util/crossFilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ test('Recalculate charts in global filter scope when charts change', () => {
id: 1,
crossFilters: {
scope: { rootPath: ['ROOT_ID'], excluded: [1, 2] },
chartsInScope: [3],
chartsInScope: [],
},
},
'2': {
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/dashboard/util/crossFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const getCrossFiltersConfiguration = (
)
: getChartIdsInFilterScope(
chartConfiguration[chartId].crossFilters.scope,
Object.values(charts).map(chart => chart.id),
[...chartsByDataSource[chartDataSource]],
dashboardLayout,
);
}
Expand Down
154 changes: 154 additions & 0 deletions superset-frontend/src/dashboard/util/getRelatedCharts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { DatasourceType } from '@superset-ui/core';
import { DatasourcesState } from '../types';
import { getRelatedCharts } from './getRelatedCharts';

describe('getRelatedCharts', () => {
const slices = [
{ datasource: 'ds1', slice_id: 1 },
{ datasource: 'ds2', slice_id: 2 },
];

const datasources: DatasourcesState = {
ds1: {
uid: 'ds1',
datasource_name: 'ds1',
table_name: 'table1',
description: '',
id: 100,
columns: [{ column_name: 'column1' }, { column_name: 'column2' }],
column_types: [],
type: DatasourceType.Table,
metrics: [],
column_formats: {},
currency_formats: {},
verbose_map: {},
main_dttm_col: '',
filter_select_enabled: true,
},
ds2: {
uid: 'ds2',
datasource_name: 'ds2',
table_name: 'table2',
description: '',
id: 200,
columns: [{ column_name: 'column3' }, { column_name: 'column4' }],
column_types: [],
type: DatasourceType.Table,
metrics: [],
column_formats: {},
currency_formats: {},
verbose_map: {},
main_dttm_col: '',
filter_select_enabled: true,
},
};

it('should return chart ids matching the dataset id', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'column1' },
datasetId: 100,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [1],
});
});

it('should return chart ids matching the column name', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'column3' },
datasetId: 999,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [2],
});
});

it('should return chart ids when column display name matches', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { displayName: 'column4' },
datasetId: 999,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [2],
});
});

it('should return scope when filterType is not filter_select', () => {
const filters = {
filterKey1: {
filterType: 'filter_time',
scope: [3, 4],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [3, 4],
});
});

it('should return an empty array if no matching charts found', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'nonexistent_column' },
datasetId: 300,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [],
});
});
});
108 changes: 108 additions & 0 deletions superset-frontend/src/dashboard/util/getRelatedCharts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
michael-s-molina marked this conversation as resolved.
Show resolved Hide resolved
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Filter } from '@superset-ui/core';
import { DatasourcesState } from '../types';

type Slice = {
datasource: string;
slice_id: number;
};

function getRelatedChartsForSelectFilter(
filter: Filter,
slices: Slice[],
datasources: DatasourcesState,
) {
return Object.values(slices)
.filter(({ datasource }) => {
const chartDatasource = datasources[datasource];
if (!chartDatasource) return false;
const { column, datasetId } = filter.targets[0];
geido marked this conversation as resolved.
Show resolved Hide resolved

return (
chartDatasource.id === datasetId ||
chartDatasource.columns.some(
col =>
col.column_name === column?.name ||
col.column_name === column?.displayName,
)
);
})
.map(slice => slice.slice_id);
}
function getRelatedChartsForCrossFilter(
filterKey: string,
slices: Slice[],
geido marked this conversation as resolved.
Show resolved Hide resolved
datasources: DatasourcesState,
): number[] {
const sourceSlice = slices[filterKey];
if (!sourceSlice) return [];

const sourceDatasource = datasources[sourceSlice.datasource];
if (!sourceDatasource) return [];

return Object.values(slices)
.filter(slice => {
if (slice.slice_id === Number[filterKey]) return false;
geido marked this conversation as resolved.
Show resolved Hide resolved
const targetDatasource = datasources[slice.datasource];
if (!targetDatasource) return false;
if (targetDatasource === sourceDatasource) return true;

return sourceDatasource.columns.some(sourceColumn =>
targetDatasource.columns.some(
targetColumn => sourceColumn.column_name === targetColumn.column_name,
michael-s-molina marked this conversation as resolved.
Show resolved Hide resolved
),
);
})
.map(slice => slice.slice_id);
}

export function getRelatedCharts(
filters: Object,
slices: Slice[],
geido marked this conversation as resolved.
Show resolved Hide resolved
datasources: DatasourcesState,
) {
return Object.entries(filters).reduce((acc, [filterKey, filter]) => {
if (filter.filterType === 'filter_select') {
return {
...acc,
[filterKey]: getRelatedChartsForSelectFilter(
geido marked this conversation as resolved.
Show resolved Hide resolved
filter,
slices,
datasources,
),
};
}
if (filter.filterType === null) {
return {
...acc,
[filterKey]: getRelatedChartsForCrossFilter(
filterKey,
slices,
datasources,
),
};
}
return {
...acc,
[filterKey]: filter.scope,
};
}, {});
}
Loading