Skip to content

Commit

Permalink
integrating dashboard filters with adhoc filters (apache#5056)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Lyons authored and john-bodley committed May 23, 2018
1 parent 17d6464 commit fa3e4e2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 53 deletions.
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):
"""
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

utils.split_adhoc_filters_into_base_filters(form_data)

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

0 comments on commit fa3e4e2

Please sign in to comment.