Skip to content

fix: Temporal filter conversion in viz migrations #33224

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

Merged
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
36 changes: 28 additions & 8 deletions superset/migrations/shared/migrate_viz/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from superset.constants import TimeGrain
from superset.migrations.shared.utils import paginated_update, try_load_json
from superset.utils import json
from superset.utils.date_parser import get_since_until

logger = logging.getLogger("alembic")

Expand Down Expand Up @@ -113,14 +114,33 @@
}

if isinstance(granularity_sqla, dict):
temporal_filter["comparator"] = None
temporal_filter["expressionType"] = "SQL"
temporal_filter["subject"] = granularity_sqla["label"]
temporal_filter["sqlExpression"] = granularity_sqla["sqlExpression"]

rv_data["adhoc_filters"] = (rv_data.get("adhoc_filters") or []) + [
temporal_filter
]
since, until = get_since_until(time_range=time_range)
if not since and not until:
temporal_filter = {}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L119 was not covered by tests
else:
temporal_filter["comparator"] = None
temporal_filter["expressionType"] = "SQL"
temporal_filter["subject"] = granularity_sqla["label"]

start_date = since.isoformat() if since else None
end_date = until.isoformat() if until else None
if start_date and end_date:
temporal_filter["sqlExpression"] = (
f"{granularity_sqla['sqlExpression']} >= '{start_date}' AND "
f"{granularity_sqla['sqlExpression']} < '{end_date}'"
)
elif start_date:
temporal_filter["sqlExpression"] = (

Check warning on line 133 in superset/migrations/shared/migrate_viz/base.py

View check run for this annotation

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/base.py#L132-L133

Added lines #L132 - L133 were not covered by tests
f"{granularity_sqla['sqlExpression']} >= '{start_date}'"
)
elif end_date:
temporal_filter["sqlExpression"] = (

Check warning on line 137 in superset/migrations/shared/migrate_viz/base.py

View check run for this annotation

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/base.py#L136-L137

Added lines #L136 - L137 were not covered by tests
f"{granularity_sqla['sqlExpression']} < '{end_date}'"
)

rv_data["adhoc_filters"] = rv_data.get("adhoc_filters") or []
if temporal_filter:
rv_data["adhoc_filters"].append(temporal_filter)

@classmethod
def upgrade_slice(cls, slc: Slice) -> None:
Expand Down
10 changes: 6 additions & 4 deletions tests/unit_tests/migrations/viz/time_related_fields_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@

SOURCE_FORM_DATA: dict[str, Any] = {
"granularity_sqla": "ds",
"time_range": "100 years ago : now",
"time_range": "100 years ago : today",
"viz_type": "pivot_table",
}

TARGET_FORM_DATA: dict[str, Any] = {
"form_data_bak": SOURCE_FORM_DATA,
"granularity_sqla": "ds",
"rowOrder": "value_z_to_a",
"time_range": "100 years ago : now",
"time_range": "100 years ago : today",
"viz_type": "pivot_table_v2",
}

Expand All @@ -40,7 +40,7 @@ def test_migration() -> None:
target["adhoc_filters"] = [
{
"clause": "WHERE",
"comparator": "100 years ago : now",
"comparator": "100 years ago : today",
"expressionType": "SIMPLE",
"operator": "TEMPORAL_RANGE",
"subject": "ds",
Expand All @@ -65,7 +65,9 @@ def test_custom_sql_time_column() -> None:
"comparator": None,
"expressionType": "SQL",
"operator": "TEMPORAL_RANGE",
"sqlExpression": "sum(ds)",
"sqlExpression": (
"sum(ds) >= '1925-04-24T00:00:00' AND sum(ds) < '2025-04-24T00:00:00'"
),
"subject": "ds",
}
]
Expand Down
Loading