Skip to content

Commit

Permalink
fix: echarts timeseries groupby (apache#11103)
Browse files Browse the repository at this point in the history
* fix: echarts timeseries groupby

* address review comment
  • Loading branch information
villebro authored and auxten committed Nov 20, 2020
1 parent 6578ce0 commit dae1e22
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
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,
)
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])


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

0 comments on commit dae1e22

Please sign in to comment.