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

[ad-hoc filters] Fixing issue with legacy filters #5525

Merged
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
30 changes: 1 addition & 29 deletions superset/migrations/versions/bddc498dd179_adhoc_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,11 @@ class Slice(Base):
def upgrade():
bind = op.get_bind()
session = db.Session(bind=bind)
mapping = {'having': 'having_filters', 'where': 'filters'}

for slc in session.query(Slice).all():
try:
params = json.loads(slc.params)

if not 'adhoc_filters' in params:
params['adhoc_filters'] = []

for clause, filters in mapping.items():
if clause in params and params[clause] != '':
params['adhoc_filters'].append({
'clause': clause.upper(),
'expressionType': 'SQL',
'filterOptionName': str(uuid.uuid4()),
'sqlExpression': params[clause],
})

if filters in params:
for filt in params[filters]:
params['adhoc_filters'].append({
'clause': clause.upper(),
'comparator': filt['val'],
'expressionType': 'SIMPLE',
'filterOptionName': str(uuid.uuid4()),
'operator': filt['op'],
'subject': filt['col'],
})

for key in ('filters', 'having', 'having_filters', 'where'):
if key in params:
del params[key]

utils.convert_legacy_filters_into_adhoc(params)
slc.params = json.dumps(params, sort_keys=True)
except Exception:
pass
Expand Down
81 changes: 67 additions & 14 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,17 +713,42 @@ def get_celery_app(config):
return _celery_app


def to_adhoc(filt, expressionType='SIMPLE', clause='where'):
result = {
'clause': clause.upper(),
'expressionType': expressionType,
'filterOptionName': str(uuid.uuid4()),
}

if expressionType == 'SIMPLE':
result.update({
'comparator': filt['val'],
'operator': filt['op'],
'subject': filt['col'],
})
elif expressionType == 'SQL':
result.update({
'sqlExpression': filt[clause],
})

return result


def merge_extra_filters(form_data):
# extra_filters are temporary/contextual filters that are external
# to the slice definition. We use those for dynamic interactive
# filters like the ones emitted by the "Filter Box" visualization
# extra_filters are temporary/contextual filters (using the legacy constructs)
# that are external to the slice definition. We use those for dynamic
# interactive filters like the ones emitted by the "Filter Box" visualization.
# Note extra_filters only support simple filters.
if 'extra_filters' in form_data:
# __form and __to are special extra_filters that target time
# boundaries. The rest of extra_filters are simple
# [column_name in list_of_values]. `__` prefix is there to avoid
# potential conflicts with column that would be named `from` or `to`
if 'filters' not in form_data:
form_data['filters'] = []
if (
'adhoc_filters' not in form_data or
not isinstance(form_data['adhoc_filters'], list)
):
form_data['adhoc_filters'] = []
date_options = {
'__time_range': 'time_range',
'__time_col': 'granularity_sqla',
Expand All @@ -734,11 +759,20 @@ def merge_extra_filters(form_data):
# Grab list of existing filters 'keyed' on the column and operator

def get_filter_key(f):
return f['col'] + '__' + f['op']
if 'expressionType' in f:
return '{}__{}'.format(f['subject'], f['operator'])
else:
return '{}__{}'.format(f['col'], f['op'])

existing_filters = {}
for existing in form_data['filters']:
if existing['col'] is not None and existing['val'] is not None:
existing_filters[get_filter_key(existing)] = existing['val']
for existing in form_data['adhoc_filters']:
if (
existing['expressionType'] == 'SIMPLE' and
existing['comparator'] is not None and
existing['subject'] is not None
):
existing_filters[get_filter_key(existing)] = existing['comparator']

for filtr in form_data['extra_filters']:
# Pull out time filters/options and merge into form data
if date_options.get(filtr['col']):
Expand All @@ -757,16 +791,16 @@ def get_filter_key(f):
sorted(existing_filters[filter_key]) !=
sorted(filtr['val'])
):
form_data['filters'] += [filtr]
form_data['adhoc_filters'].append(to_adhoc(filtr))
else:
form_data['filters'] += [filtr]
form_data['adhoc_filters'].append(to_adhoc(filtr))
else:
# Do not add filter if same value already exists
if filtr['val'] != existing_filters[filter_key]:
form_data['filters'] += [filtr]
form_data['adhoc_filters'].append(to_adhoc(filtr))
else:
# Filter not found, add it
form_data['filters'] += [filtr]
form_data['adhoc_filters'].append(to_adhoc(filtr))
# Remove extra filters from the form data since no longer needed
del form_data['extra_filters']

Expand Down Expand Up @@ -921,14 +955,33 @@ def since_until_to_time_range(form_data):
form_data['time_range'] = ' : '.join((since, until))


def convert_legacy_filters_into_adhoc(fd):
mapping = {'having': 'having_filters', 'where': 'filters'}

if 'adhoc_filters' not in fd:
fd['adhoc_filters'] = []

for clause, filters in mapping.items():
if clause in fd and fd[clause] != '':
fd['adhoc_filters'].append(to_adhoc(fd, 'SQL', clause))

if filters in fd:
for filt in fd[filters]:
fd['adhoc_filters'].append(to_adhoc(filt, 'SIMPLE', clause))

for key in ('filters', 'having', 'having_filters', 'where'):
if key in fd:
del fd[key]


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)
adhoc_filters = fd.get('adhoc_filters')
if isinstance(adhoc_filters, list):
simple_where_filters = []
simple_having_filters = []
Expand Down
3 changes: 2 additions & 1 deletion superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,8 +1281,9 @@ 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)
utils.convert_legacy_filters_into_adhoc(form_data)
merge_extra_filters(form_data)
utils.split_adhoc_filters_into_base_filters(form_data)

# merge request url params
if request.method == 'GET':
Expand Down
5 changes: 2 additions & 3 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,9 @@ def query_obj(self):
# 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)

utils.convert_legacy_filters_into_adhoc(form_data)
merge_extra_filters(form_data)

utils.split_adhoc_filters_into_base_filters(form_data)
granularity = (
form_data.get('granularity') or
form_data.get('granularity_sqla')
Expand Down
Loading