apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
61.99k stars 13.59k forks source link

feat(jinja): add option to format time filters using strftime #30323

Closed villebro closed 1 week ago

villebro commented 1 week ago

SUMMARY

A continuation on #30142, making it possible to format from_expr and to_expr using datetime.strftime. This is handy if the target column is string based using a custom format, like YYYY-MM-DD, which tends to be the typical partitioning scheme, and we want to avoid having to cast the column to a temporal type.

AFTER

Now you can format like this (assuming my_date is a VARCHAR with YYYY-MM-DD):

{% set time_filter = get_time_filter(strftime="%y-%m-%d") %}
{% set from_expr = time_filter.from_expr %}
{% set to_expr = time_filter.to_expr %}
SELECT
...
{% if from_expr or to_expr %}WHERE 1 = 1
{% if from_expr %}AND my_date >= '{{ from_expr }}'{% endif %}
{% if to_expr %}AND my_date < '{{ to_expr }}'{% endif %}
{% endif %}

This will render

SELECT
  ...
WHERE 1 = 1
AND my_date >= '2024-08-03'
AND my_date < '2024-09-03'

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

codecov[bot] commented 1 week ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 83.88%. Comparing base (76d897e) to head (36fcac0). Report is 745 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #30323 +/- ## =========================================== + Coverage 60.48% 83.88% +23.39% =========================================== Files 1931 533 -1398 Lines 76236 38494 -37742 Branches 8568 0 -8568 =========================================== - Hits 46114 32292 -13822 + Misses 28017 6202 -21815 + Partials 2105 0 -2105 ``` | [Flag](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | | |---|---|---| | [hive](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `49.01% <0.00%> (-0.16%)` | :arrow_down: | | [javascript](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | | | [mysql](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `76.86% <0.00%> (?)` | | | [postgres](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `76.93% <0.00%> (?)` | | | [presto](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `53.51% <0.00%> (-0.29%)` | :arrow_down: | | [python](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `83.88% <100.00%> (+20.40%)` | :arrow_up: | | [sqlite](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `76.38% <0.00%> (?)` | | | [unit](https://app.codecov.io/gh/apache/superset/pull/30323/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `60.60% <100.00%> (+2.98%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.