From 80be1ce6575fdcef7f47b8136c9b91bd7f9ad5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Boisseau-Sierra?= <37387755+EBoisseauSierra@users.noreply.github.com> Date: Wed, 8 Jun 2022 15:08:37 +0100 Subject: [PATCH] docs: Detail how to use Jinja parameters (#20308) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We expand the example to show how to use the `from_dttm` and `to_dttm` Jinja parameters in logic blocks (e.g. `{% if … %}`) and underline when to use the double braces and when not to. All this because, well, *one* could lost quite an extensive amount of time figuring this all out (-_-") Signed-off-by: Étienne Boisseau-Sierra --- docs/docs/installation/sql-templating.mdx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/docs/installation/sql-templating.mdx b/docs/docs/installation/sql-templating.mdx index 905f99cb9..09373d899 100644 --- a/docs/docs/installation/sql-templating.mdx +++ b/docs/docs/installation/sql-templating.mdx @@ -33,6 +33,26 @@ For example, to add a time range to a virtual dataset, you can write the followi SELECT * from tbl where dttm_col > '{{ from_dttm }}' and dttm_col < '{{ to_dttm }}' ``` +You can also use [Jinja's logic](https://jinja.palletsprojects.com/en/2.11.x/templates/#tests) +to make your query robust to clearing the timerange filter: + +```sql +SELECT * +FROM tbl +WHERE ( + {% if from_dttm is not none %} + dttm_col > '{{ from_dttm }}' AND + {% endif %} + {% if to_dttm is not none %} + dttm_col < '{{ to_dttm }}' AND + {% endif %} + true +) +``` + +Note how the Jinja parameters are called within double brackets in the query, and without in the +logic blocks. + To add custom functionality to the Jinja context, you need to overload the default Jinja context in your environment by defining the `JINJA_CONTEXT_ADDONS` in your superset configuration (`superset_config.py`). Objects referenced in this dictionary are made available for users to use