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: filter_values in jinja_context not processing adhoc_filters #9348

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 10 additions & 11 deletions superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from superset import jinja_base_context
from superset.extensions import jinja_context_manager
from superset.utils import core as utils


def url_param(param: str, default: Optional[str] = None) -> Optional[Any]:
Expand Down Expand Up @@ -94,18 +95,16 @@ def filter_values(column: str, default: Optional[str] = None) -> List[str]:
:return: returns a list of filter values
"""
form_data = json.loads(request.form.get("form_data", "{}"))
utils.harmonize_query_filters(form_data)

return_val = []
for filter_type in ["filters", "extra_filters"]:
if filter_type not in form_data:
continue

for f in form_data[filter_type]:
if f["col"] == column:
if isinstance(f["val"], list):
for v in f["val"]:
return_val.append(v)
else:
return_val.append(f["val"])
for f in form_data.get("filters", {}):
Copy link
Member

Choose a reason for hiding this comment

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

I was working on filters recently and I'm pretty sure these are now under adhoc_filters

Copy link
Member Author

Choose a reason for hiding this comment

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

This wan't completely familiar territory to me, so I might be wrong here. But currently process_query_filters() in viz..py, which is the first thing query_obj() calls, goes through a long step of transformations that first changes everything into adhoc_filters, and finally changes those to regular filters. I'm unsure why this happens, but my takeaway was that if process_query_filters is run, you are left with nothing but filters, despite how they started out.

Copy link
Member

Choose a reason for hiding this comment

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

Oh wow. This is confusing. Looks like you looked into it deeper than I have. 👍

Copy link
Member Author

@villebro villebro Mar 24, 2020

Choose a reason for hiding this comment

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

Probably something for the refactor backlog. I took another pass at how this works in practice, and these are my takeaways:

Anyway, in the short term, I think this fix is the right solution.

if f["col"] == column:
if isinstance(f["val"], list):
for v in f["val"]:
return_val.append(v)
else:
return_val.append(f["val"])

if return_val:
return return_val
Expand Down
11 changes: 11 additions & 0 deletions superset/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,17 @@ def split_adhoc_filters_into_base_filters(fd):
fd["filters"] = simple_where_filters


def harmonize_query_filters(form_data: Dict[str, Any]) -> None:
"""
Converts all filters in form_data into base filters.

:param form_data: parsed form data from request to be mutated
"""
convert_legacy_filters_into_adhoc(form_data)
merge_extra_filters(form_data)
split_adhoc_filters_into_base_filters(form_data)


def get_username() -> Optional[str]:
"""Get username if within the flask context, otherwise return noffin'"""
try:
Expand Down
3 changes: 1 addition & 2 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,7 @@ def explore(self, datasource_type=None, datasource_id=None):
form_data["datasource"] = str(datasource_id) + "__" + datasource_type

# On explore, merge legacy and extra filters into the form data
utils.convert_legacy_filters_into_adhoc(form_data)
utils.merge_extra_filters(form_data)
utils.harmonize_query_filters(form_data)

# merge request url params
if request.method == "GET":
Expand Down
14 changes: 2 additions & 12 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@
from superset.models.helpers import QueryResult
from superset.typing import VizData
from superset.utils import core as utils
from superset.utils.core import (
DTTM_ALIAS,
JS_MAX_INTEGER,
merge_extra_filters,
to_adhoc,
)
from superset.utils.core import DTTM_ALIAS, JS_MAX_INTEGER, to_adhoc

if TYPE_CHECKING:
from superset.connectors.base.models import BaseDatasource
Expand Down Expand Up @@ -280,15 +275,10 @@ def df_metrics_to_num(self, df):
if dtype.type == np.object_ and col in metrics:
df[col] = pd.to_numeric(df[col], errors="coerce")

def process_query_filters(self):
utils.convert_legacy_filters_into_adhoc(self.form_data)
merge_extra_filters(self.form_data)
utils.split_adhoc_filters_into_base_filters(self.form_data)

def query_obj(self) -> Dict[str, Any]:
"""Building a query object"""
form_data = self.form_data
self.process_query_filters()
utils.harmonize_query_filters(form_data)
gb = form_data.get("groupby") or []
metrics = self.all_metrics or []
columns = form_data.get("columns") or []
Expand Down
19 changes: 19 additions & 0 deletions tests/macro_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,25 @@ def test_filter_values_macro(self):
"filters": [{"col": "my_special_filter", "op": "in", "val": "savage"}],
}

form_data5 = {
"adhoc_filters": [
{
"expressionType": "SIMPLE",
"subject": "my_special_filter",
"operator": "in",
"comparator": ["bar"],
"clause": "WHERE",
"sqlExpression": None,
"fromFormData": True,
}
]
}

data1 = {"form_data": json.dumps(form_data1)}
data2 = {"form_data": json.dumps(form_data2)}
data3 = {"form_data": json.dumps(form_data3)}
data4 = {"form_data": json.dumps(form_data4)}
data5 = {"form_data": json.dumps(form_data5)}

with app.test_request_context(data=data1):
filter_values = jinja_context.filter_values("my_special_filter")
Expand All @@ -75,3 +90,7 @@ def test_filter_values_macro(self):
with app.test_request_context(data=data4):
filter_values = jinja_context.filter_values("my_special_filter")
self.assertEqual(filter_values, ["savage", "foo"])

with app.test_request_context(data=data5):
filter_values = jinja_context.filter_values("my_special_filter")
self.assertEqual(filter_values, ["bar"])