From 1014a327f5ed2e1e4f1daec283825e94c7640d05 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt <33317356+villebro@users.noreply.github.com> Date: Wed, 14 Dec 2022 09:43:38 +0200 Subject: [PATCH] fix(sqla): copy temporal range logic to helper (#22405) Co-authored-by: Ville Brofeldt --- superset/models/helpers.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 2582f7e8d2b6..8931ba3af689 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -65,6 +65,7 @@ from superset import app, is_feature_enabled, security_manager from superset.advanced_data_type.types import AdvancedDataTypeResponse from superset.common.db_query_status import QueryStatus +from superset.common.utils.time_range_utils import get_since_until_from_time_range from superset.constants import EMPTY_STRING, NULL_STRING from superset.db_engine_specs.base import TimestampExpression from superset.errors import ErrorLevel, SupersetError, SupersetErrorType @@ -1236,8 +1237,8 @@ def dttm_sql_literal(self, dttm: sa.DateTime, col_type: Optional[str]) -> str: def get_time_filter( self, time_col: Dict[str, Any], - start_dttm: sa.DateTime, - end_dttm: sa.DateTime, + start_dttm: Optional[sa.DateTime], + end_dttm: Optional[sa.DateTime], ) -> ColumnElement: label = "__time" col = time_col.get("column_name") @@ -1779,6 +1780,23 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma where_clause_and.append(sqla_col.like(eq)) elif op == utils.FilterOperator.ILIKE.value: where_clause_and.append(sqla_col.ilike(eq)) + elif ( + op == utils.FilterOperator.TEMPORAL_RANGE.value + and isinstance(eq, str) + and col_obj is not None + ): + _since, _until = get_since_until_from_time_range( + time_range=eq, + time_shift=time_shift, + extras=extras, + ) + where_clause_and.append( + self.get_time_filter( + time_col=col_obj, + start_dttm=_since, + end_dttm=_until, + ) + ) else: raise QueryObjectValidationError( _("Invalid filter operation type: %(op)s", op=op)