-
Notifications
You must be signed in to change notification settings - Fork 13.8k
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
feat(jinja): add advanced temporal filter functionality #30142
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #30142 +/- ##
===========================================
+ Coverage 60.48% 83.67% +23.18%
===========================================
Files 1931 529 -1402
Lines 76236 38334 -37902
Branches 8568 0 -8568
===========================================
- Hits 46114 32075 -14039
+ Misses 28017 6259 -21758
+ Partials 2105 0 -2105
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
04e2cff
to
bb73f1a
Compare
62df6e0
to
a3fbd5d
Compare
a3fbd5d
to
e6620ce
Compare
So something that comes up often in the templateized datasets using these filter values is having defaults so that users can run the "sync columns from source" action and have their query execute. The dataset modal does have "Template parameters" setting but that actually ends up overriding anything set at the dashboard or chart level which is kind of unexpected. I've been advising users to always use if/else to check if the filter values are defined otherwise fallback to a reasonable default (eg last week or day), so that their query is valid even if not run in a dashboard or chart context. It would be good to bake this default behavior into the jinja expression so that expressing this is a little less verbose. Something like:
|
That's a really good idea 👍 To keep things simple, I would prefer only having def get_time_filter(
self,
column: str | None = None,
default: str | None = None,
target_type: str | None = None,
remove_filter: bool = False,
) -> TimeFilter:
...
# logic to populate time_range from filter value
...
if time_range == NO_TIME_RANGE and default:
time_range = default
... |
8c89902
to
288e6af
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
45bde42
to
d01e3fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs & comments: A few documentation language suggestions and I pointed out a place where a sentence stops halfway.
Code: I glanced at the code, it's mostly beyond me so I'll defer to others there.
Overall: seems like a nice improvement in functionality, I appreciate the complete documentation to go with it 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I added a self-assigned task for removing docs for |
hey @villebro, This is amazing! Thank you so much for working on this new macro. 🙏 It's super powerful -- can't wait to test it. 🙌 One question: you mentioned that one of the limitations with the |
Thanks for the feedback, super happy if this is useful to someone else, too ❤️
With this it's indeed possible to extract the temporal filter for multiple different columns. However, in the context of dashboard filtering, unfortunately the dashboard time range filter doesn't yet support applying to a specific temporal column (when the time range filter applies a value, I believe it attaches to all temporal filters currently 🙁). What I'd like to see is adding an optional dataset and column dropdown in the time range filter, and if that's selected, then it will apply a temporal filter only to that column. I've heard lots of people ask for that, and it shouldn't be that difficult to implement IMO. |
that totally makes sense! 🙏 I also believe this new macro would already allow you to pull distinct temporal filters from the chart context to apply to the inner query, which is super cool. I 100% support and agree with those changes to the temporal filter - Ideally we should support selecting which temporal column is getting filtered, to allow multiple distinct filters in a dashboard (similar to the filter value). I think the only additional consideration is that right now when you create a chart, it automatically adds the dataset's main dttm column to the filter as a "placeholder", so that dashboard time range filters are applied to it. If it becomes possible to control/differentiate the column in the filter configuration, we would just need to make sure that this filter doesn't get applied to the "placeholder columns" too. |
SUMMARY
Currently the
from_dttm
andto_dttm
Jinja variables make it possible to reference the time range endpoints from within the Jinja context. However, they have some shortcomings:to_dttm
always points to the current day, even if there's no time range present in the query (this appears to be a quirk of how theget_since_until
helper function works)..isoformat()
method. This means that it's difficult to use them for database-specific formatting that's readily available inBaseEngineSpec.convert_dttm
.filter_value
andget_filters
macros).This PR adds a new Jinja macro called
get_time_filter
, which returns a dataclass with the following properties:from_expr
: The SQL expression representing the start of the time range, if availableto_expr
: The SQL expression representing the end of the time range, if availabletime_range
: The (usually) human readable raw value of the time filter (e.g. "No filter", "Last week" etc)Please refer to the added docs for how the feature works in practice:
TESTING INSTRUCTIONS
ADDITIONAL INFORMATION