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: echarts timeseries groupby #11103

Merged
merged 2 commits into from
Sep 29, 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
6 changes: 3 additions & 3 deletions superset-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion superset-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@superset-ui/legacy-preset-chart-big-number": "^0.15.3",
"@superset-ui/legacy-preset-chart-deckgl": "^0.3.1",
"@superset-ui/legacy-preset-chart-nvd3": "^0.15.3",
"@superset-ui/plugin-chart-echarts": "^0.15.3",
"@superset-ui/plugin-chart-echarts": "^0.15.4",
"@superset-ui/plugin-chart-table": "^0.15.3",
"@superset-ui/plugin-chart-word-cloud": "^0.15.3",
"@superset-ui/preset-chart-xy": "^0.15.3",
Expand Down
15 changes: 12 additions & 3 deletions superset/charts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ class ChartDataQueryObjectSchema(Schema):
)
groupby = fields.List(
fields.String(description="Columns by which to group the query.",),
allow_none=True,
Copy link
Member Author

Choose a reason for hiding this comment

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

QueryObject automatically does self.groupby = groupby or [] in the constructor, so no need to require a fields.List.

)
metrics = fields.List(
fields.Raw(),
Expand Down Expand Up @@ -781,12 +782,20 @@ class ChartDataQueryObjectSchema(Schema):
order_desc = fields.Boolean(
description="Reverse order. Default: `false`", required=False
)
extras = fields.Nested(ChartDataExtrasSchema, required=False)
columns = fields.List(fields.String(), description="",)
extras = fields.Nested(
ChartDataExtrasSchema,
description="Extra parameters to add to the query.",
required=False,
)
columns = fields.List(
fields.String(),
description="Columns which to select in the query.",
allow_none=True,
)
orderby = fields.List(
fields.List(fields.Raw()),
description="Expects a list of lists where the first element is the column "
"name which to sort by, and the second element is a boolean ",
"name which to sort by, and the second element is a boolean.",
example=[["my_col_1", False], ["my_col_2", True]],
)
where = fields.String(
Expand Down
13 changes: 6 additions & 7 deletions superset/utils/pandas_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import numpy as np
from flask_babel import gettext as _
from geopy.point import Point
from pandas import DataFrame, NamedAgg, Series
from pandas import DataFrame, NamedAgg, Series, Timestamp

from superset.exceptions import QueryObjectValidationError
from superset.utils.core import DTTM_ALIAS, PostProcessingContributionOrientation
Expand Down Expand Up @@ -93,7 +93,8 @@


def _flatten_column_after_pivot(
column: Union[str, Tuple[str, ...]], aggregates: Dict[str, Dict[str, Any]]
column: Union[float, Timestamp, str, Tuple[str, ...]],
aggregates: Dict[str, Dict[str, Any]],
) -> str:
"""
Function for flattening column names into a single string. This step is necessary
Expand All @@ -106,15 +107,13 @@ def _flatten_column_after_pivot(
:param aggregates: aggregates
:return:
"""
if isinstance(column, str):
return column
if len(column) == 1:
return column[0]
if not isinstance(column, tuple):
column = (column,)
if len(aggregates) == 1 and len(column) > 1:
# drop aggregate for single aggregate pivots with multiple groupings
# from column name (aggregates always come first in column name)
column = column[1:]
return ", ".join(column)
return ", ".join([str(col) for col in column])
Copy link
Member Author

Choose a reason for hiding this comment

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

It's a shame we have to format everything as strings for now, but hoping to remedy this once we move on to some other serialization format than JSON.



def validate_column_args(*argnames: str) -> Callable[..., Any]:
Expand Down
28 changes: 23 additions & 5 deletions tests/pandas_postprocessing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import math
from typing import Any, List, Optional

from pandas import DataFrame, Series
from pandas import DataFrame, Series, Timestamp
import pytest

from superset.exceptions import QueryObjectValidationError
Expand Down Expand Up @@ -77,6 +77,24 @@ def test_flatten_column_after_pivot(self):
),
"idx_nulls",
)
self.assertEqual(
proc._flatten_column_after_pivot(
aggregates=AGGREGATES_SINGLE, column=1234,
),
"1234",
)
self.assertEqual(
proc._flatten_column_after_pivot(
aggregates=AGGREGATES_SINGLE, column=Timestamp("2020-09-29T00:00:00"),
),
"2020-09-29 00:00:00",
)
self.assertEqual(
proc._flatten_column_after_pivot(
aggregates=AGGREGATES_SINGLE, column="idx_nulls",
),
"idx_nulls",
)
self.assertEqual(
proc._flatten_column_after_pivot(
aggregates=AGGREGATES_SINGLE, column=("idx_nulls", "col1"),
Expand All @@ -85,9 +103,9 @@ def test_flatten_column_after_pivot(self):
)
self.assertEqual(
proc._flatten_column_after_pivot(
aggregates=AGGREGATES_SINGLE, column=("idx_nulls", "col1", "col2"),
aggregates=AGGREGATES_SINGLE, column=("idx_nulls", "col1", 1234),
),
"col1, col2",
"col1, 1234",
)

# Multiple aggregate cases
Expand All @@ -100,9 +118,9 @@ def test_flatten_column_after_pivot(self):
self.assertEqual(
proc._flatten_column_after_pivot(
aggregates=AGGREGATES_MULTIPLE,
column=("idx_nulls", "asc_idx", "col1", "col2"),
column=("idx_nulls", "asc_idx", "col1", 1234),
),
"idx_nulls, asc_idx, col1, col2",
"idx_nulls, asc_idx, col1, 1234",
)

def test_pivot_without_columns(self):
Expand Down