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

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

Merged
merged 2 commits into from
Sep 18, 2024

Conversation

villebro
Copy link
Member

@villebro villebro commented Sep 18, 2024

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

  • 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

@github-actions github-actions bot added the doc Namespace | Anything related to documentation label Sep 18, 2024
@dosubot dosubot bot added the global:jinja Related to Jinja templating label Sep 18, 2024
Copy link

codecov bot commented Sep 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@             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 Coverage Δ
hive 49.01% <0.00%> (-0.16%) ⬇️
javascript ?
mysql 76.86% <0.00%> (?)
postgres 76.93% <0.00%> (?)
presto 53.51% <0.00%> (-0.29%) ⬇️
python 83.88% <100.00%> (+20.40%) ⬆️
sqlite 76.38% <0.00%> (?)
unit 60.60% <100.00%> (+2.98%) ⬆️

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.

@villebro villebro merged commit a395205 into apache:master Sep 18, 2024
42 of 43 checks passed
@villebro villebro deleted the villebro/time-format-str-strftime branch September 18, 2024 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Namespace | Anything related to documentation global:jinja Related to Jinja templating size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants