You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ms...@apache.org on 2025/08/06 19:04:48 UTC
(superset) branch msyavuz/fix/time-range-comparison-multi-column created (now dc40425398)
This is an automated email from the ASF dual-hosted git repository.
msyavuz pushed a change to branch msyavuz/fix/time-range-comparison-multi-column
in repository https://gitbox.apache.org/repos/asf/superset.git
at dc40425398 fix(Timeshift): Determine column correctly
This branch includes the following new commits:
new dc40425398 fix(Timeshift): Determine column correctly
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(superset) 01/01: fix(Timeshift): Determine column correctly
Posted by ms...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
msyavuz pushed a commit to branch msyavuz/fix/time-range-comparison-multi-column
in repository https://gitbox.apache.org/repos/asf/superset.git
commit dc404253989252d1097ed30a2f83e0e12a0be1b6
Author: Mehmet Salih Yavuz <sa...@proton.me>
AuthorDate: Wed Aug 6 22:04:17 2025 +0300
fix(Timeshift): Determine column correctly
---
superset/common/query_context_processor.py | 108 ++++++++++++++++++++++++++---
1 file changed, 99 insertions(+), 9 deletions(-)
diff --git a/superset/common/query_context_processor.py b/superset/common/query_context_processor.py
index 0d06ece281..5f0e4f2d44 100644
--- a/superset/common/query_context_processor.py
+++ b/superset/common/query_context_processor.py
@@ -456,11 +456,17 @@ class QueryContextProcessor:
return f"{(outer_from_dttm - offset_date).days} days ago"
return ""
- def processing_time_offsets( # pylint: disable=too-many-locals,too-many-statements # noqa: C901
+ def processing_time_offsets( # pylint: disable=too-many-locals,too-many-statements
self,
df: pd.DataFrame,
query_object: QueryObject,
) -> CachedTimeOffset:
+ """
+ Process time offsets for time comparison feature.
+
+ This method handles both relative time offsets (e.g., "1 week ago") and
+ absolute date range offsets (e.g., "2015-01-03 : 2015-01-04").
+ """
query_context = self._query_context
# ensure query_object is immutable
query_object_clone = copy.copy(query_object)
@@ -550,11 +556,10 @@ class QueryContextProcessor:
# Get time offset index
index = (get_base_axis_labels(query_object.columns) or [DTTM_ALIAS])[0]
- # Handle temporal filters
if is_date_range_offset and feature_flag_manager.is_feature_enabled(
"DATE_RANGE_TIMESHIFTS_ENABLED"
):
- # Create a completely new filter list to avoid conflicts
+ # Create a completely new filter list to preserve original filters
query_object_clone.filter = copy.deepcopy(query_object_clone.filter)
# Remove any existing temporal filters that might conflict
@@ -564,8 +569,12 @@ class QueryContextProcessor:
if not (flt.get("op") == FilterOperator.TEMPORAL_RANGE)
]
- # Add our specific temporal filter
- temporal_col = query_object_clone.granularity or x_axis_label
+ # Determine the temporal column with multiple fallback strategies
+ temporal_col = self._get_temporal_column_for_filter(
+ query_object_clone, x_axis_label
+ )
+
+ # Always add a temporal filter for date range offsets
if temporal_col:
new_temporal_filter: QueryObjectFilterClause = {
"col": temporal_col,
@@ -577,7 +586,22 @@ class QueryContextProcessor:
}
query_object_clone.filter.append(new_temporal_filter)
+ # Log for debugging
+ logger.debug(
+ f"Added temporal filter for date range offset: "
+ f"col={temporal_col}, range={query_object_clone.from_dttm} : {query_object_clone.to_dttm}"
+ )
+ else:
+ # This should rarely happen with proper fallbacks
+ raise QueryObjectValidationError(
+ _(
+ "Unable to identify temporal column for date range time comparison. "
+ "Please ensure your dataset has a properly configured time column."
+ )
+ )
+
else:
+ # RELATIVE OFFSET: Original logic for non-date-range offsets
# The comparison is not using a temporal column so we need to modify
# the temporal filter so we run the query with the correct time range
if not dataframe_utils.is_datetime_series(df.get(index)):
@@ -600,8 +624,7 @@ class QueryContextProcessor:
)
flt["val"] = f"{new_outer_from_dttm} : {new_outer_to_dttm}"
else:
- # If it IS a datetime series, we still need to clear conflicting
- # filters
+ # If it IS a datetime series, we still need to clear conflicting filters
query_object_clone.filter = copy.deepcopy(query_object_clone.filter)
# For relative offsets with datetime series, ensure the temporal
@@ -629,7 +652,7 @@ class QueryContextProcessor:
)
]
- # Continue with the rest of the method...
+ # Continue with the rest of the method (caching, execution, etc.)
cached_time_offset_key = (
offset if offset == original_offset else f"{offset}_{original_offset}"
)
@@ -661,7 +684,7 @@ class QueryContextProcessor:
# to the subquery so we prevent data inconsistency due to missing records
# in the dataframes when performing the join
if query_object.row_limit or query_object.row_offset:
- query_object_clone_dct["row_limit"] = current_app.config["ROW_LIMIT"]
+ query_object_clone_dct["row_limit"] = config["ROW_LIMIT"]
query_object_clone_dct["row_offset"] = 0
if isinstance(self._qc_datasource, Query):
@@ -713,6 +736,73 @@ class QueryContextProcessor:
return CachedTimeOffset(df=df, queries=queries, cache_keys=cache_keys)
+ def _get_temporal_column_for_filter( # noqa: C901
+ self, query_object: QueryObject, x_axis_label: str | None
+ ) -> str | None:
+ """
+ Helper method to reliably determine the temporal column for filtering.
+
+ This method tries multiple strategies to find the correct temporal column:
+ 1. Use explicitly set granularity
+ 2. Use x_axis_label if it's a temporal column
+ 3. Use datasource's main datetime column
+ 4. Find any datetime column in the datasource
+ 5. Look for common temporal column names
+
+ :param query_object: The query object
+ :param x_axis_label: The x-axis label from the query
+ :return: The name of the temporal column, or None if not found
+ """
+ # Strategy 1: Use explicitly set granularity
+ if query_object.granularity:
+ return query_object.granularity
+
+ # Strategy 2: Use x_axis_label if it exists
+ if x_axis_label:
+ return x_axis_label
+
+ # Strategy 3: Use datasource's main datetime column
+ if (
+ hasattr(self._qc_datasource, "main_dttm_col")
+ and self._qc_datasource.main_dttm_col
+ ):
+ return self._qc_datasource.main_dttm_col
+
+ # Strategy 4: Find any datetime column in the datasource
+ if hasattr(self._qc_datasource, "columns"):
+ for col in self._qc_datasource.columns:
+ if hasattr(col, "is_dttm") and col.is_dttm:
+ if hasattr(col, "column_name"):
+ return col.column_name
+ elif hasattr(col, "name"):
+ return col.name
+
+ # Strategy 5: Look for common temporal column names
+ if hasattr(self._qc_datasource, "column_names"):
+ common_temporal_names = [
+ "__timestamp",
+ "ds",
+ "dt",
+ "date",
+ "time",
+ "datetime",
+ "created_at",
+ "updated_at",
+ "timestamp",
+ "event_time",
+ ]
+ for common_name in common_temporal_names:
+ if common_name in self._qc_datasource.column_names:
+ return common_name
+
+ # Strategy 6: Check if there's already a temporal filter we can use as reference
+ if hasattr(query_object, "filter") and query_object.filter:
+ for flt in query_object.filter:
+ if flt.get("op") == FilterOperator.TEMPORAL_RANGE:
+ return flt.get("col")
+
+ return None
+
def _process_date_range_offset(
self, offset_df: pd.DataFrame, join_keys: list[str]
) -> tuple[pd.DataFrame, list[str]]: