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: Add deprecated fields to QueryObject schema #9579

Merged
merged 2 commits into from
Apr 18, 2020
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
34 changes: 34 additions & 0 deletions superset/charts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,19 @@ class ChartDataExtrasSchema(Schema):
enum=["today", "now"],
required=False,
)
where = fields.String(
description="WHERE clause to be added to queries using AND operator.",
required=False,
)
having = fields.String(
description="HAVING clause to be added to aggregate queries using "
"AND operator.",
required=False,
)
having_druid = fields.String(
description="HAVING filters to be added to legacy Druid datasource queries.",
required=False,
)


class ChartDataQueryObjectSchema(Schema):
Expand Down Expand Up @@ -484,6 +497,27 @@ class ChartDataQueryObjectSchema(Schema):
required=False,
example=[["my_col_1", False], ["my_col_2", True]],
)
where = fields.String(
description="WHERE clause to be added to queries using AND operator."
"This field is deprecated, and should be passed to `extras`.",
required=False,
deprecated=True,
)
having = fields.String(
description="HAVING clause to be added to aggregate queries using "
"AND operator. This field is deprecated, and should be passed "
"to `extras`.",
required=False,
deprecated=True,
)
having_filters = fields.List(
fields.Dict(),
description="HAVING filters to be added to legacy Druid datasource queries. "
"This field is deprecated, and should be passed to `extras` "
"as `filters_druid`.",
required=False,
deprecated=True,
)


class ChartDataDatasourceSchema(Schema):
Expand Down
26 changes: 25 additions & 1 deletion superset/common/query_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import hashlib
import logging
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, NamedTuple, Optional, Union

import simplejson as json
from flask_babel import gettext as _
Expand All @@ -35,6 +35,18 @@
# https://github.com/python/mypy/issues/5288


class DeprecatedExtrasField(NamedTuple):
name: str
extras_name: str


DEPRECATED_EXTRAS_FIELDS = (
DeprecatedExtrasField(name="where", extras_name="where"),
DeprecatedExtrasField(name="having", extras_name="having"),
DeprecatedExtrasField(name="having_filters", extras_name="having_druid"),
)


class QueryObject:
"""
The query object's schema matches the interfaces of DB connectors like sqla
Expand Down Expand Up @@ -75,6 +87,7 @@ def __init__(
columns: Optional[List[str]] = None,
orderby: Optional[List[List]] = None,
post_processing: Optional[List[Dict[str, Any]]] = None,
**kwargs: Any,
):
extras = extras or {}
is_sip_38 = is_feature_enabled("SIP_38_VIZ_REARCHITECTURE")
Expand Down Expand Up @@ -124,6 +137,17 @@ def __init__(

self.orderby = orderby or []

# move deprecated fields to extras
for field in DEPRECATED_EXTRAS_FIELDS:
if field.name in kwargs:
logger.warning(
f"The field `{field.name} is deprecated, and should be "
f"passed to `extras` via the `{field.extras_name}` property"
)
value = kwargs[field.name]
if value:
self.extras[field.extras_name] = value

def to_dict(self) -> Dict[str, Any]:
query_object_dict = {
"granularity": self.granularity,
Expand Down
4 changes: 2 additions & 2 deletions superset/connectors/druid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from datetime import datetime, timedelta
from distutils.version import LooseVersion
from multiprocessing.pool import ThreadPool
from typing import cast, Dict, Iterable, List, Optional, Set, Tuple, Union
from typing import Any, cast, Dict, Iterable, List, Optional, Set, Tuple, Union

import pandas as pd
import sqlalchemy as sa
Expand Down Expand Up @@ -1639,7 +1639,7 @@ def _get_having_obj(self, col: str, op: str, eq: str) -> "Having":

return cond

def get_having_filters(self, raw_filters: List[Dict]) -> "Having":
def get_having_filters(self, raw_filters: List[Dict[str, Any]]) -> "Having":
filters = None
reversed_op_map = {
FilterOperationType.NOT_EQUALS.value: FilterOperationType.EQUALS.value,
Expand Down
15 changes: 12 additions & 3 deletions tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,21 @@ def _get_query_context(self) -> Dict[str, Any]:
"datasource": {"id": slc.datasource_id, "type": slc.datasource_type},
"queries": [
{
"extras": {"where": ""},
"granularity": "ds",
"groupby": ["name"],
"is_timeseries": False,
"metrics": [{"label": "sum__num"}],
"filters": [],
"order_desc": True,
"orderby": [],
"row_limit": 100,
"time_range": "100 years ago : now",
"timeseries_limit": 0,
"timeseries_limit_metric": None,
"filters": [{"col": "gender", "op": "==", "val": "boy"}],
"having": "",
"having_filters": [],
"where": "",
}
],
}
Expand Down Expand Up @@ -660,8 +670,7 @@ def test_chart_data(self):
self.assertEqual(data["result"][0]["rowcount"], 100)

def test_invalid_chart_data(self):
"""
Query API: Test chart data query
"""Query API: Test chart data query with invalid schema
"""
self.login(username="admin")
query_context = self._get_query_context()
Expand Down