betodealmeida commented on code in PR #31867:
URL: https://github.com/apache/superset/pull/31867#discussion_r1919215854
##########
superset/utils/date_parser.py:
##########
@@ -143,6 +143,175 @@ def parse_past_timedelta(
)
+def get_relative_base(unit: str, relative_start: str | None = None) -> str:
+ """
+ Determines the relative base (`now` or `today`) based on the granularity
of the unit
+ and an optional user-provided base expression. This is used as the base
for all
+ queries parsed from `time_range_lookup`.
+
+ Args:
+ unit (str): The time unit (e.g., "second", "minute", "hour", "day",
etc.).
+ relative_start (datetime | None): Optional user-provided base time.
+
+ Returns:
+ datetime: The base time (`now`, `today`, or user-provided).
+ """
+ if relative_start is not None:
+ return relative_start
+
+ granular_units = {"second", "minute", "hour"}
+ broad_units = {"day", "week", "month", "quarter", "year"}
+
+ if unit.lower() in granular_units:
+ return "now"
+ elif unit.lower() in broad_units:
+ return "today"
+ else:
+ raise ValueError(f"Unknown unit: {unit}")
Review Comment:
I think the linter might complain here unless you do:
```suggestion
if unit.lower() in granular_units:
return "now"
if unit.lower() in broad_units:
return "today"
raise ValueError(f"Unknown unit: {unit}")
```
##########
superset/utils/date_parser.py:
##########
@@ -241,18 +410,20 @@ def get_since_until( # pylint:
disable=too-many-arguments,too-many-locals,too-m
if time_range and separator in time_range:
time_range_lookup = [
(
- r"^last\s+(day|week|month|quarter|year)$",
- lambda unit: f"DATEADD(DATETIME('{_relative_start}'), -1,
{unit})",
- ),
- (
-
r"^last\s+([0-9]+)\s+(second|minute|hour|day|week|month|year)s?$",
- lambda delta,
- unit: f"DATEADD(DATETIME('{_relative_start}'), -{int(delta)},
{unit})", # pylint: disable=line-too-long,useless-suppression
+ r"^(start of|beginning of|end
of)\s+(this|last|next|prior)\s+([0-9]+)?\s*(day|week|month|quarter|year)s?$",
# pylint: disable=line-too-long,useless-suppression # noqa: E501
+ lambda modifier, scope, delta, unit: handle_modifier_and_unit(
+ modifier,
+ scope,
+ delta,
+ unit,
+ get_relative_base(unit, relative_start),
+ ),
),
(
-
r"^next\s+([0-9]+)\s+(second|minute|hour|day|week|month|year)s?$",
- lambda delta,
- unit: f"DATEADD(DATETIME('{_relative_end}'), {int(delta)},
{unit})", # pylint: disable=line-too-long,useless-suppression
+
r"^(this|last|next|prior)\s+([0-9]+)?\s*(second|minute|day|week|month|quarter|year)s?$",
Review Comment:
Ditto here.
##########
superset/utils/date_parser.py:
##########
@@ -241,18 +410,20 @@ def get_since_until( # pylint:
disable=too-many-arguments,too-many-locals,too-m
if time_range and separator in time_range:
time_range_lookup = [
(
- r"^last\s+(day|week|month|quarter|year)$",
- lambda unit: f"DATEADD(DATETIME('{_relative_start}'), -1,
{unit})",
- ),
- (
-
r"^last\s+([0-9]+)\s+(second|minute|hour|day|week|month|year)s?$",
- lambda delta,
- unit: f"DATEADD(DATETIME('{_relative_start}'), -{int(delta)},
{unit})", # pylint: disable=line-too-long,useless-suppression
+ r"^(start of|beginning of|end
of)\s+(this|last|next|prior)\s+([0-9]+)?\s*(day|week|month|quarter|year)s?$",
# pylint: disable=line-too-long,useless-suppression # noqa: E501
Review Comment:
It's probably easier to understand the regular expression if you use
`re.VERBOSE` mode, something like:
```python
pattern = re.compile(r"""
^
(start\ of|beginning\ of|end\ of) # Match the phrases 'start of',
'beginning of', or 'end of'
\s+ # One or more spaces
(this|last|next|prior) # Match time references
\s+ # One or more spaces
([0-9]+)? # Optional number
\s* # Optional whitespace
(day|week|month|quarter|year)s? # Match time unit with optional
plural 's'
$
""", re.VERBOSE)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]