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

[Adhoc Filters] integrating dashboard filters with adhoc filters #5056

Merged
merged 1 commit into from
May 23, 2018
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
3 changes: 1 addition & 2 deletions superset/assets/src/dashboard/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ class Dashboard extends React.PureComponent {

getFormDataExtra(slice) {
const formDataExtra = Object.assign({}, slice.formData);
const extraFilters = this.effectiveExtraFilters(slice.slice_id);
formDataExtra.extra_filters = formDataExtra.filters.concat(extraFilters);
formDataExtra.extra_filters = this.effectiveExtraFilters(slice.slice_id);
return formDataExtra;
}

Expand Down
41 changes: 41 additions & 0 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,44 @@ def ensure_path_exists(path):
except OSError as exc:
if not (os.path.isdir(path) and exc.errno == errno.EEXIST):
raise


def split_adhoc_filters_into_base_filters(fd):
Copy link
Member

Choose a reason for hiding this comment

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

Typically we use the form_data variable to represent form data. It's probably best to rename fd to form_data for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@john-bodley fd is used throughout the codebase already

"""
Mutates form data to restructure the adhoc filters in the form of the four base
filters, `where`, `having`, `filters`, and `having_filters` which represent
free form where sql, free form having sql, structured where clauses and structured
having clauses.
"""
adhoc_filters = fd.get('adhoc_filters', None)
if isinstance(adhoc_filters, list):
simple_where_filters = []
simple_having_filters = []
sql_where_filters = []
sql_having_filters = []
for adhoc_filter in adhoc_filters:
expression_type = adhoc_filter.get('expressionType')
clause = adhoc_filter.get('clause')
if expression_type == 'SIMPLE':
if clause == 'WHERE':
simple_where_filters.append({
'col': adhoc_filter.get('subject'),
'op': adhoc_filter.get('operator'),
'val': adhoc_filter.get('comparator'),
})
elif clause == 'HAVING':
simple_having_filters.append({
'col': adhoc_filter.get('subject'),
'op': adhoc_filter.get('operator'),
'val': adhoc_filter.get('comparator'),
})
elif expression_type == 'SQL':
if clause == 'WHERE':
sql_where_filters.append(adhoc_filter.get('sqlExpression'))
elif clause == 'HAVING':
sql_having_filters.append(adhoc_filter.get('sqlExpression'))
fd['where'] = ' AND '.join(['({})'.format(sql) for sql in sql_where_filters])
fd['having'] = ' AND '.join(['({})'.format(sql) for sql in sql_having_filters])
fd['having_filters'] = simple_having_filters
fd['filters'] = simple_where_filters
del fd['adhoc_filters']
1 change: 1 addition & 0 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,7 @@ def explore(self, datasource_type=None, datasource_id=None):
form_data['datasource'] = str(datasource_id) + '__' + datasource_type

# On explore, merge extra filters into the form data
utils.split_adhoc_filters_into_base_filters(form_data)
merge_extra_filters(form_data)

# merge request url params
Expand Down
64 changes: 13 additions & 51 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ def query_obj(self):
groupby.remove(DTTM_ALIAS)
is_timeseries = True

# Add extra filters into the query form data
# extras are used to query elements specific to a datasource type
# for instance the extra where clause that applies only to Tables

Copy link
Member

Choose a reason for hiding this comment

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

Nit. Remove blank line.

utils.split_adhoc_filters_into_base_filters(form_data)

Copy link
Member

Choose a reason for hiding this comment

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

Nit. Remove blank line.

merge_extra_filters(form_data)

granularity = (
Expand Down Expand Up @@ -272,57 +276,15 @@ def query_obj(self):
self.from_dttm = from_dttm
self.to_dttm = to_dttm

# extras are used to query elements specific to a datasource type
# for instance the extra where clause that applies only to Tables
filters = form_data.get('filters', [])

extras = {}
filters = []
adhoc_filters = form_data.get('adhoc_filters', None)
if adhoc_filters is None:
extras = {
'where': form_data.get('where', ''),
'having': form_data.get('having', ''),
'having_druid': form_data.get('having_filters', []),
'time_grain_sqla': form_data.get('time_grain_sqla', ''),
'druid_time_origin': form_data.get('druid_time_origin', ''),
}
filters = form_data.get('filters', [])
elif isinstance(adhoc_filters, list):
simple_where_filters = []
simple_having_filters = []
sql_where_filters = []
sql_having_filters = []
for adhoc_filter in adhoc_filters:
expression_type = adhoc_filter.get('expressionType')
clause = adhoc_filter.get('clause')
if expression_type == 'SIMPLE':
if clause == 'WHERE':
simple_where_filters.append({
'col': adhoc_filter.get('subject'),
'op': adhoc_filter.get('operator'),
'val': adhoc_filter.get('comparator'),
})
elif clause == 'HAVING':
simple_having_filters.append({
'col': adhoc_filter.get('subject'),
'op': adhoc_filter.get('operator'),
'val': adhoc_filter.get('comparator'),
})
elif expression_type == 'SQL':
if clause == 'WHERE':
sql_where_filters.append(adhoc_filter.get('sqlExpression'))
elif clause == 'HAVING':
sql_having_filters.append(adhoc_filter.get('sqlExpression'))
extras = {
'where': ' AND '.join(['({})'.format(sql) for sql in sql_where_filters]),
'having': ' AND '.join(
['({})'.format(sql) for sql in sql_having_filters],
),
'having_druid': simple_having_filters,
'time_grain_sqla': form_data.get('time_grain_sqla', ''),
'druid_time_origin': form_data.get('druid_time_origin', ''),
}
filters = simple_where_filters
extras = {
'where': form_data.get('where', ''),
'having': form_data.get('having', ''),
'having_druid': form_data.get('having_filters', []),
'time_grain_sqla': form_data.get('time_grain_sqla', ''),
'druid_time_origin': form_data.get('druid_time_origin', ''),
}

d = {
'granularity': granularity,
Expand Down