Skip to content
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

fix(date_parser): fixed bug for advanced time range filter #31867

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

alexandrusoare
Copy link
Contributor

@alexandrusoare alexandrusoare commented Jan 15, 2025

Fixes #30592

SUMMARY

Fixed bug where Advanced Time Range filter showed wrong dates for queries like "last 2 minutes" , "start of next month", "end of next month" etc

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

BEFORE -> "Last 2 minutes"

image

AFTER -> "Last 2 minutes"

image

BEFORE -> "Start of this month"

image

AFTER -> "Start of this month"

image

BEFORE -> "End of next month"

image

AFTER -> "End of next month"

image

BEFORE -> "Prior 2 minutes"

image

AFTER -> "Prior 2 minutes"

image

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Fix Detected
Error Handling Insufficient Error Context ▹ view
Files scanned
File Path Reviewed
superset/utils/date_parser.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Current Korbit Configuration

General Settings
Setting Value
Review Schedule Automatic excluding drafts
Max Issue Count 10
Automatic PR Descriptions
Issue Categories
Category Enabled
Naming
Database Operations
Documentation
Logging
Error Handling
Systems and Environment
Objects and Data Structures
Readability and Maintainability
Asynchronous Processing
Design Patterns
Third-Party Libraries
Performance
Security
Functionality

Feedback and Support

Note

Korbit Pro is free for open source projects 🎉

Looking to add Korbit to your team? Get started with a free 2 week trial here

elif unit.lower() in broad_units:
return "today"
else:
raise ValueError(f"Unknown unit: {unit}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insufficient Error Context category Error Handling

Tell me more
What is the issue?

The get_relative_base function's ValueError lacks context about the function and input values.

Why this matters

During debugging, it will be harder to identify where this error originated and what the valid input values should have been.

Suggested change ∙ Feature Preview

raise ValueError(f"get_relative_base: Unknown time unit '{unit}'. Expected one of {granular_units | broad_units}")

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Copy link

codecov bot commented Jan 15, 2025

Codecov Report

Attention: Patch coverage is 86.11111% with 5 lines in your changes missing coverage. Please review.

Project coverage is 82.95%. Comparing base (76d897e) to head (fb59397).
Report is 1328 commits behind head on master.

Files with missing lines Patch % Lines
superset/utils/date_parser.py 86.11% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #31867       +/-   ##
===========================================
+ Coverage   60.48%   82.95%   +22.47%     
===========================================
  Files        1931      540     -1391     
  Lines       76236    39176    -37060     
  Branches     8568        0     -8568     
===========================================
- Hits        46114    32500    -13614     
+ Misses      28017     6676    -21341     
+ Partials     2105        0     -2105     
Flag Coverage Δ
hive 48.75% <13.88%> (-0.42%) ⬇️
javascript ?
mysql 75.45% <30.55%> (?)
postgres 75.52% <30.55%> (?)
presto 53.26% <13.88%> (-0.55%) ⬇️
python 82.95% <86.11%> (+19.47%) ⬆️
sqlite 74.99% <30.55%> (?)
unit 61.06% <86.11%> (+3.43%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@geido geido added the preset:bounty Issues that have been selected by Preset and have a bounty attached. label Jan 15, 2025
Copy link
Contributor

@msyavuz msyavuz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@Antonio-RiveroMartnez
Copy link
Member

Hey @alexandrusoare , thanks for taking care of this.

I left a comment on the issue itself, but IMHO we should instead have a look at the library that's parsing the human readable dates here and see if there's something we can address there instead or if there's a limitation on such library etc.

@alexandrusoare
Copy link
Contributor Author

Hey @alexandrusoare , thanks for taking care of this.

I left a comment on the issue itself, but IMHO we should instead have a look at the library that's parsing the human readable dates here and see if there's something we can address there instead or if there's a limitation on such library etc.

Hey @Antonio-RiveroMartnez , at first I was afraid of overcomplicating stuff as adding special queries for words like "prior", "start of" etc as well, and I tried to follow the trail to the exact issue. I think and I am not 100% sure, the library doesn't recognize correctly words like "start of", "end of", "last 2 minutes", once I saw that the error is related to the library, I decided to take care of things like this. But I will have a second look on the library and let you know what are my discoveries and thoughts

Comment on lines +165 to +170
if unit.lower() in granular_units:
return "now"
elif unit.lower() in broad_units:
return "today"
else:
raise ValueError(f"Unknown unit: {unit}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the linter might complain here unless you do:

Suggested change
if unit.lower() in granular_units:
return "now"
elif unit.lower() in broad_units:
return "today"
else:
raise ValueError(f"Unknown unit: {unit}")
if unit.lower() in granular_units:
return "now"
if unit.lower() in broad_units:
return "today"
raise ValueError(f"Unknown unit: {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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably easier to understand the regular expression if you use re.VERBOSE mode, something like:

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)

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?$",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preset:bounty Issues that have been selected by Preset and have a bounty attached. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Advanced" Date Time in Time Range Filter is Incorrect for "last", "this", "beginning", "start", "end"
5 participants