Skip to content

fix: Query object of migrated viz types #33022

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions superset/migrations/shared/migrate_viz/processors.py
Original file line number Diff line number Diff line change
@@ -35,6 +35,9 @@
):
self.data["metric"] = self.data["metrics"][0]

def build_query():
pass

Check warning on line 39 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L39

Added line #L39 was not covered by tests


class MigratePivotTable(MigrateViz):
source_viz_type = "pivot_table"
@@ -70,6 +73,9 @@

self.data["rowOrder"] = "value_z_to_a"

def build_query():
pass

Check warning on line 77 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L77

Added line #L77 was not covered by tests


class MigrateDualLine(MigrateViz):
has_x_axis_control = True
@@ -94,12 +100,18 @@
super()._migrate_temporal_filter(rv_data)
rv_data["adhoc_filters_b"] = rv_data.get("adhoc_filters") or []

def build_query():
pass

Check warning on line 104 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L104

Added line #L104 was not covered by tests


class MigrateSunburst(MigrateViz):
source_viz_type = "sunburst"
target_viz_type = "sunburst_v2"
rename_keys = {"groupby": "columns"}

def build_query():
pass

Check warning on line 113 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L113

Added line #L113 was not covered by tests


class TimeseriesChart(MigrateViz):
has_x_axis_control = True
@@ -155,6 +167,9 @@
if x_ticks_layout := self.data.get("x_ticks_layout"):
self.data["x_ticks_layout"] = 45 if x_ticks_layout == "45°" else 0

def build_query():
pass

Check warning on line 171 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L171

Added line #L171 was not covered by tests


class MigrateLineChart(TimeseriesChart):
source_viz_type = "line"
@@ -267,6 +282,9 @@
# Truncate y-axis by default to preserve layout
self.data["y_axis_showminmax"] = True

def build_query():
pass

Check warning on line 286 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L286

Added line #L286 was not covered by tests


class MigrateHeatmapChart(MigrateViz):
source_viz_type = "heatmap"
@@ -282,6 +300,9 @@
def _pre_action(self) -> None:
self.data["legend_type"] = "continuous"

def build_query():
pass

Check warning on line 304 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L304

Added line #L304 was not covered by tests


class MigrateHistogramChart(MigrateViz):
source_viz_type = "histogram"
@@ -305,6 +326,9 @@
if not groupby:
self.data["groupby"] = []

def build_query():
pass

Check warning on line 330 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L330

Added line #L330 was not covered by tests


class MigrateSankey(MigrateViz):
source_viz_type = "sankey"
@@ -316,3 +340,6 @@
if groupby and len(groupby) > 1:
self.data["source"] = groupby[0]
self.data["target"] = groupby[1]

def build_query():
pass

Check warning on line 345 in superset/migrations/shared/migrate_viz/processors.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/processors.py#L345

Added line #L345 was not covered by tests
163 changes: 163 additions & 0 deletions superset/migrations/shared/migrate_viz/query_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Any, Dict, List, Optional, Union
from enum import Enum

Check warning on line 18 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L17-L18

Added lines #L17 - L18 were not covered by tests


class ComparisonType(Enum):
VALUES = "values"
DIFFERENCE = "difference"
PERCENTAGE = "percentage"
RATIO = "ratio"

Check warning on line 25 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L21-L25

Added lines #L21 - L25 were not covered by tests


TIME_COMPARISON_SEPARATOR = "__"

Check warning on line 28 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L28

Added line #L28 was not covered by tests


def ensure_is_array(value: Optional[Union[List[Any], Any]] = None) -> List[Any]:

Check warning on line 31 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L31

Added line #L31 was not covered by tests
"""
Ensure a nullable value input is a list. Useful when consolidating
input format from a select control.
"""
if value is None:
return []
return value if isinstance(value, list) else [value]

Check warning on line 38 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L36-L38

Added lines #L36 - L38 were not covered by tests


def is_saved_metric(metric: any) -> bool:
return isinstance(metric, str)

Check warning on line 42 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L41-L42

Added lines #L41 - L42 were not covered by tests


def is_adhoc_metric_simple(metric: any) -> bool:
return (

Check warning on line 46 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L45-L46

Added lines #L45 - L46 were not covered by tests
not isinstance(metric, str)
and isinstance(metric, dict)
and metric.get("expressionType") == "SIMPLE"
)


def is_adhoc_metric_sql(metric: any) -> bool:
return (

Check warning on line 54 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L53-L54

Added lines #L53 - L54 were not covered by tests
not isinstance(metric, str)
and isinstance(metric, dict)
and metric.get("expressionType") == "SQL"
)


def is_query_form_metric(metric: any) -> bool:
return (

Check warning on line 62 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L61-L62

Added lines #L61 - L62 were not covered by tests
is_saved_metric(metric)
or is_adhoc_metric_simple(metric)
or is_adhoc_metric_sql(metric)
)


def get_metric_label(metric: dict) -> str:

Check warning on line 69 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L69

Added line #L69 was not covered by tests
"""
Get the label for a given metric.

Args:
metric (dict): The metric object.

Returns:
str: The label of the metric.
"""
if is_saved_metric(metric):
return metric
if "label" in metric and metric["label"]:
return metric["label"]
if is_adhoc_metric_simple(metric):
column_name = metric["column"].get("columnName") or metric["column"].get(

Check warning on line 84 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L79-L84

Added lines #L79 - L84 were not covered by tests
"column_name"
)
return f"{metric['aggregate']}({column_name})"
return metric["sqlExpression"]

Check warning on line 88 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L87-L88

Added lines #L87 - L88 were not covered by tests


def extract_extra_metrics(form_data: Dict[str, Any]) -> List[Any]:

Check warning on line 91 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L91

Added line #L91 was not covered by tests
"""
Extract extra metrics from the form data.

Args:
form_data (Dict[str, Any]): The query form data.

Returns:
List[Any]: A list of extra metrics.
"""
groupby = form_data.get("groupby", [])
timeseries_limit_metric = form_data.get("timeseries_limit_metric")
x_axis_sort = form_data.get("x_axis_sort")
metrics = form_data.get("metrics", [])

Check warning on line 104 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L101-L104

Added lines #L101 - L104 were not covered by tests

extra_metrics = []
limit_metric = (

Check warning on line 107 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L106-L107

Added lines #L106 - L107 were not covered by tests
ensure_is_array(timeseries_limit_metric)[0] if timeseries_limit_metric else None
)

if (

Check warning on line 111 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L111

Added line #L111 was not covered by tests
not groupby
and limit_metric
and get_metric_label(limit_metric) == x_axis_sort
and not any(get_metric_label(metric) == x_axis_sort for metric in metrics)
):
extra_metrics.append(limit_metric)

Check warning on line 117 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L117

Added line #L117 was not covered by tests

return extra_metrics

Check warning on line 119 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L119

Added line #L119 was not covered by tests


def get_metric_offsets_map(

Check warning on line 122 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L122

Added line #L122 was not covered by tests
form_data: Dict[str, List[str]], query_object: Dict[str, List[str]]
) -> Dict[str, str]:
"""
Return a dictionary mapping metric offset-labels to metric-labels.

Args:
form_data (Dict[str, List[str]]): The form data containing time comparisons.
query_object (Dict[str, List[str]]): The query object containing metrics.

Returns:
Dict[str, str]: A dictionary with offset-labels as keys and metric-labels as values.
"""
query_metrics = ensure_is_array(query_object.get("metrics", []))
time_offsets = ensure_is_array(form_data.get("time_compare", []))

Check warning on line 136 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L135-L136

Added lines #L135 - L136 were not covered by tests

metric_labels = [get_metric_label(metric) for metric in query_metrics]
metric_offset_map = {}

Check warning on line 139 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L138-L139

Added lines #L138 - L139 were not covered by tests

for metric in metric_labels:
for offset in time_offsets:
key = f"{metric}{TIME_COMPARISON_SEPARATOR}{offset}"
metric_offset_map[key] = metric

Check warning on line 144 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L141-L144

Added lines #L141 - L144 were not covered by tests

return metric_offset_map

Check warning on line 146 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L146

Added line #L146 was not covered by tests


def is_time_comparison(form_data: dict, query_object: dict) -> bool:

Check warning on line 149 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L149

Added line #L149 was not covered by tests
"""
Determine if the query involves a time comparison.

Args:
form_data (dict): The form data containing query parameters.
query_object (dict): The query object.

Returns:
bool: True if it is a time comparison, False otherwise.
"""
comparison_type = form_data.get("comparison_type")
metric_offset_map = get_metric_offsets_map(form_data, query_object)

Check warning on line 161 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L160-L161

Added lines #L160 - L161 were not covered by tests

return comparison_type in ComparisonType.values() and len(metric_offset_map) > 0

Check warning on line 163 in superset/migrations/shared/migrate_viz/query_functions.py

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/query_functions.py#L163

Added line #L163 was not covered by tests