Skip to content

fix(Table): Allow timeshifts to be overriden #34014

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 5 commits into from
Jul 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
getColorFormatters,
} from '@superset-ui/chart-controls';

import { isEmpty } from 'lodash';
import { isEmpty, merge } from 'lodash';
import isEqualColumns from './utils/isEqualColumns';
import DateWithFormatter from './utils/DateWithFormatter';
import {
Expand Down Expand Up @@ -459,7 +459,7 @@ const transformProps = (
const {
height,
width,
rawFormData: formData,
rawFormData: originalFormData,
queriesData = [],
filterState,
ownState: serverPaginationData,
Expand All @@ -471,6 +471,12 @@ const transformProps = (
emitCrossFilters,
} = chartProps;

const formData = merge(
{},
originalFormData,
originalFormData.extra_form_data,
);

const {
align_pn: alignPositiveNegative = true,
color_pn: colorPositiveNegative = true,
Expand Down
65 changes: 49 additions & 16 deletions superset/common/query_context_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ def is_valid_date(self, date_string: str) -> bool:
# If parsing fails, it's not a valid date in the format YYYY-MM-DD
return False

def is_valid_date_range(self, date_range: str) -> bool:
try:
# Attempt to parse the string as a date range in the format
# YYYY-MM-DD:YYYY-MM-DD
start_date, end_date = date_range.split(":")
datetime.strptime(start_date.strip(), "%Y-%m-%d")
datetime.strptime(end_date.strip(), "%Y-%m-%d")
return True
except ValueError:
# If parsing fails, it's not a valid date range in the format
# YYYY-MM-DD:YYYY-MM-DD
return False

def get_offset_custom_or_inherit(
self,
offset: str,
Expand Down Expand Up @@ -470,17 +483,32 @@ def processing_time_offsets( # pylint: disable=too-many-locals,too-many-stateme
# filters: [{col: 'dttm_col', op: 'TEMPORAL_RANGE', val: '2020 : 2021'}], # noqa: E501
# }
original_offset = offset
if self.is_valid_date(offset) or offset == "inherit":
offset = self.get_offset_custom_or_inherit(
if self.is_valid_date_range(offset):
try:
# Parse the specified range
offset_from_dttm, offset_to_dttm = (
get_since_until_from_time_range(time_range=offset)
)
except ValueError as ex:
raise QueryObjectValidationError(str(ex)) from ex

# Use the specified range directly
query_object_clone.from_dttm = offset_from_dttm
query_object_clone.to_dttm = offset_to_dttm
else:
if self.is_valid_date(offset) or offset == "inherit":
offset = self.get_offset_custom_or_inherit(
offset,
outer_from_dttm,
outer_to_dttm,
)
query_object_clone.from_dttm = get_past_or_future(
offset,
outer_from_dttm,
outer_to_dttm,
)
query_object_clone.from_dttm = get_past_or_future(
offset,
outer_from_dttm,
)
query_object_clone.to_dttm = get_past_or_future(offset, outer_to_dttm)
query_object_clone.to_dttm = get_past_or_future(
offset, outer_to_dttm
)

x_axis_label = get_x_axis_label(query_object.columns)
query_object_clone.granularity = (
Expand All @@ -507,14 +535,19 @@ def processing_time_offsets( # pylint: disable=too-many-locals,too-many-stateme
flt.get("val"), str
):
time_range = cast(str, flt.get("val"))
(
new_outer_from_dttm,
new_outer_to_dttm,
) = get_since_until_from_time_range(
time_range=time_range,
time_shift=offset,
)
flt["val"] = f"{new_outer_from_dttm} : {new_outer_to_dttm}"
if self.is_valid_date_range(offset):
flt["val"] = (
f"{query_object_clone.from_dttm} : {query_object_clone.to_dttm}" # noqa: E501
)
else:
(
new_outer_from_dttm,
new_outer_to_dttm,
) = get_since_until_from_time_range(
time_range=time_range,
time_shift=offset,
)
flt["val"] = f"{new_outer_from_dttm} : {new_outer_to_dttm}"
query_object_clone.filter = [
flt
for flt in query_object_clone.filter
Expand Down
Loading